Skip to content

Commit 2c57c06

Browse files
committed
fix: annotations fixed/added, code sanitized
Signed-off-by: Guennadi Maximov C <g.maxc.fox@protonmail.com>
1 parent c9ff487 commit 2c57c06

4 files changed

Lines changed: 95 additions & 55 deletions

File tree

lua/quicktest.lua

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-- main module file
22
local module = require("quicktest.module")
33

4+
---@type QuicktestConfig
45
local config = {
56
adapters = {},
67
default_win_mode = "split",
@@ -19,70 +20,84 @@ local config = {
1920
},
2021
}
2122

22-
---@class MyModule
23+
---@class Quicktest
2324
local M = {}
2425

2526
--- @type QuicktestConfig
2627
M.config = config
2728

28-
---@param args QuicktestConfig?
29-
M.setup = function(args)
30-
M.config = vim.tbl_deep_extend("force", M.config, args or {})
29+
---@param args? QuicktestConfig
30+
function M.setup(args)
31+
args = args or {}
32+
M.config = vim.tbl_deep_extend("force", M.config, args)
3133
end
3234

33-
M.current_win_mode = function()
35+
function M.current_win_mode()
3436
return module.current_win_mode(M.config.default_win_mode)
3537
end
3638

3739
--- @param mode WinModeWithoutAuto
38-
M.open_win = function(mode)
40+
function M.open_win(mode)
3941
return module.try_open_win(mode)
4042
end
4143

4244
--- @param mode WinModeWithoutAuto
43-
M.close_win = function(mode)
45+
function M.close_win(mode)
4446
return module.try_close_win(mode)
4547
end
4648

4749
--- @param mode WinModeWithoutAuto
48-
M.toggle_win = function(mode)
50+
function M.toggle_win(mode)
4951
return module.toggle_win(mode)
5052
end
5153

52-
--- @param mode WinMode?
53-
M.run_previous = function(mode)
54-
return module.run_previous(M.config, mode or "auto")
54+
--- @param mode? WinMode
55+
function M.run_previous(mode)
56+
mode = mode or "auto"
57+
return module.run_previous(M.config, mode)
5558
end
5659

57-
--- @param mode WinMode?
58-
--- @param adapter Adapter?
59-
--- @param opts AdapterRunOpts?
60-
M.run_line = function(mode, adapter, opts)
61-
return module.prepare_and_run(M.config, "line", mode or "auto", adapter or "auto", opts or {})
60+
--- @param mode? WinMode
61+
--- @param adapter? Adapter
62+
--- @param opts? AdapterRunOpts
63+
function M.run_line(mode, adapter, opts)
64+
mode = mode or "auto"
65+
adapter = adapter or "auto"
66+
opts = opts or {}
67+
return module.prepare_and_run(M.config, "line", mode, adapter, opts)
6268
end
6369

64-
--- @param mode WinMode?
65-
--- @param adapter Adapter?
66-
--- @param opts AdapterRunOpts?
67-
M.run_file = function(mode, adapter, opts)
68-
return module.prepare_and_run(M.config, "file", mode or "auto", adapter or "auto", opts or {})
70+
--- @param mode? WinMode
71+
--- @param adapter? Adapter
72+
--- @param opts? AdapterRunOpts
73+
function M.run_file(mode, adapter, opts)
74+
mode = mode or "auto"
75+
adapter = adapter or "auto"
76+
opts = opts or {}
77+
return module.prepare_and_run(M.config, "file", mode, adapter, opts)
6978
end
7079

71-
--- @param mode WinMode?
72-
--- @param adapter Adapter?
73-
--- @param opts AdapterRunOpts?
74-
M.run_dir = function(mode, adapter, opts)
75-
return module.prepare_and_run(M.config, "dir", mode or "auto", adapter or "auto", opts or {})
80+
--- @param mode? WinMode
81+
--- @param adapter? Adapter
82+
--- @param opts? AdapterRunOpts
83+
function M.run_dir(mode, adapter, opts)
84+
mode = mode or "auto"
85+
adapter = adapter or "auto"
86+
opts = opts or {}
87+
return module.prepare_and_run(M.config, "dir", mode, adapter, opts)
7688
end
7789

78-
--- @param mode WinMode?
79-
--- @param adapter Adapter?
80-
--- @param opts AdapterRunOpts?
81-
M.run_all = function(mode, adapter, opts)
82-
return module.prepare_and_run(M.config, "all", mode or "auto", adapter or "auto", opts or {})
90+
--- @param mode? WinMode
91+
--- @param adapter? Adapter
92+
--- @param opts? AdapterRunOpts
93+
function M.run_all(mode, adapter, opts)
94+
mode = mode or "auto"
95+
adapter = adapter or "auto"
96+
opts = opts or {}
97+
return module.prepare_and_run(M.config, "all", mode, adapter, opts)
8398
end
8499

85-
M.cancel_current_run = function()
100+
function M.cancel_current_run()
86101
module.kill_current_run()
87102
end
88103

lua/quicktest/fs_utils.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ M.path = (function()
2828
return path
2929
end
3030

31+
---@return string|false
3132
local function exists(filename)
3233
local stat = uv.fs_stat(filename)
3334
return stat and stat.type or false

lua/quicktest/module.lua

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@ local p = require("plenary.path")
1010
local M = {}
1111

1212
---@alias Adapter string | "auto"
13-
---@alias WinMode 'popup' | 'split' | 'auto'
14-
---@alias WinModeWithoutAuto 'popup' | 'split
13+
---@alias WinMode "popup" | "split" | "auto"
14+
---@alias WinModeWithoutAuto "popup" | "split
1515

1616
---@class AdapterRunOpts
17-
---@field additional_args string[]?
17+
---@field additional_args? string[]
1818

19-
---@alias CmdData {type: 'stdout', raw: string, output: string?, decoded: any} | {type: 'stderr', raw: string, output: string?, decoded: any} | {type: 'exit', code: number}
19+
---@class CmdData
20+
---@field type "stdout" | "stderr" | "exit"
21+
---@field raw string
22+
---@field output? string
23+
---@field decoded any
24+
---@field code? number
2025

21-
---@alias RunType 'line' | 'file' | 'dir' | 'all'
26+
---@alias RunType "line" | "file" | "dir" | "all"
2227

2328
---@class QuicktestAdapter
2429
---@field name string
@@ -27,7 +32,7 @@ local M = {}
2732
---@field build_dir_run_params fun(bufnr: integer, cursor_pos: integer[], opts: AdapterRunOpts): any
2833
---@field build_all_run_params fun(bufnr: integer, cursor_pos: integer[], opts: AdapterRunOpts): any
2934
---@field run fun(params: any, send: fun(data: CmdData)): number
30-
---@field after_run fun(params: any, results: CmdData)?
35+
---@field after_run nil|fun(params: any, results: CmdData)
3136
---@field title fun(params: any): string
3237
---@field is_enabled fun(bufnr: number, type: RunType): boolean
3338

@@ -36,13 +41,31 @@ local M = {}
3641
---@field default_win_mode WinModeWithoutAuto
3742
---@field use_builtin_colorizer boolean
3843

39-
---@alias JobStatus 'running' | 'finished' | 'canceled'
40-
---@alias CmdJob {id: number, started_at: number, finished_at?: number, pid: number?, status: JobStatus, exit_code?: number}
44+
---@alias JobStatus "running" | "finished" | "canceled"
45+
46+
---@class CmdJob
47+
---@field id number
48+
---@field started_at integer
49+
---@field finished_at? integer
50+
---@field pid? number
51+
---@field status JobStatus
52+
---@field exit_code? integer
53+
54+
---@class PreviousRun
55+
---@field type string
56+
---@field adapter_name string
57+
---@field bufname string
58+
---@field cursor_pos integer[]
59+
60+
---@alias PreviousRuns table<string, PreviousRun>
61+
4162
---@type CmdJob | nil
4263
local current_job = nil
43-
--- @type {[string]: {type: string, adapter_name: string, bufname: string, cursor_pos: integer[]}} | nil
64+
65+
---@type nil | PreviousRuns
4466
local previous_run = nil
4567

68+
---@return PreviousRuns
4669
local function load_previous_run()
4770
local config_path = p:new(vim.fn.stdpath("data"), "quicktest_previous_runs.json")
4871

@@ -60,7 +83,7 @@ end
6083

6184
local function save_previous_run()
6285
if previous_run then
63-
---@diagnostic disable-next-line: missing-parameter
86+
---@diagnostic disable-next-line:missing-parameter
6487
a.run(function()
6588
local config_path = p:new(vim.fn.stdpath("data"), "quicktest_previous_runs.json")
6689
local current_data = load_previous_run()
@@ -132,23 +155,22 @@ local stderr_ns = vim.api.nvim_create_namespace("quicktest_stderr")
132155
--- @param opts AdapterRunOpts
133156
function M.run(adapter, params, config, opts)
134157
if current_job then
135-
if current_job.pid then
136-
vim.system({ "kill", tostring(current_job.pid) }):wait()
137-
current_job = nil
138-
else
158+
if not current_job.pid then
139159
return notify.warn("Already running")
140160
end
161+
vim.system({ "kill", tostring(current_job.pid) }):wait()
162+
current_job = nil
141163
end
142164

143165
--- @param buf integer
144166
--- @param start integer
145-
--- @param finish number
167+
--- @param finish integer
146168
--- @param strict_indexing boolean
147169
--- @param replacements string[]
148170
local set_ansi_lines = function(buf, start, finish, strict_indexing, replacements)
149171
local new_lines = {}
150172
for i, line in ipairs(replacements) do
151-
new_lines[i] = string.gsub(line, "[\27\155][][()#;?%d]*[A-PRZcf-ntqry=><~]", "")
173+
new_lines[i] = line:gsub("[\27\155][][()#;?%d]*[A-PRZcf-ntqry=><~]", "")
152174
end
153175
vim.api.nvim_buf_set_lines(buf, start, finish, strict_indexing, new_lines)
154176
end
@@ -169,7 +191,7 @@ function M.run(adapter, params, config, opts)
169191
passedTime = job.finished_at - job.started_at
170192
end
171193

172-
local time_display = string.format("%.2f", passedTime / 1000) .. "s"
194+
local time_display = ("%.2f"):format(passedTime / 1000) .. "s"
173195

174196
vim.api.nvim_buf_clear_namespace(buf, status_ns, 0, -1)
175197

@@ -180,15 +202,17 @@ function M.run(adapter, params, config, opts)
180202
hl_group = "DiagnosticInfo"
181203
else
182204
if job.status == "canceled" then
183-
line = "Canceled " .. time_display
205+
line = "Canceled "
184206
hl_group = "DiagnosticWarn"
185207
elseif job.exit_code ~= 0 then
186-
line = "Failed " .. time_display
208+
line = "Failed "
187209
hl_group = "DiagnosticError"
188210
else
189-
line = "Passed " .. time_display
211+
line = "Passed "
190212
hl_group = "DiagnosticOk"
191213
end
214+
215+
line = line .. time_display
192216
end
193217

194218
vim.api.nvim_buf_set_lines(buf, line_count - 1, line_count, false, {
@@ -387,7 +411,7 @@ local function get_adapter_and_params(config, type, adapter_name, current_buffer
387411
end
388412

389413
--- @param config QuicktestConfig
390-
--- @param type 'line' | 'file' | 'dir' | 'all'
414+
--- @param type "line" | "file" | "dir" | "all"
391415
--- @param mode WinMode
392416
--- @param adapter_name Adapter
393417
--- @param opts AdapterRunOpts

lua/quicktest/notify.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
local M = {}
22

33
---@param msg string
4-
---@param level number
4+
---@param level vim.log.levels
55
local function notify(msg, level)
66
vim.schedule(function()
7-
vim.api.nvim_notify(msg, level, { title = "Quicktest" })
7+
vim.notify(msg, level, { title = "Quicktest" })
88
end)
99
end
1010

0 commit comments

Comments
 (0)