Skip to content

Commit 00b8542

Browse files
authored
chore(nvim): add logger for nvim plugin (#87)
* chore(nvim): add logger (using plenary) for nvim plugin * add logging for APIs. * logging for codecompanion tool. * logger for copilot chat. * correctly handle lower case * fix log item. * various fixes for log levels and supressing excessive logs. * update documentation. * Auto generate docs --------- Co-authored-by: Davidyz <Davidyz@users.noreply.github.com>
1 parent 20cdaa9 commit 00b8542

File tree

12 files changed

+272
-77
lines changed

12 files changed

+272
-77
lines changed

.github/workflows/panvimdoc.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ name: panvimdoc
22

33
on: [push]
44

5+
permissions:
6+
contents: write
7+
58
jobs:
69
docs:
710
runs-on: ubuntu-latest
@@ -24,3 +27,7 @@ jobs:
2427
docmappingprojectname: true # Use project name in tag when writing mapping docs
2528
shiftheadinglevelby: 0 # Shift heading levels by specified number
2629
incrementheadinglevelby: 0 # Increment heading levels by specified number
30+
- uses: stefanzweifel/git-auto-commit-action@v4
31+
with:
32+
commit_message: "Auto generate docs"
33+
branch: ${{ github.head_ref }}

doc/VectorCode.txt

Lines changed: 69 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*VectorCode.txt* For Last change: 2025 March 31
1+
*VectorCode.txt*A code repository indexing tool to supercharge your LLM experience.
22

33
==============================================================================
44
Table of Contents *VectorCode-table-of-contents*
@@ -9,6 +9,7 @@ Table of Contents *VectorCode-table-of-contents*
99
- User Command |VectorCode-neovim-plugin-user-command|
1010
- Configuration |VectorCode-neovim-plugin-configuration|
1111
- API Usage |VectorCode-neovim-plugin-api-usage|
12+
- Debugging and Logging |VectorCode-neovim-plugin-debugging-and-logging|
1213

1314
==============================================================================
1415
1. NeoVim Plugin *VectorCode-neovim-plugin*
@@ -41,6 +42,7 @@ Table of Contents *VectorCode-table-of-contents*
4142
- |VectorCode-`cacher_backend.buf_job_count(bufnr?)`|
4243
- |VectorCode-`cacher_backend.make_prompt_component(bufnr?,-component_cb?)`|
4344
- |VectorCode-built-in-query-callbacks|
45+
- |VectorCode-debugging-and-logging|
4446

4547

4648
INSTALLATION *VectorCode-neovim-plugin-installation*
@@ -176,27 +178,33 @@ This function initialises the VectorCode client and sets up some default
176178
update = false, -- set to true to enable update when `setup` is called.
177179
lsp = false,
178180
}
181+
sync_log_env_var = false,
179182
})
180183
<
181184

182185
The following are the available options for the parameter of this function: -
183-
`n_query`: number of retrieved documents. A large number gives a higher chance
184-
of including the right file, but with the risk of saturating the context window
185-
and getting truncated. Default: `1`; - `notify`: whether to show notifications
186-
when a query is completed. Default: `true`; - `timeout_ms`: timeout in
186+
`n_query`number of retrieved documents. A large number gives a higher chance of
187+
including the right file, but with the risk of saturating the context window
188+
and getting truncated. Default: `1`; - `notify`whether to show notifications
189+
when a query is completed. Default: `true`; - `timeout_ms`timeout in
187190
milliseconds for the query operation. Applies to synchronous API only. Default:
188-
`5000` (5 seconds); - `exclude_this`: whether to exclude the file you’re
191+
`5000` (5 seconds); - `exclude_this`whether to exclude the file you’re
189192
editing. Setting this to `false` may lead to an outdated version of the current
190193
file being sent to the LLM as the prompt, and can lead to generations with
191-
outdated information; - `async_opts`: default options used when registering
194+
outdated information; - `async_opts`default options used when registering
192195
buffers. See |VectorCode-`register_buffer(bufnr?,-opts?)`| for details; -
193-
`async_backend`: the async backend to use, currently either `"default"` or
194-
`"lsp"`. Default: `"default"`; - `on_setup`: some actions that can be
195-
registered to run when `setup` is called. Supported keys: - `update`: if
196-
`true`, the plugin will run `vectorcode update` on startup to update the
197-
embeddings; - `lsp`: if `true`, the plugin will try to start the LSP server on
198-
startup so that you won’t need to wait for the server loading when making
199-
your first request.
196+
`async_backend`the async backend to use, currently either `"default"` or
197+
`"lsp"`. Default: `"default"`; - `on_setup`some actions that can be registered
198+
to run when `setup` is called. Supported keys: - `update`if `true`, the plugin
199+
will run `vectorcode update` on startup to update the embeddings; - `lsp`if
200+
`true`, the plugin will try to start the LSP server on startup so that you
201+
won’t need to wait for the server loading when making your first request. -
202+
`sync_log_env_var``boolean`. If true, this plugin will automatically set the
203+
`VECTORCODE_LOG_LEVEL` environment variable for LSP or cmd processes started
204+
within your neovim session when logging is turned on for this plugin. Use at
205+
caution because the CLI write all logs to stderr, which _may_ make this plugin
206+
VERY verbose. See |VectorCode-debugging-and-logging| for details on how to turn
207+
on logging.
200208

201209
You may notice that a lot of options in `async_opts` are the same as the other
202210
options in the top-level of the main option table. This is because the
@@ -232,8 +240,8 @@ This function queries VectorCode and returns an array of results.
232240
})
233241
<
234242

235-
- `query_message`: string or a list of strings, the query messages;
236-
- `opts`: The following are the available options for this function (see |VectorCode-`setup(opts?)`| for details):
243+
- `query_message`string or a list of strings, the query messages;
244+
- `opts`The following are the available options for this function (see |VectorCode-`setup(opts?)`| for details):
237245

238246
>lua
239247
{
@@ -244,7 +252,7 @@ This function queries VectorCode and returns an array of results.
244252
}
245253
<
246254

247-
- `callback`: a callback function that takes the result of the retrieval as the
255+
- `callback`a callback function that takes the result of the retrieval as the
248256
only parameter. If this is set, the `query` function will be non-blocking and
249257
runs in an async manner. In this case, it doesn’t return any value and
250258
retrieval results can only be accessed by this callback function.
@@ -288,7 +296,7 @@ project. See the CLI manual for details <./cli.md>.
288296
require("vectorcode").check()
289297
<
290298

291-
The following are the available options for this function: - `check_item`: Only
299+
The following are the available options for this function: - `check_item`Only
292300
supports `"config"` at the moment. Checks if a project-local config is present.
293301
Return value: `true` if passed, `false` if failed.
294302

@@ -372,27 +380,27 @@ This function registers a buffer to be cached by VectorCode.
372380
})
373381
<
374382

375-
The following are the available options for this function: - `bufnr`: buffer
376-
number. Default: current buffer; - `opts`: accepts a lua table with the
377-
following keys: - `project_root`: a string of the path that overrides the
378-
detected project root. Default: `nil`. This is mostly intended to use with the
383+
The following are the available options for this function: - `bufnr`buffer
384+
number. Default: current buffer; - `opts`accepts a lua table with the following
385+
keys: - `project_root`a string of the path that overrides the detected project
386+
root. Default: `nil`. This is mostly intended to use with the
379387
|VectorCode-user-command|, and you probably should not use this directly in
380388
your config. **If you’re using the LSP backend and did not specify this
381389
value, it will be automatically detected based on .vectorcode or .git. If this
382-
fails, LSP backend will not work**; - `exclude_this`: whether to exclude the
383-
file you’re editing. Default: `true`; - `n_query`: number of retrieved
384-
documents. Default: `1`; - `debounce`: debounce time in milliseconds. Default:
385-
`10`; - `notify`: whether to show notifications when a query is completed.
386-
Default: `false`; - `query_cb`: `fun(bufnr: integer):string|string[]`, a
387-
callback function that accepts the buffer ID and returns the query message(s).
388-
Default: `require("vectorcode.utils").make_surrounding_lines_cb(-1)`. See
389-
|VectorCode-this-section| for a list of built-in query callbacks; - `events`:
390-
list of autocommand events that triggers the query. Default: `{"BufWritePost",
391-
"InsertEnter", "BufReadPost"}`; - `run_on_register`: whether to run the query
392-
when the buffer is registered. Default: `false`; - `single_job`: boolean. If
393-
this is set to `true`, there will only be one running job for each buffer, and
394-
when a new job is triggered, the last-running job will be cancelled. Default:
395-
`false`.
390+
fails, LSP backend will not work**; - `exclude_this`whether to exclude the file
391+
you’re editing. Default: `true`; - `n_query`number of retrieved documents.
392+
Default: `1`; - `debounce`debounce time in milliseconds. Default: `10`; -
393+
`notify`whether to show notifications when a query is completed. Default:
394+
`false`; - `query_cb``fun(bufnr: integer):string|string[]`, a callback function
395+
that accepts the buffer ID and returns the query message(s). Default:
396+
`require("vectorcode.utils").make_surrounding_lines_cb(-1)`. See
397+
|VectorCode-this-section| for a list of built-in query callbacks; -
398+
`events`list of autocommand events that triggers the query. Default:
399+
`{"BufWritePost", "InsertEnter", "BufReadPost"}`; - `run_on_register`whether to
400+
run the query when the buffer is registered. Default: `false`; -
401+
`single_job`boolean. If this is set to `true`, there will only be one running
402+
job for each buffer, and when a new job is triggered, the last-running job will
403+
be cancelled. Default: `false`.
396404

397405

398406
CACHER_BACKEND.QUERY_FROM_CACHE(BUFNR?)
@@ -403,10 +411,10 @@ This function queries VectorCode from cache.
403411
local query_results = cacher_backend.query_from_cache(0, {notify=false})
404412
<
405413

406-
The following are the available options for this function: - `bufnr`: buffer
407-
number. Default: current buffer; - `opts`: accepts a lua table with the
408-
following keys: - `notify`: boolean, whether to show notifications when a query
409-
is completed. Default: `false`;
414+
The following are the available options for this function: - `bufnr`buffer
415+
number. Default: current buffer; - `opts`accepts a lua table with the following
416+
keys: - `notify`boolean, whether to show notifications when a query is
417+
completed. Default: `false`;
410418

411419
Return value: an array of results. Each item of the array is in the format of
412420
`{path="path/to/your/code.lua", document="document content"}`.
@@ -425,18 +433,17 @@ project.
425433
)
426434
<
427435

428-
The following are the available options for this function: - `check_item`: any
436+
The following are the available options for this function: - `check_item`any
429437
check that works with `vectorcode check` command. If not set, it defaults to
430-
`"config"`; - `on_success`: a callback function that is called when the check
431-
passes; - `on_failure`: a callback function that is called when the check
432-
fails.
438+
`"config"`; - `on_success`a callback function that is called when the check
439+
passes; - `on_failure`a callback function that is called when the check fails.
433440

434441

435442
CACHER_BACKEND.BUF_IS_REGISTERED(BUFNR?)
436443

437444
This function checks if a buffer has been registered with VectorCode.
438445

439-
The following are the available options for this function: - `bufnr`: buffer
446+
The following are the available options for this function: - `bufnr`buffer
440447
number. Default: current buffer. Return value: `true` if registered, `false`
441448
otherwise.
442449

@@ -448,7 +455,7 @@ slightly different from `buf_is_registered`, because it does not guarantee
448455
VectorCode is actively caching the content of the buffer. It is the same as
449456
`buf_is_registered && not is_paused`.
450457

451-
The following are the available options for this function: - `bufnr`: buffer
458+
The following are the available options for this function: - `bufnr`buffer
452459
number. Default: current buffer. Return value: `true` if enabled, `false`
453460
otherwise.
454461

@@ -460,8 +467,8 @@ Returns the number of running jobs in the background.
460467

461468
CACHER_BACKEND.MAKE_PROMPT_COMPONENT(BUFNR?, COMPONENT_CB?)
462469

463-
Compile the retrieval results into a string. Parameters: - `bufnr`: buffer
464-
number. Default: current buffer; - `component_cb`: a callback function that
470+
Compile the retrieval results into a string. Parameters: - `bufnr`buffer
471+
number. Default: current buffer; - `component_cb`a callback function that
465472
formats each retrieval result, so that you can customise the control token,
466473
etc. for the component. The default is the following:
467474

@@ -471,8 +478,8 @@ etc. for the component. The default is the following:
471478
end
472479
<
473480

474-
`make_prompt_component` returns a table with 2 keys: - `count`: number of
475-
retrieved documents; - `content`: The retrieval results concatenated together
481+
`make_prompt_component` returns a table with 2 keys: - `count`number of
482+
retrieved documents; - `content`The retrieval results concatenated together
476483
into a string. Each result is formatted by `component_cb`.
477484

478485

@@ -483,19 +490,28 @@ takes the buffer ID as the only parameter, and return a string or a list of
483490
strings. The `vectorcode.utils` module provides the following callback
484491
constructor for you to play around with it, but you can easily build your own!
485492

486-
- `require("vectorcode.utils").make_surrounding_lines_cb(line_count)`: returns a
493+
- `require("vectorcode.utils").make_surrounding_lines_cb(line_count)`returns a
487494
callback that uses `line_count` lines around the cursor as the query. When
488495
`line_count` is negative, it uses the full buffer;
489-
- `require("vectorcode.utils").make_lsp_document_symbol_cb()`: returns a
496+
- `require("vectorcode.utils").make_lsp_document_symbol_cb()`returns a
490497
callback which uses the `textDocument/documentSymbol` method to retrieve a
491498
list of symbols in the current document. This will fallback to
492499
`make_surrounding_lines_cb(-1)` when there’s no LSP that supports the
493500
`documentSymbol` method;
494-
- `require("vectorcode.utils").make_changes_cb(max_num)`: returns a callback
501+
- `require("vectorcode.utils").make_changes_cb(max_num)`returns a callback
495502
that fetches `max_num` unique items from the `:changes` list. This will also
496503
fallback to `make_surrounding_lines_cb(-1)`. The default value for `max_num`
497504
is 50.
498505

506+
507+
DEBUGGING AND LOGGING *VectorCode-neovim-plugin-debugging-and-logging*
508+
509+
You can enable logging by setting `VECTORCODE_NVIM_LOG_LEVEL` environment
510+
variable to a supported log level
511+
<https://github.com/nvim-lua/plenary.nvim/blob/857c5ac632080dba10aae49dba902ce3abf91b35/lua/plenary/log.lua#L44>.
512+
The log file will be written to `stdpath("log")` or `stdpath("cache")`. On
513+
Linux, this is usually `~/.local/state/nvim/`.
514+
499515
Generated by panvimdoc <https://github.com/kdheepak/panvimdoc>
500516

501517
vim:tw=78:ts=8:noet:ft=help:norl:

docs/neovim.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* [`cacher_backend.buf_job_count(bufnr?)`](#cacher_backendbuf_job_countbufnr)
3333
* [`cacher_backend.make_prompt_component(bufnr?, component_cb?)`](#cacher_backendmake_prompt_componentbufnr-component_cb)
3434
* [Built-in Query Callbacks](#built-in-query-callbacks)
35+
* [Debugging and Logging](#debugging-and-logging)
3536

3637
<!-- mtoc-end -->
3738

@@ -154,6 +155,7 @@ require("vectorcode").setup({
154155
update = false, -- set to true to enable update when `setup` is called.
155156
lsp = false,
156157
}
158+
sync_log_env_var = false,
157159
})
158160
```
159161

@@ -180,6 +182,12 @@ The following are the available options for the parameter of this function:
180182
- `lsp`: if `true`, the plugin will try to start the LSP server on startup so
181183
that you won't need to wait for the server loading when making your first
182184
request.
185+
- `sync_log_env_var`: `boolean`. If true, this plugin will automatically set the
186+
`VECTORCODE_LOG_LEVEL` environment variable for LSP or cmd processes started
187+
within your neovim session when logging is turned on for this plugin. Use at
188+
caution because the CLI write all logs to stderr, which _may_ make this plugin
189+
VERY verbose. See [Debugging and Logging](#debugging-and-logging) for details
190+
on how to turn on logging.
183191

184192
You may notice that a lot of options in `async_opts` are the same as the other
185193
options in the top-level of the main option table. This is because the top-level
@@ -444,3 +452,11 @@ constructor for you to play around with it, but you can easily build your own!
444452
that fetches `max_num` unique items from the `:changes` list. This will also
445453
fallback to `make_surrounding_lines_cb(-1)`. The default value for `max_num`
446454
is 50.
455+
456+
## Debugging and Logging
457+
458+
You can enable logging by setting `VECTORCODE_NVIM_LOG_LEVEL` environment
459+
variable to a
460+
[supported log level](https://github.com/nvim-lua/plenary.nvim/blob/857c5ac632080dba10aae49dba902ce3abf91b35/lua/plenary/log.lua#L44).
461+
The log file will be written to `stdpath("log")` or `stdpath("cache")`. On
462+
Linux, this is usually `~/.local/state/nvim/`.

lua/vectorcode/cacher/default.lua

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ local M = {}
33
local vc_config = require("vectorcode.config")
44
local notify_opts = vc_config.notify_opts
55

6+
local logger = vc_config.logger
7+
68
---@type table<integer, VectorCode.Cache>
79
local CACHE = {}
810

@@ -25,6 +27,11 @@ local function async_runner(query_message, buf_nr)
2527
if CACHE[buf_nr] == nil or not CACHE[buf_nr].enabled then
2628
return
2729
end
30+
local buf_name
31+
vim.schedule(function()
32+
buf_name = vim.api.nvim_buf_get_name(buf_nr)
33+
logger.debug("Started default cacher job on :", buf_name)
34+
end)
2835
---@type VectorCode.Cache
2936
local cache = CACHE[buf_nr]
3037
local args = {
@@ -52,6 +59,7 @@ local function async_runner(query_message, buf_nr)
5259
vim.list_extend(args, { "--project_root", project_root })
5360
end
5461
CACHE[buf_nr].job_count = CACHE[buf_nr].job_count + 1
62+
logger.debug("vectorcode default cacher job args: ", args)
5563
local job = require("plenary.job"):new({
5664
command = "vectorcode",
5765
args = args,
@@ -65,6 +73,7 @@ local function async_runner(query_message, buf_nr)
6573
if not M.buf_is_registered(buf_nr) then
6674
return
6775
end
76+
logger.debug("vectorcode ", buf_name, " default cacher results: ", self:result())
6877
CACHE[buf_nr].job_count = CACHE[buf_nr].job_count - 1
6978
CACHE[buf_nr].jobs[self.pid] = nil
7079
local ok, json = pcall(
@@ -130,6 +139,12 @@ M.register_buffer = vc_config.check_cli_wrap(
130139
if bufnr == 0 or bufnr == nil then
131140
bufnr = vim.api.nvim_get_current_buf()
132141
end
142+
logger.info(
143+
("Registering buffer %s %s for default cacher."):format(
144+
bufnr,
145+
vim.api.nvim_buf_get_name(bufnr)
146+
)
147+
)
133148
if M.buf_is_registered(bufnr) then
134149
opts = vim.tbl_deep_extend("force", CACHE[bufnr].options, opts or {})
135150
end
@@ -140,6 +155,12 @@ M.register_buffer = vc_config.check_cli_wrap(
140155
-- update the options and/or query_cb
141156
CACHE[bufnr].options =
142157
vim.tbl_deep_extend("force", CACHE[bufnr].options, opts or {})
158+
logger.debug(
159+
("Updated `default` cacher opts for buffer %s:\n%s"):format(
160+
bufnr,
161+
vim.inspect(opts)
162+
)
163+
)
143164
else
144165
CACHE[bufnr] = {
145166
enabled = true,
@@ -196,6 +217,9 @@ M.deregister_buffer = vc_config.check_cli_wrap(
196217
if bufnr == nil or bufnr == 0 then
197218
bufnr = vim.api.nvim_get_current_buf()
198219
end
220+
logger.info(
221+
("Deregistering buffer %s %s"):format(bufnr, vim.api.nvim_buf_get_name(bufnr))
222+
)
199223
if M.buf_is_registered(bufnr) then
200224
kill_jobs(bufnr)
201225
vim.api.nvim_del_augroup_by_name(("VectorCodeCacheGroup%d"):format(bufnr))
@@ -244,13 +268,11 @@ M.query_from_cache = vc_config.check_cli_wrap(
244268
opts or {}
245269
)
246270
result = CACHE[bufnr].retrieval or {}
271+
local message = ("Retrieved %d documents from cache."):format(#result)
272+
logger.trace(("vectorcode cmd cacher for buf %s: %s"):format(bufnr, message))
247273
if opts.notify then
248274
vim.schedule(function()
249-
vim.notify(
250-
("Retrieved %d documents from cache."):format(#result),
251-
vim.log.levels.INFO,
252-
notify_opts
253-
)
275+
vim.notify(message, vim.log.levels.INFO, notify_opts)
254276
end)
255277
end
256278
end

0 commit comments

Comments
 (0)