Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indentation error on malformed formatter closing tag #2695

Closed
paul-dingemans opened this issue Jun 12, 2024 · 0 comments · Fixed by #2719
Closed

Indentation error on malformed formatter closing tag #2695

paul-dingemans opened this issue Jun 12, 2024 · 0 comments · Fixed by #2719
Milestone

Comments

@paul-dingemans
Copy link
Collaborator

paul-dingemans commented Jun 12, 2024

From nbadal/ktlint-intellij-plugin#535

Given .editorconfig:

root = true

[*.{kt,kts}]
ij_formatter_tags_enabled = true

This problem does not seem to be caused by the ktlint-intellij-plugin but in ktlint itself. I can reproduce the exact same stacktrace, by malforming the "@Formatter:on" tag:

$ ktlint-1.3.0 --stdin
08:03:08.208 [main] INFO com.pinterest.ktlint.cli.internal.KtlintCommandLine -- Enable default patterns [**/*.kt, **/*.kts]
fun foo123() {
    // @formatter:off
    listOf(
        "x" to 1, "x" to 2, "x" to 3,
        "x" to 1, "x" to 2, "x" to 3,
        "y" to 1, "y" to 2, "y" to 3,
        "z" to 1, "z" to 2, "z" to 3,
    )
    // The "@formatted:on" tag below is malformed on purpose.
    // It will cause an exception when indenting
    // @formstter:on
}

Exception in thread "main" java.lang.IllegalArgumentException: Stack should be empty:
        IndentContext(fromASTNode=Element(kotlin.FILE), toASTNode=PsiWhiteSpace, nodeIndent=, firstChildIndent=, childIndent=, lastChildIndent=, activated=true)
        IndentContext(fromASTNode=BLOCK, toASTNode=PsiElement(RBRACE), nodeIndent=, firstChildIndent=    , childIndent=    , lastChildIndent=    , activated=false)
        IndentContext(fromASTNode=PsiElement(LBRACE), toASTNode=PsiElement(RBRACE), nodeIndent=, firstChildIndent=, childIndent=    , lastChildIndent=, activated=true)
        at com.pinterest.ktlint.ruleset.standard.rules.IndentationRule.afterLastNode(IndentationRule.kt:1066)
        at com.pinterest.ktlint.rule.engine.internal.RuleExecutionContext.executeRule(RuleExecutionContext.kt:65)
        at com.pinterest.ktlint.rule.engine.internal.CodeFormatter.executeRule(CodeFormatter.kt:116)
        at com.pinterest.ktlint.rule.engine.internal.CodeFormatter.format(CodeFormatter.kt:87)
        at com.pinterest.ktlint.rule.engine.internal.CodeFormatter.format(CodeFormatter.kt:56)
        at com.pinterest.ktlint.rule.engine.internal.CodeFormatter.format(CodeFormatter.kt:27)
        at com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine.lint(KtLintRuleEngine.kt:91)
        at com.pinterest.ktlint.cli.internal.KtlintCommandLine.lint(KtlintCommandLine.kt:558)
        at com.pinterest.ktlint.cli.internal.KtlintCommandLine.process(KtlintCommandLine.kt:466)
        at com.pinterest.ktlint.cli.internal.KtlintCommandLine.lintStdin(KtlintCommandLine.kt:425)
        at com.pinterest.ktlint.cli.internal.KtlintCommandLine.lintOrFormat(KtlintCommandLine.kt:296)
        at com.pinterest.ktlint.cli.internal.KtlintCommandLine.run(KtlintCommandLine.kt:246)
        at com.github.ajalt.clikt.parsers.Parser.finalizeAndRun(Parser.kt:348)
        at com.github.ajalt.clikt.parsers.Parser.parse(Parser.kt:218)
        at com.github.ajalt.clikt.parsers.Parser.parse(Parser.kt:42)
        at com.github.ajalt.clikt.core.CliktCommand.parse(CliktCommand.kt:457)
        at com.github.ajalt.clikt.core.CliktCommand.parse$default(CliktCommand.kt:454)
        at com.github.ajalt.clikt.core.CliktCommand.main(CliktCommand.kt:474)
        at com.github.ajalt.clikt.core.CliktCommand.main(CliktCommand.kt:481)
        at com.pinterest.ktlint.Main.main(Main.kt:20)
@paul-dingemans paul-dingemans added this to the 1.3.1 milestone Jun 12, 2024
paul-dingemans added a commit that referenced this issue Jun 25, 2024
If the 'formatter:on' tag is not specified in the same scope as the 'formatter:off' tag, then the IndentationRule might throw an exception as the IndentationContextStack is not properly cleaned.

 Also, cleaned up of `CommentSuppressionHint.EOL` type as it was not used since replacing the EOL suppression hints in `1.0` release.

Closes #2695
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant