Skip to content

Commit a535c5e

Browse files
committed
merge changes from branch update into master
2 parents 4b59d4c + d526a41 commit a535c5e

9 files changed

Lines changed: 269 additions & 150 deletions

File tree

SettingsOpts.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# The following are config variables and their options for the copilot LSP which are set in the "settings" field of the client's startup
2+
- Enable: "enable",
3+
- InlineSuggestEnable: "inlineSuggest.enable",
4+
- ShowEditorCompletions: ["editor", "showEditorCompletions"],
5+
- EnableAutoCompletions: ["editor", "enableAutoCompletions"],
6+
- DelayCompletions: ["editor", "delayCompletions"],
7+
- FilterCompletions: ["editor", "filterCompletions"],
8+
- DisplayStyle: ["advanced", "displayStyle"],
9+
- SecretKey: ["advanced", "secret_key"],
10+
- SolutionLength: ["advanced", "length"],
11+
- Stops: ["advanced", "stops"],
12+
- Temperature: ["advanced", "temperature"],
13+
- TopP: ["advanced", "top_p"],
14+
- IndentationMode: ["advanced", "indentationMode"],
15+
- InlineSuggestCount: ["advanced", "inlineSuggestCount"],
16+
- ListCount: ["advanced", "listCount"],
17+
- DebugOverrideProxyUrl: ["advanced", "debug.overrideProxyUrl"],
18+
- DebugTestOverrideProxyUrl: ["advanced", "debug.testOverrideProxyUrl"],
19+
- DebugEnableGitHubTelemetry: ["advanced", "debug.githubCTSIntegrationEnabled"],
20+
- DebugOverrideEngine: ["advanced", "debug.overrideEngine"],
21+
- DebugShowScores: ["advanced", "debug.showScores"],
22+
- DebugOverrideLogLevels: ["advanced", "debug.overrideLogLevels"],
23+
- DebugFilterLogCategories: ["advanced", "debug.filterLogCategories"],
24+
- DebugUseSuffix: ["advanced", "debug.useSuffix"],
25+
- DebugAcceptSelfSignedCertificate: ["advanced", "debug.acceptSelfSignedCertificate"]

lua/copilot/SettingsOpts.md

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

lua/copilot/client.lua

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,41 @@
11
local M = { params = {} }
2-
local util = require("copilot.util")
2+
3+
local register_autocmd = function ()
4+
vim.api.nvim_create_autocmd({ "BufEnter" }, {
5+
callback = vim.schedule_wrap(M.buf_attach_copilot),
6+
})
7+
end
38

49
M.buf_attach_copilot = function()
510
if vim.tbl_contains(M.params.ft_disable, vim.bo.filetype) then return end
611
if not vim.bo.buflisted or not vim.bo.buftype == "" then return end
7-
local client_id = util.find_copilot_client()
8-
local buf_clients = vim.lsp.get_active_clients({ bufnr = 0 })
9-
if not buf_clients and client_id or (client_id and not buf_clients[client_id]) then
10-
vim.lsp.buf_attach_client(0, client_id)
11-
end
12-
end
13-
14-
local register_autocmd = function ()
15-
if vim.fn.has("nvim-0.7") > 0 then
16-
vim.api.nvim_create_autocmd({ "BufEnter" }, {
17-
callback = vim.schedule_wrap(M.buf_attach_copilot),
18-
})
19-
else
20-
vim.cmd("au BufEnter * lua vim.schedule(function() require('copilot.client').buf_attach_copilot() end)")
12+
local name = M.params.server_opts_overrides.name or "copilot"
13+
local client = vim.lsp.get_active_clients({name=name})[1]
14+
if client and not vim.lsp.buf_is_attached(0, client.id) then
15+
vim.lsp.buf_attach_client(0, client.id)
2116
end
2217
end
2318

2419
M.merge_server_opts = function (params)
2520
return vim.tbl_deep_extend("force", {
2621
cmd = { "node", require("copilot.util").get_copilot_path(params.plugin_manager_path) },
2722
name = "copilot",
28-
trace = "messages",
2923
root_dir = vim.loop.cwd(),
3024
autostart = true,
3125
on_init = function(_, _)
3226
vim.schedule(M.buf_attach_copilot)
3327
vim.schedule(register_autocmd)
28+
vim.schedule(function ()
29+
params.extensions[params.cmp.method](params.cmp.max_results)
30+
end)
3431
end,
35-
settings = {
36-
advanced = {
37-
listCount = 10, -- #completions for panel
38-
inlineSuggestCount = 3, -- #completions for getCompletions
39-
}
40-
},
32+
settings = params.settings,
4133
}, params.server_opts_overrides or {})
4234
end
4335

4436
M.start = function(params)
4537
M.params = params
46-
local client_id = vim.lsp.start_client(M.merge_server_opts(params))
47-
local client = vim.lsp.get_client_by_id(client_id)
48-
return { client_id, client }
38+
vim.lsp.start_client(M.merge_server_opts(params))
4939
end
5040

5141
return M
Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
local util = require("copilot.util")
22
local format = require("copilot_cmp.format")
33
local handler = require("copilot.handlers")
4-
local print_buf = require("copilot.print_panel")
4+
local print_buf = require("copilot.extensions.print_panel")
55

66
local panel = {
77
method = "getPanelCompletions",
88
usecmp = false,
99
buf = "",
10-
uri = "",
10+
uri = "copilot:///placeholder",
1111
}
1212

13-
local existing_matches= {}
14-
1513
panel.send_request = function (callback)
1614
local completion_params = util.get_completion_params()
1715
completion_params.panelId = panel.uri
1816
callback = callback or function () end
1917
vim.lsp.buf_request(0, panel.method, completion_params, callback)
2018
end
2119

20+
local existing_matches= {}
21+
2222
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 {}
@@ -53,43 +53,17 @@ panel.complete = vim.schedule_wrap(function (_, params, callback)
5353
callback({ isIncomplete = true })
5454
end)
5555

56-
function panel.create (opts)
57-
panel = vim.tbl_deep_extend("force", panel, opts or {})
56+
function panel.create (max_results)
57+
panel.max_results = max_results or 10
5858
panel.buf = type(panel.uri) == "number" or vim.api.nvim_create_buf(false, true)
5959
vim.api.nvim_buf_set_name(panel.buf, "copilot:///" .. tostring(panel.buf))
6060
panel.uri = vim.uri_from_bufnr(panel.buf)
6161

6262
vim.api.nvim_create_user_command("CopilotPanel", function ()
63+
panel.send_request()
6364
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
68-
end)
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 = {}
83-
end)
84-
vim.api.nvim_create_autocmd("WinClosed", {
85-
pattern = { tostring(print_buf.win) },
86-
callback = function ()
87-
handler.remove_handler_callback("PanelSolution", "pb")
88-
handler.remove_handler_callback("PanelSolutionsDone", "pb")
89-
end,
90-
once = true,
91-
})
9265
end, {})
66+
9367
return panel
9468
end
9569

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
local a = vim.api
2+
local cmd = vim.cmd
3+
local wo = vim.wo
4+
local handler = require("copilot.handlers")
5+
local format = require("copilot_cmp.format")
6+
local print_panel = {}
7+
8+
local set_text = function (full_text)
9+
vim.api.nvim_buf_set_lines(print_panel.bufnr, 0, -1, false, {})
10+
vim.api.nvim_buf_set_option(print_panel.bufnr, "modifiable", true)
11+
vim.api.nvim_buf_set_option(print_panel.bufnr, "readonly", false)
12+
13+
local ft = vim.bo.filetype
14+
vim.api.nvim_buf_call(print_panel.bufnr, function ()
15+
vim.bo.filetype = ft
16+
end)
17+
vim.api.nvim_buf_set_lines(print_panel.bufnr, 0, #full_text, false,full_text)
18+
19+
vim.api.nvim_buf_set_option(print_panel.bufnr, "modifiable", false)
20+
vim.api.nvim_buf_set_option(print_panel.bufnr, "readonly", true)
21+
end
22+
23+
local format_entry = function (number, str)
24+
local lines = {}
25+
for s in str:gmatch("[^\r\n]+") do table.insert(lines, s) end
26+
table.insert(lines, '')
27+
return {
28+
len = #lines,
29+
lines = lines,
30+
number = number,
31+
}
32+
end
33+
34+
local sort_items = function (items)
35+
local sorted = {}
36+
for fmt_string, value in pairs(items) do
37+
sorted[value.score] = fmt_string
38+
end
39+
return sorted
40+
end
41+
42+
local make_entries = function (items)
43+
local entries = {}
44+
items = sort_items(items)
45+
for number, str in ipairs(vim.tbl_values(items)) do
46+
table.insert(entries, format_entry(number, str))
47+
end
48+
for index, _ in ipairs(entries) do
49+
local last = entries[index-1]
50+
entries[index].linenr = last and last.linenr + last.len or 1
51+
end
52+
return entries
53+
end
54+
55+
local get_full_text = function (entries)
56+
return vim.tbl_flatten(vim.tbl_map(function(e)
57+
return e.lines
58+
end, entries))
59+
end
60+
61+
print_panel.add_panel_callbacks = function ()
62+
local items = {}
63+
64+
handler.add_handler_callback("PanelSolution", "pb", function (result)
65+
local formatted = format.deindent(result.displayText)
66+
items[formatted] = result
67+
end)
68+
69+
handler.add_handler_callback("PanelSolutionsDone", "pb", function ()
70+
if vim.tbl_isempty(items) then return end
71+
print_panel.entries = make_entries(items)
72+
print_panel.current = 1
73+
set_text(get_full_text(print_panel.entries))
74+
items = {}
75+
end)
76+
end
77+
78+
local create_win = function ()
79+
local oldwin = a.nvim_get_current_win() --record current window
80+
local height = tostring(math.floor(vim.api.nvim_win_get_height(oldwin)*.3))
81+
cmd(height .. "split")
82+
local win = a.nvim_get_current_win()
83+
wo.number = false
84+
wo.relativenumber = false
85+
wo.numberwidth = 1
86+
wo.signcolumn = "no"
87+
a.nvim_set_current_win(oldwin)
88+
return win
89+
end
90+
91+
print_panel.select = function (id)
92+
if not id then id = print_panel.current or 1 end
93+
local selection = print_panel.entries[id]
94+
vim.api.nvim_win_set_cursor(print_panel.win, {selection.linenr, 0})
95+
vim.cmd("normal zt")
96+
print_panel.current = id
97+
print_panel.linenr = selection.linenr
98+
end
99+
100+
print_panel.next = function ()
101+
local entries = print_panel.entries
102+
local current = print_panel.current
103+
local id = entries[current+1] and current+1 or 1
104+
print_panel.select(id)
105+
end
106+
107+
print_panel.prev = function ()
108+
local entries = print_panel.entries
109+
local current = print_panel.current
110+
local id = entries[current-1] and current-1 or #entries
111+
print_panel.select(id)
112+
end
113+
114+
print_panel.set_options = function ()
115+
local opts = {
116+
win = { fcs = "eob: ", signcolumn = "no", list = false},
117+
buf = { bufhidden = "wipe", buftype = "nofile", swapfile = false, buflisted = false, }
118+
}
119+
for option, value in pairs(opts.win) do
120+
vim.api.nvim_win_set_option(print_panel.win, option, value)
121+
end
122+
123+
for option, value in pairs(opts.buf) do
124+
vim.api.nvim_buf_set_option(print_panel.bufnr, option, value)
125+
end
126+
end
127+
128+
print_panel.create = function (bufnr)
129+
print_panel.bufnr = bufnr
130+
print_panel.win = create_win()
131+
print_panel.last = 1
132+
a.nvim_win_set_buf(print_panel.win, print_panel.bufnr)
133+
print_panel.set_options()
134+
print_panel.add_panel_callbacks()
135+
136+
local keymaps = {
137+
["j"] = print_panel.next,
138+
["k"] = print_panel.prev,
139+
["<CR>"] = print_panel.select,
140+
}
141+
142+
for key, fn in pairs(keymaps) do
143+
vim.keymap.set("n", key, fn, {
144+
silent = true,
145+
buffer = print_panel.bufnr,
146+
})
147+
end
148+
149+
local id = vim.api.nvim_create_augroup("Panel", {
150+
clear = false
151+
})
152+
153+
vim.api.nvim_create_autocmd({"TextChangedI", "TextChangedP"}, {
154+
callback = function ()
155+
print("callback")
156+
require("copilot.extensions.panel").send_request()
157+
end,
158+
group = id,
159+
})
160+
161+
vim.api.nvim_create_autocmd("WinEnter", {
162+
callback = function ()
163+
if print_panel.entries then
164+
print_panel.select(print_panel.entries.current)
165+
end
166+
end,
167+
buffer = print_panel.bufnr,
168+
group = id,
169+
})
170+
171+
vim.api.nvim_create_autocmd("WinClosed", {
172+
pattern = { tostring(print_panel.win) },
173+
callback = function ()
174+
-- cleanup panel triggers
175+
vim.api.nvim_create_augroup("Panel", { clear = true })
176+
handler.remove_handler_callback("PanelSolution", "pb")
177+
handler.remove_handler_callback("PanelSolutionsDone", "pb")
178+
end,
179+
once = true,
180+
group = id,
181+
})
182+
end
183+
184+
return print_panel
185+
186+
-- if you add back numbering:
187+
--
188+
-- local add_prefix_spacing = function (str, number)
189+
-- return string.rep(' ', number) .. str
190+
-- end
191+
--
192+
-- local entry_str = "[" .. number .. "]"
193+
-- for index, value in ipairs(lines) do
194+
-- if index == 1 then
195+
-- lines[index] = entry_str .. add_prefix_spacing(value, vim.o.shiftwidth)
196+
-- else
197+
-- lines[index] = add_prefix_spacing(value, vim.o.shiftwidth+entry_str:len())
198+
-- end
199+
-- end
200+

0 commit comments

Comments
 (0)