@@ -59,7 +59,14 @@ M.get_state = function()
5959end
6060
6161M .set_entry_finish = function (display_lnum )
62- local item = state .total_item [display_lnum + 1 ]
62+ -- Safety check: ensure display_lnum is valid and state.total_item exists
63+ if not display_lnum or not state .total_item then return end
64+
65+ -- In Lua, arrays are 1-indexed but display_lnum might be 0-indexed
66+ local index = display_lnum + 1
67+
68+ -- Check if the item exists in total_item
69+ local item = state .total_item [index ]
6370 if item then
6471 item .is_replace_finish = true
6572 end
@@ -171,8 +178,15 @@ function M.run_replace(entries)
171178 local replacer_creator = state_utils .get_replace_creator ()
172179 local replacer = replacer_creator :new (state_utils .get_replace_engine_config (), {
173180 on_done = function (result )
174- if result .ref then
181+ if result .ref and result .ref .display_lnum ~= nil then
182+ -- Set the entry as finished and mark it as replaced
175183 M .set_entry_finish (result .ref .display_lnum )
184+
185+ -- Add a safety check before accessing state.total_item
186+ if state .total_item and state .total_item [result .ref .display_lnum ] then
187+ state .total_item [result .ref .display_lnum ].is_replace = true
188+ end
189+
176190 -- Update UI by adding a checkmark to the line
177191 local bufnr = api .nvim_get_current_buf ()
178192 local line = result .ref .display_lnum
@@ -183,16 +197,41 @@ function M.run_replace(entries)
183197 0 ,
184198 { virt_text = { { ' ✓' , ' String' } }, virt_text_pos = ' eol' }
185199 )
186- -- Trigger renderer redraw
200+
201+ -- If we have a renderer, trigger a full redraw
187202 if state .renderer then
188- print (" redrawing" )
189- state .renderer :redraw ()
203+ -- Update the node in the UI
204+ local tree = state .renderer :get_component_by_id (" results-tree" )
205+ -- Check if tree exists and has the get_nodes method
206+ if tree and type (tree ) == " table" and type (tree .get_nodes ) == " function" then
207+ local success , nodes = pcall (function ()
208+ return tree :get_nodes ()
209+ end )
210+
211+ if success and nodes then
212+ for _ , node in ipairs (nodes ) do
213+ -- Add safety check for node.display_lnum
214+ if node .display_lnum and node .display_lnum == result .ref .display_lnum then
215+ node .is_done = true
216+ -- This triggers the prepare_node function
217+ pcall (function () state .renderer :redraw () end )
218+ break
219+ end
220+ end
221+ else
222+ -- If we can't get nodes, just redraw
223+ pcall (function () state .renderer :redraw () end )
224+ end
225+ else
226+ -- If tree doesn't exist or doesn't have get_nodes, just redraw
227+ pcall (function () state .renderer :redraw () end )
228+ end
190229 end
191230 end
192231 end ,
193232 on_error = function (result )
194- if result .ref then
195- vim .notify (" Error replacing: " .. result .value , vim .log .levels .ERROR )
233+ if result .ref and result . ref . display_lnum ~= nil then
234+ vim .notify (" Error replacing: " .. ( result .value or " unknown error " ) , vim .log .levels .ERROR )
196235 -- Add error mark to the line
197236 local bufnr = api .nvim_get_current_buf ()
198237 local line = result .ref .display_lnum
@@ -205,8 +244,10 @@ function M.run_replace(entries)
205244 )
206245 -- Trigger renderer redraw
207246 if state .renderer then
208- print (" redrawing" )
209- state .renderer :redraw ()
247+ -- Make sure renderer has redraw method
248+ if type (state .renderer ) == " table" and type (state .renderer .redraw ) == " function" then
249+ pcall (function () state .renderer :redraw () end )
250+ end
210251 end
211252 end
212253 end ,
@@ -245,44 +286,54 @@ M.run_delete_line = function(entries)
245286 local replacer_creator = state_utils .get_replace_creator ()
246287 local replacer = replacer_creator :new (state_utils .get_replace_engine_config (), {
247288 on_done = function (result )
248- if result .ref then
289+ if result .ref and result . ref . display_lnums then
249290 done_item = done_item + 1
250291 local value = result .ref
251292 state .status_line = ' Delete line: ' .. done_item .. ' Error:' .. error_item
252293 for _ , display_lnum in ipairs (value .display_lnums ) do
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- )
294+ if display_lnum ~= nil then
295+ M .set_entry_finish (display_lnum )
296+ api .nvim_buf_set_extmark (
297+ state .bufnr ,
298+ config .namespace ,
299+ display_lnum ,
300+ 0 ,
301+ { virt_text = { { ' DONE' , ' String' } }, virt_text_pos = ' eol' }
302+ )
303+ end
261304 end
262305 -- Trigger renderer redraw
263306 if state .renderer then
264- state .renderer :redraw ()
307+ -- Make sure renderer has redraw method
308+ if type (state .renderer ) == " table" and type (state .renderer .redraw ) == " function" then
309+ pcall (function () state .renderer :redraw () end )
310+ end
265311 end
266312 end
267313 end ,
268314 on_error = function (result )
269- if result .ref then
315+ if result .ref and result . ref . display_lnums then
270316 error_item = error_item + 1
271317 local value = result .ref
272318 state .status_line = ' Delete line: ' .. done_item .. ' Error:' .. error_item
273319 for _ , display_lnum in ipairs (value .display_lnums ) do
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- )
320+ if display_lnum ~= nil then
321+ M .set_entry_finish (display_lnum )
322+ api .nvim_buf_set_extmark (
323+ state .bufnr ,
324+ config .namespace ,
325+ display_lnum ,
326+ 0 ,
327+ { virt_text = { { ' ERROR' , ' Error' } }, virt_text_pos = ' eol' }
328+ )
329+ end
282330 end
283331 -- Trigger renderer redraw
284332 if state .renderer then
285- state .renderer :redraw ()
333+ -- Make sure renderer has redraw method
334+ if type (state .renderer ) == " table" and type (state .renderer .redraw ) == " function" then
335+ pcall (function () state .renderer :redraw () end )
336+ end
286337 end
287338 end
288339 end ,
0 commit comments