Skip to content

Commit 8c1aa2d

Browse files
committed
improved cmp panel completions
1 parent 9d3e9d0 commit 8c1aa2d

File tree

4 files changed

+74
-34
lines changed

4 files changed

+74
-34
lines changed

lua/copilot/handlers.lua

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
1-
local oldprint = print
2-
local print = function (x) oldprint(vim.inspect(x)) end
3-
41
local lsp_handlers = {
2+
callbacks = {
3+
["PanelSolution"] = {},
4+
["PanelSolutionsDone"] = {}
5+
},
6+
}
7+
8+
local handlers = {
59
["PanelSolution"] = function (_, result, _, config)
6-
config.callback(result)
10+
for _, callback in pairs(config.callbacks) do
11+
callback(result)
12+
end
713
end,
8-
9-
["PanelSolutionsDone"] = function (err, result, ctx, config)
10-
config.callback()
14+
["PanelSolutionsDone"] = function (_, _, _, config)
15+
for _, callback in pairs(config.callbacks) do
16+
callback()
17+
end
1118
end
1219
}
1320

21+
-- require name so not confusing
22+
lsp_handlers.add_handler_callback = function (handler, fn_name, fn)
23+
lsp_handlers.callbacks[handler][fn_name] = fn
24+
vim.lsp.handlers[handler] = vim.lsp.with(handlers[handler], {
25+
callbacks = lsp_handlers.callbacks[handler]
26+
})
27+
end
28+
29+
lsp_handlers.remove_handler_callback = function (handler, fn_name)
30+
lsp_handlers.callbacks[handler][fn_name] = nil
31+
vim.lsp.handlers[handler] = vim.lsp.with(handlers[handler], {
32+
callbacks = lsp_handlers.callbacks[handler]
33+
})
34+
end
35+
1436
return lsp_handlers

lua/copilot/init.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ local defaults = {
77
plugin_manager_path = vim.fn.stdpath("data") .. "/site/pack/packer",
88
server_opts_overrides = {},
99
ft_disable = {},
10-
cmp_method = "getCompletionsCycling"
10+
cmp_method = "getPanelCompletions"
1111
}
1212

1313
local config_handler = function(opts)
@@ -31,7 +31,6 @@ M.setup = function(opts)
3131
vim.schedule(function () client.start(user_config) end)
3232
if user_config.cmp_method == "getPanelCompletions" then
3333
local panel = require("copilot.panel").create()
34-
print(vim.inspect(panel))
3534
require("copilot_cmp").setup(panel.complete)
3635
elseif user_config.cmp_method == "getCompletionsCycling" then
3736
require("copilot_cmp").setup()

lua/copilot/panel.lua

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,78 @@
11
local util = require("copilot.util")
2+
local format = require("copilot_cmp.format")
23

34
local panel = {
4-
n_results = 5,
55
method = "getPanelCompletions",
66
usecmp = false,
7+
cache_line = true,
78
buf = "",
89
uri = "",
910
}
1011

11-
local completions = {}
12+
local existing_matches= {}
13+
14+
panel.send_request = function (callback)
15+
local completion_params = util.get_completion_params()
16+
completion_params.panelId = panel.uri
17+
callback = callback or function () end
18+
vim.lsp.buf_request(0, panel.method, completion_params, callback)
19+
end
1220

1321
panel.complete = vim.schedule_wrap(function (_, params, callback)
22+
local context = params.context
23+
existing_matches[context.bufnr] = existing_matches[context.bufnr] or {}
24+
existing_matches[context.bufnr][context.cursor.row] = existing_matches[context.bufnr][context.cursor.row] or {}
25+
1426
local add_completion = function (result)
15-
local format = require("copilot_cmp.format").format_item
1627
if result then
1728
result.text = result.displayText
18-
local formatted = format(params, result)
19-
completions[formatted.label] = formatted
29+
local formatted = format.format_item(params, result)
30+
existing_matches[context.bufnr][context.cursor.row][formatted.label] = formatted
2031
vim.schedule(function() callback({
2132
isIncomplete = true,
22-
items = vim.tbl_values(completions)
33+
items = vim.tbl_values(existing_matches[context.bufnr][context.cursor.row])
2334
}) end)
2435
end
2536
end
2637

2738
local completed = function ()
28-
vim.schedule(function()
29-
vim.schedule(function() callback({
30-
isIncomplete = false,
31-
items = vim.tbl_values(completions)
32-
}) end)
33-
completions = { isIncomplete = true, items = {} }
34-
end)
39+
vim.schedule(function() callback({
40+
isIncomplete = false,
41+
items = vim.tbl_values(existing_matches[context.bufnr][context.cursor.row])
42+
}) end)
43+
if not panel.cache_line then
44+
existing_matches[context.bufnr][context.cursor.row] = {}
45+
end
3546
end
3647

37-
local completion_params = util.get_completion_params(panel.method)
38-
completion_params.panelId = panel.uri
39-
vim.lsp.buf_request(0, panel.method, completion_params, function () end)
48+
local handler = require("copilot.handlers").add_handler_callback
49+
handler("PanelSolution", "cmp", add_completion)
50+
handler("PanelSolutionsDone", "cmp", completed)
4051

41-
local handlers = require("copilot.handlers")
52+
panel.send_request()
4253

43-
vim.lsp.handlers["PanelSolution"] = vim.lsp.with(handlers["PanelSolution"], {
44-
callback = add_completion
45-
})
46-
vim.lsp.handlers["PanelSolutionDone"] = vim.lsp.with(handlers["PanelSolutionDone"], {
47-
completed
48-
})
4954
callback({ isIncomplete = true })
50-
5155
end)
5256

5357
function panel.create (opts)
5458
panel = vim.tbl_deep_extend("force", panel, opts or {})
5559
panel.buf = type(panel.uri) == "number" or vim.api.nvim_create_buf(false, true)
5660
vim.api.nvim_buf_set_name(panel.buf, "copilot:///" .. tostring(panel.buf))
5761
panel.uri = vim.uri_from_bufnr(panel.buf)
62+
vim.api.nvim_create_user_command("CopilotPanel", function ()
63+
local panel_suggestions = {}
64+
local handlers = require("copilot.handlers")
65+
handlers.add_handler_callback("PanelSolution", "print", function (result)
66+
table.insert(panel_suggestions, format.clean_insertion(result.displayText))
67+
end)
68+
handlers.add_handler_callback("PanelSolutionsDone", "print", function ()
69+
local print_buf = require("copilot.print_buf").init()
70+
print_buf.print(panel_suggestions)
71+
handlers.remove_handler_callback("PanelSolution", "print")
72+
handlers.remove_handler_callback("PanelSolutionsDone", "print")
73+
end)
74+
panel.send_request()
75+
end, {})
5876
return panel
5977
end
6078

lua/copilot/print_buf.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ M.clear = function ()
3131
end
3232

3333
M.print = function (input)
34+
vim.api.nvim_buf_set_option(M.buf, "filetype", vim.bo.filetype)
3435
if not cnl_valid() or not buf_valid() then M.validate()
3536
elseif not win_valid() then M.show() end
3637
if type(input) == "table" then input = vim.inspect(input) end
@@ -98,7 +99,7 @@ M.new = function ()
9899
a.nvim_win_set_buf(M.win, M.buf)
99100
end
100101

101-
M.init = function ()
102+
M.init = function (opts)
102103
M.initialized = true
103104
local linked_win = a.nvim_get_current_win()
104105
M.print_buf = M.exists() or M.new()

0 commit comments

Comments
 (0)