Skip to content

Commit

Permalink
Handle default key maps properly
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed Jun 5, 2024
1 parent 8369845 commit 79d688e
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 125 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ use {
},
-- Enable/Disable the default keymaps in inline completion.
use_default_keymaps = true,
-- Default keymaps
keymaps = {
inline = {
['<TAB>'] = 'accept_all_suggestions',
['<C-Down>'] = 'accept_line',
['<C-Right>'] = 'accept_word',
['<C-Up>'] = 'revoke_line',
['<C-Left>'] = 'revoke_word',
['<A-\\>'] = 'triggering_completion',
},
chat = {
['q'] = 'close'
}
},
-- Setting for source completion.
source_completion = {
-- Enable source completion.
Expand Down
76 changes: 2 additions & 74 deletions lua/fittencode/bindings.lua → lua/fittencode/commands.lua
Original file line number Diff line number Diff line change
@@ -1,52 +1,9 @@
local api = vim.api

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

local M = {}

function M.setup_autocmds()
api.nvim_create_autocmd({ 'CursorHoldI' }, {
group = Base.augroup('CursorHold'),
pattern = '*',
callback = function()
InlineEngine.on_cursor_hold()
end,
desc = 'On Cursor Hold',
})

api.nvim_create_autocmd({ 'CursorMovedI' }, {
group = Base.augroup('CursorMoved'),
pattern = '*',
callback = function()
InlineEngine.on_cursor_moved()
end,
desc = 'On Cursor Moved',
})

api.nvim_create_autocmd({ 'TextChangedI' }, {
group = Base.augroup('TextChanged'),
pattern = '*',
callback = function()
InlineEngine.on_text_changed()
end,
desc = 'On Text Changed',
})

api.nvim_create_autocmd({ 'BufLeave', 'InsertLeave' }, {
group = Base.augroup('Leave'),
pattern = '*',
callback = function()
InlineEngine.on_leave()
end,
desc = 'On Leave',
})
end

---@class FittenCommands
---@field login function
---@field logout function
Expand Down Expand Up @@ -159,7 +116,7 @@ local function _summarize_text(...)
return _action_apis_wrap_content(API.summarize_text, ...)
end

function M.setup_commands()
function M.setup()
---@type FittenCommands
local commands = {
-- Arguments: Nop
Expand Down Expand Up @@ -219,7 +176,7 @@ function M.setup_commands()
table.remove(actions, 1)
return cmd(unpack(actions))
end
Log.debug('Invalid command; fargs: {}', line.fargs)
Log.debug('Invalid command fargs: {}', line.fargs)
end, {
complete = function(_, line)
local args = vim.split(vim.trim(line), '%s+')
Expand All @@ -246,33 +203,4 @@ 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()
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

function M.setup_keyfilters()
vim.on_key(function(key)
vim.schedule(function()
InlineEngine.on_key(key)
end)
end)
end

return M
19 changes: 19 additions & 0 deletions lua/fittencode/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ local defaults = {
},
-- Enable/Disable the default keymaps in inline completion.
use_default_keymaps = true,
-- Default keymaps
keymaps = {
inline = {
['<TAB>'] = 'accept_all_suggestions',
['<C-Down>'] = 'accept_line',
['<C-Right>'] = 'accept_word',
['<C-Up>'] = 'revoke_line',
['<C-Left>'] = 'revoke_word',
['<A-\\>'] = 'triggering_completion',
},
chat = {
['q'] = 'close'
}
},
-- Setting for source completion.
---@class SourceCompletionOptions
source_completion = {
Expand Down Expand Up @@ -128,7 +142,12 @@ M.internal = {

---@param opts? FittenCodeOptions
function M.setup(opts)
---@class FittenCodeOptions
M.options = vim.tbl_deep_extend('force', defaults, opts or {})
if M.options.use_default_keymaps == false then
M.options.keymaps.inline = {}
M.options.keymaps.chat = {}
end
end

return M
28 changes: 15 additions & 13 deletions lua/fittencode/engines/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,9 @@ function ActionsEngine.start_action(action, opts)
local window = api.nvim_get_current_win()
local buffer = api.nvim_win_get_buf(window)

chat:create()
chat:create({
keymaps = Config.options.keymaps.chat,
})
if not opts.headless then
chat:show()
fn.win_gotoid(window)
Expand Down Expand Up @@ -665,18 +667,6 @@ local chat_callbacks = {
end,
}

function ActionsEngine.setup()
chat = Chat:new(chat_callbacks)
content = Content:new(chat)
tasks = TaskScheduler:new()
tasks:setup()
status = Status:new({
tag = 'ActionsEngine',
ready_idle = true,
})
setup_actions_menu()
end

---@return integer
function ActionsEngine.get_status()
return status:get_current()
Expand All @@ -698,4 +688,16 @@ function ActionsEngine.toggle_chat()
end
end

function ActionsEngine.setup()
chat = Chat:new(chat_callbacks)
content = Content:new(chat)
tasks = TaskScheduler:new()
tasks:setup()
status = Status:new({
tag = 'ActionsEngine',
ready_idle = true,
})
setup_actions_menu()
end

return ActionsEngine
114 changes: 97 additions & 17 deletions lua/fittencode/engines/inline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ local CURSORMOVED_DEBOUNCE_TIME = 120
---@type uv_timer_t
local cursormoved_timer = nil

function M.setup()
model = Model:new()
tasks = TaskScheduler:new()
tasks:setup()
status = Status:new({ tag = 'InlineEngine' })
end

local function suggestions_modify_enabled()
return M.is_inline_enabled() and M.has_suggestions()
end
Expand Down Expand Up @@ -461,17 +454,21 @@ end, {
'<Delete>',
})

function M.on_key(key)
if api.nvim_get_mode().mode == 'i' then
if vim.tbl_contains(FILTERED_KEYS, key) then
M.reset()
if Config.options.inline_completion.disable_completion_when_delete then
ignore_event = true
local function setup_keyfilters()
vim.on_key(function(key)
vim.schedule(function()
if api.nvim_get_mode().mode == 'i' then
if vim.tbl_contains(FILTERED_KEYS, key) then
M.reset()
if Config.options.inline_completion.disable_completion_when_delete then
ignore_event = true
end
else
ignore_event = false
end
end
else
ignore_event = false
end
end
end)
end)
end

function M.on_cursor_hold()
Expand Down Expand Up @@ -510,4 +507,87 @@ function M.on_cursor_moved()
end, CURSORMOVED_DEBOUNCE_TIME)
end

local KEYS = {
accept_all_suggestions = { true },
accept_char = { true },
accept_word = { true },
accept_line = { true },
revoke_char = { true },
revoke_word = { true },
revoke_line = { true },
triggering_completion = { false }
}

local function setup_keymaps()
for key, value in pairs(Config.options.keymaps.inline) do
Base.map('i', key, function()
local v = KEYS[value]
if v == nil then
return
end
if v[1] then
if M.has_suggestions() then
M[value]()
else
Lines.feedkeys(key)
end
else
M[value]()
end
end)
end
end

local function setup_autocmds()
api.nvim_create_autocmd({ 'CursorHoldI' }, {
group = Base.augroup('CursorHold'),
pattern = '*',
callback = function()
M.on_cursor_hold()
end,
desc = 'On Cursor Hold',
})

api.nvim_create_autocmd({ 'CursorMovedI' }, {
group = Base.augroup('CursorMoved'),
pattern = '*',
callback = function()
M.on_cursor_moved()
end,
desc = 'On Cursor Moved',
})

api.nvim_create_autocmd({ 'TextChangedI' }, {
group = Base.augroup('TextChanged'),
pattern = '*',
callback = function()
M.on_text_changed()
end,
desc = 'On Text Changed',
})

api.nvim_create_autocmd({ 'BufLeave', 'InsertLeave' }, {
group = Base.augroup('Leave'),
pattern = '*',
callback = function()
M.on_leave()
end,
desc = 'On Leave',
})
end

function M.setup()
model = Model:new()
tasks = TaskScheduler:new()
tasks:setup()
status = Status:new({ tag = 'InlineEngine' })
if Config.options.completion_mode == 'inline' then
setup_keymaps()
setup_keyfilters()
setup_autocmds()
elseif Config.options.completion_mode == 'source' then
require('fittencode.sources').setup()
end
end

return M
19 changes: 4 additions & 15 deletions lua/fittencode/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,13 @@ function M.setup(opts)

require('fittencode.log').setup()
require('fittencode.rest.manager').setup()
local sessions = require('fittencode.sessions')
sessions.setup()
require('fittencode.engines').setup()
require('fittencode.sessions').setup()
require('fittencode.prompt_providers').setup()
require('fittencode.color').setup_highlight()
local Bindings = require('fittencode.bindings')
Bindings.setup_commands()

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

require('fittencode.sessions').load_last_session()
require('fittencode.commands').setup()
sessions.load_last_session()
end

setmetatable(M, {
Expand Down
Loading

0 comments on commit 79d688e

Please sign in to comment.