Skip to content

Commit

Permalink
simple psi removals
Browse files Browse the repository at this point in the history
  • Loading branch information
mgroth0 committed Dec 10, 2024
1 parent 94624ba commit e962b64
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 45 deletions.
1 change: 1 addition & 0 deletions ktlint-rule-engine-core/api/ktlint-rule-engine-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public final class com/pinterest/ktlint/rule/engine/core/api/ASTNodeExtensionKt
public static final fun firstChildLeafOrSelf (Lorg/jetbrains/kotlin/com/intellij/lang/ASTNode;)Lorg/jetbrains/kotlin/com/intellij/lang/ASTNode;
public static final fun getCOMMENT_TOKENS ()Lorg/jetbrains/kotlin/com/intellij/psi/tree/TokenSet;
public static final fun getColumn (Lorg/jetbrains/kotlin/com/intellij/lang/ASTNode;)I
public static final fun getPrevSiblingIgnoringWhitespaceAndComments (Lorg/jetbrains/kotlin/com/intellij/lang/ASTNode;)Lorg/jetbrains/kotlin/com/intellij/lang/ASTNode;
public static final fun hasModifier (Lorg/jetbrains/kotlin/com/intellij/lang/ASTNode;Lorg/jetbrains/kotlin/com/intellij/psi/tree/IElementType;)Z
public static final fun hasNewLineInClosedRange (Lorg/jetbrains/kotlin/com/intellij/lang/ASTNode;Lorg/jetbrains/kotlin/com/intellij/lang/ASTNode;)Z
public static final fun indent (Lorg/jetbrains/kotlin/com/intellij/lang/ASTNode;Z)Ljava/lang/String;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
import org.jetbrains.kotlin.com.intellij.psi.tree.IElementType
import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet
import org.jetbrains.kotlin.psi.psiUtil.leaves
import org.jetbrains.kotlin.psi.psiUtil.siblings
import org.jetbrains.kotlin.util.prefixIfNot
import org.jetbrains.kotlin.utils.addToStdlib.applyIf
import kotlin.reflect.KClass
Expand Down Expand Up @@ -574,3 +575,9 @@ public fun ASTNode.replaceWith(node: ASTNode) {
public fun ASTNode.remove() {
treeParent.removeChild(this)
}

public fun ASTNode.getPrevSiblingIgnoringWhitespaceAndComments(): ASTNode? =
siblings(forward = false)
.filter {
!it.isWhiteSpace() && !COMMENT_TOKENS.contains(it.elementType)
}.firstOrNull()
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class AnnotationSpacingRule : StandardRule("annotation-spacing") {
},
{
// Disallow multiple white spaces as well as comments
if (it.psi is PsiWhiteSpace) {
if (it.isWhiteSpace()) {
val s = it.text
// Ensure at least one occurrence of two line breaks
s.indexOf("\n") != s.lastIndexOf("\n")
Expand All @@ -90,8 +90,7 @@ public class AnnotationSpacingRule : StandardRule("annotation-spacing") {
)
if (next != null) {
if (node.elementType != ElementType.FILE_ANNOTATION_LIST && next.isPartOfComment()) {
val psi = node.psi
emit(psi.endOffset, ERROR_MESSAGE, true)
emit(node.textRange.endOffset, ERROR_MESSAGE, true)
.ifAutocorrectAllowed {
// Special-case autocorrection when the annotation is separated from the annotated construct
// by a comment: we need to swap the order of the comment and the annotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import com.pinterest.ktlint.rule.engine.core.api.upsertWhitespaceBeforeMe
import com.pinterest.ktlint.rule.engine.core.util.safeAs
import com.pinterest.ktlint.ruleset.standard.StandardRule
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtFunctionLiteral
import org.jetbrains.kotlin.psi.stubs.elements.KtTokenSets

/**
* Insert a blank line before declarations. No blank line is inserted before between a class or method signature and the first declaration
Expand Down Expand Up @@ -150,7 +150,7 @@ public class BlankLineBeforeDeclarationRule :
}

node
.takeIf { it.psi is KtDeclaration }
.takeIf { KtTokenSets.DECLARATION_TYPES.contains(it.elementType) }
?.takeIf {
val prevLeaf = it.prevLeaf()
prevLeaf != null && (!prevLeaf.isWhiteSpace() || !prevLeaf.text.startsWith("\n\n"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.pinterest.ktlint.rule.engine.core.api.editorconfig.MAX_LINE_LENGTH_PR
import com.pinterest.ktlint.rule.engine.core.api.hasModifier
import com.pinterest.ktlint.rule.engine.core.api.ifAutocorrectAllowed
import com.pinterest.ktlint.rule.engine.core.api.indent
import com.pinterest.ktlint.rule.engine.core.api.isLeaf
import com.pinterest.ktlint.rule.engine.core.api.isPartOfComment
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpace
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithNewline
Expand All @@ -54,7 +55,6 @@ import com.pinterest.ktlint.rule.engine.core.api.upsertWhitespaceBeforeMe
import com.pinterest.ktlint.ruleset.standard.StandardRule
import org.ec4j.core.model.PropertyType
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafElement

/**
* Formats the class signature according to https://kotlinlang.org/docs/coding-conventions.html#class-headers
Expand Down Expand Up @@ -651,7 +651,7 @@ public class ClassSignatureRule :
private fun List<ASTNode>.collectLeavesRecursively(): List<ASTNode> = flatMap { it.collectLeavesRecursively() }

private fun ASTNode.collectLeavesRecursively(): List<ASTNode> =
if (psi is LeafElement) {
if (isLeaf()) {
listOf(this)
} else {
children()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.rule.engine.core.api.AutocorrectDecision
import com.pinterest.ktlint.rule.engine.core.api.ElementType
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ANNOTATION_ENTRY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS_BODY
Expand All @@ -17,6 +18,7 @@ import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EditorConfig
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_SIZE_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_STYLE_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.firstChildLeafOrSelf
import com.pinterest.ktlint.rule.engine.core.api.hasModifier
import com.pinterest.ktlint.rule.engine.core.api.ifAutocorrectAllowed
import com.pinterest.ktlint.rule.engine.core.api.isPartOfComment
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithNewline
Expand All @@ -29,7 +31,6 @@ import com.pinterest.ktlint.rule.engine.core.api.upsertWhitespaceAfterMe
import com.pinterest.ktlint.rule.engine.core.api.upsertWhitespaceBeforeMe
import com.pinterest.ktlint.ruleset.standard.StandardRule
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.psi.KtClass

/**
*
Expand Down Expand Up @@ -61,7 +62,7 @@ public class EnumWrappingRule :
) {
node
.takeIf { node.elementType == CLASS }
?.takeIf { (node.psi as KtClass).isEnum() }
?.takeIf { node.hasModifier(ElementType.ENUM_KEYWORD) }
?.findChildByType(CLASS_BODY)
?.let { classBody ->
visitEnumClass(classBody, emit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.pinterest.ktlint.rule.engine.core.api.editorconfig.MAX_LINE_LENGTH_PR
import com.pinterest.ktlint.rule.engine.core.api.ifAutocorrectAllowed
import com.pinterest.ktlint.rule.engine.core.api.indent
import com.pinterest.ktlint.rule.engine.core.api.isCodeLeaf
import com.pinterest.ktlint.rule.engine.core.api.isLeaf
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpace
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithNewline
import com.pinterest.ktlint.rule.engine.core.api.nextCodeLeaf
Expand All @@ -56,7 +57,6 @@ import com.pinterest.ktlint.ruleset.standard.rules.FunctionSignatureRule.Functio
import org.ec4j.core.model.PropertyType
import org.ec4j.core.model.PropertyType.PropertyValueParser.EnumValueParser
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.utils.addToStdlib.ifTrue

Expand Down Expand Up @@ -700,7 +700,7 @@ public class FunctionSignatureRule :
private fun List<ASTNode>.collectLeavesRecursively(): List<ASTNode> = flatMap { it.collectLeavesRecursively() }

private fun ASTNode.collectLeavesRecursively(): List<ASTNode> =
if (psi is LeafElement) {
if (isLeaf()) {
listOf(this)
} else {
children()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.logger.api.initKtLintKLogger
import com.pinterest.ktlint.rule.engine.core.api.AutocorrectDecision
import com.pinterest.ktlint.rule.engine.core.api.COMMENT_TOKENS
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ANNOTATED_EXPRESSION
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ANNOTATION
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ANNOTATION_ENTRY
Expand Down Expand Up @@ -124,7 +125,6 @@ import io.github.oshai.kotlinlogging.KotlinLogging
import org.ec4j.core.model.PropertyType
import org.ec4j.core.model.PropertyType.PropertyValueParser
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiComment
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.psi.psiUtil.leaves
Expand Down Expand Up @@ -1191,7 +1191,7 @@ public class IndentationRule :
}

nextLeaf
?.parent(strict = false) { it.psi is PsiComment }
?.parent(strict = false) { COMMENT_TOKENS.contains(it.elementType) }
?.let { comment ->
if (text.endsWith("\n")) {
processedButNoIndentationChangedNeeded()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.rule.engine.core.api.AutocorrectDecision
import com.pinterest.ktlint.rule.engine.core.api.ElementType
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ABSTRACT_KEYWORD
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ACTUAL_KEYWORD
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ANNOTATION_ENTRY
Expand Down Expand Up @@ -34,16 +35,14 @@ import com.pinterest.ktlint.rule.engine.core.api.ifAutocorrectAllowed
import com.pinterest.ktlint.ruleset.standard.StandardRule
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet
import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.KtDeclarationModifierList

@SinceKtlint("0.7", STABLE)
public class ModifierOrderRule : StandardRule("modifier-order") {
override fun beforeVisitChildNodes(
node: ASTNode,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> AutocorrectDecision,
) {
if (node.psi is KtDeclarationModifierList) {
if (node.elementType == ElementType.MODIFIER_LIST) {
val modifierArr = node.getChildren(tokenSet)
val sorted = modifierArr.copyOf().apply { sortWith(compareBy { ORDERED_MODIFIERS.indexOf(it.elementType) }) }
if (!modifierArr.contentEquals(sorted)) {
Expand All @@ -69,7 +68,7 @@ public class ModifierOrderRule : StandardRule("modifier-order") {
}

private fun squashAnnotations(sorted: Array<ASTNode>): List<String> {
val nonAnnotationModifiers = sorted.filter { it.psi !is KtAnnotationEntry }
val nonAnnotationModifiers = sorted.filter { it.elementType != ElementType.ANNOTATION_ENTRY }
return if (nonAnnotationModifiers.size != sorted.size) {
listOf("@Annotation...") + nonAnnotationModifiers.map { it.text }
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.rule.engine.core.api.AutocorrectDecision
import com.pinterest.ktlint.rule.engine.core.api.ElementType
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS_BODY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ENUM_ENTRY
Expand All @@ -26,13 +27,10 @@ import com.pinterest.ktlint.ruleset.standard.StandardRule
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.kdoc.psi.api.KDoc
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.KtDoWhileExpression
import org.jetbrains.kotlin.psi.KtIfExpression
import org.jetbrains.kotlin.psi.KtLoopExpression
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType

@SinceKtlint("0.1", STABLE)
public class NoSemicolonsRule :
Expand Down Expand Up @@ -84,11 +82,10 @@ public class NoSemicolonsRule :

this is PsiWhiteSpace -> {
nextLeaf {
val psi = it.psi
it !is PsiWhiteSpace &&
it !is PsiComment &&
psi.getStrictParentOfType<KDoc>() == null &&
psi.getStrictParentOfType<KtAnnotationEntry>() == null
it.parent(ElementType.KDOC) == null &&
it.parent(ElementType.ANNOTATION_ENTRY) == null
}.let { nextLeaf ->
nextLeaf == null ||
// \s+ and then eof
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.rule.engine.core.api.AutocorrectDecision
import com.pinterest.ktlint.rule.engine.core.api.ElementType
import com.pinterest.ktlint.rule.engine.core.api.ElementType.EOL_COMMENT
import com.pinterest.ktlint.rule.engine.core.api.ElementType.WHITE_SPACE
import com.pinterest.ktlint.rule.engine.core.api.RuleId
Expand All @@ -13,7 +14,6 @@ import com.pinterest.ktlint.rule.engine.core.api.parent
import com.pinterest.ktlint.ruleset.standard.StandardRule
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.kdoc.psi.api.KDoc

@SinceKtlint("0.1", STABLE)
public class NoTrailingSpacesRule : StandardRule("no-trailing-spaces") {
Expand Down Expand Up @@ -74,7 +74,7 @@ public class NoTrailingSpacesRule : StandardRule("no-trailing-spaces") {
}
}

private fun ASTNode.isPartOfKDoc() = parent(strict = false) { it.psi is KDoc } != null
private fun ASTNode.isPartOfKDoc() = parent(strict = false) { it.elementType == ElementType.KDOC } != null

private fun ASTNode.hasTrailingSpacesBeforeNewline() = text.contains(SPACE_OR_TAB_BEFORE_NEWLINE_REGEX)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.rule.engine.core.api.AutocorrectDecision
import com.pinterest.ktlint.rule.engine.core.api.ElementType
import com.pinterest.ktlint.rule.engine.core.api.ElementType.MODIFIER_LIST
import com.pinterest.ktlint.rule.engine.core.api.RuleId
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.EXPERIMENTAL
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.STABLE
import com.pinterest.ktlint.rule.engine.core.api.children
import com.pinterest.ktlint.rule.engine.core.api.getPrevSiblingIgnoringWhitespaceAndComments
import com.pinterest.ktlint.rule.engine.core.api.ifAutocorrectAllowed
import com.pinterest.ktlint.rule.engine.core.api.indent
import com.pinterest.ktlint.rule.engine.core.api.isPartOfComment
Expand All @@ -16,11 +18,9 @@ import com.pinterest.ktlint.rule.engine.core.api.prevCodeLeaf
import com.pinterest.ktlint.rule.engine.core.api.upsertWhitespaceBeforeMe
import com.pinterest.ktlint.ruleset.standard.StandardRule
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespaceAndComments
import org.jetbrains.kotlin.psi.psiUtil.leaves
import org.jetbrains.kotlin.psi.stubs.elements.KtTokenSets

/**
* @see https://youtrack.jetbrains.com/issue/KT-35106
Expand All @@ -32,7 +32,7 @@ public class SpacingBetweenDeclarationsWithAnnotationsRule : StandardRule("spaci
node: ASTNode,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> AutocorrectDecision,
) {
if (node.psi is KtDeclaration && node.isAnnotated()) {
if (KtTokenSets.DECLARATION_TYPES.contains(node.elementType) && node.isAnnotated()) {
visitDeclaration(node, emit)
}
}
Expand All @@ -42,9 +42,8 @@ public class SpacingBetweenDeclarationsWithAnnotationsRule : StandardRule("spaci
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> AutocorrectDecision,
) {
node
.psi
?.getPrevSiblingIgnoringWhitespaceAndComments(withItself = false)
?.takeIf { it is KtDeclaration }
?.getPrevSiblingIgnoringWhitespaceAndComments()
?.takeIf { KtTokenSets.DECLARATION_TYPES.contains(it.elementType) }
?.takeIf { prevDeclaration -> hasNoBlankLineBetweenDeclarations(node, prevDeclaration) }
?.let {
val prevLeaf = node.prevCodeLeaf()?.nextLeaf { it.isWhiteSpace() }!!
Expand All @@ -61,16 +60,16 @@ public class SpacingBetweenDeclarationsWithAnnotationsRule : StandardRule("spaci
private fun ASTNode.isAnnotated(): Boolean =
findChildByType(MODIFIER_LIST)
?.children()
?.any { it.psi is KtAnnotationEntry }
?.any { it.elementType == ElementType.ANNOTATION_ENTRY }
?: false

private fun hasNoBlankLineBetweenDeclarations(
node: ASTNode,
prevDeclaration: PsiElement,
prevDeclaration: ASTNode,
) = node
.leaves(false)
.takeWhile { it.isWhiteSpace() || it.isPartOfComment() }
.takeWhile { it.psi != prevDeclaration }
.takeWhile { it != prevDeclaration }
.none { it.isBlankLine() }

private fun ASTNode.isBlankLine() = isWhiteSpace() && text.count { it == '\n' } > 1
Expand Down
Loading

0 comments on commit e962b64

Please sign in to comment.