forked from zbirenbaum/copilot.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.lua
More file actions
74 lines (63 loc) · 1.81 KB
/
util.lua
File metadata and controls
74 lines (63 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
local M = {}
local format_pos = function()
local pos = vim.api.nvim_win_get_cursor(0)
return { character = pos[2], line = pos[1] - 1 }
end
local get_relfile = function()
local file, _ = string.gsub(vim.api.nvim_buf_get_name(0), vim.loop.cwd() .. "/", "")
return file
end
M.find_copilot_client = function()
for _, client in ipairs(vim.lsp.get_active_clients()) do
if client.name == "copilot" then
return client.id
end
end
end
M.find_copilot_buf_client = function()
for _, client in ipairs(vim.lsp.buf_get_clients(0)) do
if client.name == "copilot" then
return client.id
end
end
end
M.get_completion_params = function()
local params = {
options = vim.empty_dict(),
doc = {
relativePath = get_relfile(),
source = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), "\n"),
languageId = vim.bo.filetype,
insertSpaces = true,
tabsize = vim.bo.shiftwidth,
indentsize = vim.bo.shiftwidth,
position = format_pos(),
path = vim.api.nvim_buf_get_name(0),
},
}
return params
end
M.get_copilot_path = function(plugin_path)
for _, loc in ipairs({ "/opt", "/start", "" }) do
local copilot_path = plugin_path .. loc .. "/copilot.lua/copilot/index.js"
if vim.fn.filereadable(copilot_path) ~= 0 then
return copilot_path
end
end
end
local function completion_handler(_, result, _, _)
print(vim.inspect(result))
end
M.register_completion_handler = function(handler)
if handler then
completion_handler = handler
end
end
M.send_completion_request = function()
local params = M.get_completion_params()
vim.lsp.buf_request(0, "getCompletions", params, completion_handler)
end
M.create_request_autocmd = function(group)
vim.api.nvim_create_autocmd(group, { callback = M.send_completion_request })
end
return M