Gradle: keep comments with their dependency when sorting (fixes #7516)#7517
Merged
Gradle: keep comments with their dependency when sorting (fixes #7516)#7517
Conversation
…ncies The Gradle parser stores line-comments preceding a statement on that statement's prefix. The previous implementation overwrote each sorted statement's prefix with the prefix at its new position, causing comments to remain in place while dependencies moved around them. Now each statement keeps its own prefix, and only the leading whitespace of the new first statement is harmonized to preserve block-opening formatting. Fixes #7516
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
SortDependencieswas misplacing comments because the parser stores preceding line-comments on the next statement's prefix, and the previous implementation overwrote each sorted statement's prefix with the prefix at its new position — leaving comments in place while dependencies moved around them.Each statement now keeps its own prefix during sorting, and only the leading whitespace of the new first statement is harmonized so block-opening formatting stays consistent. Blank-line groupings between dependencies are preserved.
Adds tests for Groovy DSL, Kotlin DSL, and multi-group blank-line layouts.
Fixes org.openrewrite.gradle.SortDependencies ignores comments thus misplacing them #7516
Test plan
./gradlew :rewrite-gradle:test --tests "org.openrewrite.gradle.SortDependenciesTest"(8/8 pass)./gradlew :rewrite-gradle:test --tests "org.openrewrite.gradle.GradleBestPracticesTest"(no regressions)