Skip to content

Commit

Permalink
bugfix/indentation-inside-string-templates(#758)
Browse files Browse the repository at this point in the history
### What's done:
  * Fixed bugs
  * Added tests
  • Loading branch information
aktsay6 committed Feb 11, 2021
1 parent 496c9a3 commit ed19ffd
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import com.pinterest.ktlint.core.ast.ElementType.FILE
import com.pinterest.ktlint.core.ast.ElementType.LBRACE
import com.pinterest.ktlint.core.ast.ElementType.LBRACKET
import com.pinterest.ktlint.core.ast.ElementType.LITERAL_STRING_TEMPLATE_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.LONG_TEMPLATE_ENTRY_END
import com.pinterest.ktlint.core.ast.ElementType.LONG_TEMPLATE_ENTRY_START
import com.pinterest.ktlint.core.ast.ElementType.LPAR
import com.pinterest.ktlint.core.ast.ElementType.RBRACE
import com.pinterest.ktlint.core.ast.ElementType.RBRACKET
Expand Down Expand Up @@ -325,15 +327,15 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule("indentation"

companion object {
const val INDENT_SIZE = 4
private val increasingTokens = listOf(LPAR, LBRACE, LBRACKET)
private val decreasingTokens = listOf(RPAR, RBRACE, RBRACKET)
private val increasingTokens = listOf(LPAR, LBRACE, LBRACKET, LONG_TEMPLATE_ENTRY_START)
private val decreasingTokens = listOf(RPAR, RBRACE, RBRACKET, LONG_TEMPLATE_ENTRY_END)
private val matchingTokens = increasingTokens.zip(decreasingTokens)
}
}

/**
* @property expected expected indentation as a number of spaces
* @property actual actial indentation as a number of spaces
* @property actual actual indentation as a number of spaces
*/
internal data class IndentationError(val expected: Int, val actual: Int)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.pinterest.ktlint.core.ast.ElementType.IS_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.KDOC_END
import com.pinterest.ktlint.core.ast.ElementType.KDOC_LEADING_ASTERISK
import com.pinterest.ktlint.core.ast.ElementType.KDOC_SECTION
import com.pinterest.ktlint.core.ast.ElementType.LONG_STRING_TEMPLATE_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.LPAR
import com.pinterest.ktlint.core.ast.ElementType.OPERATION_REFERENCE
import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,15 +608,25 @@ class IndentationRuleWarnTest : LintTestBase(::IndentationRule) {

@Test
@Tag(WarningNames.WRONG_INDENTATION)
fun `test`() {
fun `should trigger on string templates starting with new line`() {
lintMethod(
"""
|fun foo(some: String) {
| val a = "${'$'}{
| expression
| }"
| fun bar() {
| val a = "${'$'}{
| expression
| .foo()
| .bar()
| }"
| }
|
| val b = "${'$'}{ foo().bar() }"
|}
""".trimMargin()
|
""".trimMargin(),
LintError(4, 1, ruleId, warnText(12, 8), true),
LintError(5, 1, ruleId, warnText(16, 12), true),
LintError(6, 1, ruleId, warnText(16, 12), true)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,13 @@ data class Example(val field1: Type1,
else
foobaz()
}

fun some() {
val a = "${
foo().bar()
}"

val b = "${baz().foo()}"
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ fun foo(
else
foobaz()
}

fun some() {
val a = "${
foo().bar()
}"

val b = "${baz().foo()}"
}
}

0 comments on commit ed19ffd

Please sign in to comment.