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
266 lines (230 loc) · 7.79 KB
/
Copy pathutil.lua
File metadata and controls
266 lines (230 loc) · 7.79 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
local config = require("copilot.config")
local logger = require("copilot.logger")
local M = {
VAR_ATTACH_STATUS = "copilot_lua_attach_status",
VAR_PREVIOUS_FT = "copilot_lua_previous_ft",
ATTACH_STATUS_MANUALLY_DETACHED = "manually detached",
ATTACH_STATUS_FORCE_ATTACHED = "force attached",
ATTACH_STATUS_ATTACHED = "attached",
ATTACH_STATUS_NOT_ATTACHED_PREFIX = "not attached based on ",
ATTACH_STATUS_NOT_YET_REQUESTED = "attach not yet requested",
}
---@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.lua",
-- reflects version of github/copilot-language-server-release
version = "1.508.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()
local plugin_dir = M.get_plugin_path()
return vim.fn.systemlist(string.format("git -C %s rev-parse HEAD", plugin_dir))[1]
end)
copilot_lua_version = plugin_version_ok and plugin_version or "dev"
end
return copilot_lua_version
end
---@param bufnr? integer
---@return boolean should_attach
---@return string? no_attach_reason
function M.should_attach(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
if bufnr == 0 then
bufnr = vim.api.nvim_get_current_buf()
end
if not vim.api.nvim_buf_is_valid(bufnr) then
return false, "Invalid buffer"
end
local ft = config.filetypes
local ft_disabled, ft_disabled_reason = require("copilot.client.filetypes").is_ft_disabled(vim.bo[bufnr].filetype, ft)
if ft_disabled then
return not ft_disabled, ft_disabled_reason
end
local bufname = vim.api.nvim_buf_get_name(bufnr)
local conf_attach = config.should_attach(bufnr, bufname)
if not conf_attach then
return false, "should_attach config"
end
return true
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 = {
uri = params.textDocument.uri,
version = vim.api.nvim_buf_get_var(0, "changedtick"),
relativePath = relative_path(absolute),
insertSpaces = vim.o.expandtab,
tabSize = vim.fn.shiftwidth(),
indentSize = vim.fn.shiftwidth(),
position = params.position,
}
return doc
end
-- Used by copilot.cmp so watch out if moving it
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,
version = params.doc.version,
relativePath = params.doc.relativePath,
}
params.position = params.doc.position
return params
end
function M.get_plugin_path()
local copilot_path = vim.api.nvim_get_runtime_file("lua/copilot/init.lua", false)[1]
if vim.fn.filereadable(copilot_path) ~= 0 then
return vim.fn.fnamemodify(copilot_path, ":h:h:h")
else
logger.error("could not read" .. copilot_path)
end
end
---@param str string
---@return integer
function M.strutf16len(str)
if vim.fn.exists("*strutf16len") == 1 then
return vim.fn.strutf16len(str)
else
return vim.fn.strchars(vim.fn.substitute(str, [==[\\%#=2[^\u0001-\uffff]]==], " ", "g"))
end
end
---@param bufnr integer
---@param status string
function M.set_buffer_attach_status(bufnr, status)
vim.api.nvim_buf_set_var(bufnr, M.VAR_ATTACH_STATUS, status)
end
---@param bufnr integer
---@return string
function M.get_buffer_attach_status(bufnr)
local ok, result = pcall(vim.api.nvim_buf_get_var, bufnr, M.VAR_ATTACH_STATUS)
return (ok and result) or M.ATTACH_STATUS_NOT_YET_REQUESTED
end
---@param bufnr integer
---@param filetype string
function M.set_buffer_previous_ft(bufnr, filetype)
vim.api.nvim_buf_set_var(bufnr, M.VAR_PREVIOUS_FT, filetype)
end
---@param bufnr integer
---@return string|nil
function M.get_buffer_previous_ft(bufnr)
local ok, result = pcall(vim.api.nvim_buf_get_var, bufnr, M.VAR_PREVIOUS_FT)
return (ok and result) or nil
end
---@param cmd string|string[]
---@param append string|string[]
---@return string[]
function M.append_command(cmd, append)
local full_cmd = {}
-- first append the base command
if type(cmd) == "string" then
table.insert(full_cmd, cmd)
elseif type(cmd) == "table" then
for _, part in ipairs(cmd) do
if part ~= nil then
table.insert(full_cmd, part)
end
end
end
-- then append the additional parts
if type(append) == "string" then
table.insert(full_cmd, append)
elseif type(append) == "table" then
for _, part in ipairs(append) do
if part ~= nil then
table.insert(full_cmd, part)
end
end
end
return full_cmd
end
---@param server_path string
---@param server_type ServerType
---@param node_version string|nil
---@return string[]
function M.get_node_args(server_path, server_type, node_version)
local args = { server_path, "--stdio" }
local node_version_major = tonumber(string.match(node_version or "", "^(%d+)%.")) or 0
if (server_type == "nodejs") and (node_version_major < 25) then
table.insert(args, 1, "--experimental-sqlite")
end
return args
end
---@param current_bufnr integer
---@param max_extra_chars integer
---@return string
function M.get_related_files_context(current_bufnr, max_extra_chars)
local context = {}
local total_chars = 0
local bufs = vim.api.nvim_list_bufs()
local current_ft = vim.bo[current_bufnr].filetype
for _, bufnr in ipairs(bufs) do
if bufnr ~= current_bufnr and vim.api.nvim_buf_is_valid(bufnr) and vim.api.nvim_buf_is_loaded(bufnr) then
local ft = vim.bo[bufnr].filetype
if ft == current_ft and ft ~= "" then
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, 100, false) -- Limit to first 100 lines for now
if #lines > 0 then
local path = vim.api.nvim_buf_get_name(bufnr)
local filename = vim.fn.fnamemodify(path, ":t")
local content = table.concat(lines, "\n")
local header = string.format("\n-- File: %s\n", filename)
if total_chars + #header + #content <= max_extra_chars then
table.insert(context, header .. content)
total_chars = total_chars + #header + #content
else
local remaining = max_extra_chars - total_chars - #header
if remaining > 100 then
table.insert(context, header .. content:sub(1, remaining))
end
break
end
end
end
end
end
return table.concat(context, "\n")
end
---@param bufnr integer
---@param position { line: integer, character: integer }
---@return string prefix
---@return string suffix
function M.get_fim_context(bufnr, position)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local prefix_lines = {}
for i = 1, position.line do
table.insert(prefix_lines, lines[i])
end
local current_line = lines[position.line + 1] or ""
table.insert(prefix_lines, current_line:sub(1, position.character))
local prefix = table.concat(prefix_lines, "\n")
local suffix_lines = {}
table.insert(suffix_lines, current_line:sub(position.character + 1))
for i = position.line + 2, #lines do
table.insert(suffix_lines, lines[i])
end
local suffix = table.concat(suffix_lines, "\n")
-- DeepSeek suggests around 2048 characters for FIM context.
return prefix:sub(-2048), suffix:sub(1, 2048)
end
return M