Skip to content

Commit a226d53

Browse files
committed
add support for post-completion format execution
1 parent d16f222 commit a226d53

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

lua/copilot_cmp/format.lua

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local formatter= {}
22

3+
local close_chars = { [')'] = true, [']'] = true, ['}'] = true }
34
local shorten = function (str)
45
local short_prefix = string.sub(str, 0, 20)
56
local short_suffix = string.sub(str, string.len(str)-15, string.len(str))
@@ -44,14 +45,25 @@ local check_exists = function (text_list)
4445
end
4546

4647
local format_insert_text = function (deindented, ctx)
48+
-- if ctx.cursor_after_line[1]
49+
4750
local indent_string = get_indent_string(ctx)
51+
4852
local text_list = str_to_list(deindented)
53+
54+
--do this before check_exists so that we end after existing
55+
local fmt_info = {
56+
startl = ctx.cursor.row,
57+
endl = #text_list + ctx.cursor.row - 1,
58+
n_lines = #text_list,
59+
}
4960
-- this is necessary because first line starts at cursor pos
5061
for line_idx = 2, #text_list do
5162
text_list[line_idx] = indent_string .. text_list[line_idx]
5263
end
5364
text_list = check_exists(text_list)
54-
return table.concat(text_list, '\n')
65+
local fmt_string = table.concat(text_list, '\n')
66+
return fmt_string, fmt_info
5567
end
5668

5769
local format_label_text = function (item)
@@ -61,12 +73,18 @@ local format_label_text = function (item)
6173
end
6274

6375
local format_item = function(item, params)
76+
-- local after_without_ws = params.context.cursor_after_line:match("^%s*(.-)%s*$")
77+
-- if #after_without_ws > 0 and close_chars[after_without_ws] then
78+
-- item.text = item.text .. after_without_ws .. '\n'
79+
-- end
6480
local deindented = formatter.deindent(item.text)
65-
local insert_text = format_insert_text(deindented, params.context)
81+
local insert_text, fmt_info = format_insert_text(deindented, params.context)
6682
local label_text = format_label_text(item)
83+
6784
return {
6885
copilot = true, -- for comparator, only availiable in panel, not cycling
6986
score = item.score or nil,
87+
fmt_info = fmt_info,
7088
label = label_text,
7189
filterText = label_text:sub(0, label_text:len()-1),
7290
kind = 1,

lua/copilot_cmp/init.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,19 @@ local find_buf_client = function()
3939
end
4040
end
4141

42-
M.setup = function(completion_method)
42+
M.setup = function(opts)
4343
M._on_insert_enter = function()
4444
local cmp = require("cmp")
4545
local copilot = find_buf_client()
4646
if copilot and not M.client_source_map[copilot.id] then
47-
local s = source.new(copilot, completion_method)
47+
local s = source.new(copilot, opts)
4848
if s:is_available() then
4949
M.client_source_map[copilot.id] = cmp.register_source("copilot", s)
5050
end
5151
end
5252
end
5353

5454
vim.api.nvim_create_autocmd({ "InsertEnter" }, { callback = M._on_insert_enter })
55-
5655
end
5756

5857
return M

lua/copilot_cmp/source.lua

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@ local source = {}
33
function source:get_keyword_pattern()
44
return "\\w\\+.*"
55
end
6+
67
source.get_trigger_characters = function()
78
return { "\t", "\n", ".", ":", "(", "'", '"', "[", ",", "#", "*", "@", "|", "=", "-", "{", "/", "\\", " ", "+", "?"}
89
end
910

11+
source.autofmt = function (_, completion_item, callback)
12+
vim.schedule(function ()
13+
local fmt_info = completion_item.fmt_info
14+
vim.api.nvim_win_set_cursor(0, {fmt_info.startl, 0})
15+
vim.cmd("silent! normal " .. tostring(fmt_info.n_lines) .. "==")
16+
local endl_contents = vim.api.nvim_buf_get_lines(0, fmt_info.endl-1, fmt_info.endl+1, false)[1] or ""
17+
vim.api.nvim_win_set_cursor(0, {fmt_info.endl, #endl_contents})
18+
end)
19+
return callback()
20+
end
21+
1022
source.is_available = function(self)
1123
-- client is stopped.
1224
if self.client.is_stopped() then
@@ -22,9 +34,15 @@ source.is_available = function(self)
2234
return true
2335
end
2436

25-
source.new = function(client, completion_fn)
37+
source.new = function(client, opts)
38+
opts = opts or {}
39+
local completion_fn = opts.completion_fn
40+
print(vim.inspect(opts))
41+
2642
local completion_functions = require("copilot_cmp.completion_functions")
2743
local self = setmetatable({ timer = vim.loop.new_timer() }, { __index = source })
44+
45+
self.execute = opts.autofmt and source.autofmt or nil
2846
self.client = client
2947
self.request_ids = {}
3048
self.complete = completion_fn and completion_functions.init(completion_fn) or completion_functions.init("getCompletionsCycling")

0 commit comments

Comments
 (0)