Skip to content

Commit 346d06a

Browse files
authored
feat: add copilot.panel module (zbirenbaum#50)
* refactor: use copilot.api in copilot.handlers module * feat: add copilot.panel module * feat(panel): use virtual text as marker
1 parent 1898423 commit 346d06a

File tree

7 files changed

+559
-292
lines changed

7 files changed

+559
-292
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ The following is the default configuration:
6464
```lua
6565
panel = { -- no config options yet
6666
enabled = true,
67+
auto_refresh = false,
68+
keymap = {
69+
jump_prev = "[[",
70+
jump_next = "]]",
71+
accept = "<CR>",
72+
refresh = "gr",
73+
open = "<M-CR>"
74+
},
6775
},
6876
suggestion = {
6977
enabled = true,
@@ -84,16 +92,10 @@ server_opts_overrides = {},
8492

8593
##### panel
8694

87-
Enabling panel creates the `CopilotPanel` command, which allows you to preview completions in a split window. Navigating to the split window allows you to jump between them and see each one. (<CR> to accept completion not yet implemented, coming soon)
88-
89-
```lua
90-
require("copilot").setup {
91-
panel = {
92-
enabled = false,
93-
}
94-
},
95+
Enabling panel creates the `CopilotPanel` command, which allows you to preview suggestions
96+
in a split window.
9597

96-
```
98+
If `auto_refresh` is `true`, the suggestions are refreshed as you type in the buffer.
9799

98100
#### suggestion
99101

lua/copilot/client.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ M.merge_server_opts = function (params)
3838
vim.schedule(register_autocmd)
3939
end,
4040
handlers = {
41-
-- PanelSolution = api.handlers.PanelSolution,
42-
-- PanelSolutionsDone = api.handlers.PanelSolutionsDone,
41+
PanelSolution = api.handlers.PanelSolution,
42+
PanelSolutionsDone = api.handlers.PanelSolutionsDone,
4343
statusNotification = api.handlers.statusNotification,
4444
}
4545
}, params.server_opts_overrides or {})

lua/copilot/extensions/panel.lua

Lines changed: 0 additions & 31 deletions
This file was deleted.

lua/copilot/extensions/print_panel.lua

Lines changed: 0 additions & 207 deletions
This file was deleted.

lua/copilot/handlers.lua

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,24 @@
1-
local lsp_handlers = {
2-
callbacks = {
3-
["PanelSolution"] = {},
4-
["PanelSolutionsDone"] = {}
5-
},
6-
}
1+
local api = require("copilot.api")
72

8-
local handlers = {
9-
["PanelSolution"] = function (_, result, _, config)
10-
if not result then return "err" end
11-
if result.panelId and config.callbacks[result.panelId] then
12-
config.callbacks[result.panelId](result)
13-
elseif not config.callbacks[result.panelId] and result.panelId then
14-
return
15-
else
16-
for _, callback in pairs(config.callbacks) do callback() end
17-
end
18-
end,
3+
---@deprecated
4+
local handlers = {}
195

20-
["PanelSolutionsDone"] = function (_, _, _, config)
21-
for _, callback in pairs(config.callbacks) do
22-
callback()
23-
end
24-
end
25-
}
26-
27-
lsp_handlers.add_handler_callback = function (handler, fn_name, fn)
28-
lsp_handlers.callbacks[handler][fn_name] = fn
29-
vim.lsp.handlers[handler] = vim.lsp.with(handlers[handler], {
30-
callbacks = lsp_handlers.callbacks[handler]
31-
})
6+
-- use `require("copilot.api").register_panel_handlers()`
7+
---@deprecated
8+
handlers.add_handler_callback = function(method, panelId, fn)
9+
api.panel.callback[method][panelId] = fn
3210
end
3311

34-
lsp_handlers.remove_handler_callback = function (handler, fn_name)
35-
lsp_handlers.callbacks[handler][fn_name] = nil
36-
vim.lsp.handlers[handler] = vim.lsp.with(handlers[handler], {
37-
callbacks = lsp_handlers.callbacks[handler]
38-
})
12+
-- use `require("copilot.api").unregister_panel_handlers()`
13+
---@deprecated
14+
handlers.remove_handler_callback = function(method, panelId)
15+
api.panel.callback[method][panelId] = nil
3916
end
4017

41-
lsp_handlers.remove_all_name = function (fn_name)
42-
for handler, _ in pairs(lsp_handlers.callbacks) do
43-
lsp_handlers.remove_handler_callback(handler, fn_name)
44-
end
18+
-- use `require("copilot.api").unregister_panel_handlers()`
19+
---@deprecated
20+
handlers.remove_all_name = function(panelId)
21+
api.unregister_panel_handlers(panelId)
4522
end
4623

47-
return lsp_handlers
24+
return handlers

lua/copilot/init.lua

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
local M = { client_info = nil }
22
local client = require("copilot.client")
33
local highlight = require("copilot.highlight")
4+
local panel = require("copilot.panel")
45
local suggestion = require("copilot.suggestion")
56
local defaults = {
6-
panel = { -- no config options yet
7+
panel = {
78
enabled = true,
9+
auto_refresh = false,
10+
keymap = {
11+
jump_prev = "[[",
12+
jump_next = "]]",
13+
accept = "<CR>",
14+
refresh = "gr",
15+
open = "<M-CR>"
16+
}
817
},
918
suggestion = {
1019
enabled = true,
@@ -38,9 +47,7 @@ local create_cmds = function (_)
3847
end, {})
3948

4049
vim.api.nvim_create_user_command("CopilotPanel", function ()
41-
local panel = require("copilot.extensions.panel").create()
42-
panel.send_request()
43-
require("copilot.extensions.print_panel").create(panel.buf)
50+
require("copilot.panel").open()
4451
end, {})
4552

4653
vim.api.nvim_create_user_command("CopilotAuth", function()
@@ -59,6 +66,7 @@ M.setup = function(opts)
5966
client.start(user_config)
6067

6168
if user_config.panel.enabled then
69+
panel.setup(user_config.panel)
6270
create_cmds(user_config)
6371
end
6472

0 commit comments

Comments
 (0)