Skip to content

Commit

Permalink
Add file annotations rule
Browse files Browse the repository at this point in the history
  • Loading branch information
t-kameyama committed May 13, 2020
1 parent a86d1c7 commit 33b22a2
Show file tree
Hide file tree
Showing 5 changed files with 325 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ fun ASTNode.isPartOf(klass: KClass<out PsiElement>): Boolean {
fun ASTNode.isPartOfString() =
parent(STRING_TEMPLATE, strict = false) != null

fun ASTNode?.isWhiteSpace() =
this != null && elementType == WHITE_SPACE
fun ASTNode?.isWhiteSpaceWithNewline() =
this != null && elementType == WHITE_SPACE && textContains('\n')
fun ASTNode?.isWhiteSpaceWithoutNewline() =
Expand Down Expand Up @@ -228,3 +230,6 @@ fun ASTNode.visit(enter: (node: ASTNode) -> Unit, exit: (node: ASTNode) -> Unit)
this.getChildren(null).forEach { it.visit(enter, exit) }
exit(this)
}

fun ASTNode.lineNumber(): Int? =
this.psi.containingFile?.viewProvider?.document?.getLineNumber(this.startOffset)?.let { it + 1 }
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ import com.pinterest.ktlint.core.ast.ElementType.VALUE_ARGUMENT
import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER
import com.pinterest.ktlint.core.ast.children
import com.pinterest.ktlint.core.ast.isPartOf
import com.pinterest.ktlint.core.ast.isPartOfComment
import com.pinterest.ktlint.core.ast.isWhiteSpace
import com.pinterest.ktlint.core.ast.lineNumber
import com.pinterest.ktlint.core.ast.nextSibling
import com.pinterest.ktlint.core.ast.prevSibling
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.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespaceAndComments
import org.jetbrains.kotlin.psi.psiUtil.nextLeaf

/**
Expand All @@ -30,6 +37,7 @@ class AnnotationRule : Rule("annotation") {
"Multiple annotations should not be placed on the same line as the annotated construct"
const val annotationsWithParametersAreNotOnSeparateLinesErrorMessage =
"Annotations with parameters should all be placed on separate lines prior to the annotated construct"
const val fileAnnotationsShouldBeSeparated = "File annotations should be separated from packages with a blank line"
}

override fun visit(
Expand Down Expand Up @@ -62,7 +70,8 @@ class AnnotationRule : Rule("annotation") {
.take(annotations.size)
.toList()

val noWhiteSpaceAfterAnnotation = whiteSpaces.isEmpty() || whiteSpaces.last().nextSibling is KtAnnotationEntry
val noWhiteSpaceAfterAnnotation = node.elementType != FILE_ANNOTATION_LIST &&
(whiteSpaces.isEmpty() || whiteSpaces.last().nextSibling is KtAnnotationEntry)
if (noWhiteSpaceAfterAnnotation) {
emit(
annotations.last().endOffset - 1,
Expand Down Expand Up @@ -106,6 +115,29 @@ class AnnotationRule : Rule("annotation") {
}
}
}

if (node.elementType == FILE_ANNOTATION_LIST) {
val lineNumber = node.lineNumber()
val next = node.nextSibling {
!it.isWhiteSpace() && it.textLength > 0 && !(it.isPartOfComment() && it.lineNumber() == lineNumber)
}
val nextLineNumber = next?.lineNumber()
if (lineNumber != null && nextLineNumber != null) {
val diff = nextLineNumber - lineNumber
if (diff < 2) {
emit(0, fileAnnotationsShouldBeSeparated, true)
if (autoCorrect) {
if (diff == 0) {
node.psi.getNextSiblingIgnoringWhitespaceAndComments(withItself = false)?.node
?.prevSibling { it.isWhiteSpace() }
?.let { (it as? LeafPsiElement)?.delete() }
next.treeParent.addChild(PsiWhiteSpaceImpl("\n"), next)
}
next.treeParent.addChild(PsiWhiteSpaceImpl("\n"), next)
}
}
}
}
}

private fun getNewlineWithIndent(modifierListRoot: ASTNode): String {
Expand Down
Loading

0 comments on commit 33b22a2

Please # to comment.