|
1 | 1 | 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") |
5 | 2 |
|
6 | 3 | local panel = { |
7 | | - method = "getPanelCompletions", |
8 | 4 | usecmp = false, |
9 | | - client = vim.lsp.get_active_clients({name="copilot"})[1], |
| 5 | + client = {}, |
10 | 6 | buf = "", |
11 | 7 | uri = "copilot:///placeholder", |
12 | 8 | requests = {}, |
13 | 9 | } |
14 | 10 |
|
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] |
17 | 13 | if not panel.client then return end |
18 | 14 | 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) |
22 | 18 | end |
23 | 19 |
|
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 |
57 | 23 | panel.max_results = max_results or 10 |
58 | 24 | panel.buf = type(panel.uri) == "number" or vim.api.nvim_create_buf(false, true) |
59 | 25 | vim.api.nvim_buf_set_name(panel.buf, "copilot:///" .. tostring(panel.buf)) |
60 | 26 | 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 | | - |
68 | 27 | return panel |
69 | 28 | end |
70 | 29 |
|
|
0 commit comments