Skip to content

Commit

Permalink
Add new experimental rule string-template-indent for `ktlint_offici…
Browse files Browse the repository at this point in the history
…al` code style

This rule forces multiline string templates which are post-fixed with `.trimIndent()` to be formatted consistently. The opening and closing `"""` are placed on separate lines and the indentation of the content of the template is aligned with the `"""`.

Closes #925
  • Loading branch information
paul-dingemans committed Mar 21, 2023
1 parent 26caef8 commit aafc250
Show file tree
Hide file tree
Showing 14 changed files with 1,013 additions and 169 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ Previously the default value for `.editorconfig` property `max_line_length` was
* Recognize Kotlin Script when linting and formatting code from `stdin` with KtLint CLI ([#1832](https://github.com/pinterest/ktlint/issues/1832))
* Add new experimental rule `no-blank-line-in-list` for `ktlint_official` code style. This rule disallows blank lines to be used in super type lists, type argument lists, type constraint lists, type parameter lists, value argument lists, and value parameter lists. This rule can also be run for other code styles but then its needs to be explicitly enabled. ([#1224](https://github.com/pinterest/ktlint/issues/1224))
* Add new experimental rule `multiline-expression-wrapping` for `ktlint_official` code style. This forces a multiline expression as value in an assignment to start on a separate line. This rule can also be run for other code styles but then its needs to be explicitly enabled. ([#1217](https://github.com/pinterest/ktlint/issues/1217))
* Add new experimental rule `string-template-indent` for `ktlint_official` code style. This forces multiline string templates which are post-fixed with `.trimIndent()` to be formatted consistently. The opening and closing `"""` are placed on separate lines and the indentation of the content of the template is aligned with the `"""`. This rule can also be run for other code styles but then its needs to be explicitly enabled. ([#925](https://github.com/pinterest/ktlint/issues/925))

### Removed

Expand Down
40 changes: 40 additions & 0 deletions docs/rules/experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,46 @@ Consistent spacing between function name and opening parenthesis.

Rule id: `spacing-between-function-name-and-opening-parenthesis`

### String template indent

Enforce consistent string template indentation for multiline string templates which are post-fixed with `.trimIndent()`. The opening and closing `"""` are placed on separate lines and the indentation of the content of the template is aligned with the `"""`.

=== "[:material-heart:](#) Ktlint (ktlint_official code style)"

```kotlin
val foo =
"""
line1
line2
""".trimIndent()
fun foo() {
// The opening """ can not be wrapped to next line as that would result in a compilation error
return """
line1
line2
""".trimIndent()
}
```
=== "[:material-heart:](#) Ktlint (non ktlint_official code style)"

```kotlin
val foo = """
line1
line2
""".trimIndent()
fun foo() {
return """
line1
line2
""".trimIndent()
}
```

Rule id: `string-template-indent`

!!! Note
This rule is only run when `ktlint_code_style` is set to `ktlint_official` or when the rule is enabled explicitly.

### Try catch finally spacing

Enforce consistent spacing in `try { .. } catch { .. } finally { .. }`.
Expand Down
3 changes: 2 additions & 1 deletion ktlint-cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ val shadowJarExecutable by tasks.registering(DefaultTask::class) {
doLast {
val execFile = outputs.files.files.first()
execFile.appendText(
"""#!/bin/sh
"""
#!/bin/sh
# From this SO answer: https://stackoverflow.com/a/56243046
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,13 @@ class ASTNodeExtensionTest {
fun `Given some identifiers at different indentation levels`() {
val code =
"""
class Foo1 {
val foo2 = "foo2"
class Foo1 {
val foo2 = "foo2"
fun foo3() {
val foo4 = "foo4"
}
fun foo3() {
val foo4 = "foo4"
}
}
""".trimIndent()

val actual =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ class EditorConfigTest {
writeRootEditorConfigFile(
//language=EditorConfig
"""
[*.{kt,kts}]
$ktlintTestRuleExecutionProperty1 = disabled
$ktlintTestRuleExecutionProperty2 = disabled
[*.{kt,kts}]
$ktlintTestRuleExecutionProperty1 = disabled
$ktlintTestRuleExecutionProperty2 = disabled
""".trimIndent(),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,14 @@ internal class EditorConfigLoader(
private fun MutableMap<String, Property>.prettyPrint(normalizedFilePath: Path?) =
map { entry -> "${entry.key}: ${entry.value.sourceValue}" }
.joinToString(
prefix = "Effective editorconfig properties${
if (normalizedFilePath == null) {
""
} else {
" for file '$normalizedFilePath'"
}
}:\n\t",
prefix =
"Effective editorconfig properties${
if (normalizedFilePath == null) {
""
} else {
" for file '$normalizedFilePath'"
}
}:\n\t",
separator = "\n\t",
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ internal class EditorConfigLoaderTest {
writeRootEditorConfigFile(
//language=EditorConfig
"""
[*.{kt,kts}]
some_property = some_value
[*.{kt,kts}]
some_property = some_value
""".trimIndent(),
)
}
Expand Down Expand Up @@ -297,8 +297,8 @@ internal class EditorConfigLoaderTest {
writeRootEditorConfigFile(
//language=EditorConfig
"""
[*.{kt,kts}]
some_property = some_value
[*.{kt,kts}]
some_property = some_value
""".trimIndent(),
)
}
Expand Down Expand Up @@ -334,8 +334,8 @@ internal class EditorConfigLoaderTest {
writeRootEditorConfigFile(
//language=EditorConfig
"""
[*.{kt,kts}]
some_property_1 = some_value_1
[*.{kt,kts}]
some_property_1 = some_value_1
""".trimIndent(),
)
}
Expand All @@ -355,8 +355,8 @@ internal class EditorConfigLoaderTest {
writeRootEditorConfigFile(
//language=EditorConfig
"""
[*.{kt,kts}]
some_property = some_value
[*.{kt,kts}]
some_property = some_value
""".trimIndent(),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import com.pinterest.ktlint.ruleset.standard.rules.SpacingAroundUnaryOperatorRul
import com.pinterest.ktlint.ruleset.standard.rules.SpacingBetweenDeclarationsWithAnnotationsRule
import com.pinterest.ktlint.ruleset.standard.rules.SpacingBetweenDeclarationsWithCommentsRule
import com.pinterest.ktlint.ruleset.standard.rules.SpacingBetweenFunctionNameAndOpeningParenthesisRule
import com.pinterest.ktlint.ruleset.standard.rules.StringTemplateIndentRule
import com.pinterest.ktlint.ruleset.standard.rules.StringTemplateRule
import com.pinterest.ktlint.ruleset.standard.rules.TrailingCommaOnCallSiteRule
import com.pinterest.ktlint.ruleset.standard.rules.TrailingCommaOnDeclarationSiteRule
Expand Down Expand Up @@ -147,6 +148,7 @@ public class StandardRuleSetProvider :
RuleProvider { SpacingBetweenDeclarationsWithCommentsRule() },
RuleProvider { SpacingBetweenFunctionNameAndOpeningParenthesisRule() },
RuleProvider { StringTemplateRule() },
RuleProvider { StringTemplateIndentRule() },
RuleProvider { TrailingCommaOnCallSiteRule() },
RuleProvider { TrailingCommaOnDeclarationSiteRule() },
RuleProvider { TryCatchFinallySpacingRule() },
Expand Down
Loading

0 comments on commit aafc250

Please sign in to comment.