Skip to content

Gradle: keep comments with their dependency when sorting (fixes #7516)#7517

Merged
timtebeek merged 1 commit intomainfrom
tim/gradle-sort-comments
Apr 29, 2026
Merged

Gradle: keep comments with their dependency when sorting (fixes #7516)#7517
timtebeek merged 1 commit intomainfrom
tim/gradle-sort-comments

Conversation

@timtebeek
Copy link
Copy Markdown
Member

@timtebeek timtebeek commented Apr 29, 2026

Summary

  • SortDependencies was 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)

…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
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Apr 29, 2026
@timtebeek timtebeek merged commit bcbb2e9 into main Apr 29, 2026
1 check passed
@timtebeek timtebeek deleted the tim/gradle-sort-comments branch April 29, 2026 10:10
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Apr 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

org.openrewrite.gradle.SortDependencies ignores comments thus misplacing them

1 participant