Skip to content

Commit d5fa4f1

Browse files
committed
feat!: add keymaps to options
1 parent 173c015 commit d5fa4f1

File tree

4 files changed

+117
-81
lines changed

4 files changed

+117
-81
lines changed

README.md

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,63 +19,52 @@
1919

2020
## Usage
2121

22-
To use the plugin, add the following to your Neovim configuration:
23-
2422
```lua
25-
return {
23+
{
2624
"copilotlsp-nvim/copilot-lsp",
27-
init = function()
28-
vim.g.copilot_nes_debounce = 500
29-
vim.lsp.enable("copilot_ls")
30-
vim.keymap.set("n", "<tab>", function()
31-
local bufnr = vim.api.nvim_get_current_buf()
32-
local state = vim.b[bufnr].nes_state
33-
if state then
34-
-- Try to jump to the start of the suggestion edit.
35-
-- If already at the start, then apply the pending suggestion and jump to the end of the edit.
36-
local _ = require("copilot-lsp.nes").walk_cursor_start_edit()
37-
or (
38-
require("copilot-lsp.nes").apply_pending_nes()
39-
and require("copilot-lsp.nes").walk_cursor_end_edit()
40-
)
41-
return nil
42-
else
43-
-- Resolving the terminal's inability to distinguish between `TAB` and `<C-i>` in normal mode
44-
return "<C-i>"
45-
end
46-
end, { desc = "Accept Copilot NES suggestion", expr = true })
47-
end,
25+
opts = {},
4826
}
4927
```
5028

51-
#### Clearing suggestions with Escape
29+
## Default configuration
5230

53-
You can map the `<Esc>` key to clear suggestions while preserving its other functionality:
31+
You don't need to configure anything, but you can customize the defaults: `move_count_threshold` is the most important setting - it controls how many cursor moves happen before suggestions are cleared. Higher values make suggestions persist longer.
5432

5533
```lua
56-
-- Clear copilot suggestion with Esc if visible, otherwise preserve default Esc behavior
57-
vim.keymap.set("n", "<esc>", function()
58-
if not require("copilot-lsp.nes").clear() then
59-
-- fallback to other functionality
60-
end
61-
end, { desc = "Clear Copilot suggestion or fallback" })
34+
require('copilot-lsp').setup({
35+
nes = {
36+
auto_trigger = false,
37+
debounce = 500,
38+
move_count_threshold = 3,
39+
distance_threshold = 40,
40+
clear_on_large_distance = true,
41+
count_horizontal_moves = true,
42+
reset_on_approaching = true,
43+
}
44+
keymaps = {
45+
request_nes = nil,
46+
accept_nes = nil,
47+
clear_nes = nil,
48+
},
49+
})
6250
```
6351

64-
## Default Configuration
65-
66-
### NES (Next Edit Suggestion) Smart Clearing
52+
### Keymap Configuration
6753

68-
You don’t need to configure anything, but you can customize the defaults:
69-
`move_count_threshold` is the most important. It controls how many cursor moves happen before suggestions are cleared. Higher = slower to clear.
54+
**By default, no keymaps are set** to avoid breaking existing configurations, you need to explicitly set them:
7055

7156
```lua
7257
require('copilot-lsp').setup({
73-
nes = {
74-
move_count_threshold = 3, -- Clear after 3 cursor movements
75-
}
58+
keymaps = {
59+
request_nes = "<leader>re",
60+
accept_nes = "<tab>",
61+
clear_nes = "<esc>",
62+
},
7663
})
7764
```
7865

66+
When `accept_nes` is set to `<tab>`, it will fallback to `<C-i>` in normal mode when no suggestion is active (resolving the terminal's inability to distinguish between Tab and Ctrl-I).
67+
7968
### Blink Integration
8069

8170
```lua

lsp/copilot_ls.lua

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,45 +29,4 @@ return {
2929
end,
3030
}),
3131
root_dir = vim.uv.cwd(),
32-
on_init = function(client)
33-
local au = vim.api.nvim_create_augroup("copilotlsp.init", { clear = true })
34-
--NOTE: Inline Completions
35-
--TODO: We dont currently use this code path, so comment for now until a UI is built
36-
-- vim.api.nvim_create_autocmd("TextChangedI", {
37-
-- callback = function()
38-
-- inline_completion.request_inline_completion(2)
39-
-- end,
40-
-- group = au,
41-
-- })
42-
43-
-- TODO: make this configurable for key maps, or just expose commands to map in config
44-
-- vim.keymap.set("i", "<c-i>", function()
45-
-- inline_completion.request_inline_completion(1)
46-
-- end)
47-
48-
--NOTE: NES Completions
49-
local debounced_request = require("copilot-lsp.util").debounce(
50-
require("copilot-lsp.nes").request_nes,
51-
vim.g.copilot_nes_debounce or 500
52-
)
53-
vim.api.nvim_create_autocmd({ "TextChangedI", "TextChanged" }, {
54-
callback = function()
55-
debounced_request(client)
56-
end,
57-
group = au,
58-
})
59-
60-
--NOTE: didFocus
61-
vim.api.nvim_create_autocmd("BufEnter", {
62-
callback = function()
63-
local td_params = vim.lsp.util.make_text_document_params()
64-
client:notify("textDocument/didFocus", {
65-
textDocument = {
66-
uri = td_params.uri,
67-
},
68-
})
69-
end,
70-
group = au,
71-
})
72-
end,
7332
}

lua/copilot-lsp/config.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
---@class copilotlsp.config.keymaps
2+
---@field request_nes? string? Keymap to request a suggestion
3+
---@field accept_nes? string? Keymap to accept a suggestion
4+
---@field clear_nes? string? Keymap to clear a suggestion
5+
16
---@class copilotlsp.config.nes
7+
---@field auto_trigger boolean Whether to automatically trigger suggestions
8+
---@field debounce integer Debounce time in milliseconds
29
---@field move_count_threshold integer Number of cursor movements before clearing suggestion
310
---@field distance_threshold integer Maximum line distance before clearing suggestion
411
---@field clear_on_large_distance boolean Whether to clear suggestion when cursor is far away
@@ -9,14 +16,22 @@ local M = {}
916

1017
---@class copilotlsp.config
1118
---@field nes copilotlsp.config.nes
19+
---@field keymaps copilotlsp.config.keymaps
1220
M.defaults = {
1321
nes = {
22+
auto_trigger = false,
23+
debounce = 500,
1424
move_count_threshold = 3,
1525
distance_threshold = 40,
1626
clear_on_large_distance = true,
1727
count_horizontal_moves = true,
1828
reset_on_approaching = true,
1929
},
30+
keymaps = {
31+
request_nes = nil,
32+
accept_nes = nil,
33+
clear_nes = nil,
34+
},
2035
}
2136

2237
---@type copilotlsp.config

lua/copilot-lsp/init.lua

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,79 @@ M.config = config.config
1313
function M.setup(opts)
1414
config.setup(opts)
1515
M.config = config.config
16+
17+
vim.lsp.config("copilot_ls", {
18+
on_init = function(client)
19+
vim.api.nvim_create_autocmd("BufEnter", {
20+
callback = function()
21+
local td_params = vim.lsp.util.make_text_document_params()
22+
client:notify("textDocument/didFocus", {
23+
textDocument = {
24+
uri = td_params.uri,
25+
},
26+
})
27+
end,
28+
group = vim.api.nvim_create_augroup("copilot_ls", { clear = true }),
29+
desc = "Trigger when entering a buffer",
30+
})
31+
32+
if M.config.nes.auto_trigger then
33+
local debounced_request =
34+
require("copilot-lsp.util").debounce(require("copilot-lsp.nes").request_nes, M.config.nes.debounce)
35+
vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, {
36+
group = vim.api.nvim_create_augroup("copilot-lsp", { clear = true }),
37+
callback = function()
38+
debounced_request(client)
39+
end,
40+
desc = "Debounced request for Copilot NES",
41+
})
42+
end
43+
44+
if M.config.keymaps.request_nes then
45+
vim.keymap.set("n", M.config.keymaps.request_nes, function()
46+
require("copilot-lsp.nes").request_nes(client)
47+
end, { desc = "Request Copilot NES" })
48+
end
49+
50+
if M.config.keymaps.accept_nes then
51+
local is_tab = M.config.keymaps.accept_nes:lower() == "<tab>"
52+
vim.keymap.set("n", M.config.keymaps.accept_nes, function()
53+
local bufnr = vim.api.nvim_get_current_buf()
54+
local state = vim.b[bufnr].nes_state
55+
if state then
56+
local _ = require("copilot-lsp.nes").walk_cursor_start_edit()
57+
or (
58+
require("copilot-lsp.nes").apply_pending_nes()
59+
and require("copilot-lsp.nes").walk_cursor_end_edit()
60+
)
61+
return nil
62+
else
63+
-- Only fallback to <C-i> if the mapped key is <tab>
64+
-- This resolves terminal's inability to distinguish between TAB and <C-i>
65+
if is_tab then
66+
return "<C-i>"
67+
end
68+
return nil
69+
end
70+
end, {
71+
desc = "Accept Copilot NES",
72+
expr = is_tab,
73+
})
74+
end
75+
76+
if M.config.keymaps.clear_nes then
77+
vim.keymap.set("n", M.config.keymaps.clear_nes, function()
78+
if not require("copilot-lsp.nes").clear() then
79+
return M.config.keymaps.clear_nes
80+
end
81+
82+
return nil
83+
end, { desc = "Clear Copilot NES", expr = true })
84+
end
85+
end,
86+
})
87+
88+
vim.lsp.enable("copilot_ls")
1689
end
1790

1891
return M

0 commit comments

Comments
 (0)