Skip to content

Commit 9d742b5

Browse files
committed
refactor(nvim): Add Selene linting and improve assertions
1 parent 45395bc commit 9d742b5

File tree

15 files changed

+77
-35
lines changed

15 files changed

+77
-35
lines changed

.github/workflows/selene.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Selene check
2+
3+
on:
4+
push:
5+
branches:
6+
- "main"
7+
paths:
8+
- "lua/**/*.lua"
9+
- "plugin/*.lua"
10+
pull_request:
11+
12+
jobs:
13+
selene:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
- name: Run Selene check
18+
uses: NTBBloodbath/selene-action@v1.0.0
19+
with:
20+
token: ${{ secrets.GITHUB_TOKEN }}
21+
args: lua/
22+
version: 0.28.0

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ coverage:
1919
pdm run coverage run -m pytest; \
2020
pdm run coverage html; \
2121
pdm run coverage report -m
22+
23+
lint:
24+
pdm run ruff check src/**/*.py; \
25+
pdm run basedpyright src/**/*.py; \
26+
selene lua/**/*.lua plugin/*.lua

docs/CONTRIBUTING.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ them locally before you open the PR.
2222

2323
This project also runs static analysis with
2424
[basedpyright](https://docs.basedpyright.com). GitHub Action will also run the
25-
check when a PR is submitted.
25+
check when a PR is submitted. This, as well as `ruff check`, are both included
26+
in `make lint`.
2627

2728
You may also find it helpful to
2829
[enable logging](https://github.com/Davidyz/VectorCode/blob/main/docs/cli.md#debugging-and-diagnosing)
@@ -35,6 +36,9 @@ formatted (stylua) and appropriately type-annotated, you're good. I do have
3536
plans to write some tests, but before that happens, formatting and type
3637
annotations are the only things that you need to take special care of.
3738

39+
The lua codebase is linted by [selene](https://github.com/Kampfkarren/selene).
40+
You may run `make lint` or call `selene` from the CLI to lint the code.
41+
3842
You may find it useful to
3943
[enable logging](https://github.com/Davidyz/VectorCode/blob/main/docs/cli.md#debugging-and-diagnosing)
4044
when you're poking around the codebase.

lua/vectorcode/cacher/default.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ local function async_runner(query_message, buf_nr)
7777
end
7878
logger.debug("vectorcode ", buf_name, " default cacher results: ", json_result)
7979
CACHE[buf_nr].job_count = CACHE[buf_nr].job_count - 1
80-
assert(job_pid ~= nil)
80+
assert(job_pid ~= nil, "Failed to fetch the job pid.")
8181
CACHE[buf_nr].jobs[job_pid] = nil
8282

8383
if exit_code ~= 0 then
@@ -189,7 +189,7 @@ M.register_buffer = vc_config.check_cli_wrap(
189189
or (vim.uv.clock_gettime("realtime").sec - cache.last_run) > opts.debounce
190190
then
191191
local cb = cache.options.query_cb
192-
assert(type(cb) == "function")
192+
assert(type(cb) == "function", "`cb` should be a function.")
193193
async_runner(cb(bufnr), bufnr)
194194
end
195195
end,
@@ -249,7 +249,7 @@ M.buf_is_registered = function(bufnr)
249249
if bufnr == 0 or bufnr == nil then
250250
bufnr = vim.api.nvim_get_current_buf()
251251
end
252-
return type(CACHE[bufnr]) == "table" and CACHE[bufnr] ~= {}
252+
return type(CACHE[bufnr]) == "table" and not vim.tbl_isempty(CACHE[bufnr])
253253
end
254254

255255
M.query_from_cache = vc_config.check_cli_wrap(

lua/vectorcode/cacher/lsp.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ local client_id = nil
2323
---@param project_root string
2424
---@return string?
2525
local function check_project_root(bufnr, project_root)
26-
assert(bufnr ~= 0)
26+
assert(bufnr ~= 0, "Need a non-zero bufnr")
2727
if project_root == nil then
2828
local cwd = vim.uv.cwd() or "."
2929
local result = vim.fs.root(cwd, ".vectorcode") or vim.fs.root(cwd, ".git")
@@ -76,7 +76,7 @@ local function kill_jobs(bufnr)
7676
if client == nil then
7777
return
7878
end
79-
for request_id, time in pairs(CACHE[bufnr].jobs) do
79+
for request_id, _ in pairs(CACHE[bufnr].jobs) do
8080
job_runner.stop_job(request_id)
8181
end
8282
cleanup_lsp_requests()
@@ -93,7 +93,7 @@ local function async_runner(query_message, buf_nr)
9393
buf_name = vim.api.nvim_buf_get_name(buf_nr)
9494
logger.debug("Started lsp cacher job on :", buf_name)
9595
end)
96-
assert(client_id ~= nil)
96+
assert(client_id ~= nil, "LSP client hasn't been initialised.")
9797
---@type VectorCode.Cache
9898
local cache = CACHE[buf_nr]
9999
local args = {

lua/vectorcode/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ return {
130130
logger.info("Received setup opts:\n", opts)
131131
opts = opts or {}
132132
setup_config = vim.tbl_deep_extend("force", config, opts or {})
133-
for k, v in pairs(setup_config.async_opts) do
133+
for k, _ in pairs(setup_config.async_opts) do
134134
if
135135
setup_config[k] ~= nil
136136
and (opts.async_opts == nil or opts.async_opts[k] == nil)

lua/vectorcode/init.lua

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,15 @@ M.vectorise = vc_config.check_cli_wrap(
7979
function(files, project_root)
8080
logger.info("vectorcode.vectorise: ", files, project_root)
8181
local args = { "--pipe", "vectorise" }
82-
if project_root ~= nil then
83-
vim.list_extend(args, { "--project_root", project_root })
84-
elseif
85-
M.check("config", function(obj)
86-
if obj.code == 0 then
87-
project_root = obj.stdout
88-
end
89-
end)
82+
if
83+
project_root ~= nil
84+
or (
85+
M.check("config", function(obj)
86+
if obj.code == 0 then
87+
project_root = obj.stdout
88+
end
89+
end)
90+
)
9091
then
9192
vim.list_extend(args, { "--project_root", project_root })
9293
end
@@ -203,7 +204,7 @@ end
203204
---@return string[]
204205
M.prompts = vc_config.check_cli_wrap(function()
205206
local result, error = jobrunner.run({ "prompts", "-p" }, -1, 0)
206-
if result == nil or result == {} then
207+
if result == nil or vim.tbl_isempty(result) then
207208
logger.warn(vim.inspect(error))
208209
if vc_config.get_user_config().notify then
209210
notify(vim.inspect(error))

lua/vectorcode/integrations/codecompanion/func_calling_tool.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ return check_cli_wrap(function(opts)
3737
cmds = {
3838
---@param agent CodeCompanion.Agent
3939
---@param action table
40-
---@param input table
4140
---@return nil|{ status: string, msg: string }
42-
function(agent, action, input, cb)
41+
function(agent, action, _, cb)
4342
logger.info("CodeCompanion tool called with the following arguments:\n", action)
4443
job_runner = cc_common.initialise_runner(opts.use_lsp)
45-
assert(job_runner ~= nil)
44+
assert(job_runner ~= nil, "Jobrunner not initialised!")
4645
assert(
4746
type(cb) == "function",
4847
"Please upgrade CodeCompanion.nvim to at least 13.5.0"

lua/vectorcode/integrations/copilotchat.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ end
3737
---@async
3838
local run_job = async.wrap(function(args, use_lsp, bufnr, callback)
3939
local runner = get_runner(use_lsp)
40-
assert(runner ~= nil)
40+
assert(runner ~= nil, "Failed to initialize the runner!")
4141
runner.run_async(args, callback, bufnr)
4242
end, 4)
4343

lua/vectorcode/jobrunner/lsp.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ end
3939

4040
function jobrunner.run(args, timeout_ms, bufnr)
4141
jobrunner.init(false)
42-
assert(CLIENT ~= nil)
43-
assert(bufnr ~= nil)
42+
assert(CLIENT ~= nil, "Failed to initialize the LSP server!")
43+
assert(bufnr ~= nil, "Need to pass the buffer number!")
4444
if timeout_ms == nil or timeout_ms < 0 then
4545
timeout_ms = 2 ^ 31 - 1
4646
end
4747
args = require("vectorcode.jobrunner").find_root(args, bufnr)
4848

4949
local result, err, code
50-
jobrunner.run_async(args, function(res, err, e_code)
50+
jobrunner.run_async(args, function(res, e, e_code)
5151
result = res
52-
err = err
52+
err = e
5353
code = e_code
5454
end, bufnr)
5555
vim.wait(timeout_ms, function()
@@ -60,7 +60,7 @@ end
6060

6161
function jobrunner.run_async(args, callback, bufnr)
6262
assert(jobrunner.init(false))
63-
assert(bufnr ~= nil)
63+
assert(bufnr ~= nil, "Need to pass the buffer number!")
6464
if not CLIENT.attached_buffers[bufnr] then
6565
if vim.lsp.buf_attach_client(bufnr, CLIENT.id) then
6666
local uri = vim.uri_from_bufnr(bufnr)

0 commit comments

Comments
 (0)