From 3db56105cc007830366dd5f1ef079f3d58e5ffea Mon Sep 17 00:00:00 2001 From: Monchi Date: Wed, 2 Oct 2019 20:10:24 +0900 Subject: [PATCH] Insert spacing after annotation --- .../ruleset/experimental/AnnotationRule.kt | 17 ++++++++++++++++- .../ruleset/experimental/AnnotationRuleTest.kt | 17 +++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/AnnotationRule.kt b/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/AnnotationRule.kt index 4d5d043fc4..2815b5b2cc 100644 --- a/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/AnnotationRule.kt +++ b/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/AnnotationRule.kt @@ -3,11 +3,13 @@ package com.pinterest.ktlint.ruleset.experimental import com.pinterest.ktlint.core.Rule import com.pinterest.ktlint.core.ast.ElementType.MODIFIER_LIST import com.pinterest.ktlint.core.ast.children +import com.pinterest.ktlint.core.ast.upsertWhitespaceBeforeMe import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.PsiComment import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement import org.jetbrains.kotlin.psi.KtAnnotationEntry +import org.jetbrains.kotlin.psi.psiUtil.endOffset import org.jetbrains.kotlin.psi.psiUtil.nextLeaf /** @@ -55,6 +57,15 @@ class AnnotationRule : Rule("annotation") { .take(annotations.size) .toList() + val noWhiteSpaceAfterAnnotation = whiteSpaces.isEmpty() || whiteSpaces.last().nextSibling is KtAnnotationEntry + if (noWhiteSpaceAfterAnnotation) { + emit( + annotations.last().endOffset - 1, + "Missing spacing after ${annotations.last().text}", + true + ) + } + val multipleAnnotationsOnSameLineAsAnnotatedConstruct = annotations.size > 1 && !whiteSpaces.last().textContains('\n') val annotationsWithParametersAreNotOnSeparateLines = @@ -86,11 +97,15 @@ class AnnotationRule : Rule("annotation") { it.substring(it.lastIndexOf('\n')) } + if (noWhiteSpaceAfterAnnotation) { + (annotations.last().nextLeaf() as LeafPsiElement).upsertWhitespaceBeforeMe(" ") + } if (annotationsWithParametersAreNotOnSeparateLines) { whiteSpaces.forEach { (it as LeafPsiElement).rawReplaceWithText(newLineWithIndent) } - } else if (multipleAnnotationsOnSameLineAsAnnotatedConstruct) { + } + if (multipleAnnotationsOnSameLineAsAnnotatedConstruct) { (whiteSpaces.last() as LeafPsiElement).rawReplaceWithText(newLineWithIndent) } } diff --git a/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/AnnotationRuleTest.kt b/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/AnnotationRuleTest.kt index bd6b612690..af96e34f2e 100644 --- a/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/AnnotationRuleTest.kt +++ b/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/AnnotationRuleTest.kt @@ -172,6 +172,23 @@ class AnnotationRuleTest { ) } + @Test + fun `lint spacing after annotations`() { + assertThat( + AnnotationRule().lint( + """ + class A { + @SomeAnnotation("value")val x: String + } + """.trimIndent() + ) + ).containsExactly( + LintError( + 2, 28, "annotation", "Missing spacing after @SomeAnnotation(\"value\")" + ) + ) + } + @Test fun `lint annotations with params should not be placed on same line before annotated construct`() { assertThat(