Skip to content

Commit

Permalink
Fix indenting of commented code after opening bracket
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Jun 12, 2024
1 parent 41dec1d commit 32fcc7b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Sources/FormattingHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,10 @@ extension Formatter {
}
if nextIndex + 1 < endOfScope, next(.nonSpace, after: nextIndex)?.isLinebreak == false {
var indent = indent
if (self.index(of: .nonSpace, after: nextIndex) ?? 0) < endOfScope {
if let nextNonSpaceIndex = self.index(of: .nonSpace, after: nextIndex),
nextNonSpaceIndex < endOfScope,
!isCommentedCode(at: nextNonSpaceIndex)
{
indent += options.indent
}
endOfScope += insertSpace(indent, at: nextIndex + 1)
Expand Down
19 changes: 19 additions & 0 deletions Tests/RulesTests+Indentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2393,6 +2393,25 @@ class IndentTests: RulesTests {
testFormatting(for: input, rule: FormatRules.indent)
}

func testCommentedCodeAfterBracketNotIndented() {
let input = """
let foo = [
// first,
second,
]
"""
testFormatting(for: input, rule: FormatRules.indent)
}

func testCommentedCodeAfterBracketNotIndented2() {
let input = """
let foo = [first,
// second,
third]
"""
testFormatting(for: input, rule: FormatRules.indent)
}

// TODO: maybe need special case handling for this?
func testIndentWrappedTrailingComment() {
let input = """
Expand Down

0 comments on commit 32fcc7b

Please sign in to comment.