Skip to content

Commit 1032341

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

File tree

4 files changed

+110
-81
lines changed

4 files changed

+110
-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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@ local M = {}
1111
---@field nes copilotlsp.config.nes
1212
M.defaults = {
1313
nes = {
14+
auto_trigger = false,
15+
debounce = 500,
1416
move_count_threshold = 3,
1517
distance_threshold = 40,
1618
clear_on_large_distance = true,
1719
count_horizontal_moves = true,
1820
reset_on_approaching = true,
1921
},
22+
keymaps = {
23+
request_nes = nil,
24+
accept_nes = nil,
25+
clear_nes = nil,
26+
},
2027
}
2128

2229
---@type copilotlsp.config

lua/copilot-lsp/init.lua

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local config = require("copilot-lsp.config")
2+
-- local nes = require("copilot-lsp.nes")
23

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

1892
return M

0 commit comments

Comments
 (0)