Skip to content

Commit 768ee28

Browse files
committed
refactor
1 parent f24f19f commit 768ee28

File tree

1 file changed

+25
-30
lines changed
  • lua/codecompanion/interactions/chat/tools/builtin/insert_edit_into_file

1 file changed

+25
-30
lines changed

lua/codecompanion/interactions/chat/tools/builtin/insert_edit_into_file/init.lua

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,15 @@ end
330330
---@param edits table[]
331331
---@return table[], table[]
332332
local function partition_edits_by_type(edits)
333-
local block_edits = {}
334333
local substring_edits = {}
334+
local block_edits = {}
335335

336336
for i, edit in ipairs(edits) do
337+
local wrapper = { edit = edit, original_index = i }
337338
if edit.replaceAll and edit.oldText and not edit.oldText:find("\n") then
338-
table.insert(substring_edits, { edit = edit, original_index = i })
339+
table.insert(substring_edits, wrapper)
339340
else
340-
table.insert(block_edits, { edit = edit, original_index = i })
341+
table.insert(block_edits, wrapper)
341342
end
342343
end
343344

@@ -349,17 +350,12 @@ end
349350
---@return string
350351
local function extract_explanation(action)
351352
local explanation = action.explanation or (action.edits and action.edits[1] and action.edits[1].explanation)
352-
353-
if explanation and explanation ~= "" then
354-
return "\n" .. explanation
355-
end
356-
357-
return ""
353+
return (explanation and explanation ~= "") and ("\n" .. explanation) or ""
358354
end
359355

360-
---Process substring replaceAll edits in parallel to avoid overlaps
356+
---Execute substring replacements in parallel to avoid overlaps
361357
---@param content string
362-
---@param edits table[]
358+
---@param edits table[] Array of unwrapped edit objects
363359
---@return string|nil content, string|nil error
364360
local function process_substring_edits(content, edits)
365361
local replacements = {}
@@ -404,9 +400,9 @@ local function extract_edits(edits)
404400
end, edits)
405401
end
406402

407-
---Apply substring edits in parallel and record results
403+
---Validate and process substring edits in parallel
408404
---@param content string
409-
---@param edits table[]
405+
---@param edits table[] Array of {edit: table, original_index: number} wrappers
410406
---@return string|nil content, table|nil error
411407
local function validate_and_process_substring_edits(content, edits)
412408
local edited_content, error = process_substring_edits(content, extract_edits(edits))
@@ -423,22 +419,22 @@ local function validate_and_process_substring_edits(content, edits)
423419
return edited_content, nil
424420
end
425421

426-
---Build metadata for substring edits
427-
---@param edits table[]
428-
---@return table[] results, table[] strategies
422+
---Build metadata for successfully processed substring edits
423+
---@param edits table[] Array of {edit: table, original_index: number} wrappers
424+
---@return table[] results, table[] selected_strategy
429425
local function build_substring_metadata(edits)
430426
local results = {}
431427
local selected_strategy = {}
428+
local metadata = {
429+
strategy = "substring_exact_match_parallel",
430+
confidence = 1.0,
431+
selection_reason = "parallel_processing",
432+
auto_selected = true,
433+
}
432434

433435
for _, item in ipairs(edits) do
434-
table.insert(results, {
435-
edit_index = item.original_index,
436-
strategy = "substring_exact_match_parallel",
437-
confidence = 1.0,
438-
selection_reason = "parallel_processing",
439-
auto_selected = true,
440-
})
441-
table.insert(selected_strategy, "substring_exact_match_parallel")
436+
table.insert(results, vim.tbl_extend("force", metadata, { edit_index = item.original_index }))
437+
table.insert(selected_strategy, metadata.strategy)
442438
end
443439

444440
return results, selected_strategy
@@ -609,8 +605,9 @@ end
609605
local function process_edits(content, edits, opts)
610606
opts = opts or {}
611607

612-
local selected_strategies = {}
608+
local results = {}
613609
local block_edits = {}
610+
local selected_strategies = {}
614611

615612
edits, block_edits = partition_edits_by_type(edits)
616613

@@ -621,9 +618,9 @@ local function process_edits(content, edits, opts)
621618
end
622619

623620
content = edited_content --[[@as string]]
624-
local results, strategy_results = build_substring_metadata(edits)
625-
vim.list_extend(results, results)
626-
vim.list_extend(selected_strategies, strategy_results)
621+
local substring_results, substring_strategies = build_substring_metadata(edits)
622+
vim.list_extend(results, substring_results)
623+
vim.list_extend(selected_strategies, substring_strategies)
627624
end
628625

629626
local block_list = extract_edits(block_edits)
@@ -638,8 +635,6 @@ local function process_edits(content, edits, opts)
638635
return conflicts
639636
end
640637

641-
local results = {}
642-
643638
for _, edit_wrapper in ipairs(block_edits) do
644639
local edit = edit_wrapper.edit
645640
local original_index = edit_wrapper.original_index

0 commit comments

Comments
 (0)