Skip to content

Conversation

@micuintus
Copy link

@micuintus micuintus commented Nov 21, 2025

Restrict formatting only to the edited line range for clang-format
When using the Edit tool, clang-format now only formats the specific lines
that were changed, rather than reformatting the entire file. This prevents
unrelated formatting changes from cluttering the diff.

Closes #4603

@github-actions github-actions bot force-pushed the dev branch 2 times, most recently from 917250b to f4593c6 Compare November 22, 2025 05:02
@micuintus micuintus changed the title feat(editing): Add skipAutoFormatting Parameter to Edit Tools feat(formater): restrict formatting to only the changed range of a file. Nov 22, 2025
@github-actions github-actions bot force-pushed the dev branch 2 times, most recently from df8bdf9 to 0dd5039 Compare November 22, 2025 18:21
@micuintus
Copy link
Author

@rekram1-node What is the state on this? Are you considering these suggestions?

@rekram1-node
Copy link
Collaborator

rekram1-node commented Nov 24, 2025

Yeah I see what you are trying to solve there. I would need some time to look into it more to review but I will say this looks very vibe coded so I won't just auto merge without a throughout review

@micuintus
Copy link
Author

@rekram1-node

I would need some time to look into it more to review but I will say this looks very vibe coded so I won't just auto merge without a throughout review

Yes, I did use opencode for this PR ;). I am obviously new to this codebase and not a particular TypeScript expert. I had opencode run through several iterations with several models (Sonnet 4.5, GLM 4.6, Gemini 3, CODEX) though, letting the models review and discuss their results; asked them to asses the solution and the code quality, find bugs, make suggestions for simplification and improvement etc.

But yeah, this code needs to be reviewed, of course :)

I started with the line-number-based range API of clang-format and actually left it as a separate commit (the rest is squashed into the subsequent commit). Then I checked which interface Qt Creator uses: char/byte range API for subfile formatting --- and as it enables us to apply the same logic with Prettier I thought this makes sense as a "source of truth" format.

Aside from my use case (legacy code bases with humongous unformatted areas): Auto-formatting only the affected part of the file that opencode had touched makes a lot of sense to me; formatting the rest of the file should be a different task / step anyways IMO.

Having such a system in place would not only be very beneficial for my workflow, I believe it would make opencode a better tool for everyone.

Feel feel to request changes or I'd also be happy to hand this branch over.

@micuintus
Copy link
Author

@rekram1-node Is there any way I can increase your confidence with that topic?

@micuintus
Copy link
Author

@rekram1-node Is there any way I can increase your confidence with that topic?

@rekram1-node bump :)

@thdxr
Copy link
Contributor

thdxr commented Dec 3, 2025

this is good we're gonna merge it, aiden will follow up

@rekram1-node
Copy link
Collaborator

@micuintus can you update your code to follow some of our style guidelines a bit better?
https://github.com/sst/opencode/blob/dev/STYLE_GUIDE.md

Ex: change changedRanges to just ranges, stuff like that

@micuintus
Copy link
Author

micuintus commented Dec 5, 2025

@micuintus can you update your code to follow some of our style guidelines a bit better? https://github.com/sst/opencode/blob/dev/STYLE_GUIDE.md

Ex: change changedRanges to just ranges, stuff like that

@rekram1-node Sorry for the delay, I only just saw this. -> Done: 2d47783

Anything else?

@rekram1-node
Copy link
Collaborator

/review

@github-actions
Copy link
Contributor

github-actions bot commented Dec 5, 2025

lgtm

@rekram1-node
Copy link
Collaborator

u can ignore that fail its whatever, ill merge tonight

Michael Voigt added 3 commits December 9, 2025 11:16
When using the Edit tool, clang-format now only formats the specific lines
that were changed, rather than reformatting the entire file. This prevents
unrelated formatting changes from cluttering the diff.

Adds calculateChangedLines() to detect modified line ranges from diffs,
and updates clang-format to use --lines flag for targeted formatting.
Other formatters continue to format the entire file as before.

Closes sst#4603
Switch from line-based to exact character/byte range formatting with a
robust DiffRange class. This refactoring improves compatibility across
different formatters and adds comprehensive validation.

Changes:
- Replace line ranges with exact character and byte offset tracking
- Implement DiffRange class to encapsulate range logic and conversions
- Add support for Prettier (character offsets) and clang-format (byte offsets)
- Handle Unicode multi-byte characters correctly
- Merge adjacent ranges to reduce formatter invocations
- Add validation to fromJSON() for negative/invalid ranges
- Extract clampOffset() helper to reduce code duplication
- Remove unused toByteOffsets() method
- Cache Buffer.byteLength() calls to avoid redundant computation
- Fix duplicate getCachedByteOffsets() calls in merge()

Ref sst#4603
…ature

- calculateChangedRanges -> calculateRanges
- Simplify variable names (charOffset etc.)
- Replace else statements with early returns
- mergeAdjacentRanges -> mergeRanges

Style guide: https://github.com/sst/opencode/blob/dev/STYLE_GUIDE.md

Ref sst#4603
@micuintus
Copy link
Author

/review

@rekram1-node
Copy link
Collaborator

lol only i can trigger it

@micuintus
Copy link
Author

@rekram1-node

lol only i can trigger it

LOL #FAIL :)

u can ignore that fail its whatever, ill merge tonight

Anything else I can do to help?

@micuintus
Copy link
Author

Hm?

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.

[FEATURE]: Restrict formatting only to the edited range for clang-format (and Prettier)

3 participants