Skip to content

Commit ba3bc44

Browse files
committed
Added CopilotPanel command to preview options in window
1 parent 8c1aa2d commit ba3bc44

4 files changed

Lines changed: 88 additions & 150 deletions

File tree

lua/copilot/handlers.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ local lsp_handlers = {
77

88
local handlers = {
99
["PanelSolution"] = function (_, result, _, config)
10-
for _, callback in pairs(config.callbacks) do
11-
callback(result)
10+
if result then
11+
for _, callback in pairs(config.callbacks) do
12+
callback(result)
13+
end
1214
end
1315
end,
1416
["PanelSolutionsDone"] = function (_, _, _, config)

lua/copilot/panel.lua

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
local util = require("copilot.util")
22
local format = require("copilot_cmp.format")
3+
local handler = require("copilot.handlers")
4+
local print_buf = require("copilot.print_panel")
35

46
local panel = {
57
method = "getPanelCompletions",
68
usecmp = false,
7-
cache_line = true,
89
buf = "",
910
uri = "",
1011
}
@@ -18,36 +19,34 @@ panel.send_request = function (callback)
1819
vim.lsp.buf_request(0, panel.method, completion_params, callback)
1920
end
2021

21-
panel.complete = vim.schedule_wrap(function (_, params, callback)
22-
local context = params.context
22+
local verify_existing = function (context)
2323
existing_matches[context.bufnr] = existing_matches[context.bufnr] or {}
2424
existing_matches[context.bufnr][context.cursor.row] = existing_matches[context.bufnr][context.cursor.row] or {}
25+
end
26+
27+
panel.complete = vim.schedule_wrap(function (_, params, callback)
28+
local context = params.context
29+
verify_existing(context)
2530

2631
local add_completion = function (result)
27-
if result then
28-
result.text = result.displayText
29-
local formatted = format.format_item(params, result)
30-
existing_matches[context.bufnr][context.cursor.row][formatted.label] = formatted
31-
vim.schedule(function() callback({
32-
isIncomplete = true,
33-
items = vim.tbl_values(existing_matches[context.bufnr][context.cursor.row])
34-
}) end)
35-
end
32+
result.text = result.displayText
33+
local formatted = format.format_item(params, result)
34+
existing_matches[context.bufnr][context.cursor.row][formatted.label] = formatted
35+
callback({
36+
isIncomplete = true,
37+
items = vim.tbl_values(existing_matches[context.bufnr][context.cursor.row])
38+
})
3639
end
3740

3841
local completed = function ()
39-
vim.schedule(function() callback({
42+
callback({
4043
isIncomplete = false,
4144
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
45+
})
4646
end
4747

48-
local handler = require("copilot.handlers").add_handler_callback
49-
handler("PanelSolution", "cmp", add_completion)
50-
handler("PanelSolutionsDone", "cmp", completed)
48+
handler.add_handler_callback("PanelSolution", "cmp", add_completion)
49+
handler.add_handler_callback("PanelSolutionsDone", "cmp", completed)
5150

5251
panel.send_request()
5352

@@ -59,19 +58,38 @@ function panel.create (opts)
5958
panel.buf = type(panel.uri) == "number" or vim.api.nvim_create_buf(false, true)
6059
vim.api.nvim_buf_set_name(panel.buf, "copilot:///" .. tostring(panel.buf))
6160
panel.uri = vim.uri_from_bufnr(panel.buf)
61+
6262
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))
63+
print_buf.create(panel.buf)
64+
local items = {}
65+
handler.add_handler_callback("PanelSolution", "pb", function (result)
66+
local formatted = format.deindent(result.displayText)
67+
items[formatted] = 1
6768
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")
69+
handler.add_handler_callback("PanelSolutionsDone", "pb", function ()
70+
local item_list = vim.tbl_add_reverse_lookup(vim.tbl_keys(items))
71+
local result_text = vim.tbl_flatten(vim.tbl_map(function(v)
72+
local s = vim.fn.split(v, '\n')
73+
local text = vim.tbl_map(function (t)
74+
local number_string = "[" .. item_list[v] .. "]"
75+
local str = (s[1] == t and number_string .. string.rep(' ', vim.o.shiftwidth)) or string.rep(' ', vim.o.shiftwidth+string.len(number_string))
76+
return str .. t
77+
end, s)
78+
table.insert(text, '')
79+
return text
80+
end, item_list))
81+
print_buf.set_text(result_text)
82+
items = {}
7383
end)
74-
panel.send_request()
84+
vim.api.nvim_create_autocmd("WinClosed", {
85+
pattern = { tostring(print_buf.win) },
86+
callback = function ()
87+
print("CopilotPanel closed")
88+
handler.remove_handler_callback("PanelSolution", "pb")
89+
handler.remove_handler_callback("PanelSolutionsDone", "pb")
90+
end,
91+
once = true,
92+
})
7593
end, {})
7694
return panel
7795
end

lua/copilot/print_buf.lua

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

lua/copilot/print_panel.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
local a = vim.api
2+
local cmd = vim.cmd
3+
local wo = vim.wo
4+
5+
local print_buf = {}
6+
7+
print_buf.set_text = function (full_text)
8+
local ft = vim.bo.filetype
9+
vim.api.nvim_buf_call(print_buf.bufnr, function ()
10+
vim.bo.filetype = ft
11+
end)
12+
vim.api.nvim_buf_set_lines(print_buf.bufnr, 0, -1, false, {})
13+
vim.api.nvim_buf_set_var(print_buf.bufnr, "modifiable", 1)
14+
vim.api.nvim_buf_set_var(print_buf.bufnr, "readonly", 0)
15+
vim.api.nvim_buf_set_lines(print_buf.bufnr, 0, #full_text, false,full_text)
16+
end
17+
18+
local create_win = function ()
19+
local oldwin = a.nvim_get_current_win() --record current window
20+
cmd("vsplit")
21+
local win = a.nvim_get_current_win()
22+
wo.number = false
23+
wo.relativenumber = false
24+
wo.numberwidth = 1
25+
wo.signcolumn = "no"
26+
a.nvim_set_current_win(oldwin)
27+
return win
28+
end
29+
30+
print_buf.create = function (bufnr)
31+
print_buf.bufnr = bufnr
32+
print_buf.win = create_win()
33+
a.nvim_win_set_buf(print_buf.win, print_buf.bufnr)
34+
end
35+
36+
return print_buf

0 commit comments

Comments
 (0)