Skip to content

Commit

Permalink
Refactor keymaps
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed Jun 4, 2024
1 parent 1283fb6 commit 8369845
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
28 changes: 17 additions & 11 deletions lua/fittencode/bindings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -246,18 +246,24 @@ function M.setup_commands()
})
end

local KEYMAPS = {
{ '<TAB>', API.accept_all_suggestions },
{ '<C-Down>', API.accept_line },
{ '<C-Right>', API.accept_word },
{ '<C-Up>', API.revoke_line },
{ '<C-Left>', API.revoke_word },
}

function M.setup_keymaps()
Base.map('i', '<Tab>', function()
if API.has_suggestions() then
API.accept_all_suggestions()
else
Lines.tab()
end
end)
Base.map('i', '<C-Down>', API.accept_line)
Base.map('i', '<C-Right>', API.accept_word)
Base.map('i', '<C-Up>', API.revoke_line)
Base.map('i', '<C-Left>', API.revoke_word)
for _, keymap in ipairs(KEYMAPS) do
Base.map('i', keymap[1], function()
if API.has_suggestions() then
keymap[2]()
else
Lines.feedkeys(keymap[1])
end
end)
end
Base.map('i', '<A-\\>', API.triggering_completion)
end

Expand Down
8 changes: 2 additions & 6 deletions lua/fittencode/views/lines.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@ local function is_spaces_line(line)
end

---@param chars string
local function feedkeys(chars)
function M.feedkeys(chars)
local keys = api.nvim_replace_termcodes(chars, true, false, true)
api.nvim_feedkeys(keys, 'in', true)
end

local function undojoin()
feedkeys('<C-g>u')
end

function M.tab()
feedkeys('<Tab>')
M.feedkeys('<C-g>u')
end

---@param line string
Expand Down

0 comments on commit 8369845

Please sign in to comment.