[PR #2545] Add utility function to map positions from non formatted to formatted code #10348

Closed
opened 2026-02-16 18:14:58 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/2545

State: closed
Merged: No


In preparation of #2529

This function let you map positions from a non formatted code string to a formatted one. It should be run with 2 strings where the only difference is that one is formatted and the other is not, if other changes are there too it may fail

The function sues diff_match_patch to map characters positions from one string to the other

Added thorough tests that demonstrate this function works and #2529 is feasible

This approach has problems where the input snippet would be formatted and new characters would be removed or added at the boundaries. For example when converting x => z to (x) => z. But there is good news: we can just format the code before the tool call to ensure this never happens. If the code before the tool call is already formatted then the boundaries cannot have insertions or deletions

Next steps to add support for https://github.com/sst/opencode/issues/2529:

  • when an agent reads a file in the repo, format it before returning it to the LLM. This way the diffing algorithm will always be successful because code outside the replacement is always formatted already
  • After model does the edit, format the file. Get the formatted substring using the function in this PR
  • On next request replace the edit tool input with the formatted substring
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/2545 **State:** closed **Merged:** No --- In preparation of #2529 This function let you map positions from a non formatted code string to a formatted one. It should be run with 2 strings where the only difference is that one is formatted and the other is not, if other changes are there too it may fail The function sues `diff_match_patch` to map characters positions from one string to the other Added thorough tests that demonstrate this function works and #2529 is feasible This approach has problems where the input snippet would be formatted and new characters would be removed or added at the boundaries. For example when converting `x => z` to `(x) => z`. But there is good news: we can just format the code before the tool call to ensure this never happens. If the code before the tool call is already formatted then the boundaries cannot have insertions or deletions Next steps to add support for https://github.com/sst/opencode/issues/2529: - when an agent reads a file in the repo, format it before returning it to the LLM. This way the diffing algorithm will always be successful because code outside the replacement is always formatted already - After model does the edit, format the file. Get the formatted substring using the function in this PR - On next request replace the edit tool input with the formatted substring
yindo added the pull-request label 2026-02-16 18:14:58 -05:00
yindo closed this issue 2026-02-16 18:14:58 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#10348