Skip to content

Commit 490c730

Browse files
committed
Reorg and consolidate codebase, move completion logic from copilot.lua into copilot_cmp
1 parent 82e696b commit 490c730

File tree

3 files changed

+31
-61
lines changed

3 files changed

+31
-61
lines changed

lua/copilot/client.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ M.buf_attach_copilot = function()
1313
local client = vim.lsp.get_active_clients({name=name})[1]
1414
if client and not vim.lsp.buf_is_attached(0, client.id) then
1515
vim.lsp.buf_attach_client(0, client.id)
16+
client.completion_function = M.params.extensions
1617
end
1718
end
1819

@@ -25,7 +26,6 @@ M.merge_server_opts = function (params)
2526
on_init = function(_, _)
2627
vim.schedule(M.buf_attach_copilot)
2728
vim.schedule(register_autocmd)
28-
params.extensions[params.cmp.method](params.cmp.max_results)
2929
end,
3030
settings = params.settings,
3131
}, params.server_opts_overrides or {})

lua/copilot/extensions/panel.lua

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,29 @@
11
local util = require("copilot.util")
2-
local format = require("copilot_cmp.format")
3-
local handler = require("copilot.handlers")
4-
local print_buf = require("copilot.extensions.print_panel")
52

63
local panel = {
7-
method = "getPanelCompletions",
84
usecmp = false,
9-
client = vim.lsp.get_active_clients({name="copilot"})[1],
5+
client = {},
106
buf = "",
117
uri = "copilot:///placeholder",
128
requests = {},
139
}
1410

15-
panel.send_request = function (callback)
16-
panel.client = not vim.tbl_isempty(panel.client) and panel.client or vim.lsp.get_active_clients({name="copilot"})[1]
11+
panel.send_request = function (opts)
12+
local client = opts.client or not vim.tbl_isempty(panel.client) and panel.client or vim.lsp.get_active_clients({name="copilot"})[1]
1713
if not panel.client then return end
1814
local completion_params = util.get_completion_params()
19-
completion_params.panelId = panel.uri
20-
callback = callback or function () end
21-
return panel.client.rpc.request(panel.method, completion_params, callback)
15+
completion_params.panelId = opts.uri or panel.uri
16+
local callback = opts.callback or function () end
17+
return client.rpc.request("getPanelCompletions", completion_params, callback)
2218
end
2319

24-
local existing_matches= {}
25-
26-
local verify_existing = function (context)
27-
existing_matches[context.bufnr] = existing_matches[context.bufnr] or {}
28-
existing_matches[context.bufnr][context.cursor.row] = existing_matches[context.bufnr][context.cursor.row] or {}
29-
end
30-
31-
panel.complete = vim.schedule_wrap(function (_, params, callback)
32-
local context = params.context
33-
verify_existing(context)
34-
local sent, id
35-
36-
local add_completion = function (result)
37-
result.text = result.displayText
38-
local formatted = format.format_item(params, result)
39-
existing_matches[context.bufnr][context.cursor.row][formatted.label] = formatted
40-
end
41-
42-
local completed = function ()
43-
callback({
44-
isIncomplete = false,
45-
items = vim.tbl_values(existing_matches[context.bufnr][context.cursor.row])
46-
})
47-
end
48-
49-
handler.add_handler_callback("PanelSolution", "cmp", add_completion)
50-
handler.add_handler_callback("PanelSolutionsDone", "cmp", completed)
51-
if sent and id then panel.client.rpc.cancel_request(id) end
52-
sent, id = panel.send_request()
53-
callback({ isIncomplete = true })
54-
end)
55-
56-
function panel.create (max_results)
20+
function panel.create (client, max_results)
21+
panel.client = client or vim.lsp.get_active_clients({name="copilot"})[1]
22+
if not client then print("Error, copilot not running") end
5723
panel.max_results = max_results or 10
5824
panel.buf = type(panel.uri) == "number" or vim.api.nvim_create_buf(false, true)
5925
vim.api.nvim_buf_set_name(panel.buf, "copilot:///" .. tostring(panel.buf))
6026
panel.uri = vim.uri_from_bufnr(panel.buf)
61-
panel.client = vim.lsp.get_active_clients({name="copilot"})
62-
63-
vim.api.nvim_create_user_command("CopilotPanel", function ()
64-
panel.send_request()
65-
print_buf.create(panel.buf)
66-
end, {})
67-
6827
return panel
6928
end
7029

lua/copilot/init.lua

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@ local client = require("copilot.client")
33

44
local defaults = {
55
cmp = {
6-
method = "getCompletionsCycling",
7-
max_results = 5,
6+
enabled = true,
7+
method = "getPanelCompletions",
88
},
9-
extensions = {
10-
getPanelCompletions = function ()
11-
require("copilot_cmp").setup(require("copilot.extensions.panel").complete)
12-
end,
13-
getCompletionsCycling = function ()
14-
require("copilot_cmp").setup()
15-
end,
9+
panel = { -- no config options yet
10+
enabled = true,
1611
},
1712
ft_disable = {},
1813
plugin_manager_path = vim.fn.stdpath("data") .. "/site/pack/packer",
@@ -25,6 +20,14 @@ local defaults = {
2520
}
2621
}
2722

23+
local create_cmds = function (_)
24+
vim.api.nvim_create_user_command("CopilotPanel", function ()
25+
local panel = require("copilot.extensions.panel").create()
26+
panel.send_request()
27+
require("copilot.extensions.print_panel").create(panel.buf)
28+
end, {})
29+
end
30+
2831
local config_handler = function(opts)
2932
local user_config = opts and vim.tbl_deep_extend("force", defaults, opts) or defaults
3033
return user_config
@@ -34,7 +37,15 @@ M.setup = function(opts)
3437
local user_config = config_handler(opts)
3538
vim.schedule(function ()
3639
client.start(user_config)
37-
require("copilot.extensions.panel").create()
40+
41+
if user_config.cmp.enabled then
42+
require("copilot_cmp").setup(user_config.cmp.method)
43+
end
44+
45+
if user_config.panel.enabled then
46+
create_cmds(user_config)
47+
end
48+
3849
end)
3950
end
4051

0 commit comments

Comments
 (0)