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

Bugfix. Indentation inside string templates(#758) #765

Merged
merged 9 commits into from
Feb 16, 2021
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 @@ -606,5 +606,29 @@ class IndentationRuleWarnTest : LintTestBase(::IndentationRule) {
)
}

@Test
@Tag(WarningNames.WRONG_INDENTATION)
fun `should trigger on string templates starting with new line`() {
lintMethod(
"""
|fun foo(some: String) {
| fun bar() {
| val a = "${'$'}{
| expression
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I corrected expression as it should be, but then it incorrectly detects the indentation of the bottom lines

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

| .foo()
| .bar()
| }"
| }
|
| val b = "${'$'}{ foo().bar() }"
|}
|
""".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)
)
}

private fun warnText(expected: Int, actual: Int) = "${WRONG_INDENTATION.warnText()} expected $expected but was $actual"
}
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()}"
}
}