Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ require('copilot').setup({
auto_trigger = false,
hide_during_completion = true,
debounce = 75,
trigger_on_accept = true,
keymap = {
accept = "<M-l>",
accept_word = false,
Expand Down Expand Up @@ -171,6 +172,7 @@ require("copilot.panel").refresh()

When `auto_trigger` is `true`, copilot starts suggesting as soon as you enter insert mode.
When `auto_trigger` is `false`, use the `next`, `prev` or `accept` keymap to trigger copilot suggestion.
When `trigger_on_accept` is `false`, the keypress will be passed to the buffer as-is, instead of triggering completion.

To toggle auto trigger for the current buffer, use `require("copilot.suggestion").toggle_auto_trigger()`.

Expand Down
3 changes: 3 additions & 0 deletions lua/copilot/config/suggestion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
---@field auto_trigger boolean Whether to trigger the suggestion automatically
---@field hide_during_completion boolean Whether to hide the suggestion during completion
---@field debounce integer Debounce time in milliseconds
---@field trigger_on_accept boolean To either trigger the suggestion on accept or pass the keystroke to the buffer
---@field keymap SuggestionKeymapConfig Keymap for the suggestion

---@class (exact) SuggestionKeymapConfig
Expand All @@ -20,6 +21,7 @@ local suggestion = {
auto_trigger = false,
hide_during_completion = true,
debounce = 15,
trigger_on_accept = true,
keymap = {
accept = "<M-l>",
accept_word = false,
Expand All @@ -36,6 +38,7 @@ function suggestion.validate(config)
vim.validate("auto_trigger", config.auto_trigger, "boolean")
vim.validate("hide_during_completion", config.hide_during_completion, "boolean")
vim.validate("debounce", config.debounce, { "number", "nil" })
vim.validate("trigger_on_accept", config.trigger_on_accept, "boolean")
vim.validate("keymap", config.keymap, "table")
vim.validate("keymap.accept", config.keymap.accept, { "string", "boolean" })
vim.validate("keymap.accept_word", config.keymap.accept_word, { "string", "boolean" })
Expand Down
13 changes: 11 additions & 2 deletions lua/copilot/suggestion/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,16 @@ end

local function set_keymap(keymap)
if keymap.accept then
vim.keymap.set("i", keymap.accept, M.accept, {
vim.keymap.set("i", keymap.accept, function()
local ctx = get_ctx()
-- If we trigger on accept but the suggestion has not been triggered yet, we let it go through so it does
if (config.suggestion.trigger_on_accept and not ctx.first) or M.is_visible() then
M.accept()
else
local termcode = vim.api.nvim_replace_termcodes(keymap.accept, true, false, true)
vim.api.nvim_feedkeys(termcode, "n", true)
end
end, {
desc = "[copilot] accept suggestion",
silent = true,
})
Expand Down Expand Up @@ -475,7 +484,7 @@ function M.accept(modifier)
logger.trace("suggestion accept", ctx)

-- no suggestion request yet
if not ctx.first then
if (not ctx.first) and config.suggestion.trigger_on_accept then
logger.trace("suggestion accept, not first request", ctx)
schedule(ctx)
return
Expand Down
1 change: 1 addition & 0 deletions lua/copilot/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function M.get_doc()
return doc
end

-- Used by copilot.cmp to watch out if moving it
function M.get_doc_params(overrides)
overrides = overrides or {}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--|---------|-----
01|123
02|456
03|7
04|~
05|~
06|~
07|~
08|~
09|<e] [+] 3,3-9
10|-- INSERT --

--|---------|-----
01|000000000000000
02|000000000000000
03|000000000000000
04|111111111111111
05|111111111111111
06|111111111111111
07|111111111111111
08|111111111111111
09|222222222222222
10|333333333333444
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--|---------|-----
01|123
02|456
03|789
04|~
05|~
06|~
07|~
08|~
09|<e] [+] 3,2 All
10|-- INSERT --

--|---------|-----
01|000000000000000
02|000000000000000
03|011000000000000
04|222222222222222
05|222222222222222
06|222222222222222
07|222222222222222
08|222222222222222
09|333333333333333
10|444444444444555
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--|---------|-----
01|123
02|456
03|7
04|~
05|~
06|~
07|~
08|~
09|<e] [+] 3,2 All
10|-- INSERT --

--|---------|-----
01|000000000000000
02|000000000000000
03|000000000000000
04|111111111111111
05|111111111111111
06|111111111111111
07|111111111111111
08|111111111111111
09|222222222222222
10|333333333333444
79 changes: 79 additions & 0 deletions tests/test_suggestion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,83 @@ T["suggestion()"]["suggestion works"] = function()
reference_screenshot(child.get_screenshot())
end

T["suggestion()"]["auto_trigger is false, will not show ghost test"] = function()
child.o.lines, child.o.columns = 10, 15
child.lua([[M.setup({
suggestion = {
auto_trigger = false,
},
logger = {
file_log_level = vim.log.levels.TRACE,
file = "./tests/logs/test_suggestion.log",
},
filetypes = {
["*"] = true,
},
})]])

-- look for a synchronous way to wait for engine to be up
vim.loop.sleep(500)
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7")
vim.loop.sleep(3000)
child.lua("vim.wait(0)")

reference_screenshot(child.get_screenshot())
end

T["suggestion()"]["accept keymap to trigger sugestion"] = function()
child.o.lines, child.o.columns = 10, 15
child.lua([[M.setup({
suggestion = {
auto_trigger = false,
keymap = {
accept = "<Tab>",
},
},
logger = {
file_log_level = vim.log.levels.TRACE,
file = "./tests/logs/test_suggestion.log",
},
filetypes = {
["*"] = true,
},
})]])

-- look for a synchronous way to wait for engine to be up
vim.loop.sleep(500)
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7", "<Tab>")
vim.loop.sleep(3000)
child.lua("vim.wait(0)")

reference_screenshot(child.get_screenshot())
end

T["suggestion()"]["accept keymap, no suggestion, execute normal keystroke"] = function()
child.o.lines, child.o.columns = 10, 15
child.lua([[M.setup({
suggestion = {
auto_trigger = false,
trigger_on_accept = false,
keymap = {
accept = "<Tab>",
},
},
logger = {
file_log_level = vim.log.levels.TRACE,
file = "./tests/logs/test_suggestion.log",
},
filetypes = {
["*"] = true,
},
})]])

-- look for a synchronous way to wait for engine to be up
vim.loop.sleep(500)
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7", "<Tab>")
vim.loop.sleep(3000)
child.lua("vim.wait(0)")

reference_screenshot(child.get_screenshot())
end

return T