forked from Davidyz/VectorCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcacher.lua
More file actions
343 lines (326 loc) · 9.83 KB
/
Copy pathcacher.lua
File metadata and controls
343 lines (326 loc) · 9.83 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
local M = {}
local vc_config = require("vectorcode.config")
local notify_opts = vc_config.notify_opts
---@class VectorCodeCache
---@field enabled boolean
---@field retrieval VectorCodeResult[]?
---@field options VectorCodeRegisterOpts
---@field jobs table<integer, boolean>
---@field last_run integer?
---@param bufnr integer
local function kill_jobs(bufnr)
local cache = vim.b[bufnr].vectorcode_cache ---@cast cache VectorCodeCache
if cache ~= nil then
for job_pid, is_running in pairs(cache.jobs) do
if is_running == true then
vim.uv.kill(job_pid, 15)
end
end
end
end
---@param query_message string|string[]
---@param buf_nr integer
local function async_runner(query_message, buf_nr)
if not vim.b[buf_nr].vectorcode_cache.enabled then
return
end
local cache = vim.api.nvim_buf_get_var(buf_nr, "vectorcode_cache")
vim.api.nvim_buf_set_var(buf_nr, "vectorcode_cache", cache)
local args = {
"query",
"--pipe",
"-n",
tostring(cache.options.n_query),
}
if type(query_message) == "string" then
query_message = { query_message }
end
vim.list_extend(args, query_message)
if cache.exclude_this then
vim.list_extend(args, { "--exclude", vim.api.nvim_buf_get_name(buf_nr) })
end
local job = require("plenary.job"):new({
command = "vectorcode",
args = args,
detached = true,
on_start = function() end,
on_exit = function(self, code, signal)
if not M.buf_is_registered(buf_nr) then
return
end
vim.schedule(function()
---@type VectorCodeCache
local cache = vim.api.nvim_buf_get_var(buf_nr, "vectorcode_cache")
cache.jobs[self.pid] = nil
vim.api.nvim_buf_set_var(buf_nr, "vectorcode_cache", cache)
end)
local ok, json = pcall(
vim.json.decode,
table.concat(self:result()) or "[]",
{ array = true, object = true }
)
if not ok or code ~= 0 then
vim.schedule(function()
if vim.api.nvim_buf_get_var(buf_nr, "vectorcode_cache").options.notify then
vim.notify(
"Retrieval failed:\n" .. table.concat(self:result()),
vim.log.levels.WARN,
notify_opts
)
end
end)
return
end
vim.schedule(function()
---@type VectorCodeCache
local cache = vim.api.nvim_buf_get_var(buf_nr, "vectorcode_cache")
cache.retrieval = json or {}
vim.api.nvim_buf_set_var(buf_nr, "vectorcode_cache", cache)
if cache.options.notify then
vim.notify(
("Caching for buffer %d has completed."):format(buf_nr),
vim.log.levels.INFO,
notify_opts
)
end
end)
end,
})
vim.schedule(function()
job:start()
---@type VectorCodeCache
local cache = vim.api.nvim_buf_get_var(buf_nr, "vectorcode_cache")
cache.jobs[job.pid] = true
if cache.options.notify then
vim.notify(
("Caching for buffer %d has started."):format(buf_nr),
vim.log.levels.INFO,
notify_opts
)
end
cache.last_run = vim.uv.clock_gettime("realtime").sec
vim.api.nvim_buf_set_var(buf_nr, "vectorcode_cache", cache)
end)
end
---@class VectorCodeRegisterOpts: VectorCodeConfig
---@field query_cb VectorCodeQueryCallback
---@field debounce integer?
---@field run_on_register boolean
M.register_buffer = vc_config.check_cli_wrap(
---@param bufnr integer?
---@param opts VectorCodeRegisterOpts?
---@param query_cb VectorCodeQueryCallback?
---@param events string[]?
---@param debounce integer?
function(bufnr, opts, query_cb, events, debounce)
if bufnr == 0 or bufnr == nil then
bufnr = vim.api.nvim_get_current_buf()
end
if query_cb ~= nil or events ~= nil or debounce ~= nil then
vim.schedule(function()
vim.deprecate(
"query_cb, events and debounce",
"opts",
"0.3.0",
"VectorCode",
true
)
end)
end
if M.buf_is_registered(bufnr) then
opts =
vim.tbl_deep_extend("force", opts or {}, vim.b[bufnr].vectorcode_cache.options)
end
opts = vim.tbl_deep_extend(
"force",
{
query_cb = require("vectorcode.utils").surrounding_lines_cb(-1),
events = { "BufWritePost", "InsertEnter", "BufReadPost" },
debounce = 10,
n_query = 1,
notify = false,
timeout_ms = 5000,
exclude_this = true,
run_on_register = false,
},
vc_config.get_user_config(),
opts or {},
{ query_cb = query_cb, events = events, debounce = debounce }
)
if M.buf_is_registered(bufnr) then
-- update the options and/or query_cb
vim.schedule(function()
local cache = vim.b[bufnr].vectorcode_cache
cache.options = vim.tbl_deep_extend("force", cache.options, opts or {})
vim.api.nvim_buf_set_var(
bufnr or vim.api.nvim_get_current_buf(),
"vectorcode_cache",
cache
)
end)
else
vim.schedule(function()
---@type VectorCodeCache
vim.api.nvim_buf_set_var(bufnr, "vectorcode_cache", {
enabled = true,
retrieval = nil,
options = opts,
jobs = {},
})
end)
end
vim.schedule(function()
if opts.run_on_register then
async_runner(opts.query_cb(bufnr), bufnr)
end
local group = vim.api.nvim_create_augroup(
("VectorCodeCacheGroup%d"):format(bufnr),
{ clear = true }
)
vim.api.nvim_create_autocmd(opts.events, {
group = group,
callback = function()
assert(
vim.b[bufnr].vectorcode_cache ~= nil,
"buffer vectorcode cache not registered"
)
vim.schedule(function()
local cache = vim.api.nvim_buf_get_var(bufnr, "vectorcode_cache") ---@cast cache VectorCodeCache
if
cache.last_run == nil
or (vim.uv.clock_gettime("realtime").sec - cache.last_run)
> opts.debounce
then
local cb = cache.options.query_cb
async_runner(cb(bufnr), bufnr)
end
end)
end,
buffer = bufnr,
desc = "Run query on certain autocmd",
})
vim.api.nvim_create_autocmd("BufWinLeave", {
buffer = bufnr,
desc = "Kill all running VectorCode async jobs.",
group = group,
callback = function()
kill_jobs(bufnr)
end,
})
end)
end
)
M.deregister_buffer = vc_config.check_cli_wrap(
---@param bufnr integer?
---@param opts {notify:boolean}
function(bufnr, opts)
opts = opts or { notify = false }
if bufnr == nil or bufnr == 0 then
bufnr = vim.api.nvim_get_current_buf()
end
if M.buf_is_registered(bufnr) then
kill_jobs(bufnr)
vim.api.nvim_del_augroup_by_name(("VectorCodeCacheGroup%d"):format(bufnr))
vim.api.nvim_buf_set_var(bufnr, "vectorcode_cache", nil)
if opts.notify then
vim.notify(
("VectorCode Caching has been unregistered for buffer %d."):format(bufnr),
vim.log.levels.INFO,
notify_opts
)
end
else
vim.notify(
("VectorCode Caching hasn't been registered for buffer %d."):format(bufnr),
vim.log.levels.ERROR,
notify_opts
)
end
end
)
---@param bufnr integer?
---@return boolean
M.buf_is_registered = function(bufnr)
if bufnr == 0 or bufnr == nil then
bufnr = vim.api.nvim_get_current_buf()
end
return type(vim.b[bufnr].vectorcode_cache) == "table"
end
M.query_from_cache = vc_config.check_cli_wrap(
---@param bufnr integer?
---@return VectorCodeResult[]
function(bufnr)
local result = {}
if bufnr == 0 or bufnr == nil then
bufnr = vim.api.nvim_get_current_buf()
end
if M.buf_is_registered(bufnr) then
---@type VectorCodeCache
local vectorcode_cache = vim.api.nvim_buf_get_var(bufnr, "vectorcode_cache")
result = vectorcode_cache.retrieval or {}
if vectorcode_cache.options.notify then
vim.notify(
("Retrieved %d documents from cache."):format(#result),
vim.log.levels.INFO,
notify_opts
)
end
end
return result
end
)
---@param bufnr integer
---@param component_cb (fun(result:VectorCodeResult):string)?
---@return {content:string, count:integer}
function M.make_prompt_component(bufnr, component_cb)
if bufnr == 0 or bufnr == nil then
bufnr = vim.api.nvim_get_current_buf()
end
if not M.buf_is_registered(bufnr) then
return { content = "", count = 0 }
end
if component_cb == nil then
---@type fun(result:VectorCodeResult):string
component_cb = function(result)
return "<|file_sep|>" .. result.path .. "\n" .. result.document
end
end
local final_component = ""
local retrieval = M.query_from_cache(bufnr)
for _, file in pairs(retrieval) do
final_component = final_component .. component_cb(file)
end
return { content = final_component, count = #retrieval }
end
function M.lualine()
vim.deprecate(
'require("vectorcode.cacher").lualine()',
'require("vectorcode.integrations").lualine()',
"0.3.0",
"VectorCode",
false
)
return require("vectorcode.integrations").lualine()
end
---@param check_item string?
---@param on_success fun(out: vim.SystemCompleted)?
---@param on_failure fun(out: vim.SystemCompleted)?
function M.async_check(check_item, on_success, on_failure)
if not vc_config.has_cli() then
if on_failure ~= nil then
on_failure()
end
return
end
check_item = check_item or "config"
local return_code
vim.system({ "vectorcode", "check", check_item }, {}, function(out)
if out.code == 0 and type(on_success) == "function" then
on_success(out)
elseif out.code ~= 0 and type(on_failure) == "function" then
on_failure(out)
end
end)
return return_code == 0
end
return M