GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Keep the indentation of code that is replaced or formatted in place by jkschneider · Pull Request #10089 · openrewrite/rewrite

Conversation

@jkschneider

@jkschneider jkschneider commented Nov 20, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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:

  • Keep the indentation of a statement that RemoveUnneededBlock unwraps on its line rewrite-static-analysis#1275 (ReplaceValidateNotNullHavingVarargsWithObjectsRequireNonNullTest)
  • Indent UseMapOfTest's double-brace input like its method rewrite-migrate-java#1479 (UseMapOfTest)
  • Indent SystemOutToLoggingTest's doNotDeleteFile input with four spaces rewrite-logging-frameworks#337 (SystemOutToLoggingTest)
  • Indent JUnitAssertEqualsToAssertThatTest's custom-class test with four spaces rewrite-testing-frameworks#1343 (JUnitAssertEqualsToAssertThatTest)
  • moderneinc/rewrite-static-analysis#138 (StringBuilderIsEmptyTest)

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@github-project-automation github-project-automation Bot added this to OpenRewrite Nov 20, 2026
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Nov 20, 2026
@moderne-meeseeks moderne-meeseeks Bot assigned jkschneider Nov 20, 2026
This was referenced Nov 20, 2026
Keep the indentation of a statement that RemoveUnneededBlock unwraps on its line openrewrite/rewrite-static-analysis#1275
Merged
Indent UseMapOfTest's double-brace input like its method openrewrite/rewrite-migrate-java#1479
Merged
Indent SystemOutToLoggingTest's doNotDeleteFile input with four spaces openrewrite/rewrite-logging-frameworks#337
Merged
Indent JUnitAssertEqualsToAssertThatTest's custom-class test with four spaces openrewrite/rewrite-testing-frameworks#1343
Merged
Indent WebSecurityConfigurerAdapterTest's in-memory configuration inputs like their class openrewrite/rewrite-spring#1323
Merged
Keep the formatting of the builder chain MigrateRequestConfig extends openrewrite/rewrite-apache#193
Merged
Validate that recipes keep the indentation of lines they edit in place #10090
Merged
Keep the indentation of a logger field whose modifiers ChangeLoggersToPrivate changes openrewrite/rewrite-logging-frameworks#338
Merged
Keep the indentation of a method whose parameter ChangeMethodParameter changes openrewrite/rewrite-spring#1324
Merged
Keep the indentation of declarations FinalizeLocalVariables and MoveFieldAnnotationToType edit in place openrewrite/rewrite-static-analysis#1276
Merged
Preserve local XML indentation when inserting or replacing tags #10073
Closed
Preserve local indentation in JavaTemplate edits #10074
Closed
jkschneider added a commit to openrewrite/rewrite-static-analysis that referenced this pull request Nov 20, 2026
@jkschneider
Keep the indentation of a statement that RemoveUnneededBlock unwraps …
0d20d63
…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
@jkschneider
Indent UseMapOfTest's double-brace input like its method (#1479)
f0cf8b9
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
@jkschneider
Indent SystemOutToLoggingTest's doNotDeleteFile input with four spaces (
ad0c9c7
#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
@jkschneider
Indent JUnitAssertEqualsToAssertThatTest's custom-class test with fou…
b04fd48
…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
@jkschneider
Indent WebSecurityConfigurerAdapterTest's in-memory configuration inp…
4e4d537
…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
@jkschneider
Keep the formatting of the builder chain MigrateRequestConfig extends (…
dea73a7
…#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
@jkschneider
Keep the indentation of a logger field whose modifiers ChangeLoggersT…
ef260d0
…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
@jkschneider
Keep the indentation of a method whose parameter ChangeMethodParamete…
353edbe
…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
@jkschneider
Keep the indentation of declarations FinalizeLocalVariables and MoveF…
593cbe9
…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.
jkschneider added 3 commits November 20, 2026 08:34
@jkschneider
Keep the indentation of a line whose code a JavaTemplate replaces
3542f79
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.
@jkschneider
Keep the indentation of code that maybeAutoFormat formats in place
e94616c
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.
@jkschneider
Keep the indentation of XML that is replaced or formatted in place
e2f982d
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

Uh oh!

There was an error while loading. Please reload this page.

@jkschneider
jkschneider deleted the template-keep-replaced-indentation branch November 20, 2026 13:50
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Nov 20, 2026
This was referenced Nov 20, 2026
Indent text blocks with the whitespace of the concatenation they replace openrewrite/rewrite-migrate-java#1477
Merged
Keep the indentation of the statement a security DSL conversion rewrites openrewrite/rewrite-spring#1326
Merged
Keep the indentation of a declaration's line when its annotations or modifiers change #10112
Merged
Keep the blank lines above code that is replaced or formatted in place #10079
Merged
Keep the blank lines above a replaced XML element #10081
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@jkschneider jkschneider

Labels

None yet

Projects

OpenRewrite
Archived in project

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@jkschneider
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.