Skip to content

Commit

Permalink
Remove PsiFile.lineSeparator (#7240)
Browse files Browse the repository at this point in the history
* Use built-in functions to store & retrieve detected line separator

detekt only needs to set this itself when compiling KtFile directly.
When other methods are used to create KtFiles the line separator is
automatically set.

* Remove PsiFile.lineSeparator

* Add line ending tests for autocorrect

* Trigger automatic line separator detection in tests

This is required when loading KtFile using standard methods
  • Loading branch information
3flex committed May 6, 2024
1 parent b01bc7a commit 1d6b738
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 19 deletions.
Expand Up @@ -286,5 +286,35 @@ class RunnerSpec {
""".trimIndent()
)
}

@Test
fun `keeps LF line endings after autocorrect`() {
val inputPath = resourceAsPath("/autocorrect/SingleRuleLF.kt")

assertThatThrownBy {
Runner(parseArguments(args + inputPath.toString()), outPrintStream, errPrintStream).execute()
}.isInstanceOf(IssuesFound::class.java)

assertThat(errPrintStream.toString()).isEmpty()
assertThat(outPrintStream.toString())
.contains("${inputPath.absolutePathString()}:3:1: Needless blank line(s) [NoConsecutiveBlankLines]")
.contains("$modificationMessagePrefix${inputPath.absolutePathString()}$modificationMessageSuffix")
assertThat(inputPath).content().isEqualTo("class Test {\n\n}\n")
}

@Test
fun `keeps CRLF line endings after autocorrect`() {
val inputPath = resourceAsPath("/autocorrect/SingleRuleCRLF.kt")

assertThatThrownBy {
Runner(parseArguments(args + inputPath.toString()), outPrintStream, errPrintStream).execute()
}.isInstanceOf(IssuesFound::class.java)

assertThat(errPrintStream.toString()).isEmpty()
assertThat(outPrintStream.toString())
.contains("${inputPath.absolutePathString()}:3:1: Needless blank line(s) [NoConsecutiveBlankLines]")
.contains("$modificationMessagePrefix${inputPath.absolutePathString()}$modificationMessageSuffix")
assertThat(inputPath).content().isEqualTo("class Test {\r\n\r\n}\r\n")
}
}
}
2 changes: 2 additions & 0 deletions detekt-cli/src/test/resources/autocorrect/.gitattributes
@@ -0,0 +1,2 @@
SingleRuleLF.kt text eol=lf
SingleRuleCRLF.kt text eol=crlf
9 changes: 9 additions & 0 deletions detekt-cli/src/test/resources/autocorrect/SingleRuleCRLF.kt
@@ -0,0 +1,9 @@
class Test {







}
9 changes: 9 additions & 0 deletions detekt-cli/src/test/resources/autocorrect/SingleRuleLF.kt
@@ -0,0 +1,9 @@
class Test {







}
@@ -1,7 +1,6 @@
package io.gitlab.arturbosch.detekt.core

import io.github.detekt.psi.absolutePath
import io.github.detekt.psi.lineSeparator
import io.gitlab.arturbosch.detekt.api.Detektion
import io.gitlab.arturbosch.detekt.api.FileProcessListener
import io.gitlab.arturbosch.detekt.api.Notification
Expand All @@ -27,7 +26,9 @@ class KtFileModifier : FileProcessListener {
private fun KtFile.unnormalizeContent(): String =
StringUtilRt.convertLineSeparators(
checkNotNull(modifiedText),
lineSeparator
checkNotNull(virtualFile.detectedLineSeparator) {
"Line separator was not automatically detected. This is unexpected."
}
)
}

Expand Down
@@ -1,7 +1,6 @@
package io.github.detekt.parser

import io.github.detekt.psi.absolutePath
import io.github.detekt.psi.lineSeparator
import org.intellij.lang.annotations.Language
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtilRt
Expand Down Expand Up @@ -30,7 +29,7 @@ open class KtCompiler(
return psiFile.apply {
val normalizedAbsolutePath = path.absolute().normalize()
this.absolutePath = normalizedAbsolutePath
this.lineSeparator = content.determineLineSeparator()
virtualFile.detectedLineSeparator = content.determineLineSeparator()
}
}
}
Expand Down
@@ -1,6 +1,5 @@
package io.github.detekt.parser

import io.github.detekt.psi.lineSeparator
import io.github.detekt.test.utils.resourceAsPath
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatIllegalArgumentException
Expand All @@ -21,14 +20,20 @@ class KtCompilerSpec {
fun `Kotlin file with LF line separators has extra user data`() {
val ktFile = ktCompiler.compile(path.resolve("DefaultLf.kt"))

assertThat(ktFile.lineSeparator).isEqualTo("\n")
// visit file to trigger line detection
ktFile.accept(KtTreeVisitorVoid())

assertThat(ktFile.virtualFile.detectedLineSeparator).isEqualTo("\n")
}

@Test
fun `Kotlin file with CRLF line separators has extra user data`() {
val ktFile = ktCompiler.compile(path.resolve("DefaultCrLf.kt"))

assertThat(ktFile.lineSeparator).isEqualTo("\r\n")
// visit file to trigger line detection
ktFile.accept(KtTreeVisitorVoid())

assertThat(ktFile.virtualFile.detectedLineSeparator).isEqualTo("\r\n")
}

@Test
Expand Down
5 changes: 0 additions & 5 deletions detekt-psi-utils/api/detekt-psi-utils.api
Expand Up @@ -13,11 +13,6 @@ public final class io/github/detekt/psi/FunctionMatcher$Companion {
public final fun fromFunctionSignature (Ljava/lang/String;)Lio/github/detekt/psi/FunctionMatcher;
}

public final class io/github/detekt/psi/KeysKt {
public static final fun getLineSeparator (Lorg/jetbrains/kotlin/com/intellij/psi/PsiFile;)Ljava/lang/String;
public static final fun setLineSeparator (Lorg/jetbrains/kotlin/com/intellij/psi/PsiFile;Ljava/lang/String;)V
}

public final class io/github/detekt/psi/KtFilesKt {
public static final fun absolutePath (Lorg/jetbrains/kotlin/com/intellij/psi/PsiFile;)Ljava/nio/file/Path;
public static final fun fileNameWithoutSuffix (Lorg/jetbrains/kotlin/com/intellij/psi/PsiFile;Ljava/util/List;)Ljava/lang/String;
Expand Down
7 changes: 0 additions & 7 deletions detekt-psi-utils/src/main/kotlin/io/github/detekt/psi/Keys.kt

This file was deleted.

0 comments on commit 1d6b738

Please sign in to comment.