Skip to content

Commit 9d3e9d0

Browse files
committed
panel completions for cmp working
1 parent c331a58 commit 9d3e9d0

File tree

6 files changed

+65
-59
lines changed

6 files changed

+65
-59
lines changed

lua/copilot/api.lua

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

lua/copilot/client.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,10 @@ M.merge_server_opts = function (params)
3232
vim.schedule(M.buf_attach_copilot)
3333
vim.schedule(register_autocmd)
3434
end,
35-
handlers = params.panel and {
36-
["PanelSolution"] = params.panel.save_completions,
37-
},
3835
settings = {
3936
advanced = {
4037
listCount = 10, -- #completions for panel
41-
inlineSuggestCount = 1, -- #completions for getCompletions
38+
inlineSuggestCount = 3, -- #completions for getCompletions
4239
}
4340
},
4441
}, params.server_opts_overrides or {})

lua/copilot/handlers.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
local oldprint = print
2+
local print = function (x) oldprint(vim.inspect(x)) end
3+
4+
local lsp_handlers = {
5+
["PanelSolution"] = function (_, result, _, config)
6+
config.callback(result)
7+
end,
8+
9+
["PanelSolutionsDone"] = function (err, result, ctx, config)
10+
config.callback()
11+
end
12+
}
13+
14+
return lsp_handlers

lua/copilot/init.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ M.setup = function(opts)
3030
local user_config = config_handler(opts)
3131
vim.schedule(function () client.start(user_config) end)
3232
if user_config.cmp_method == "getPanelCompletions" then
33-
require("copilot.api").panel.create()
33+
local panel = require("copilot.panel").create()
34+
print(vim.inspect(panel))
35+
require("copilot_cmp").setup(panel.complete)
3436
elseif user_config.cmp_method == "getCompletionsCycling" then
3537
require("copilot_cmp").setup()
3638
end

lua/copilot/panel.lua

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,60 @@
1-
local source = require("copilot_cmp.source")
1+
local util = require("copilot.util")
2+
23
local panel = {
34
n_results = 5,
4-
ready = {}
5+
method = "getPanelCompletions",
6+
usecmp = false,
7+
buf = "",
8+
uri = "",
59
}
610

7-
local util = require("copilot.util")
8-
local completions = require("copilot_cmp")
9-
10-
local results_callback = function (err, result)
11-
panel.n_results = result.solutionCountTarget
12-
end
13-
14-
local notify_callback = function (err, result)
15-
end
11+
local completions = {}
1612

17-
panel.save_completions = function (_, solution)
18-
if solution then
19-
local found = false
20-
for i, item in ipairs(panel.results) do
21-
if item.displayText == solution.displayText then
22-
panel.results[i] = not found and solution or nil
23-
found = true
24-
end
13+
panel.complete = vim.schedule_wrap(function (_, params, callback)
14+
local add_completion = function (result)
15+
local format = require("copilot_cmp.format").format_item
16+
if result then
17+
result.text = result.displayText
18+
local formatted = format(params, result)
19+
completions[formatted.label] = formatted
20+
vim.schedule(function() callback({
21+
isIncomplete = true,
22+
items = vim.tbl_values(completions)
23+
}) end)
2524
end
26-
if not found then table.insert(panel.results, solution) end
2725
end
28-
end
2926

30-
function panel.init (client_info)
31-
panel.client_id = client_info.client_id or util.find_copilot_client()
32-
panel.client = client_info and client_info.client
33-
panel.results = {}
34-
panel.cur_req = nil
35-
panel.method = "getPanelCompletions"
36-
panel.__index = function (i) return panel.results[i] end
37-
source.complete = function (_, params, callback)
38-
vim.lsp.buf_request(0, panel.method, util.get_completion_params(panel.method), function(err, result)
39-
if result then
40-
if panel.results and #panel.results >= 0 then
41-
local entries = source.format_completions(_, params, panel.results)
42-
callback(entries)
43-
panel.results = {}
44-
else
45-
callback({ isIncomplete = true })
46-
end
47-
end
27+
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 = {} }
4834
end)
4935
end
36+
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)
40+
41+
local handlers = require("copilot.handlers")
42+
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+
})
49+
callback({ isIncomplete = true })
50+
51+
end)
52+
53+
function panel.create (opts)
54+
panel = vim.tbl_deep_extend("force", panel, opts or {})
55+
panel.buf = type(panel.uri) == "number" or vim.api.nvim_create_buf(false, true)
56+
vim.api.nvim_buf_set_name(panel.buf, "copilot:///" .. tostring(panel.buf))
57+
panel.uri = vim.uri_from_bufnr(panel.buf)
5058
return panel
5159
end
5260

lua/copilot/util.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ M.get_completion_params = function(method)
4141
local uri = vim.uri_from_bufnr(0)
4242
local params = {
4343
options = vim.empty_dict(),
44-
panelId = (method == "getPanelCompletions") and "fuckyou" or nil,
4544
doc = {
4645
source = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), "\n"),
4746
relativePath = rel_path,

0 commit comments

Comments
 (0)