Skip to content

Commit 9406631

Browse files
committed
Stylua
1 parent 140c2ff commit 9406631

1 file changed

Lines changed: 42 additions & 115 deletions

File tree

lua/spectre/actions.lua

Lines changed: 42 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,12 @@ end
4040

4141
M.select_entry = function()
4242
local entry = M.get_current_entry()
43-
if not entry then
44-
return
45-
end
43+
if not entry then return end
4644

47-
local full_path = vim.fn.fnamemodify(entry.filename, ':p')
48-
if not vim.fn.filereadable(full_path) then
49-
return
50-
end
45+
local full_path = vim.fn.fnamemodify(entry.filename, ":p")
46+
if not vim.fn.filereadable(full_path) then return end
5147

52-
vim.cmd('edit ' .. full_path)
48+
vim.cmd("edit " .. full_path)
5349
api.nvim_win_set_cursor(0, { entry.lnum, entry.col - 1 })
5450
end
5551

@@ -63,16 +59,7 @@ M.get_state = function()
6359
end
6460

6561
M.set_entry_finish = function(display_lnum)
66-
-- Safety check: ensure display_lnum is valid and state.total_item exists
67-
if not display_lnum or not state.total_item then
68-
return
69-
end
70-
71-
-- In Lua, arrays are 1-indexed but display_lnum might be 0-indexed
72-
local index = display_lnum + 1
73-
74-
-- Check if the item exists in total_item
75-
local item = state.total_item[index]
62+
local item = state.total_item[display_lnum + 1]
7663
if item then
7764
item.is_replace_finish = true
7865
end
@@ -83,20 +70,16 @@ function M.get_current_entry()
8370
local cursor_pos = api.nvim_win_get_cursor(0)
8471
local line = api.nvim_buf_get_lines(bufnr, cursor_pos[1] - 1, cursor_pos[1], false)[1]
8572

86-
if not line then
87-
return nil
88-
end
73+
if not line then return nil end
8974

90-
local filename, lnum, col = line:match('([^:]+):(%d+):(%d+):')
91-
if not filename or not lnum or not col then
92-
return nil
93-
end
75+
local filename, lnum, col = line:match("([^:]+):(%d+):(%d+):")
76+
if not filename or not lnum or not col then return nil end
9477

9578
return {
9679
filename = filename,
9780
lnum = tonumber(lnum),
9881
col = tonumber(col),
99-
text = line:match(':[^:]+$'):sub(2),
82+
text = line:match(":[^:]+$"):sub(2),
10083
}
10184
end
10285

@@ -110,7 +93,7 @@ function M.get_all_entries()
11093
col = item.col,
11194
text = item.text,
11295
display_lnum = display_lnum - 1,
113-
is_replace_finish = item.is_replace_finish or false,
96+
is_replace_finish = item.is_replace_finish or false
11497
})
11598
end
11699
end
@@ -120,7 +103,7 @@ end
120103
M.send_to_qf = function()
121104
local entries = M.get_all_entries()
122105
if #entries == 0 then
123-
vim.notify('No entries to send to quickfix')
106+
vim.notify("No entries to send to quickfix")
124107
return
125108
end
126109

@@ -135,7 +118,7 @@ M.send_to_qf = function()
135118
end
136119

137120
vim.fn.setqflist(qf_list)
138-
vim.cmd('copen')
121+
vim.cmd("copen")
139122
end
140123

141124
-- input that comand to run on vim
@@ -171,7 +154,7 @@ function M.run_current_replace()
171154
if entry then
172155
M.run_replace({ entry })
173156
else
174-
vim.notify('Not found any entry to replace.')
157+
vim.notify("Not found any entry to replace.")
175158
end
176159
end
177160

@@ -180,23 +163,16 @@ local is_running = false
180163
function M.run_replace(entries)
181164
entries = entries or M.get_all_entries()
182165
if #entries == 0 then
183-
vim.notify('No entries to replace')
166+
vim.notify("No entries to replace")
184167
return
185168
end
186169

187170
vim.schedule(function()
188171
local replacer_creator = state_utils.get_replace_creator()
189172
local replacer = replacer_creator:new(state_utils.get_replace_engine_config(), {
190173
on_done = function(result)
191-
if result.ref and result.ref.display_lnum ~= nil then
192-
-- Set the entry as finished and mark it as replaced
174+
if result.ref then
193175
M.set_entry_finish(result.ref.display_lnum)
194-
195-
-- Add a safety check before accessing state.total_item
196-
if state.total_item and state.total_item[result.ref.display_lnum] then
197-
state.total_item[result.ref.display_lnum].is_replace = true
198-
end
199-
200176
-- Update UI by adding a checkmark to the line
201177
local bufnr = api.nvim_get_current_buf()
202178
local line = result.ref.display_lnum
@@ -207,47 +183,16 @@ function M.run_replace(entries)
207183
0,
208184
{ virt_text = { { '', 'String' } }, virt_text_pos = 'eol' }
209185
)
210-
211-
-- If we have a renderer, trigger a full redraw
186+
-- Trigger renderer redraw
212187
if state.renderer then
213-
-- Update the node in the UI
214-
local tree = state.renderer:get_component_by_id('results-tree')
215-
-- Check if tree exists and has the get_nodes method
216-
if tree and type(tree) == 'table' and type(tree.get_nodes) == 'function' then
217-
local success, nodes = pcall(function()
218-
return tree:get_nodes()
219-
end)
220-
221-
if success and nodes then
222-
for _, node in ipairs(nodes) do
223-
-- Add safety check for node.display_lnum
224-
if node.display_lnum and node.display_lnum == result.ref.display_lnum then
225-
node.is_done = true
226-
-- This triggers the prepare_node function
227-
pcall(function()
228-
state.renderer:redraw()
229-
end)
230-
break
231-
end
232-
end
233-
else
234-
-- If we can't get nodes, just redraw
235-
pcall(function()
236-
state.renderer:redraw()
237-
end)
238-
end
239-
else
240-
-- If tree doesn't exist or doesn't have get_nodes, just redraw
241-
pcall(function()
242-
state.renderer:redraw()
243-
end)
244-
end
188+
print("redrawing")
189+
state.renderer:redraw()
245190
end
246191
end
247192
end,
248193
on_error = function(result)
249-
if result.ref and result.ref.display_lnum ~= nil then
250-
vim.notify('Error replacing: ' .. (result.value or 'unknown error'), vim.log.levels.ERROR)
194+
if result.ref then
195+
vim.notify("Error replacing: " .. result.value, vim.log.levels.ERROR)
251196
-- Add error mark to the line
252197
local bufnr = api.nvim_get_current_buf()
253198
local line = result.ref.display_lnum
@@ -260,12 +205,8 @@ function M.run_replace(entries)
260205
)
261206
-- Trigger renderer redraw
262207
if state.renderer then
263-
-- Make sure renderer has redraw method
264-
if type(state.renderer) == 'table' and type(state.renderer.redraw) == 'function' then
265-
pcall(function()
266-
state.renderer:redraw()
267-
end)
268-
end
208+
print("redrawing")
209+
state.renderer:redraw()
269210
end
270211
end
271212
end,
@@ -304,58 +245,44 @@ M.run_delete_line = function(entries)
304245
local replacer_creator = state_utils.get_replace_creator()
305246
local replacer = replacer_creator:new(state_utils.get_replace_engine_config(), {
306247
on_done = function(result)
307-
if result.ref and result.ref.display_lnums then
248+
if result.ref then
308249
done_item = done_item + 1
309250
local value = result.ref
310251
state.status_line = 'Delete line: ' .. done_item .. ' Error:' .. error_item
311252
for _, display_lnum in ipairs(value.display_lnums) do
312-
if display_lnum ~= nil then
313-
M.set_entry_finish(display_lnum)
314-
api.nvim_buf_set_extmark(
315-
state.bufnr,
316-
config.namespace,
317-
display_lnum,
318-
0,
319-
{ virt_text = { { '󰄲 DONE', 'String' } }, virt_text_pos = 'eol' }
320-
)
321-
end
253+
M.set_entry_finish(display_lnum)
254+
api.nvim_buf_set_extmark(
255+
state.bufnr,
256+
config.namespace,
257+
display_lnum,
258+
0,
259+
{ virt_text = { { '󰄲 DONE', 'String' } }, virt_text_pos = 'eol' }
260+
)
322261
end
323262
-- Trigger renderer redraw
324263
if state.renderer then
325-
-- Make sure renderer has redraw method
326-
if type(state.renderer) == 'table' and type(state.renderer.redraw) == 'function' then
327-
pcall(function()
328-
state.renderer:redraw()
329-
end)
330-
end
264+
state.renderer:redraw()
331265
end
332266
end
333267
end,
334268
on_error = function(result)
335-
if result.ref and result.ref.display_lnums then
269+
if result.ref then
336270
error_item = error_item + 1
337271
local value = result.ref
338272
state.status_line = 'Delete line: ' .. done_item .. ' Error:' .. error_item
339273
for _, display_lnum in ipairs(value.display_lnums) do
340-
if display_lnum ~= nil then
341-
M.set_entry_finish(display_lnum)
342-
api.nvim_buf_set_extmark(
343-
state.bufnr,
344-
config.namespace,
345-
display_lnum,
346-
0,
347-
{ virt_text = { { '󰄱 ERROR', 'Error' } }, virt_text_pos = 'eol' }
348-
)
349-
end
274+
M.set_entry_finish(display_lnum)
275+
api.nvim_buf_set_extmark(
276+
state.bufnr,
277+
config.namespace,
278+
display_lnum,
279+
0,
280+
{ virt_text = { { '󰄱 ERROR', 'Error' } }, virt_text_pos = 'eol' }
281+
)
350282
end
351283
-- Trigger renderer redraw
352284
if state.renderer then
353-
-- Make sure renderer has redraw method
354-
if type(state.renderer) == 'table' and type(state.renderer.redraw) == 'function' then
355-
pcall(function()
356-
state.renderer:redraw()
357-
end)
358-
end
285+
state.renderer:redraw()
359286
end
360287
end
361288
end,

0 commit comments

Comments
 (0)