Skip to content

Commit

Permalink
feat: add custom description builder (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wansmer authored Oct 22, 2024
1 parent ac74a80 commit 590d872
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ local default_config = {
---@type string[] Names of layouts. If empty, will handle all configured layouts.
use_layouts = {},
---@type table Fallback layouts
---Custom description builder:
--- old_desc - original description,
--- method - 'translate' (map translated lhs) or 'feedkeys' (call `nvim_feedkeys` with original lhs)
--- lhs - original left-hand side for translation
---should return new description as a string. If error is occurs or non-string is returned, original builder with `LM ()` prefix will use
---@type nil|function(old_desc, method, lhs): string
custom_desc = nil,
layouts = {
---@type table Fallback layout item. Name of key is a name of language
ru = {
Expand Down
9 changes: 8 additions & 1 deletion lua/langmapper/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ M.config = {
disable_hack_modes = { 'i' },
---@type table Modes whose mappings will be checked during automapping.
automapping_modes = { 'n', 'v', 'x', 's' },
---@type string Standart English layout (on Mac, It may be different in your case.)
---@type string Standard English layout (on Mac, It may be different in your case.)
default_layout = [[~QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?`qwertyuiop[]asdfghjkl;'zxcvbnm,./]],
---@type string[] Names of layouts. If empty, will handle all configured layouts.
use_layouts = {},
---Custom description builder:
--- old_desc - original description,
--- method - 'translate' (map translated lhs) or 'feedkeys' (call `nvim_feedkeys` with original lhs)
--- lhs - original left-hand side for translation
---should return new description as a string. If error is occurs or non-string is returned, original builder with `LM ()` prefix will use
---@type nil|function(old_desc, method, lhs): string
custom_desc = nil,
---@type table Fallback layouts
layouts = {
---@type table Fallback layout item. Name of key is a name of language
Expand Down
7 changes: 7 additions & 0 deletions lua/langmapper/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ local M = {}
---@param lhs string
---@return string
function M.update_desc(old_desc, method, lhs)
if c.config.custom_desc and type(c.config.custom_desc) == 'function' then
local ok, new_desc = pcall(c.config.custom_desc, old_desc, method, lhs)
if ok and type(new_desc) == 'string' then
return new_desc
end
end

old_desc = old_desc and old_desc or ''
local pack = old_desc ~= '' and old_desc .. ' ' or ''
local new_desc = pack .. 'LM (' .. method .. ' ' .. '"' .. lhs .. '")'
Expand Down

0 comments on commit 590d872

Please sign in to comment.