|
1 | | -local source = require("copilot_cmp.source") |
| 1 | +local util = require("copilot.util") |
| 2 | + |
2 | 3 | local panel = { |
3 | 4 | n_results = 5, |
4 | | - ready = {} |
| 5 | + method = "getPanelCompletions", |
| 6 | + usecmp = false, |
| 7 | + buf = "", |
| 8 | + uri = "", |
5 | 9 | } |
6 | 10 |
|
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 = {} |
16 | 12 |
|
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) |
25 | 24 | end |
26 | | - if not found then table.insert(panel.results, solution) end |
27 | 25 | end |
28 | | -end |
29 | 26 |
|
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 = {} } |
48 | 34 | end) |
49 | 35 | 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) |
50 | 58 | return panel |
51 | 59 | end |
52 | 60 |
|
|
0 commit comments