Share
Embed
Lint that Kotlin
Code formatting is often not taken seriously and sometimes even omitted, but it's actually something that brings enjoyment to our eyes while doing code reviews. In this talk, we will dig into ktlint (Kotlin formatting linter), its internals (UAST, Psi), how to create a custom rule and how to have the formatting automated.
Roman Zavarnitsyn
May 08, 2019
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
67 slides
-
Kotlin code formatting fun someMethod(): Unit { val someVar =
resolveVar() if (someVar>=0) { // valid println("Var is valid and is equal to ${someVar}") } } -
Kotlin code formatting fun someMethod(): Unit { val someVar =
resolveVar() if (someVar>=0) { // valid println("Var is valid and is equal to ${someVar}") } } -
Kotlin code formatting fun someMethod(): Unit { val someVar =
resolveVar() if (someVar>=0) { // valid println("Var is valid and is equal to ${someVar}") } } -
Kotlin code formatting fun someMethod(): Unit { val someVar =
resolveVar() if (someVar>=0) { // valid println("Var is valid and is equal to ${someVar}") } } -
Kotlin code formatting fun someMethod(): Unit { val someVar =
resolveVar() if (someVar>=0) { // valid println("Var is valid and is equal to ${someVar}") } } -
Kotlin code formatting fun someMethod(): Unit { val someVar =
resolveVar() if (someVar>=0) { // valid println("Var is valid and is equal to ${someVar}") } // empty line } -
Kotlin code formatting fun someMethod(): Unit { val someVar =
resolveVar() if (someVar>=0) { // valid println("Var is valid and is equal to ${someVar}") } } -
ktlint • Reflects official Kotlin style guide (+ Android) •
Built-in formatter • Customizable output (plain, json, checkstyle, html) • Customizable ruleset • Integration with Maven, Gradle, IntelliJ IDEA -
$ sh ktlint SomeFile.kt fun someMethod(): Unit { val someVar
= resolveVar() if (someVar>=0) { // valid println("Var is valid and is equal to ${someVar}") } } SomeFile.kt:1:19: Unnecessary "Unit" return type SomeFile.kt:2:1: Unexpected newline before "{" SomeFile.kt:4:16: Missing spacing around ">=" SomeFile.kt:5:20: Redundant curly braces SomeFile.kt:7:1: Unexpected blank line(s) before "}" -
$ sh ktlint SomeFile.kt --format fun someMethod(): Unit { {
val someVar = resolveVar() if (someVar>=0) { // valid println("Var is valid and is equal to ${someVar}") } } } } -
$ sh ktlint SomeFile.kt --format fun someMethod() { { val
someVar = resolveVar() if (someVar >= 0) { // valid println("Var is valid and is equal to $someVar") } } } } -
import resolver.resolveVar fun someMethod() { { val someVar = resolveVar()
if (someVar >= 0) { // valid println("Var is valid and is equal to $someVar") } } } } File -
import resolver.resolveVar fun someMethod() { val someVar = resolveVar() if
(someVar >= 0) { // valid println("Var is valid and is equal to $someVar") } } File • Imports -
import resolver.resolveVar fun someMethod() { { val someVar = resolveVar()
if (someVar >= 0) { // valid println("Var is valid and is equal to $someVar") } } } } File • Imports • Functions -
import resolver.resolveVar fun someMethod() { val someVar = resolveVar() if
(someVar >= 0) { // valid println("Var is valid and is equal to $someVar") } } File • Imports • Functions • Properties -
import resolver.resolveVar fun someMethod() { val someVar = resolveVar() if
(someVar >= 0) { // valid println("Var is valid and is equal to $someVar") } } } } File • Imports • Functions • Properties • Expressions -
import resolver.resolveVar fun someMethod() { val someVar = resolveVar() if
(someVar >= 0) { // valid println("Var is valid and is equal to $someVar") } } } File • Imports • Functions • Properties • Expressions • Operations -
import resolver.resolveVar fun someMethod() { val someVar = resolveVar() if
(someVar >= 0) { // valid println("Var is valid and is equal to $someVar") }} }} File • Imports • Functions • Properties • Expressions • Operations Psi Psi Psi Psi Psi Psi -
import resolver.resolveVar fun someMethod() {{ val someVar = resolveVar() if
(someVar >= 0) {{// valid println("Var is valid and is equal to $someVar") }} }} PsiFile PsiImports PsiFunctions PsiProperties PsiExpressions PsiOperations -
import resolver.resolveVar fun someMethod() {{ val someVar = resolveVar() if
(someVar >= 0) {{// valid println("Var is valid and is equal to $someVar") }} }} PsiFile : PsiElement PsiImports : PsiElement PsiFunctions : PsiElement PsiProperties : PsiElement PsiExpressions : PsiElement PsiOperations : PsiElement -
import resolver.resolveVar fun someMethod() {{ val someVar = resolveVar() if
(someVar >= 0) {{// valid println("Var is valid and is equal to $someVar") }} }} PsiFile : PsiElement PsiImports : PsiElement PsiFunctions : PsiElement PsiProperties : PsiElement PsiExpressions : PsiElement PsiOperations : PsiElement PsiWhiteSpace : PsiElement -
$ sh ktlint SomeFile.kt --print-ast ~.psi.KtFile (FILE) ~.psi.KtPackageDirective (PACKAGE_DIRECTIVE) ""
~.psi.KtImportList (IMPORT_LIST) "" ~.psi.KtNamedFunction (FUN) ~.c.i.p.impl.source.tree.LeafPsiElement (FUN_KEYWORD) "fun" ~.c.i.p.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) " " ~.c.i.p.impl.source.tree.LeafPsiElement (IDENTIFIER) "someMethod" ~.psi.KtParameterList (VALUE_PARAMETER_LIST) ~.c.i.p.impl.source.tree.LeafPsiElement (LPAR) "(" ~.c.i.p.impl.source.tree.LeafPsiElement (RPAR) ")" ~.c.i.p.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) " " ~.psi.KtBlockExpression (BLOCK) ~.c.i.p.impl.source.tree.LeafPsiElement (LBRACE) "{" ~.c.i.p.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) "\n " ~.psi.KtProperty (PROPERTY) ~.c.i.p.impl.source.tree.LeafPsiElement (VAL_KEYWORD) "val" ~.c.i.p.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) " " ~.c.i.p.impl.source.tree.LeafPsiElement (IDENTIFIER) "someVar" fun someMethod() { val someVar = resolveVar() if (someVar >= 0) { // valid println("Var is valid and is equal to $someVar") } } -
$ sh ktlint SomeFile.kt --print-ast ~.psi.KtFile (FILE) ~.psi.KtPackageDirective (PACKAGE_DIRECTIVE) ""
~.psi.KtImportList (IMPORT_LIST) "" ~.psi.KtNamedFunction (FUN) ~.c.i.p.impl.source.tree.LeafPsiElement (FUN_KEYWORD) "fun" ~.c.i.p.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) " " ~.c.i.p.impl.source.tree.LeafPsiElement (IDENTIFIER) "someMethod" ~.psi.KtParameterList (VALUE_PARAMETER_LIST) ~.c.i.p.impl.source.tree.LeafPsiElement (LPAR) "(" ~.c.i.p.impl.source.tree.LeafPsiElement (RPAR) ")" ~.c.i.p.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) " " ~.psi.KtBlockExpression (BLOCK) ~.c.i.p.impl.source.tree.LeafPsiElement (LBRACE) "{" ~.c.i.p.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) "\n " ~.psi.KtProperty (PROPERTY) ~.c.i.p.impl.source.tree.LeafPsiElement (VAL_KEYWORD) "val" ~.c.i.p.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) " " ~.c.i.p.impl.source.tree.LeafPsiElement (IDENTIFIER) "someVar" fun someMethod() { val someVar = resolveVar() if (someVar >= 0) { // valid println("Var is valid and is equal to $someVar") } } -
$ sh ktlint SomeFile.kt --print-ast ~.psi.KtFile (FILE) ~.psi.KtPackageDirective (PACKAGE_DIRECTIVE) ""
~.psi.KtImportList (IMPORT_LIST) "" ~.psi.KtNamedFunction (FUN) ~.c.i.p.impl.source.tree.LeafPsiElement (FUN_KEYWORD) "fun" ~.c.i.p.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) " " ~.c.i.p.impl.source.tree.LeafPsiElement (IDENTIFIER) "someMethod" ~.psi.KtParameterList (VALUE_PARAMETER_LIST) ~.c.i.p.impl.source.tree.LeafPsiElement (LPAR) "(" ~.c.i.p.impl.source.tree.LeafPsiElement (RPAR) ")" ~.c.i.p.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) " " ~.psi.KtBlockExpression (BLOCK) ~.c.i.p.impl.source.tree.LeafPsiElement (LBRACE) "{" ~.c.i.p.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) "\n " ~.psi.KtProperty (PROPERTY) ~.c.i.p.impl.source.tree.LeafPsiElement (VAL_KEYWORD) "val" ~.c.i.p.impl.source.tree.PsiWhiteSpaceImpl (WHITE_SPACE) " " ~.c.i.p.impl.source.tree.LeafPsiElement (IDENTIFIER) "someVar" fun someMethod() { val someVar = resolveVar() if (someVar >= 0) { // valid println("Var is valid and is equal to $someVar") } } -
import resolver.resolveVar fun someMethod() { val someVar = resolveVar() if
(someVar >= 0) { // valid println("Var is valid and is equal to $someVar") } } val file: KtFile -
import resolver.resolveVar fun someMethod() { val someVar = resolveVar() if
(someVar >= 0) { // valid println("Var is valid and is equal to $someVar") } } val file: KtFile val method = file.findChildByTypeOrClass(KtStubElementTypes.FUNCTION_TYPE, KtFunctionType::class.java) -
import resolver.resolveVar fun someMethod() { val someVar = resolveVar() if
(someVar >= 0) { // valid println("Var is valid and is equal to $someVar") } } val file: KtFile val method = file.findChildByTypeOrClass(KtStubElementTypes.FUNCTION_TYPE, KtFunctionType::class.java) val operator = method.findDescendantOfType<LeafPsiElement> { it.elementType == KtTokens.GTEQ } -
Range operator for (i in 1..100) when (score) { in
1..3 -> "bad" in 4..8 -> "good" in 9..10 -> "excellent" } } -
Range operator when (score) { in 1 .. 3 ->
"bad" in 4 .. 8 -> "good" in 9 .. 10 -> "excellent" } } for (i in 1 .. 100) -
Writing a custom rule class SpacingAroundRangeOperatorRule : Rule("range-spacing") { override
fun visit( node: ASTNode, autoCorrect: Boolean, emit: ( offset: Int, errorMessage: String, canBeAutoCorrected: Boolean ) -> Unit ) { // Implementation... } } for (i in 1 .. 100) -
Writing a custom rule class SpacingAroundRangeOperatorRule : Rule("range-spacing") { override
fun visit( node: ASTNode, autoCorrect: Boolean, emit: ( offset: Int, errorMessage: String, canBeAutoCorrected: Boolean ) -> Unit ) { // Implementation... } } for (i in 1 .. 100) -
Writing a custom rule class SpacingAroundRangeOperatorRule : Rule("range-spacing") { override
fun visit( node: ASTNode, autoCorrect: Boolean, emit: ( offset: Int, errorMessage: String, canBeAutoCorrected: Boolean ) -> Unit ) { // Implementation... } } for (i in 1 .. 100) -
Writing a custom rule class SpacingAroundRangeOperatorRule : Rule("range-spacing") { override
fun visit( node: ASTNode, autoCorrect: Boolean, emit: ( offset: Int, errorMessage: String, canBeAutoCorrected: Boolean ) -> Unit ) { // Implementation... } } } } for (i in 1 .. 100) -
Writing a custom rule class SpacingAroundRangeOperatorRule : Rule("range-spacing") { override
fun visit(...) { ... } } } } for (i in 1 .. 100) -
Writing a custom rule class SpacingAroundRangeOperatorRule : Rule("range-spacing") { override
fun visit(...) { if (node.elementType == RANGE) { val prevLeaf = node.prevLeaf() val nextLeaf = node.nextLeaf() } } } } } for (i in 1 .. 100) -
Writing a custom rule class SpacingAroundRangeOperatorRule : Rule("range-spacing") { override
fun visit(...) { if (node.elementType == RANGE) { val prevLeaf = node.prevLeaf() val nextLeaf = node.nextLeaf() } } } KtSingleValueToken RANGE = new KtSingleValueToken("RANGE", ".."); for (i in 1 .. 100) -
Writing a custom rule class SpacingAroundRangeOperatorRule : Rule("range-spacing") { override
fun visit(...) { if (node.elementType == RANGE) { val prevLeaf = node.prevLeaf() val nextLeaf = node.nextLeaf() } } } for (i in 1 .. 100) -
Writing a custom rule class SpacingAroundRangeOperatorRule : Rule("range-spacing") {{ override
fun visit(...) {{ if (node.elementType == RANGE) {{ val prevLeaf = node.prevLeaf() val nextLeaf = node.nextLeaf() }} }} }} for (i in 1 .. 100) -
Writing a custom rule class SpacingAroundRangeOperatorRule : Rule("range-spacing") {{ override
fun visit(...) {{ if (node.elementType == RANGE) {{ val prevLeaf = node.prevLeaf() val nextLeaf = node.nextLeaf() when {{ prevLeaf is PsiWhiteSpace && nextLeaf is PsiWhiteSpace -> {{ emit(node.startOffset, "Unexpected spacing around \"..\"") if (autoCorrect) { prevLeaf.treeParent.removeChild(prevLeaf) nextLeaf.treeParent.removeChild(nextLeaf) }} }} }} }} }}} }} for (i in 1 .. 100) -
Writing a custom rule class SpacingAroundRangeOperatorRule : Rule("range-spacing") { override
fun visit(...) { if (node.elementType == RANGE) { val prevLeaf = node.prevLeaf() val nextLeaf = node.nextLeaf() when { prevLeaf is PsiWhiteSpace && nextLeaf is PsiWhiteSpace -> { emit(node.startOffset, "Unexpected spacing around \"..\"") if (autoCorrect) { prevLeaf.treeParent.removeChild(prevLeaf) nextLeaf.treeParent.removeChild(nextLeaf) } } } } } } for (i in 1 .. 100) -
Writing a custom rule class SpacingAroundRangeOperatorRule : Rule("range-spacing") { override
fun visit(...) { if (node.elementType == RANGE) { val prevLeaf = node.prevLeaf() val nextLeaf = node.nextLeaf() when { prevLeaf is PsiWhiteSpace && nextLeaf is PsiWhiteSpace -> { emit(node.startOffset, "Unexpected spacing around \"..\"") if (autoCorrect) { prevLeaf.treeParent.removeChild(prevLeaf) nextLeaf.treeParent.removeChild(nextLeaf) } } } } } } } for (i in 1 .. 100) -
Writing a custom rule class SpacingAroundRangeOperatorRule : Rule("range-spacing") {{ override
fun visit(...) {{ if (node.elementType == RANGE) {{ val prevLeaf = node.prevLeaf() val nextLeaf = node.nextLeaf() when {{ prevLeaf is PsiWhiteSpace && nextLeaf is PsiWhiteSpace -> {{{ emit(node.startOffset, "Unexpected spacing around \"..\"") if (autoCorrect) {{ prevLeaf.treeParent.removeChild(prevLeaf) nextLeaf.treeParent.removeChild(nextLeaf) }} }} }} }} }} }} for (i in 1 .. 100) -
Writing a custom rule class SpacingAroundRangeOperatorRule : Rule("range-spacing") {{ override
fun visit(...) {{ if (node.elementType == RANGE) {{ val prevLeaf = node.prevLeaf() val nextLeaf = node.nextLeaf() when {{ ... prevLeaf is PsiWhiteSpace -> {{ emit(node.startOffset, "Unexpected spacing before \"..\"") if (autoCorrect) {{ prevLeaf.treeParent.removeChild(prevLeaf) }} }} }} }} }} }} for (i in 1 .. 100) -
Writing a custom rule class SpacingAroundRangeOperatorRule : Rule("range-spacing") {{ override
fun visit(...) {{ if (node.elementType == RANGE) {{ val prevLeaf = node.prevLeaf() val nextLeaf = node.nextLeaf() when {{ ... nextLeaf is PsiWhiteSpace -> {{ emit(node.startOffset, "Unexpected spacing after \"..\"") if (autoCorrect) {{ nextLeaf.treeParent.removeChild(nextLeaf) }} }} }} }} }} }} for (i in 1 .. 100) -
Automation • Prevent everyone from pushing unformatted code • Moreover,
do the formatting automatically on each commit • If the unformatted code leaks to remote, CI bot leaves a comment on a pull request -
Git hooks • pre-commit, pre-push, post-commit, etc. • Are essentially
shell scripts • Live under version control -
Git hooks (pre-commit) #!/bin/bash echo "Running ktlint formatter..." files=$(git diff
--cached --name-only --diff-filter=ACM | grep '\.kt\?$') # Format only staged files using ktlint chmod a+x tools/ktlint/ktlint tools/ktlint/ktlint -F --android $files # Stage the changes after formatting git add $files echo "ktlint formatted the code successfully." exit 0 -
files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.kt\?$') --- a/sample/src/main/kotlin/com/sample/DeletedClass.java
+++ b/sample/src/main/kotlin/com/sample/SomeClass.java @@ -0,0 +1,4 @@ +class SomeClass { +} +++ b/sample/src/main/kotlin/com/sample/SomeFile.kt @@ -0,0 +1,8 @@ +fun someMethod(): Unit +{ + val someVar = resolveVar() + if (someVar>=0) { // valid + println("Var is valid and is equal to ${someVar}") + } + +} -
files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.kt\?$') sample/src/main/kotlin/com/sample/DeletedClass.java sample/src/main/kotlin/com/sample/SomeClass.java
sample/src/main/kotlin/com/sample/SomeFile.kt -
Git hooks (pre-commit) #!/bin/bash echo "Running ktlint formatter..." files=$(git diff
--cached --name-only --diff-filter=ACM | grep '\.kt\?$') # Format only staged files using ktlint chmod a+x tools/ktlint/ktlint tools/ktlint/ktlint -F --android $files # Stage the changes after formatting git add $files echo "ktlint formatted the code successfully." exit 0 -
Git hooks (pre-commit) #!/bin/bash echo "Running ktlint formatter..." files=$(git diff
--cached --name-only --diff-filter=ACM | grep '\.kt\?$') # Format only staged files using ktlint chmod a+x tools/ktlint/ktlint tools/ktlint/ktlint -F --android $files # Stage the changes after formatting git add $files echo "ktlint formatted the code successfully." exit 0 -
Git hooks (pre-commit) #!/bin/bash echo "Running ktlint formatter..." files=$(git diff
--cached --name-only --diff-filter=ACM | grep '\.kt\?$') # Format only staged files using ktlint chmod a+x tools/ktlint/ktlint tools/ktlint/ktlint -F --android $files # Stage the changes after formatting git add $files echo "ktlint formatted the code successfully." exit 0 -
CI setup (Jenkins + Bitbucket) • A job, which runs
over the opened pull requests • When there’s a new commit on PR - runs static code analysis (ktlint is part of them) • A Jenkins plugin that allows to leave comments on PRs