Skip to content

Commit 175174a

Browse files
authored
refactor(cli, nvim): some nix-friendly refactoring
- configurable executable path for the neovim plugin. - Documentation mentions the Nix packages (for both CLI and nvim plugin).
2 parents 2ed7d79 + 7ccea9f commit 175174a

File tree

9 files changed

+91
-32
lines changed

9 files changed

+91
-32
lines changed

doc/VectorCode.txt

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Table of Contents *VectorCode-table-of-contents*
1010
- Configuration |VectorCode-neovim-plugin-configuration|
1111
- API Usage |VectorCode-neovim-plugin-api-usage|
1212
- Debugging and Logging |VectorCode-neovim-plugin-debugging-and-logging|
13+
2. Links |VectorCode-links|
1314

1415
==============================================================================
1516
1. NeoVim Plugin *VectorCode-neovim-plugin*
@@ -22,6 +23,7 @@ Table of Contents *VectorCode-table-of-contents*
2223
[!NOTE] When the neovim plugin doesn’t work properly, please try upgrading
2324
the CLI tool to the latest version before opening an issue.
2425
- |VectorCode-installation|
26+
- |VectorCode-nix|
2527
- |VectorCode-integrations|
2628
- |VectorCode-user-command|
2729
- |VectorCode-`vectorcode-register`|
@@ -91,6 +93,13 @@ you can use the following plugin spec:
9193
<https://archlinux.org/packages/extra/x86_64/neovim/> repository of Arch
9294
Linux).
9395

96+
NIX ~
97+
98+
There’s a community-maintained nix package
99+
<https://nixpk.gs/pr-tracker.html?pr=413395> submitted by @sarahec
100+
<https://github.com/sarahec> for the Neovim plugin.
101+
102+
94103
INTEGRATIONS *VectorCode-neovim-plugin-integrations*
95104

96105
The wiki <https://github.com/Davidyz/VectorCode/wiki/Neovim-Integrations>
@@ -160,6 +169,9 @@ This function initialises the VectorCode client and sets up some default
160169
>lua
161170
-- Default configuration
162171
require("vectorcode").setup({
172+
cli_cmds = {
173+
vectorcode = "vectorcode",
174+
},
163175
async_opts = {
164176
debounce = 10,
165177
events = { "BufWritePost", "InsertEnter", "BufReadPost" },
@@ -183,22 +195,25 @@ This function initialises the VectorCode client and sets up some default
183195
<
184196

185197
The following are the available options for the parameter of this function: -
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
190-
milliseconds for the query operation. Applies to synchronous API only. Default:
191-
`5000` (5 seconds); - `exclude_this`whether to exclude the file you’re
192-
editing. Setting this to `false` may lead to an outdated version of the current
193-
file being sent to the LLM as the prompt, and can lead to generations with
194-
outdated information; - `async_opts`default options used when registering
195-
buffers. See |VectorCode-`register_buffer(bufnr?,-opts?)`| for details; -
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. -
198+
`cli_cmds`A table to customize the CLI command names / paths used by the
199+
plugin. Supported key: - `vectorcode`The command / path to use for the main CLI
200+
tool. Default: `"vectorcode"`. - `n_query`number of retrieved documents. A
201+
large number gives a higher chance of including the right file, but with the
202+
risk of saturating the context window and getting truncated. Default: `1`; -
203+
`notify`whether to show notifications when a query is completed. Default:
204+
`true`; - `timeout_ms`timeout in milliseconds for the query operation. Applies
205+
to synchronous API only. Default: `5000` (5 seconds); - `exclude_this`whether
206+
to exclude the file you’re editing. Setting this to `false` may lead to an
207+
outdated version of the current file being sent to the LLM as the prompt, and
208+
can lead to generations with outdated information; - `async_opts`default
209+
options used when registering buffers. See
210+
|VectorCode-`register_buffer(bufnr?,-opts?)`| for details; - `async_backend`the
211+
async backend to use, currently either `"default"` or `"lsp"`. Default:
212+
`"default"`; - `on_setup`some actions that can be registered to run when
213+
`setup` is called. Supported keys: - `update`if `true`, the plugin will run
214+
`vectorcode update` on startup to update the embeddings; - `lsp`if `true`, the
215+
plugin will try to start the LSP server on startup so that you won’t need to
216+
wait for the server loading when making your first request. -
202217
`sync_log_env_var``boolean`. If true, this plugin will automatically set the
203218
`VECTORCODE_LOG_LEVEL` environment variable for LSP or cmd processes started
204219
within your neovim session when logging is turned on for this plugin. Use at
@@ -512,6 +527,11 @@ variable to a supported log level
512527
The log file will be written to `stdpath("log")` or `stdpath("cache")`. On
513528
Linux, this is usually `~/.local/state/nvim/`.
514529

530+
==============================================================================
531+
2. Links *VectorCode-links*
532+
533+
1. *@sarahec*:
534+
515535
Generated by panvimdoc <https://github.com/kdheepak/panvimdoc>
516536

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

docs/cli.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* [Chromadb](#chromadb)
99
* [For Windows Users](#for-windows-users)
1010
* [Legacy Environments](#legacy-environments)
11+
* [Nix](#nix)
1112
* [Getting Started](#getting-started)
1213
* [Refreshing Embeddings](#refreshing-embeddings)
1314
* [If Anything Goes Wrong...](#if-anything-goes-wrong)
@@ -107,6 +108,11 @@ numpy `v1.x`. If this doesn't help, please open an issue with your OS, CPU
107108
architecture, python version and the vectorcode virtual environment
108109
(`pipx runpip vectorcode freeze`).
109110

111+
### Nix
112+
113+
A community-maintained Nix package is available
114+
[here](https://search.nixos.org/packages?channel=unstable&from=0&size=50&sort=relevance&type=packages&query=vectorcode).
115+
110116
## Getting Started
111117

112118
`cd` into your project root repo, and run:

docs/neovim.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<!-- mtoc-start -->
1313

1414
* [Installation](#installation)
15+
* [Nix](#nix)
1516
* [Integrations](#integrations)
1617
* [User Command](#user-command)
1718
* [`VectorCode register`](#vectorcode-register)
@@ -80,6 +81,11 @@ you can use the following plugin spec:
8081
> [Extra](https://archlinux.org/packages/extra/x86_64/neovim/)
8182
> repository of Arch Linux).
8283
84+
### Nix
85+
86+
There's a community-maintained [nix package](https://nixpk.gs/pr-tracker.html?pr=413395)
87+
submitted by [@sarahec](https://github.com/sarahec) for the Neovim plugin.
88+
8389
## Integrations
8490

8591
[The wiki](https://github.com/Davidyz/VectorCode/wiki/Neovim-Integrations)
@@ -137,6 +143,9 @@ This function initialises the VectorCode client and sets up some default
137143
```lua
138144
-- Default configuration
139145
require("vectorcode").setup({
146+
cli_cmds = {
147+
vectorcode = "vectorcode",
148+
},
140149
async_opts = {
141150
debounce = 10,
142151
events = { "BufWritePost", "InsertEnter", "BufReadPost" },
@@ -160,6 +169,9 @@ require("vectorcode").setup({
160169
```
161170

162171
The following are the available options for the parameter of this function:
172+
- `cli_cmds`: A table to customize the CLI command names / paths used by the plugin.
173+
Supported key:
174+
- `vectorcode`: The command / path to use for the main CLI tool. Default: `"vectorcode"`.
163175
- `n_query`: number of retrieved documents. A large number gives a higher chance
164176
of including the right file, but with the risk of saturating the context
165177
window and getting truncated. Default: `1`;

lua/vectorcode/config.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ local cacher = nil
1515

1616
---@type VectorCode.Opts
1717
local config = {
18+
cli_cmds = {
19+
vectorcode = "vectorcode",
20+
},
1821
async_opts = {
1922
debounce = 10,
2023
events = { "BufWritePost", "InsertEnter", "BufReadPost" },
@@ -24,6 +27,7 @@ local config = {
2427
query_cb = require("vectorcode.utils").make_surrounding_lines_cb(-1),
2528
run_on_register = false,
2629
single_job = false,
30+
timeout_ms = 5000,
2731
},
2832
async_backend = "default",
2933
exclude_this = true,
@@ -40,7 +44,7 @@ local setup_config = vim.deepcopy(config, true)
4044
local lsp_configs = function()
4145
---@type vim.lsp.ClientConfig
4246
local cfg =
43-
{ cmd = { "vectorcode-server" }, root_markers = { ".vectorcode", ".git" } }
47+
{ cmd = { "vectorcode-server" }, root_markers = { ".vectorcode", ".git" } } -- NOTE: This can be overriden by `vim.lsp.config`
4448
if vim.lsp.config ~= nil and vim.lsp.config.vectorcode_server ~= nil then
4549
-- nvim >= 0.11.0
4650
cfg = vim.tbl_deep_extend("force", cfg, vim.lsp.config.vectorcode_server)
@@ -73,7 +77,7 @@ local notify_opts = { title = "VectorCode" }
7377
---@param opts {notify:boolean}?
7478
local has_cli = function(opts)
7579
opts = opts or { notify = false }
76-
local ok = vim.fn.executable("vectorcode") == 1
80+
local ok = vim.fn.executable(setup_config.cli_cmds.vectorcode) == 1
7781
if not ok and opts.notify then
7882
vim.notify("VectorCode CLI is not executable!", vim.log.levels.ERROR, notify_opts)
7983
end
@@ -140,6 +144,8 @@ return {
140144
)
141145
end
142146
end
147+
setup_config.cli_cmds.vectorcode =
148+
vim.fs.normalize(setup_config.cli_cmds.vectorcode)
143149
startup_handler(setup_config)
144150
logger.info("Finished processing opts:\n", setup_config)
145151
end

lua/vectorcode/jobrunner/cmd.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@ function runner.run_async(args, callback, bufnr)
1212
else
1313
callback = nil
1414
end
15-
local cmd = { "vectorcode" }
16-
args = require("vectorcode.jobrunner").find_root(args, bufnr)
17-
vim.list_extend(cmd, args)
1815
logger.debug(
1916
("cmd jobrunner for buffer %s args: %s"):format(bufnr, vim.inspect(args))
2017
)
2118
---@diagnostic disable-next-line: missing-fields
2219
local job = Job:new({
23-
command = "vectorcode",
20+
command = require("vectorcode.config").get_user_config().cli_cmds.vectorcode,
2421
args = args,
2522
on_exit = function(self, code, signal)
2623
jobs[self.pid] = nil
@@ -49,9 +46,13 @@ function runner.run_async(args, callback, bufnr)
4946
end
5047
end,
5148
})
52-
job:start()
53-
jobs[job.pid] = job
54-
return tonumber(job.pid)
49+
local ok = pcall(job.start, job)
50+
if ok then
51+
jobs[job.pid] = job
52+
return tonumber(job.pid)
53+
else
54+
logger.error("Failed to start job.")
55+
end
5556
end
5657

5758
function runner.run(args, timeout_ms, bufnr)

lua/vectorcode/jobrunner/lsp.lua

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
if
2-
vim.fn.executable("vectorcode-server") ~= 1
3-
or vim.system({ "vectorcode-server", "--version" }):wait().code ~= 0
4-
then
5-
return nil
6-
end
1+
local vc_config = require("vectorcode.config")
72

83
---@type VectorCode.JobRunner
94
local jobrunner = {}
105

116
---@type vim.lsp.Client
127
local CLIENT = nil
138

14-
local vc_config = require("vectorcode.config")
159
local notify_opts = vc_config.notify_opts
1610
local logger = vc_config.logger
1711

@@ -93,6 +87,7 @@ function jobrunner.run_async(args, callback, bufnr)
9387
)
9488
local _, id = CLIENT:request(
9589
vim.lsp.protocol.Methods.workspace_executeCommand,
90+
-- NOTE: This is not a hardcoded executable, but rather part of our LSP implementation.
9691
{ command = "vectorcode", arguments = args },
9792
function(err, result, _, _)
9893
if type(callback) == "function" then

lua/vectorcode/types.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@
2323
---@field update boolean `vectorcode update`
2424
---@field lsp boolean whether to start LSP server on startup (default is to delay it to the first LSP request)
2525

26+
---@class VectorCode.CliCmds Cli commands to use
27+
---@field vectorcode string vectorcode cli command or full path
28+
2629
---Options passed to `setup`.
2730
---@class VectorCode.Opts : VectorCode.QueryOpts
2831
---@field async_opts VectorCode.RegisterOpts Default options to use for registering a new buffer for async cache.
32+
---@field cli_cmds VectorCode.CliCmds
2933
---@field on_setup VectorCode.OnSetup
3034
---@field async_backend "default"|"lsp"
3135
---@field sync_log_env_var boolean Whether to automatically set `VECTORCODE_LOG_LEVEL` when `VECTORCODE_NVIM_LOG_LEVEL` is detected. !! WARNING: THIS MAY RESULT IN EXCESSIVE LOG MESSAGES DUE TO STDERR BEING POPULATED BY CLI LOGS

src/vectorcode/lsp_main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import traceback
88
import uuid
99

10+
import shtab
11+
1012
try: # pragma: nocover
1113
from lsprotocol import types
1214
from pygls.exceptions import (
@@ -64,6 +66,12 @@ def get_arg_parser():
6466
type=str,
6567
default="",
6668
)
69+
shtab.add_argument_to(
70+
parser,
71+
["-s", "--print-completion"],
72+
parent=parser,
73+
help="Print completion script.",
74+
)
6775
return parser
6876

6977

src/vectorcode/mcp_main.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pathlib import Path
88
from typing import Optional
99

10+
import shtab
1011
from chromadb.api import AsyncClientAPI
1112
from chromadb.api.models.AsyncCollection import AsyncCollection
1213
from chromadb.errors import InvalidCollectionException
@@ -60,6 +61,12 @@ def get_arg_parser():
6061
default=False,
6162
help="Whether to include the output of `vectorcode ls` in the tool description.",
6263
)
64+
shtab.add_argument_to(
65+
parser,
66+
["-s", "--print-completion"],
67+
parent=parser,
68+
help="Print completion script.",
69+
)
6370
return parser
6471

6572

0 commit comments

Comments
 (0)