diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/SpacingAroundParensRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/SpacingAroundParensRule.kt index 9c01b0765a..daa1d2a79b 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/SpacingAroundParensRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/SpacingAroundParensRule.kt @@ -4,6 +4,7 @@ import com.github.shyiko.ktlint.core.Rule import com.github.shyiko.ktlint.core.ast.ElementType.IDENTIFIER import com.github.shyiko.ktlint.core.ast.ElementType.LPAR import com.github.shyiko.ktlint.core.ast.ElementType.RPAR +import com.github.shyiko.ktlint.core.ast.ElementType.SUPER_KEYWORD import com.github.shyiko.ktlint.core.ast.ElementType.VALUE_ARGUMENT_LIST import com.github.shyiko.ktlint.core.ast.ElementType.VALUE_PARAMETER_LIST import com.github.shyiko.ktlint.core.ast.nextLeaf @@ -11,6 +12,11 @@ import com.github.shyiko.ktlint.core.ast.prevLeaf import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace +/** + * Ensures there are no extra spaces around parentheses. + * + * See https://kotlinlang.org/docs/reference/coding-conventions.html, "Horizontal Whitespace" + */ class SpacingAroundParensRule : Rule("paren-spacing") { override fun visit( @@ -23,7 +29,9 @@ class SpacingAroundParensRule : Rule("paren-spacing") { val nextLeaf = node.nextLeaf() val spacingBefore = if (node.elementType == LPAR) { prevLeaf is PsiWhiteSpace && !prevLeaf.textContains('\n') && - prevLeaf.prevLeaf()?.elementType == IDENTIFIER && ( + (prevLeaf.prevLeaf()?.elementType == IDENTIFIER || + // Super keyword needs special-casing + prevLeaf.prevLeaf()?.elementType == SUPER_KEYWORD) && ( node.treeParent?.elementType == VALUE_PARAMETER_LIST || node.treeParent?.elementType == VALUE_ARGUMENT_LIST ) diff --git a/ktlint-ruleset-standard/src/test/resources/spec/paren-spacing/format-expected.kt.spec b/ktlint-ruleset-standard/src/test/resources/spec/paren-spacing/format-expected.kt.spec index b073ad5a05..e537e5c53f 100644 --- a/ktlint-ruleset-standard/src/test/resources/spec/paren-spacing/format-expected.kt.spec +++ b/ktlint-ruleset-standard/src/test/resources/spec/paren-spacing/format-expected.kt.spec @@ -1,4 +1,5 @@ fun main() { + super() val a = ((1 + 2) / 3) fn((1 + 2) / 3) fn((1 + 2) / 3) diff --git a/ktlint-ruleset-standard/src/test/resources/spec/paren-spacing/format.kt.spec b/ktlint-ruleset-standard/src/test/resources/spec/paren-spacing/format.kt.spec index 7399979ed6..e976de625a 100644 --- a/ktlint-ruleset-standard/src/test/resources/spec/paren-spacing/format.kt.spec +++ b/ktlint-ruleset-standard/src/test/resources/spec/paren-spacing/format.kt.spec @@ -1,4 +1,5 @@ fun main() { + super () val a = ( (1 + 2) / 3 ) fn( (1 + 2) / 3) fn ((1 + 2) / 3) diff --git a/ktlint-ruleset-standard/src/test/resources/spec/paren-spacing/lint.kt.spec b/ktlint-ruleset-standard/src/test/resources/spec/paren-spacing/lint.kt.spec index 514555d771..f438774584 100644 --- a/ktlint-ruleset-standard/src/test/resources/spec/paren-spacing/lint.kt.spec +++ b/ktlint-ruleset-standard/src/test/resources/spec/paren-spacing/lint.kt.spec @@ -1,4 +1,5 @@ fun main() { + super () val a = ( (1 + 2) / 3 ) fn( (1 + 2) / 3) fn ((1 + 2) / 3) @@ -19,10 +20,11 @@ fun main() { } // expect -// 2:14:Unexpected spacing after "(" -// 2:26:Unexpected spacing before ")" -// 3:8:Unexpected spacing after "(" -// 4:7:Unexpected spacing before "(" -// 5:8:Unexpected spacing after "(" -// 6:16:Unexpected spacing before "(" -// 7:8:Unexpected spacing after "(" +// 2:10:Unexpected spacing before "(" +// 3:14:Unexpected spacing after "(" +// 3:26:Unexpected spacing before ")" +// 4:8:Unexpected spacing after "(" +// 5:7:Unexpected spacing before "(" +// 6:8:Unexpected spacing after "(" +// 7:16:Unexpected spacing before "(" +// 8:8:Unexpected spacing after "("