Open nebulon42 opened 3 months ago
Maybe the issue is not detected in the tests because of the use of trimmedLinesOf
which seems to strip the whitespace away.
A quick fix would be to transform the newline chars that are added by the StringBuilder
to the system newline characters and additionally trim away trailing carriage returns (if present) from the tokenization.
Index: sort/src/main/kotlin/com/squareup/sort/kotlin/KotlinSorter.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/sort/src/main/kotlin/com/squareup/sort/kotlin/KotlinSorter.kt b/sort/src/main/kotlin/com/squareup/sort/kotlin/KotlinSorter.kt
--- a/sort/src/main/kotlin/com/squareup/sort/kotlin/KotlinSorter.kt (revision 7496c0a0281bcc61aac8c84db2c80de982efbe20)
+++ b/sort/src/main/kotlin/com/squareup/sort/kotlin/KotlinSorter.kt (date 1723210091951)
@@ -87,7 +87,7 @@
override fun exitNamedBlock(ctx: NamedBlockContext) {
if (ctx.isDependencies) {
- rewriter.replace(ctx.start, ctx.stop, dependenciesBlock())
+ rewriter.replace(ctx.start, ctx.stop, dependenciesBlock().replace("\n", System.lineSeparator()))
// Whenever we exit a dependencies block, clear this map. Each block will be treated separately.
mutableDependencies.clear()
@@ -148,10 +148,10 @@
newOrder += declaration
// Write preceding comments if there are any
- if (texts.comment != null) appendLine(texts.comment)
+ if (texts.comment != null) appendLine(texts.comment.replace("\r", ""))
append(indent.repeat(level))
- appendLine(texts.declarationText)
+ appendLine(texts.declarationText.replace("\r", ""))
}
}
I have tried this out in a snapshot JAR and it fixes the problem for me (in Kotlin syntax, do not have Groovy available). It feels a bit ugly that is why I didn't open a PR for it yet. Since I lack the deeper knowledge about the library I'm unsure if there is a better solution.
I have a build file with CRLF line endings and am using the autocrlf setting with Git. When running the sorter via the Gradle plugin (sortDependencies) then the dependencies are sorted but the file is then converted to LF and the dependencies block which looked like this before
looks like this afterwards
So a lot of extra whitespace is introdued. Version is 0.7.
Is there a way to keep the file line endings as they were before and avoid the extra whitespace with sorting?