diff --git a/ktlint-rule-engine-core/api/ktlint-rule-engine-core.api b/ktlint-rule-engine-core/api/ktlint-rule-engine-core.api index aafbd92d76..5b1e3cef45 100644 --- a/ktlint-rule-engine-core/api/ktlint-rule-engine-core.api +++ b/ktlint-rule-engine-core/api/ktlint-rule-engine-core.api @@ -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; diff --git a/ktlint-rule-engine-core/src/main/kotlin/com/pinterest/ktlint/rule/engine/core/api/ASTNodeExtension.kt b/ktlint-rule-engine-core/src/main/kotlin/com/pinterest/ktlint/rule/engine/core/api/ASTNodeExtension.kt index d63ba26cfc..4cc8ee81c4 100644 --- a/ktlint-rule-engine-core/src/main/kotlin/com/pinterest/ktlint/rule/engine/core/api/ASTNodeExtension.kt +++ b/ktlint-rule-engine-core/src/main/kotlin/com/pinterest/ktlint/rule/engine/core/api/ASTNodeExtension.kt @@ -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 @@ -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() diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/AnnotationSpacingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/AnnotationSpacingRule.kt index ae1709a2b2..b7abfc0d6e 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/AnnotationSpacingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/AnnotationSpacingRule.kt @@ -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") @@ -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 diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRule.kt index 29a28e18cb..8b02750821 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRule.kt @@ -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 @@ -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")) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRule.kt index 381f96ad3d..0d294a2bb2 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRule.kt @@ -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 @@ -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 @@ -651,7 +651,7 @@ public class ClassSignatureRule : private fun List.collectLeavesRecursively(): List = flatMap { it.collectLeavesRecursively() } private fun ASTNode.collectLeavesRecursively(): List = - if (psi is LeafElement) { + if (isLeaf()) { listOf(this) } else { children() diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/EnumWrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/EnumWrappingRule.kt index 63d9ba4080..bb18315407 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/EnumWrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/EnumWrappingRule.kt @@ -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 @@ -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 @@ -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 /** * @@ -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) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRule.kt index 20cda849ac..cb3e21e25f 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRule.kt @@ -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 @@ -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 @@ -700,7 +700,7 @@ public class FunctionSignatureRule : private fun List.collectLeavesRecursively(): List = flatMap { it.collectLeavesRecursively() } private fun ASTNode.collectLeavesRecursively(): List = - if (psi is LeafElement) { + if (isLeaf()) { listOf(this) } else { children() diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRule.kt index 10495e83a4..d547e09723 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRule.kt @@ -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 @@ -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 @@ -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() diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ModifierOrderRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ModifierOrderRule.kt index 021243e44b..2f10b864f3 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ModifierOrderRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ModifierOrderRule.kt @@ -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 @@ -34,8 +35,6 @@ 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") { @@ -43,7 +42,7 @@ public class ModifierOrderRule : StandardRule("modifier-order") { 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)) { @@ -69,7 +68,7 @@ public class ModifierOrderRule : StandardRule("modifier-order") { } private fun squashAnnotations(sorted: Array): List { - 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 { diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/NoSemicolonsRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/NoSemicolonsRule.kt index d4ccd8b115..d571ab863b 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/NoSemicolonsRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/NoSemicolonsRule.kt @@ -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 @@ -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 : @@ -84,11 +82,10 @@ public class NoSemicolonsRule : this is PsiWhiteSpace -> { nextLeaf { - val psi = it.psi it !is PsiWhiteSpace && it !is PsiComment && - psi.getStrictParentOfType() == null && - psi.getStrictParentOfType() == null + it.parent(ElementType.KDOC) == null && + it.parent(ElementType.ANNOTATION_ENTRY) == null }.let { nextLeaf -> nextLeaf == null || // \s+ and then eof diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/NoTrailingSpacesRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/NoTrailingSpacesRule.kt index 3d78892c69..f7df20a84b 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/NoTrailingSpacesRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/NoTrailingSpacesRule.kt @@ -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 @@ -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") { @@ -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) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingBetweenDeclarationsWithAnnotationsRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingBetweenDeclarationsWithAnnotationsRule.kt index bb0e59c8c7..f9531fcd8d 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingBetweenDeclarationsWithAnnotationsRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingBetweenDeclarationsWithAnnotationsRule.kt @@ -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 @@ -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 @@ -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) } } @@ -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() }!! @@ -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 diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRule.kt index 1ba6e741b1..63f71446eb 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRule.kt @@ -22,6 +22,7 @@ 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.editorconfig.EditorConfig import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EditorConfigProperty +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.isCodeLeaf @@ -41,10 +42,8 @@ import org.jetbrains.kotlin.com.intellij.psi.PsiElement 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.com.intellij.psi.tree.TokenSet -import org.jetbrains.kotlin.psi.KtClass import org.jetbrains.kotlin.psi.KtCollectionLiteralExpression import org.jetbrains.kotlin.psi.KtDestructuringDeclaration -import org.jetbrains.kotlin.psi.KtEnumEntry import org.jetbrains.kotlin.psi.KtFunctionLiteral import org.jetbrains.kotlin.psi.KtParameterList import org.jetbrains.kotlin.psi.KtValueArgumentList @@ -191,11 +190,10 @@ public class TrailingCommaOnDeclarationSiteRule : node: ASTNode, emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> AutocorrectDecision, ) { - val psi = node.psi - require(psi is KtClass) + require(node.elementType == ElementType.CLASS) node - .takeIf { psi.isEnum() } + .takeIf { node.hasModifier(ElementType.ENUM_KEYWORD) } ?.findChildByType(ElementType.CLASS_BODY) ?.takeUnless { it.noEnumEntries() } ?.let { classBody -> @@ -223,12 +221,12 @@ public class TrailingCommaOnDeclarationSiteRule : } } - private fun ASTNode.noEnumEntries() = children().none { it.psi is KtEnumEntry } + private fun ASTNode.noEnumEntries() = children().none { it.elementType == ElementType.ENUM_ENTRY } private fun ASTNode.lastTwoEnumEntriesAreOnSameLine(): Boolean { val lastTwoEnumEntries = children() - .filter { it.psi is KtEnumEntry } + .filter { it.elementType == ElementType.ENUM_ENTRY } .toList() .takeLast(2) @@ -243,7 +241,7 @@ public class TrailingCommaOnDeclarationSiteRule : */ private fun ASTNode.findNodeAfterLastEnumEntry() = children() - .lastOrNull { it.psi is KtEnumEntry } + .lastOrNull { it.elementType == ElementType.ENUM_ENTRY } ?.children() ?.singleOrNull { it.elementType == SEMICOLON } ?: lastChildNode diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRule.kt index c4551d4b35..c69e8ae877 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRule.kt @@ -3,6 +3,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.AutocorrectDecision.NO_AUTOCORRECT +import com.pinterest.ktlint.rule.engine.core.api.COMMENT_TOKENS import com.pinterest.ktlint.rule.engine.core.api.ElementType import com.pinterest.ktlint.rule.engine.core.api.ElementType.ANNOTATION import com.pinterest.ktlint.rule.engine.core.api.ElementType.ARROW @@ -78,7 +79,6 @@ import com.pinterest.ktlint.rule.engine.core.api.upsertWhitespaceBeforeMe import com.pinterest.ktlint.ruleset.standard.StandardRule import io.github.oshai.kotlinlogging.KotlinLogging 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.com.intellij.psi.tree.IElementType import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet @@ -386,7 +386,7 @@ public class WrappingRule : // value( // ), // a comment // c, d - nextSibling.treeNext?.treeNext?.psi !is PsiComment + nextSibling.treeNext?.treeNext?.let { !COMMENT_TOKENS.contains(it.elementType) } != false ) { requireNewlineAfterLeaf(nextSibling, emit) }