Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address TextLocation for Wrapping #4998

Merged
merged 1 commit into from Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}
}