opencode icon indicating copy to clipboard operation
opencode copied to clipboard

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

Open micuintus opened this issue 1 month ago • 7 comments

Range-Based Code Formatting Enhancement

Overview

Implemented intelligent range-based formatting that only formats edited code sections, preventing unrelated formatting changes from cluttering diffs when using the Edit tool.

micuintus avatar Nov 21 '25 18:11 micuintus

@micuintus you can do this already:

{
  "$schema": "https://opencode.ai/config.json",
  "formatter": false,
}

rekram1-node avatar Nov 21 '25 18:11 rekram1-node

@rekram1-node But that means formatting is disabled globally? I cannot tell my LLM in the same session to skip formatting as I could with 6e2d694?

micuintus avatar Nov 21 '25 18:11 micuintus

I guess I see what u mean, kinda hesitant to be adding things to edit tool tho

rekram1-node avatar Nov 21 '25 18:11 rekram1-node

@rekram1-node

I guess I see what u mean, kinda hesitant to be adding things to edit tool tho

Gotcha!

I went back to the drawing board and realised: What I actually want is not to discard the formatting, but restrict the auto-formatting to only the sections of the file that were changed.

  1. I updated this issue's description and the PR https://github.com/sst/opencode/pull/4604
  2. Seems be working like a charm (no pun intended :P)
  3. For me it resolves a VERY annoying issue, which rendered OpenCode unusable in these use cases (1000 of formatting changes)

micuintus avatar Nov 22 '25 12:11 micuintus

@micuintus you can do this already:

{
  "$schema": "https://opencode.ai/config.json",
  "formatter": false,
}

I've tried this, and I get:

opencode --continue                                                                                   1293ms  Thu 27 Nov 18:11:59 2025
↳ Invalid input: expected record, received boolean formatter

Are you sure this is right? I want to disable this as it's a really annoying thing to have on by default and makes any PR to a codebase that might have its own custom rules spammed with irrelevant formatting changes.

I've tried manually disabling uv and ruff by using the custom entries as defined here but, my Python code still is littered with random newlines in places not even adjusted. Is there a way to disable this globally? I don't want my coding agent editor to reformat anything

jamesforwardnwboxed avatar Nov 27 '25 18:11 jamesforwardnwboxed

@micuintus you can do this already: I've tried manually disabling uv and ruff by using the custom entries as defined here but, my Python code still is littered with random newlines in places not even adjusted. Is there a way to disable this globally? I don't want my coding agent editor to reformat anything

Have you tried out my branch? When making changes with opencode it has to make some changes to the code -- that is the whole idea after all :). As LLMs have the tendency to make weirdly formatted code (trailing whitespaces, wrong indentation) it makes sense to autoformat these changes -- they have to be formatted somehow.

micuintus avatar Nov 28 '25 11:11 micuintus

@rekram1-node bump :)

micuintus avatar Dec 02 '25 09:12 micuintus

@jamesforwardnwboxed you must be on an older version of opencode?

rekram1-node avatar Dec 02 '25 16:12 rekram1-node

@micuintus yeah sorry for delays super busy rn

rekram1-node avatar Dec 02 '25 16:12 rekram1-node