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

Fix indent parenthesized expression #2127

Merged
merged 7 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
In code style ktlint_official do not indent the closing parenthesis…
… of a PARENTHESIZED expression

CLoses #920
  • Loading branch information
paul-dingemans committed Jul 15, 2023
commit 8044836d388d550489ce42b05b87e0bb5a2939f3
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).

* Allow to disable ktlint in `.editorconfig` for a glob ([#2100](https://github.com/pinterest/ktlint/issues/2100))
* Fix wrapping of nested function literals `wrapping` ([#2106](https://github.com/pinterest/ktlint/issues/2106))
* Do not indent class body for classes having a long super type list in code style `ktlint_official` as it is inconsistent compared to other class bodies `indent` [#2115](https://github.com/pinterest/ktlint/issues/2115)
* Fix spacing around colon in annotations `spacing-around-colon` ([#2093](https://github.com/pinterest/ktlint/issues/2093))
* Do not indent class body for classes having a long super type list in code style `ktlint_official` as it is inconsistent compared to other class bodies `indent` [#2115](https://github.com/pinterest/ktlint/issues/2115)
* In code style `ktlint_official` do not indent the closing parenthesis of a PARENTHESIZED expression `indent` [#920](https://github.com/pinterest/ktlint/issues/920)

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,16 @@ public class IndentationRule :
node.elementType == SECONDARY_CONSTRUCTOR ->
visitSecondaryConstructor(node)

node.elementType == PARENTHESIZED &&
node.treeParent.treeParent.elementType != IF ->
startIndentContext(node)
node.elementType == PARENTHESIZED ->
if (codeStyle == ktlint_official) {
// Contrary to the IntelliJ IDEA default formatter, do not indent the closing RPAR
startIndentContext(
fromAstNode = node,
lastChildIndent = ""
)
} else if (node.treeParent.treeParent.elementType != IF) {
startIndentContext(node)
}

node.elementType == BINARY_WITH_TYPE ||
node.elementType == SUPER_TYPE_ENTRY ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5322,6 +5322,24 @@ internal class IndentationRuleTest {
.isFormattedAs(formattedCode)
}

@Test
fun `Issue 920 - Given ktlint_official codestyle and a PARENTHESIZED expression`() {
val code =
"""
val foobar =
if (true) {
(
foo()
)
} else {
bar()
}
""".trimIndent()
indentationRuleAssertThat(code)
.withEditorConfigOverride(CODE_STYLE_PROPERTY to ktlint_official)
.hasNoLintViolations()
}

private companion object {
val INDENT_STYLE_TAB =
INDENT_STYLE_PROPERTY to PropertyType.IndentStyleValue.tab
Expand Down