Skip to content

Commit e3ec958

Browse files
committed
Add comprehensive type annotations to core modules
1 parent 72f56f7 commit e3ec958

8 files changed

Lines changed: 185 additions & 11 deletions

File tree

lua/spectre/actions.lua

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---@module 'spectre.actions'
12
local api = vim.api
23
local config = require('spectre.config')
34
local state = require('spectre.state')
@@ -7,6 +8,11 @@ local utils = require('spectre.utils')
78

89
local M = {}
910

11+
---Open a file at the given position, optionally in a specific window.
12+
---@param filename string
13+
---@param lnum number
14+
---@param col number
15+
---@param winid number?
1016
local open_file = function(filename, lnum, col, winid)
1117
if winid ~= nil then
1218
vim.fn.win_gotoid(winid)
@@ -17,13 +23,19 @@ local open_file = function(filename, lnum, col, winid)
1723
pcall(api.nvim_win_set_cursor, 0, { lnum, col })
1824
end
1925

26+
---Check if a filename is an absolute path.
27+
---@param filename string
28+
---@return boolean
2029
local is_absolute = function(filename)
2130
if vim.loop.os_uname().sysname == 'Windows_NT' then
2231
return string.find(filename, '%a:\\') == 1
2332
end
2433
return string.sub(filename, 1, 1) == '/'
2534
end
2635

36+
---Resolve a filename to an absolute path using the current working directory.
37+
---@param filename string
38+
---@return string
2739
local get_file_path = function(filename)
2840
-- if the path is absolute, return as is
2941
if is_absolute(filename) then
@@ -38,6 +50,7 @@ local get_file_path = function(filename)
3850
return vim.fn.expand(state.cwd) .. Path.path.sep .. filename
3951
end
4052

53+
---Open the file for the search result under the cursor.
4154
M.select_entry = function()
4255
local t = M.get_current_entry()
4356
if t == nil then
@@ -50,6 +63,13 @@ M.select_entry = function()
5063
end
5164
end
5265

66+
---@class SpectreSearchState
67+
---@field query SpectreQuery
68+
---@field cwd string|nil
69+
---@field options table<string, boolean>
70+
71+
---Get a copy of the current search state (query, cwd, options).
72+
---@return SpectreSearchState
5373
M.get_state = function()
5474
local result = {
5575
query = state.query,
@@ -59,13 +79,17 @@ M.get_state = function()
5979
return vim.deepcopy(result)
6080
end
6181

82+
---Mark an entry as finished (already replaced).
83+
---@param display_lnum number
6284
M.set_entry_finish = function(display_lnum)
6385
local item = state.total_item[display_lnum + 1]
6486
if item then
6587
item.is_replace_finish = true
6688
end
6789
end
6890

91+
---Get the search result entry at the current cursor position.
92+
---@return SpectreEntry|nil
6993
M.get_current_entry = function()
7094
if not state.total_item then
7195
return
@@ -79,6 +103,8 @@ M.get_current_entry = function()
79103
end
80104
end
81105

106+
---Get all active (non-disabled) search result entries.
107+
---@return SpectreEntry[]
82108
M.get_all_entries = function()
83109
local entries = {}
84110
for _, item in pairs(state.total_item) do
@@ -91,6 +117,8 @@ M.get_all_entries = function()
91117
return entries
92118
end
93119

120+
---Send all search results to the quickfix list.
121+
---@return SpectreEntry[]
94122
M.send_to_qf = function()
95123
local entries = M.get_all_entries()
96124
vim.fn.setqflist(entries, 'r')
@@ -107,7 +135,7 @@ M.send_to_qf = function()
107135
return entries
108136
end
109137

110-
-- input that comand to run on vim
138+
---Build and feed a vim substitute command for the current search/replace.
111139
M.replace_cmd = function()
112140
M.send_to_qf()
113141
local replace_cmd = ''
@@ -135,6 +163,7 @@ M.replace_cmd = function()
135163
end
136164
end
137165

166+
---Run replace for the entry at the current cursor position.
138167
M.run_current_replace = function()
139168
local entry = M.get_current_entry()
140169
if entry then
@@ -146,6 +175,8 @@ end
146175

147176
local is_running = false
148177

178+
---Run replace on the given entries (or all entries if nil).
179+
---@param entries SpectreEntry[]|nil
149180
M.run_replace = function(entries)
150181
if is_running == true then
151182
print('it is already running')
@@ -223,6 +254,8 @@ M.delete_line_file_current = function()
223254
end
224255
end
225256

257+
---Delete lines from files for the given entries (or all entries if nil).
258+
---@param entries SpectreEntry[]|nil
226259
M.run_delete_line = function(entries)
227260
entries = entries or M.get_all_entries()
228261
local done_item = 0
@@ -289,6 +322,7 @@ M.run_delete_line = function(entries)
289322
end
290323
end
291324

325+
---Show a picker to select from configured search templates.
292326
M.select_template = function()
293327
if not state.user_config.open_template or #state.user_config.open_template == 0 then
294328
vim.notify('You need to set open_template on setup function.')
@@ -311,6 +345,7 @@ M.select_template = function()
311345
end)
312346
end
313347

348+
---Copy the current line's text content to a register.
314349
M.copy_current_line = function()
315350
local line_text = vim.api.nvim_get_current_line()
316351
local row = unpack(vim.api.nvim_win_get_cursor(0))

lua/spectre/config.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,48 @@
11
local api = vim.api
22

3+
---@class SpectreMapping
4+
---@field map string
5+
---@field cmd string
6+
---@field desc string
7+
8+
---@class SpectreEngineOption
9+
---@field value string
10+
---@field icon string
11+
---@field desc string
12+
13+
---@class SpectreEngineConfig
14+
---@field cmd string
15+
---@field args string[]|nil
16+
---@field options table<string, SpectreEngineOption>
17+
---@field warn boolean?
18+
319
---@class SpectreConfig
20+
---@field filetype string
21+
---@field namespace number
22+
---@field namespace_ui number
23+
---@field namespace_header number
24+
---@field namespace_status number
25+
---@field namespace_result number
26+
---@field lnum_UI number
27+
---@field line_result number
28+
---@field line_sep_start string
29+
---@field result_padding string
30+
---@field line_sep string
31+
---@field color_devicons boolean
32+
---@field open_cmd string|function
33+
---@field live_update boolean
34+
---@field lnum_for_results boolean
35+
---@field highlight table<string, string>
36+
---@field mapping table<string, SpectreMapping>
37+
---@field find_engine table<string, SpectreEngineConfig>
38+
---@field replace_engine table<string, SpectreEngineConfig>
39+
---@field default table
40+
---@field replace_vim_cmd string
41+
---@field use_trouble_qf boolean
42+
---@field is_open_target_win boolean
43+
---@field is_insert_mode boolean
44+
---@field is_block_ui_break boolean
45+
---@field open_template table[]
446
local config = {
547
filetype = 'spectre_panel',
648
namespace = api.nvim_create_namespace('SEARCH_PANEL'),

lua/spectre/highlight.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
---@module 'spectre.highlight'
12
local M = {}
23

4+
---Set the default highlight groups for the Spectre UI.
5+
---Uses `default = true` so user-defined highlights take priority.
36
M.set_hl = function()
47
vim.api.nvim_set_hl(0, 'SpectreHeader', { link = 'Comment', default = true })
58
vim.api.nvim_set_hl(0, 'SpectreBody', { link = 'String', default = true })

lua/spectre/replace/init.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
---@module 'spectre.replace'
2+
---Replace engine factory. Lazily loads replace engine modules by name.
13
local base = require('spectre.replace.base')
24
local r = {}
5+
6+
---Get a replace engine by name.
7+
---@param key string Engine name (e.g., "sed", "sd", "oxi")
8+
---@return table engine Replace engine creator
39
r.get = function(key)
410
assert(key ~= nil, 'key no nil')
511
local ok, engine = pcall(require, 'spectre.replace.' .. key)

lua/spectre/search/init.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
---@module 'spectre.search'
2+
---Search engine factory. Lazily loads search engine modules by name.
13
local base = require('spectre.search.base')
24
local s = {}
5+
6+
---Get a search engine by name.
7+
---@param key string Engine name (e.g., "rg", "ag")
8+
---@return table engine Search engine creator
39
s.get = function(key)
410
assert(key ~= nil, 'key no nil')
511
local ok, engine = pcall(require, 'spectre.search.' .. key)

lua/spectre/state.lua

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,35 @@
44
---@field path string
55
---@field is_file boolean
66

7+
---@class SpectreEntry
8+
---@field filename string
9+
---@field lnum number
10+
---@field col number
11+
---@field text string
12+
---@field search_text string?
13+
---@field replace_text string?
14+
---@field display_lnum number?
15+
---@field disable boolean?
16+
---@field is_replace_finish boolean?
17+
718
---@class SpectreState
819
---@field user_config SpectreConfig
920
---@field status_line string
1021
---@field cwd string|nil
1122
---@field query SpectreQuery
1223
---@field query_backup SpectreQuery|nil
13-
---@field options table
24+
---@field options table<string, boolean>
1425
---@field is_running boolean
1526
---@field is_open boolean
16-
---@field total_item table
27+
---@field total_item table<number, SpectreEntry>
1728
---@field regex any
1829
---@field finder_instance any|nil
1930
---@field async_id number
2031
---@field target_winid number
2132
---@field target_bufnr number
33+
---@field bufnr number|nil
34+
---@field vt table<string, any>
35+
---@field view table<string, any>
2236
local state = {
2337
-- current config
2438
status_line = '',

lua/spectre/state_utils.lua

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1+
---@module 'spectre.state_utils'
12
local state = require('spectre.state')
23
local search_engine = require('spectre.search')
34
local replace_engine = require('spectre.replace')
45
local M = {}
56

7+
---Get the search engine creator for the configured find command.
8+
---@return table
69
M.get_finder_creator = function()
710
return search_engine[state.user_config.default.find.cmd]
811
end
912

13+
---Get the replace engine creator for the configured replace command.
14+
---@return table
1015
M.get_replace_creator = function()
1116
return replace_engine[state.user_config.default.replace.cmd]
1217
end
1318

19+
---Get enabled option values for a given engine configuration.
20+
---@param cfg SpectreEngineConfig Engine configuration with options
21+
---@return string[] options_value List of active option values
1422
local get_options = function(cfg)
1523
local options_value = {}
1624
for key, value in pairs(state.options) do
@@ -21,31 +29,48 @@ local get_options = function(cfg)
2129
return options_value
2230
end
2331

32+
---Get the replace engine configuration with active options applied.
33+
---@return SpectreEngineConfig
2434
M.get_replace_engine_config = function()
2535
local cfg = state.user_config.replace_engine[state.user_config.default.replace.cmd] or {}
2636
cfg = vim.deepcopy(cfg)
2737
cfg.options_value = get_options(cfg)
2838
return cfg
2939
end
3040

41+
---Get the search engine configuration with active options applied.
42+
---@return SpectreEngineConfig
3143
M.get_search_engine_config = function()
3244
local cfg = state.user_config.find_engine[state.user_config.default.find.cmd] or {}
3345
cfg = vim.deepcopy(cfg)
3446
cfg.options_value = get_options(cfg)
3547
return cfg
3648
end
3749

50+
---Get the current user configuration.
51+
---@return SpectreConfig
3852
M.config = function()
3953
return state.user_config
4054
end
4155

56+
---Check if a search option is enabled.
57+
---@param key string
58+
---@return boolean
4259
M.has_options = function(key)
4360
return state.options[key] == true
4461
end
4562

63+
---@class SpectreStatusLineOptions
64+
---@field separator string?
65+
---@field seprator string? Typo-compatible alias for separator
66+
---@field main_color string?
67+
68+
---Generate a status line configuration for integration with status line plugins.
69+
---@param opt SpectreStatusLineOptions|nil Options with separator, main_color fields
70+
---@return table spectre Status line configuration table
4671
M.status_line = function(opt)
4772
opt = opt or {}
48-
local slant_right = opt.seprator or ''
73+
local slant_right = opt.separator or opt.seprator or ''
4974
local main_color = opt.main_color or 'black'
5075
local spectre = {
5176
filetypes = { 'spectre_panel' },

0 commit comments

Comments
 (0)