Skip to content

Commit 1eb6f51

Browse files
authored
feat: add toggle_auto_trigger keymap support (zbirenbaum#631)
1 parent c13f6f5 commit 1eb6f51

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ require('copilot').setup({
133133
enabled = true,
134134
auto_trigger = false,
135135
hide_during_completion = true,
136-
debounce = 75,
136+
debounce = 15,
137137
trigger_on_accept = true,
138138
keymap = {
139139
accept = "<M-l>",
@@ -142,6 +142,7 @@ require('copilot').setup({
142142
next = "<M-]>",
143143
prev = "<M-[>",
144144
dismiss = "<C-]>",
145+
toggle_auto_trigger = false,
145146
},
146147
},
147148
nes = {

lua/copilot/config/suggestion.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ local suggestion = {
3333
next = "<M-]>",
3434
prev = "<M-[>",
3535
dismiss = "<C-]>",
36+
toggle_auto_trigger = false,
3637
},
3738
},
3839
}
@@ -51,6 +52,7 @@ function suggestion.validate(config)
5152
vim.validate("keymap.next", config.keymap.next, { "string", "boolean" })
5253
vim.validate("keymap.prev", config.keymap.prev, { "string", "boolean" })
5354
vim.validate("keymap.dismiss", config.keymap.dismiss, { "string", "boolean" })
55+
vim.validate("keymap.toggle_auto_trigger", config.keymap.toggle_auto_trigger, { "string", "boolean" })
5456
end
5557

5658
return suggestion

lua/copilot/suggestion/init.lua

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ function M.set_keymap(bufnr)
169169

170170
return false
171171
end, "[copilot] dismiss suggestion", bufnr)
172+
173+
keymaps.register_keymap(
174+
"i",
175+
keymap.toggle_auto_trigger,
176+
M.toggle_auto_trigger,
177+
"[copilot] toggle auto trigger",
178+
bufnr
179+
)
172180
end
173181

174182
---@param bufnr integer
@@ -184,6 +192,7 @@ function M.unset_keymap(bufnr)
184192
keymaps.unset_keymap_if_exists("i", keymap.next, bufnr)
185193
keymaps.unset_keymap_if_exists("i", keymap.prev, bufnr)
186194
keymaps.unset_keymap_if_exists("i", keymap.dismiss, bufnr)
195+
keymaps.unset_keymap_if_exists("i", keymap.toggle_auto_trigger, bufnr)
187196
end
188197

189198
local function stop_timer()
@@ -733,7 +742,21 @@ end
733742

734743
-- toggles auto trigger for the current buffer
735744
function M.toggle_auto_trigger()
736-
vim.b.copilot_suggestion_auto_trigger = not should_auto_trigger()
745+
local bufnr = vim.api.nvim_get_current_buf()
746+
local new_state = not should_auto_trigger()
747+
vim.b.copilot_suggestion_auto_trigger = new_state
748+
749+
-- Long debounce will still show suggestions after toggling in
750+
-- insert mode, so we clear them manually.
751+
if not new_state then
752+
local ctx = get_ctx()
753+
clear(ctx)
754+
M.clear_preview()
755+
else
756+
request_suggestion_when_auto_trigger(bufnr)
757+
end
758+
759+
logger.trace("auto trigger toggled to " .. tostring(new_state))
737760
end
738761

739762
local function on_insert_leave()

0 commit comments

Comments
 (0)