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
252 lines (213 loc) · 6.14 KB
/
util.lua
File metadata and controls
252 lines (213 loc) · 6.14 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
local config = require("copilot.config")
local M = {}
local id = 0
function M.get_next_id()
id = id + 1
return id
end
---@return { editorInfo: copilot_editor_info, editorPluginInfo: copilot_editor_plugin_info }
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.7.0",
},
}
return info
end
local copilot_lua_version = nil
function M.get_copilot_lua_version()
if not copilot_lua_version then
local plugin_version_ok, plugin_version = pcall(function()
return vim.fn.systemlist("git rev-parse HEAD")[1]
end)
copilot_lua_version = plugin_version_ok and plugin_version or "dev"
end
return copilot_lua_version
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 internal_filetypes = {
yaml = false,
markdown = false,
help = false,
gitcommit = false,
gitrebase = false,
hgcommit = false,
svn = false,
cvs = false,
["."] = false,
}
---@param ft string
---@param filetypes table<string, boolean>
---@return boolean ft_disabled
---@return string? ft_disabled_reason
local function is_ft_disabled(ft, filetypes)
if filetypes[ft] ~= nil then
return not filetypes[ft], string.format("'filetype' %s rejected by config filetypes[%s]", ft, ft)
end
local short_ft = string.gsub(ft, "%..*", "")
if filetypes[short_ft] ~= nil then
return not filetypes[short_ft], string.format("'filetype' %s rejected by config filetypes[%s]", ft, short_ft)
end
if filetypes["*"] ~= nil then
return not filetypes["*"], string.format("'filetype' %s rejected by config filetypes[%s]", ft, "*")
end
if internal_filetypes[short_ft] ~= nil then
return not internal_filetypes[short_ft], string.format("'filetype' %s rejected by internal_filetypes[%s]", ft, short_ft)
end
return false
end
---@return boolean should_attach
---@return string? no_attach_reason
function M.should_attach()
local ft_disabled, ft_disabled_reason = is_ft_disabled(vim.bo.filetype, config.get("filetypes"))
if ft_disabled then
return not ft_disabled, ft_disabled_reason
end
if not vim.bo.buflisted then
return false, "buffer not 'buflisted'"
end
if not vim.bo.buftype == "" then
return false, "buffer 'buftype' is " .. vim.bo.buftype
end
return true
end
function M.is_attached(client)
client = client or M.get_copilot_client()
return client and vim.lsp.buf_is_attached(0, client.id) or false
end
local eol_by_fileformat = {
unix = "\n",
dos = "\r\n",
mac = "\r",
}
local language_normalization_map = {
bash = "shellscript",
bst = "bibtex",
cs = "csharp",
cuda = "cuda-cpp",
dosbatch = "bat",
dosini = "ini",
gitcommit = "git-commit",
gitrebase = "git-rebase",
make = "makefile",
objc = "objective-c",
objcpp = "objective-cpp",
ps1 = "powershell",
raku = "perl6",
sh = "shellscript",
text = "plaintext",
}
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
---@return copilot_editor_configuration
function M.get_editor_configuration()
local conf = config.get()
local filetypes = vim.deepcopy(conf.filetypes)
if filetypes["*"] == nil then
filetypes = vim.tbl_deep_extend("keep", filetypes, internal_filetypes)
end
---@type string[]
local disabled_filetypes = vim.tbl_filter(function(ft)
return filetypes[ft] == false
end, vim.tbl_keys(filetypes))
table.sort(disabled_filetypes)
return {
enableAutoCompletions = not not (conf.panel.enabled or conf.suggestion.enabled),
disabledLanguages = disabled_filetypes,
}
end
M.get_copilot_path = function()
local copilot_path = vim.api.nvim_get_runtime_file('copilot/index.js', false)[1]
if vim.fn.filereadable(copilot_path) ~= 0 then
return copilot_path
else
print("[Copilot] could not read" .. copilot_path)
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