Skip to content

Commit

Permalink
Delete key pressed, resetting inline completion
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed May 18, 2024
1 parent c9a7027 commit 19f3cd9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
35 changes: 19 additions & 16 deletions lua/fittencode/bindings.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
local api = vim.api

local ActionsEngine = require('fittencode.engines.actions')
local API = require('fittencode.api').api
local Base = require('fittencode.base')
local Config = require('fittencode.config')
local InlineEngine = require('fittencode.engines.inline')
local Log = require('fittencode.log')
local Lines = require('fittencode.views.lines')
local Log = require('fittencode.log')

local M = {}

local ignore = false

-- Debounce time for advance function, in milliseconds
local ADVANCE_DEBOUNCE_TIME = 120

---@type uv_timer_t
local advance_timer = nil

Expand Down Expand Up @@ -221,25 +223,26 @@ function M.setup_keymaps()
Base.map('i', '<C-Right>', API.accept_word)
end

function M.setup_keyfilters()
-- '<80>kd', '<80>kD' in Lua
local keycodes = {
'<Backspace>',
'<Delete>',
}
local internal_keys = {}
vim.tbl_map(function(trigger)
internal_keys[#internal_keys + 1] = api.nvim_replace_termcodes(trigger, true, true, true)
end, keycodes)
-- '<80>kd', '<80>kD' in Lua
local FILTERED_KEYS = {}
vim.tbl_map(function(trigger)
FILTERED_KEYS[#FILTERED_KEYS + 1] = api.nvim_replace_termcodes(trigger, true, true, true)
end, {
'<Backspace>',
'<Delete>',
})

function M.setup_keyfilters()
vim.on_key(function(key)
vim.schedule(function()
if api.nvim_get_mode().mode == 'i' then
if vim.tbl_contains(internal_keys, key) then
-- Log.debug('Ignore key: {}', key)
ignore = true
if vim.tbl_contains(FILTERED_KEYS, key) then
Log.debug('Delete key pressed, resetting inline completion')
InlineEngine.reset()
if Config.options.inline_completion.disable_completion_when_delete then
ignore = true
end
else
-- Log.debug('Accept key: {}', key)
ignore = false
end
end
Expand Down
4 changes: 1 addition & 3 deletions lua/fittencode/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ function M.setup(opts)

if Config.options.completion_mode == 'inline' then
Bindings.setup_autocmds()
Bindings.setup_keyfilters()
if Config.options.use_default_keymaps then
Bindings.setup_keymaps()
end
if Config.options.inline_completion.disable_completion_when_delete then
Bindings.setup_keyfilters()
end
elseif Config.options.completion_mode == 'source' then
require('fittencode.sources').setup()
end
Expand Down

0 comments on commit 19f3cd9

Please sign in to comment.