Skip to content

Keep the indentation of code that is replaced or formatted in place - #10089

Merged
jkschneider merged 3 commits into
mainfrom
template-keep-replaced-indentation
Nov 20, 2026
Merged

jkschneider merged 3 commits into
mainfrom
template-keep-replaced-indentation

Conversation

@jkschneider

@jkschneider jkschneider commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

A recipe that replaces or formats code in place gave the line the indentation that a file formatted from scratch would give it. In a hand-indented file that is visible churn on the very line the recipe changed: tabs among spaces, or eight spaces where the line had seven. A Java 25 migration of AT&T's code showed it on every System.out.println line that became IO.println, and on the <release> tags it updated.

The line now keeps its indentation:

  • JavaTemplate replacements of an expression, statement, annotation list, type parameters, implemented interfaces or else branch keep the indentation and comments that the replaced code's line started with.
  • JavaVisitor.maybeAutoFormat and XmlVisitor.maybeAutoFormat keep the indentation of code that a recipe changed where it stands, when the recipe left it at the indentation it had. Code that was given another indentation, such as a statement hoisted out of a block, or a fresh line break, is indented by the formatter as before.
  • AddOrUpdateChild keeps the indentation of an XML child that it replaces.

In every case the formatter still chooses the blank lines above the line and formats the lines the code nests. Nothing is read from template text.

Four core tests whose inputs indent a replaced line unusually now expect that indentation kept. In files formatted to their style, which is nearly every test, output is unchanged: the core suites and rewrite-migrate-java and rewrite-static-analysis pass, except for two recipe tests with unusually indented inputs whose expected output changes (UseMapOfTest#changeDoubleBraceInitForNonStringTypes, ReplaceValidateNotNullHavingVarargsWithObjectsRequireNonNullTest#replaceMethodsWithThreeArg). Those inputs are tidied in their repositories first.

Merge after these, which make tests that recorded a recomputed indentation pass either way:

They come from running the recipe tests of 75 openrewrite and moderneinc repositories with this change. A second run, with the XML part of this change, is in progress.

This is the fix that the proposed keepEditedLineIndentation recipe quality check (#10090) needs, so it merges before that check.

@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Nov 20, 2026
jkschneider added a commit to openrewrite/rewrite-static-analysis that referenced this pull request Nov 20, 2026
…on its line (#1275)

`RemoveUnneededBlock` formats each statement it moves out of a removed block. A statement that
shared the block's line, as in `{ System.out.println("hello!"); }`, takes the place of the block on
that line but got the indentation the formatter computes instead of the one the line had. It now
keeps the line's indentation. Statements on lines of their own are formatted as before.

The input of
`ReplaceValidateNotNullHavingVarargsWithObjectsRequireNonNullTest#replaceMethodsWithThreeArg`
indented its statement two spaces deeper than the method body, so its expected output depended on
the recipe recomputing the indentation of the line it replaces. It is now indented like the method
body, and the expected output is unchanged.

These prepare for openrewrite/rewrite#10089, which keeps the indentation of a line whose code a
recipe replaces or formats in place, and openrewrite/rewrite#10090, a recipe quality check, on by
default, that a line a recipe edits in place keeps its indentation. The tests pass with the
published rewrite snapshot and with both of those changes.
jkschneider added a commit to openrewrite/rewrite-migrate-java that referenced this pull request Nov 20, 2026
The input of `changeDoubleBraceInitForNonStringTypes` indented its double-brace initialization six
spaces deeper than the method around it, so its expected output depended on `UseMapOf` recomputing
the indentation of the line it replaces. Indented like its method, the input gives the same
expected output whether that line keeps its indentation or not.

These prepare for openrewrite/rewrite#10089, which keeps the indentation of a line whose code a
recipe replaces or formats in place, and openrewrite/rewrite#10090, a recipe quality check, on by
default, that a line a recipe edits in place keeps its indentation. The tests pass with the
published rewrite snapshot and with both of those changes.
jkschneider added a commit to openrewrite/rewrite-logging-frameworks that referenced this pull request Nov 20, 2026
#337)

The input of `doNotDeleteFile` was indented with two spaces. Its expected output recorded the
recipe re-indenting the class to four spaces, except the method's closing brace, and the
`System.out.println` line it replaces to eight. With openrewrite/rewrite#10089 that line keeps its
indentation instead. Indented with four spaces, like the expected output, the input gives the same
expected output either way.

When it adds the logger field, `SystemOutToLogging` still re-indents a class indented with two
spaces to four. That is a separate issue.

These prepare for openrewrite/rewrite#10089, which keeps the indentation of a line whose code a
recipe replaces or formats in place, and openrewrite/rewrite#10090, a recipe quality check, on by
default, that a line a recipe edits in place keeps its indentation. The tests pass with the
published rewrite snapshot and with both of those changes.
jkschneider added a commit to openrewrite/rewrite-testing-frameworks that referenced this pull request Nov 20, 2026
…r spaces (#1343)

`shouldImportWhenCustomClassIsUsed` was indented with two spaces, and its expected output recorded
the replaced assertion at six, two spaces deeper than the method body. With
openrewrite/rewrite#10089 a line whose code a `JavaTemplate` replaces keeps its indentation.
Indented with four spaces, the test expects the same output either way.

These prepare for openrewrite/rewrite#10089, which keeps the indentation of a line whose code a
recipe replaces or formats in place, and openrewrite/rewrite#10090, a recipe quality check, on by
default, that a line a recipe edits in place keeps its indentation. The tests pass with the
published rewrite snapshot and with both of those changes.
jkschneider added a commit to openrewrite/rewrite-spring that referenced this pull request Nov 20, 2026
…uts like their class (#1323)

The inputs of `inMemoryConfig` and `inMemoryConfigWithUserBuilder` indented `configure` two spaces
deeper than its annotation, and the `auth.inMemoryAuthentication()` statement two spaces deeper
than the statement before it. Their expected outputs recorded the recipe recomputing the
indentation of the `return` statement that replaces that line, which the quality check in
openrewrite/rewrite#10090 rejects. Indented like their class, the inputs give the same expected
outputs.

These prepare for openrewrite/rewrite#10089, which keeps the indentation of a line whose code a
recipe replaces or formats in place, and openrewrite/rewrite#10090, a recipe quality check, on by
default, that a line a recipe edits in place keeps its indentation. The tests pass with the
published rewrite snapshot and with both of those changes.
jkschneider added a commit to openrewrite/rewrite-apache that referenced this pull request Nov 20, 2026
…#193)

`MigrateRequestConfig` appends `.setConnectionManager(...)` to an `HttpClientBuilder` chain with a
`JavaTemplate` whose argument is the chain, and the template re-formatted the chain. In the test,
`.setDefaultRequestConfig(requestConfig)` moved from 12 spaces to 16 while `.build()` below it
stayed at 12. The recipe now puts back the chain as it was written, so the line keeps its
indentation.

These prepare for openrewrite/rewrite#10089, which keeps the indentation of a line whose code a
recipe replaces or formats in place, and openrewrite/rewrite#10090, a recipe quality check, on by
default, that a line a recipe edits in place keeps its indentation. The tests pass with the
published rewrite snapshot and with both of those changes.
jkschneider added a commit to openrewrite/rewrite-logging-frameworks that referenced this pull request Nov 20, 2026
…oPrivate changes (#338)

`ChangeLoggersToPrivate` formatted the field whose modifiers it changes with `autoFormat`, which
gives the declaration's line the indentation of a file formatted from scratch. In a file indented
by hand, or with tabs among spaces, that moves a line the recipe only edited. `maybeAutoFormat`
formats the same way, and with openrewrite/rewrite#10089 it keeps the indentation of the line where
a declaration it changes stays. Until that is released, the output is unchanged.

Found by running the tests of `ChangeLoggersToPrivate` and `Slf4jBestPractices` with one more space
at the start of each indented line that opens nothing, the opt-in shifted-input mode of
openrewrite/rewrite#10090. With openrewrite/rewrite#10089 the mode flagged 7 of those tests before
this change and none after. The tests pass with the published rewrite snapshot and with both of
those changes.
jkschneider added a commit to openrewrite/rewrite-spring that referenced this pull request Nov 20, 2026
…r changes (#1324)

`ChangeMethodParameter` formatted the method whose parameter it changes with `autoFormat`, which
gives the declaration's line the indentation of a file formatted from scratch. In a file indented
by hand, or with tabs among spaces, that moves a line the recipe only edited. `maybeAutoFormat`
formats the same way, and with openrewrite/rewrite#10089 it keeps the indentation of the line where
a declaration it changes stays. Until that is released, the output is unchanged.

Found by running the tests of `ChangeMethodParameter` with one more space at the start of each
indented line that opens nothing, the opt-in shifted-input mode of openrewrite/rewrite#10090. With
openrewrite/rewrite#10089 the mode flagged 16 of `ChangeMethodParameter`'s tests before this change
and none after. The tests pass with the published rewrite snapshot and with both of those changes.
jkschneider added a commit to openrewrite/rewrite-static-analysis that referenced this pull request Nov 20, 2026
…ieldAnnotationToType edit in place (#1276)

`FinalizeLocalVariables` and `MoveFieldAnnotationToType` formatted the declaration they change with
`autoFormat`, which gives the declaration's line the indentation of a file formatted from scratch.
In a file indented by hand, or with tabs among spaces, that moves a line the recipe only edited.
`maybeAutoFormat` formats the same way, and with openrewrite/rewrite#10089 it keeps the indentation
of the line where a declaration it changes stays. Until that is released, the output is unchanged.

Found by running the tests of `FinalizeLocalVariables` and `MoveFieldAnnotationToType` with one
more space at the start of each indented line that opens nothing, the opt-in shifted-input mode of
openrewrite/rewrite#10090. With openrewrite/rewrite#10089 the mode flagged 10 of those tests (7 of
`FinalizeLocalVariables`, 3 of `MoveFieldAnnotationToType`) before this change and none after. The
tests pass with the published rewrite snapshot and with both of those changes.
A template that replaced code starting its line formatted the replacement from scratch, so the line took the
indentation the file's detected style gives that depth: tabs among the spaces of a mixed file, or eight spaces where
the line had seven. The replacement now keeps the indentation and comments the replaced code's line started with,
as does a declaration or statement whose annotations, type parameters, implemented interfaces or else branch a
template replaces. The formatter still chooses the blank lines above the line and formats the lines it nests.

Four tests whose inputs indent a replaced line unusually now expect that indentation kept.
maybeAutoFormat formats code a recipe changed where it stands, but gave its line the indentation of a file formatted
from scratch, as JavaTemplate replacements did. When the recipe left the code on the line and at the indentation it
had, the line now keeps that indentation, and the comments above it when the recipe left them alone; comments the
recipe changed keep their formatting. Code the recipe gave another indentation, such as a statement hoisted out of
the block it was in, is indented by the formatter as before. The formatter still chooses the blank lines above the
line and formats the lines it nests. JavaTemplate replacements share the same rule.
AddOrUpdateChild replaced an existing child with a tag formatted from scratch, and XmlVisitor.maybeAutoFormat did the
same for an element a recipe changed where it stands, so the line took the document's detected indentation: tabs
among spaces, or another depth. Both now keep the indentation the line had when the element stays where it was, as
Java code does. The formatter still chooses the blank lines above the line and formats what the element nests.
@jkschneider
jkschneider force-pushed the template-keep-replaced-indentation branch from 921f255 to e2f982d Compare November 20, 2026 13:34
@jkschneider
jkschneider marked this pull request as ready for review November 20, 2026 13:50
@jkschneider
jkschneider merged commit cb0b46b into main Nov 20, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant