Skip to content

Commit

Permalink
Fix unwanted insertion of leading space
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Oct 26, 2020
1 parent 520a487 commit 5833b62
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Sources/FormattingHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,13 @@ extension Formatter {
}
index = commaIndex
}

// Insert linebreak and indent after opening paren
if let nextIndex = self.index(of: .nonSpaceOrComment, after: i) {
if !tokens[nextIndex].isLinebreak {
insertLinebreak(at: nextIndex)
}
if nextIndex + 1 < endOfScope {
if nextIndex + 1 < endOfScope, next(.nonSpace, after: nextIndex)?.isLinebreak == false {
var indent = indent
if (self.index(of: .nonSpace, after: nextIndex) ?? 0) < endOfScope {
indent += options.indent
Expand Down
1 change: 1 addition & 0 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,7 @@ public struct _FormatRules {
/// meaning and leads to noise in commits.
public let trailingSpace = FormatRule(
help: "Remove trailing space at end of a line.",
orderAfter: ["wrap", "wrapArguments"],
options: ["trimwhitespace"]
) { formatter in
formatter.forEach(.space) { i, _ in
Expand Down
13 changes: 13 additions & 0 deletions Tests/RulesTests+Wrapping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,19 @@ extension RulesTests {
exclude: ["wrap"])
}

func testWrapArgumentsNoIndentBlankLines() {
let input = """
let foo = [
bar,
]
"""
let options = FormatOptions(wrapCollections: .beforeFirst)
testFormatting(for: input, rule: FormatRules.wrapArguments, options: options,
exclude: ["wrap", "blankLinesAtStartOfScope", "blankLinesAtEndOfScope"])
}

// MARK: closingParenOnSameLine = true

func testParenOnSameLineWhenWrapAfterFirstConvertedToWrapBefore() {
Expand Down
1 change: 1 addition & 0 deletions Tests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,7 @@ extension RulesTests {
("testWrapArgumentsDoesNotAffectSubscript", testWrapArgumentsDoesNotAffectSubscript),
("testWrapArgumentsDoesntIndentClosingBracket", testWrapArgumentsDoesntIndentClosingBracket),
("testWrapArgumentsDoesntIndentTrailingComment", testWrapArgumentsDoesntIndentTrailingComment),
("testWrapArgumentsNoIndentBlankLines", testWrapArgumentsNoIndentBlankLines),
("testWrapBeforeFirstIfMaxLengthExceeded", testWrapBeforeFirstIfMaxLengthExceeded),
("testWrapChainedFunctionAfterSubscriptCollection", testWrapChainedFunctionAfterSubscriptCollection),
("testWrapChainedFunctionInSubscriptCollection", testWrapChainedFunctionInSubscriptCollection),
Expand Down

0 comments on commit 5833b62

Please sign in to comment.