Skip to content

fix(tools): @insert_edit_into_file tool: Allow patch starting by empty line before a @@ identifier. #1663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

Jufralice
Copy link
Contributor

@Jufralice Jufralice commented Jun 17, 2025

This is especially usefull when the LLM forget to add the begin and end markers. It indeed uses to start with for example "\n@@Focus()" we previously assumed that this was the start of a new focus and we then inserted the ongoing 'patch' in the table. But in this case, the current patch is empty and trying to parse an empty patch returned an error. With this change, we only insert the ongoing patch into the table if it is not empty.

A new test case has been added to validate the correct handling of patches that start with empty lines. A duplicate test name was also resolved for clarity.

Description

This PR is a little complement to the nice work done by @pratyushmittal in this #1653
It allows patches starting by empty line before a @@ identifier.

Checklist

  • I've read the contributing guidelines and have adhered to them in this PR
  • I've updated CodeCompanion.has in the init.lua file for my new feature
  • I've added test coverage for this fix/feature
  • I've updated the README and/or relevant docs pages
  • I've run make all to ensure docs are generated, tests pass and my formatting is applied

This is especially usefull when the LLM forget to add the begin and end
markers. It indeed uses to start with for example "\\n@@Focus()"
we previously assumed that this was the start of a new focus and we then
inserted the ongoing 'patch' in the table. But in this case, the current patch is
empty and trying to parse an empty patch returned an error.
With this change, we only insert the ongoing patch into the table if it
is not empty.

A new test case has been added to validate the correct handling of patches that start with empty lines. A duplicate test name was also resolved for clarity.
Comment on lines 105 to 106
-- only when the next line uses @@ identifier
table.insert(changes, change)
change = get_new_change()
elseif line:sub(1, 1) == "-" then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good catch.

Just a small nitpick, can we add a noop do end to show we need to do nothing in this else block?

Something like:

    elseif line == "" and lines[i + 1] and lines[i + 1]:match("^@@") then
      -- empty lines can be part of pre/post context
      -- we treat empty lines as new change block and not as post context
      -- only when the next line uses @@ identifier
      -- skip this line and do nothing
      do
      end
    elseif line:sub(1, 1) == "-" then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea. thank you @pratyushmittal .

@olimorris
Copy link
Owner

I'm viewing this on my mobile so may have missed some detail but can we also update the tests to be patch tests after @pratyushmittal's test refactor was merged.

@Jufralice
Copy link
Contributor Author

I'm viewing this on my mobile so may have missed some detail but can we also update the tests to be patch tests after @pratyushmittal's test refactor was merged.

Hi @olimorris ,
of course.
done ✅

@olimorris olimorris merged commit 370ec56 into olimorris:main Jun 24, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants