Skip to content

Commit

Permalink
Address TextLocation for Wrapping (#4998)
Browse files Browse the repository at this point in the history
  • Loading branch information
chao2zhang committed Jun 27, 2022
1 parent 3b7cae1 commit 5170077
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
Expand Up @@ -38,7 +38,8 @@ class Indentation(config: Config) : FormattingRule(config) {
)

/**
* [wrapping] is working with file's [node] and we don't want to highlight the whole file
* [IndentationRule] has visitor modifier RunOnRootNodeOnly, so [node] is always the root file.
* Override the parent implementation to highlight the entire file.
*/
override fun getTextLocationForViolation(node: ASTNode, offset: Int): TextLocation {
val relativeEnd = node.text
Expand Down
Expand Up @@ -2,9 +2,11 @@ package io.gitlab.arturbosch.detekt.formatting.wrappers

import com.pinterest.ktlint.ruleset.standard.WrappingRule
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.TextLocation
import io.gitlab.arturbosch.detekt.api.internal.ActiveByDefault
import io.gitlab.arturbosch.detekt.api.internal.AutoCorrectable
import io.gitlab.arturbosch.detekt.formatting.FormattingRule
import org.jetbrains.kotlin.com.intellij.lang.ASTNode

/**
* See [ktlint-website](https://ktlint.github.io#rule-indentation) for documentation.
Expand All @@ -15,4 +17,13 @@ class Wrapping(config: Config) : FormattingRule(config) {

override val wrapping = WrappingRule()
override val issue = issueFor("Reports missing newlines (e.g. between parentheses of a multi-line function call")

/**
* [Wrapping] has visitor modifier RunOnRootNodeOnly, so [node] is always the root file.
* Override the parent implementation to highlight the entire file.
*/
override fun getTextLocationForViolation(node: ASTNode, offset: Int): TextLocation {
// Use offset + 1 since Wrapping always reports the location of missing new line.
return TextLocation(offset, offset + 1)
}
}
Expand Up @@ -2,7 +2,7 @@ package io.gitlab.arturbosch.detekt.formatting

import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.formatting.wrappers.Wrapping
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.assert
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

Expand All @@ -22,8 +22,16 @@ class WrappingSpec {
class A() : B,
C {
}
interface B
interface C
""".trimIndent()

assertThat(subject.lint(code)).hasSize(1)
subject.lint(code).assert()
.hasSize(1)
.hasSourceLocation(1, 12)
.hasTextLocations(11 to 12)
}
}

0 comments on commit 5170077

Please sign in to comment.