Skip to content

Validate that recipes keep the indentation of lines they edit in place - #10090

Merged
jkschneider merged 6 commits into
mainfrom
indent-preservation-check
Nov 20, 2026
Merged

jkschneider merged 6 commits into
mainfrom
indent-preservation-check

Conversation

@jkschneider

@jkschneider jkschneider commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

A new recipe quality check, keepEditedLineIndentation, on by default: a line that a recipe edits in place keeps its leading whitespace. A line is edited in place when its code changes and the lines around it don't: one line in, one line out, between lines the recipe left alone.

The comparison diffs each result with leading whitespace ignored, so only lines whose code changed are compared. It leaves out:

  • runs of consecutive changed lines, which are usually code that changed shape, such as an if-else chain that became switch cases;
  • a line edited inside code that moved, whose unchanged neighbors changed indentation too;
  • a replacement that shares too little text with the old line to be the same line;
  • the space that aligns a doc comment's *;
  • YAML and Python, whose indentation is structure, binary files, and formatting recipes.

A test opts out with RecipeQualityValidation.builder().keepEditedLineIndentation(false). Two tests here do: a ColdFusion NormalizeFormat test whose ad-hoc recipe moves a removed modifier's space into the function's prefix on purpose, and a Ruby heredoc test of the naive way to replace a body, which drops the body's indentation with its text.

With the system property org.openrewrite.test.keepEditedLineIndentation.shiftedInput, each recipe test also reruns the recipe on its Java and XML sources with one more space at the start of each indented line that opens nothing, and applies the same check. Test inputs are formatted to their style, so a recipe that recomputes a line's indentation from the style produces the expected output; this mode shows it, as hand-indented files do. It is off by default, a measuring tool for sweeping recipe repositories.

The check compares source printed without markers, so a search result printed in front of a line's indentation is not an edit, and it reads whitespace escapes that a line's code starts with as the whitespace they stand for.

The fixes this check needs are merged. Running the recipe tests of 75 openrewrite and moderneinc repositories with it found them:

Three more recipe PRs switch autoFormat to maybeAutoFormat so that those recipes keep a declaration's line too (openrewrite/rewrite-logging-frameworks#338, openrewrite/rewrite-spring#1324, openrewrite/rewrite-static-analysis#1276). A last run over the nine repositories that changed since, with #10089 and this check, found nothing for the check. Tests that need a JavaScript, Python or C# runtime did not run.

@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Nov 20, 2026
@jkschneider
jkschneider force-pushed the indent-preservation-check branch from 111bcb6 to ea66c10 Compare November 20, 2026 12:38
@jkschneider
jkschneider changed the base branch from main to template-keep-replaced-indentation November 20, 2026 12:38
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.
@jkschneider
jkschneider force-pushed the template-keep-replaced-indentation branch from 921f255 to e2f982d Compare November 20, 2026 13:34
@jkschneider
jkschneider force-pushed the indent-preservation-check branch from 44863d2 to 03ae33c Compare November 20, 2026 13:34
Base automatically changed from template-keep-replaced-indentation to main November 20, 2026 13:50
A new recipe quality check, keepEditedLineIndentation, on by default: a line that a recipe edits in place, one line
in and one line out between lines it left alone, keeps its leading whitespace. Code that replaces the first element
on a line takes over the prefix of the element it replaced.

The comparison diffs each result's text with leading whitespace ignored, so only lines whose code changed are
compared. It leaves out runs of consecutive changed lines, which are usually code that changed shape; lines edited
inside code that moved, whose unchanged neighbors changed indentation too; replacements that share too little text
with the old line to be the same line; the space that aligns a doc comment's star; YAML and Python, whose indentation
is structure; binary files; and formatting recipes.
With the system property org.openrewrite.test.keepEditedLineIndentation.shiftedInput set, each recipe test reruns the
recipe on the Java and XML sources it changed with one more space at the start of each line that opens nothing, and
checks the lines it edits in place against that input. Test inputs are formatted to their style, so a recipe that
formats what it changes, or builds a prefix from the style, gets the expected output; a statement one space deeper
than its parent plus the style's unit shows it, as hand-indented files do. Off by default: a measuring tool for
sweeping recipe repositories.
…dentation

A search result on plain text prints in front of the line's leading whitespace, so a search recipe looked like it
reindented the line it found. The indentation checks now compare the source as it is written.
A recipe that escapes the tabs a text block line starts with keeps the line's whitespace, spelled differently, so the
indentation check reads the escapes a line's code starts with as the whitespace they stand for.
The test removes a modifier and checks that normalizing moves its space into the function's prefix, which deepens
the line on purpose.
The test replaces a heredoc body with a quoted literal the naive way, dropping the body's indentation with its
text, to check that the terminator still starts its own line.
@jkschneider
jkschneider force-pushed the indent-preservation-check branch from 03ae33c to 29b883f Compare November 20, 2026 13:50
@jkschneider
jkschneider marked this pull request as ready for review November 20, 2026 13:52
@jkschneider
jkschneider merged commit e3ce456 into main Nov 20, 2026
1 check passed
@jkschneider
jkschneider deleted the indent-preservation-check branch November 20, 2026 13:52
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Nov 20, 2026
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