Skip to content

Commit

Permalink
Add missing space between fun keyword and identifier when latter is…
Browse files Browse the repository at this point in the history
… wrapped between backticks (#2890)

Closes #2879
  • Loading branch information
paul-dingemans authored Dec 3, 2024
1 parent fcccb9f commit e098f10
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.pinterest.ktlint.rule.engine.core.api.nextLeaf
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.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl

/**
* Lints and formats the spacing after the fun keyword
Expand All @@ -26,14 +27,30 @@ public class FunKeywordSpacingRule : StandardRule("fun-keyword-spacing") {
node
.takeIf { it.elementType == FUN_KEYWORD }
?.nextLeaf(includeEmpty = true)
?.takeIf { it.elementType == ElementType.WHITE_SPACE && it.text != " " }
?.let { whiteSpaceAfterFunKeyword ->
emit(
whiteSpaceAfterFunKeyword.startOffset,
"Single space expected after the fun keyword",
true,
).ifAutocorrectAllowed {
(whiteSpaceAfterFunKeyword as LeafPsiElement).rawReplaceWithText(" ")
?.let { leafAfterFunKeyword ->
when {
leafAfterFunKeyword.elementType == ElementType.WHITE_SPACE && leafAfterFunKeyword.text != " " -> {
emit(
leafAfterFunKeyword.startOffset,
"Single space expected after the fun keyword",
true,
).ifAutocorrectAllowed {
(leafAfterFunKeyword as LeafPsiElement).rawReplaceWithText(" ")
}
}

leafAfterFunKeyword.elementType == ElementType.IDENTIFIER -> {
// Identifier can only be adjacent to fun keyword in case the identifier is wrapped between backticks:
// fun`foo`() {}
emit(leafAfterFunKeyword.startOffset, "Space expected between the fun keyword and backtick", true)
.ifAutocorrectAllowed {
leafAfterFunKeyword.treeParent.addChild(PsiWhiteSpaceImpl(" "), leafAfterFunKeyword)
}
}

else -> {
Unit
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,19 @@ class FunKeywordSpacingRuleTest {
.hasLintViolation(1, 4, "Single space expected after the fun keyword")
.isFormattedAs(formattedCode)
}

@Test
fun `Issue 2879 - Given a function with name between backticks then the fun keyword and name should be separated by a space`() {
val code =
"""
fun`foo or bar`() = "foo"
""".trimIndent()
val formattedCode =
"""
fun `foo or bar`() = "foo"
""".trimIndent()
funKeywordSpacingRuleAssertThat(code)
.hasLintViolation(1, 4, "Space expected between the fun keyword and backtick")
.isFormattedAs(formattedCode)
}
}

0 comments on commit e098f10

Please sign in to comment.