@@ -225,6 +225,19 @@ function RenderState:update_part_data(part_id, part_ref)
225225 part_data .part = part_ref
226226end
227227
228+ --- Helper to update action line numbers
229+ --- @param action table Action to update
230+ --- @param delta integer Line offset to apply
231+ local function shift_action_lines (action , delta )
232+ if action .display_line then
233+ action .display_line = action .display_line + delta
234+ end
235+ if action .range then
236+ action .range .from = action .range .from + delta
237+ action .range .to = action .range .to + delta
238+ end
239+ end
240+
228241--- Add actions to a part
229242--- @param part_id string Part ID
230243--- @param actions table[] Actions to add
@@ -239,11 +252,7 @@ function RenderState:add_actions(part_id, actions, offset)
239252
240253 for _ , action in ipairs (actions ) do
241254 if offset ~= 0 then
242- action .display_line = action .display_line + offset
243- if action .range then
244- action .range .from = action .range .from + offset
245- action .range .to = action .range .to + offset
246- end
255+ shift_action_lines (action , offset )
247256 end
248257 table.insert (part_data .actions , action )
249258 end
@@ -330,6 +339,7 @@ function RenderState:shift_all(from_line, delta)
330339 end
331340
332341 local found_content_before_from_line = false
342+ local shifted = false
333343
334344 for i = # state .messages , 1 , - 1 do
335345 local msg_wrapper = state .messages [i ]
@@ -341,6 +351,7 @@ function RenderState:shift_all(from_line, delta)
341351 if msg_data .line_start >= from_line then
342352 msg_data .line_start = msg_data .line_start + delta
343353 msg_data .line_end = msg_data .line_end + delta
354+ shifted = true
344355 elseif msg_data .line_end < from_line then
345356 found_content_before_from_line = true
346357 end
@@ -356,16 +367,11 @@ function RenderState:shift_all(from_line, delta)
356367 if part_data .line_start >= from_line then
357368 part_data .line_start = part_data .line_start + delta
358369 part_data .line_end = part_data .line_end + delta
370+ shifted = true
359371
360372 if part_data .actions then
361373 for _ , action in ipairs (part_data .actions ) do
362- if action .display_line then
363- action .display_line = action .display_line + delta
364- end
365- if action .range then
366- action .range .from = action .range .from + delta
367- action .range .to = action .range .to + delta
368- end
374+ shift_action_lines (action , delta )
369375 end
370376 end
371377 elseif part_data .line_end < from_line then
@@ -377,12 +383,13 @@ function RenderState:shift_all(from_line, delta)
377383 end
378384
379385 if found_content_before_from_line then
380- self :_rebuild_line_index ()
381- return
386+ break
382387 end
383388 end
384389
385- self :_rebuild_line_index ()
390+ if shifted then
391+ self :_rebuild_line_index ()
392+ end
386393end
387394
388395--- Rebuild line index from current state
0 commit comments