1+ --- @module ' spectre.actions'
12local api = vim .api
23local config = require (' spectre.config' )
34local state = require (' spectre.state' )
@@ -7,6 +8,11 @@ local utils = require('spectre.utils')
78
89local 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 ?
1016local 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 })
1824end
1925
26+ --- Check if a filename is an absolute path.
27+ --- @param filename string
28+ --- @return boolean
2029local 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 ) == ' /'
2534end
2635
36+ --- Resolve a filename to an absolute path using the current working directory.
37+ --- @param filename string
38+ --- @return string
2739local 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
3951end
4052
53+ --- Open the file for the search result under the cursor.
4154M .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
5164end
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
5373M .get_state = function ()
5474 local result = {
5575 query = state .query ,
@@ -59,13 +79,17 @@ M.get_state = function()
5979 return vim .deepcopy (result )
6080end
6181
82+ --- Mark an entry as finished (already replaced).
83+ --- @param display_lnum number
6284M .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
6789end
6890
91+ --- Get the search result entry at the current cursor position.
92+ --- @return SpectreEntry | nil
6993M .get_current_entry = function ()
7094 if not state .total_item then
7195 return
@@ -79,6 +103,8 @@ M.get_current_entry = function()
79103 end
80104end
81105
106+ --- Get all active (non-disabled) search result entries.
107+ --- @return SpectreEntry[]
82108M .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
92118end
93119
120+ --- Send all search results to the quickfix list.
121+ --- @return SpectreEntry[]
94122M .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
108136end
109137
110- -- input that comand to run on vim
138+ --- Build and feed a vim substitute command for the current search/replace.
111139M .replace_cmd = function ()
112140 M .send_to_qf ()
113141 local replace_cmd = ' '
@@ -135,6 +163,7 @@ M.replace_cmd = function()
135163 end
136164end
137165
166+ --- Run replace for the entry at the current cursor position.
138167M .run_current_replace = function ()
139168 local entry = M .get_current_entry ()
140169 if entry then
146175
147176local is_running = false
148177
178+ --- Run replace on the given entries (or all entries if nil).
179+ --- @param entries SpectreEntry[] | nil
149180M .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
224255end
225256
257+ --- Delete lines from files for the given entries (or all entries if nil).
258+ --- @param entries SpectreEntry[] | nil
226259M .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
290323end
291324
325+ --- Show a picker to select from configured search templates.
292326M .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 )
312346end
313347
348+ --- Copy the current line's text content to a register.
314349M .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 ))
0 commit comments