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
143 lines (124 loc) · 3.29 KB
/
util.lua
File metadata and controls
143 lines (124 loc) · 3.29 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
local M = {}
local id = 0
function M.get_next_id()
id = id + 1
return id
end
---@return copilot_set_editor_info_params
function M.get_editor_info()
local info = {
editorInfo = {
name = "Neovim",
version = string.match(vim.fn.execute("version"), "NVIM v(%S+)"),
},
editorPluginInfo = {
name = "copilot.vim",
version = '1.5.3',
},
}
return info
end
-- keep for debugging reasons
local get_capabilities = function ()
return {
capabilities = {
textDocumentSync = {
change = 2,
openClose = true
},
workspace = {
workspaceFolders = {
changeNotifications = true,
supported = true
}
}
}
}
end
M.get_copilot_client = function()
-- vim.lsp.get_active_clients({name="copilot"}) -- not in 0.7
for _, client in pairs(vim.lsp.get_active_clients()) do
if client.name == "copilot" then return client end
end
end
local eol_by_fileformat = {
unix = "\n",
dos = "\r\n",
mac = "\r",
}
local language_normalization_map = {
text = "plaintext",
javascriptreact = "javascript",
jsx = "javascript",
typescriptreact = "typescript",
}
local function language_for_file_type(filetype)
-- trim filetypes after dot, e.g. `yaml.gotexttmpl` -> `yaml`
local ft = string.gsub(filetype, "%..*", "")
if not ft or ft == "" then
ft = "text"
end
return language_normalization_map[ft] or ft
end
local function relative_path(absolute)
local relative = vim.fn.fnamemodify(absolute, ":.")
if string.sub(relative, 0, 1) == "/" then
return vim.fn.fnamemodify(absolute, ":t")
end
return relative
end
function M.get_doc()
local absolute = vim.api.nvim_buf_get_name(0)
local params = vim.lsp.util.make_position_params(0, "utf-16") -- copilot server uses utf-16
local doc = {
languageId = language_for_file_type(vim.bo.filetype),
path = absolute,
uri = params.textDocument.uri,
relativePath = relative_path(absolute),
insertSpaces = vim.o.expandtab,
tabSize = vim.fn.shiftwidth(),
indentSize = vim.fn.shiftwidth(),
position = params.position,
}
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
if vim.bo.endofline and vim.bo.fixendofline then
table.insert(lines, "")
end
doc.source = table.concat(lines, eol_by_fileformat[vim.bo.fileformat] or "\n")
return doc
end
function M.get_doc_params(overrides)
overrides = overrides or {}
local params = vim.tbl_extend("keep", {
doc = vim.tbl_extend("force", M.get_doc(), overrides.doc or {}),
}, overrides)
params.textDocument = {
uri = params.doc.uri,
languageId = params.doc.languageId,
relativePath = params.doc.relativePath,
}
params.position = params.doc.position
return params
end
-- use `require("copilot.util").get_doc_params()`
---@deprecated
M.get_completion_params = function(opts)
return M.get_doc_params(opts)
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
M.auth = function ()
local c = M.get_copilot_client()
if not c then
print("[Copilot] not running yet!")
return
end
require("copilot.auth").setup(c)
end
return M