All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pinterest.ktlint.ruleset.standard.rules.SpacingAroundAngleBracketsRule.kt Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUN_KEYWORD
import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_ARGUMENT_LIST
import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_PARAMETER_LIST
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VAL_KEYWORD
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VAR_KEYWORD
import com.pinterest.ktlint.rule.engine.core.api.ElementType.WHITE_SPACE
import com.pinterest.ktlint.rule.engine.core.api.RuleId
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithoutNewline
import com.pinterest.ktlint.rule.engine.core.api.nextLeaf
import com.pinterest.ktlint.rule.engine.core.api.prevLeaf
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.LeafElement

public class SpacingAroundAngleBracketsRule : StandardRule("spacing-around-angle-brackets") {
    override fun beforeVisitChildNodes(
        node: ASTNode,
        autoCorrect: Boolean,
        emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
    ) {
        if (node.elementType != TYPE_PARAMETER_LIST && node.elementType != TYPE_ARGUMENT_LIST) {
            return
        }

        val openingBracket = node.firstChildNode
        if (openingBracket != null) {
            // Check for rogue spacing before an opening bracket, e.g. Map 
            val beforeLeftAngle = openingBracket.prevLeaf()
            if (beforeLeftAngle?.elementType == WHITE_SPACE) {
                // Ignore when the whitespace is preceded by certain keywords, e.g. fun  func(arg: T) {}
                if (!ELEMENT_TYPES_ALLOWING_PRECEDING_WHITESPACE.contains(beforeLeftAngle.prevLeaf()?.elementType)) {
                    emit(beforeLeftAngle.startOffset, "Unexpected spacing before \"<\"", true)
                    if (autoCorrect) {
                        beforeLeftAngle.treeParent.removeChild(beforeLeftAngle)
                    }
                }
            }

            // Check for rogue spacing after an opening bracket
            val afterLeftAngle = openingBracket.nextLeaf()
            if (afterLeftAngle?.elementType == WHITE_SPACE) {
                if (afterLeftAngle.isWhiteSpaceWithoutNewline()) {
                    // when spacing does not include any new lines, e.g. Map< String, Int>
                    emit(afterLeftAngle.startOffset, "Unexpected spacing after \"<\"", true)
                    if (autoCorrect) {
                        afterLeftAngle.treeParent.removeChild(afterLeftAngle)
                    }
                } else {
                    // when spacing contains at least one new line, e.g.
                    // SomeGenericType<[whitespace]
                    //
                    //      String, Int, String>
                    // gets converted to
                    // SomeGenericType<
                    //      String, Int, String>
                    val newLineWithIndent = afterLeftAngle.text.trimBeforeLastLine()
                    if (autoCorrect) {
                        (afterLeftAngle as LeafElement).rawReplaceWithText(newLineWithIndent)
                    }
                }
            }
        }

        val closingBracket = node.lastChildNode
        if (closingBracket != null) {
            val beforeRightAngle = closingBracket.prevLeaf()
            // Check for rogue spacing before a closing bracket
            if (beforeRightAngle?.elementType == WHITE_SPACE) {
                if (beforeRightAngle.isWhiteSpaceWithoutNewline()) {
                    // when spacing does not include any new lines, e.g. Map
                    emit(beforeRightAngle.startOffset, "Unexpected spacing before \">\"", true)
                    if (autoCorrect) {
                        beforeRightAngle.treeParent.removeChild(beforeRightAngle)
                    }
                } else {
                    // when spacing contains at least one new line, e.g.
                    // SomeGenericType
                    // gets converted to
                    // SomeGenericType
                    val newLineWithIndent = beforeRightAngle.text.trimBeforeLastLine()
                    if (autoCorrect) {
                        (beforeRightAngle as LeafElement).rawReplaceWithText(newLineWithIndent)
                    }
                }
            }
        }
    }

    private fun String.trimBeforeLastLine() = this.substring(this.lastIndexOf('\n'))

    private companion object {
        val ELEMENT_TYPES_ALLOWING_PRECEDING_WHITESPACE = setOf(VAL_KEYWORD, VAR_KEYWORD, FUN_KEYWORD)
    }
}

public val SPACING_AROUND_ANGLE_BRACKETS_RULE_ID: RuleId = SpacingAroundAngleBracketsRule().ruleId




© 2015 - 2024 Weber Informatics LLC | Privacy Policy