Skip to content

Commit

Permalink
Fixed another linewrap edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Oct 4, 2016
1 parent d9a75b0 commit 939506a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 37 deletions.
4 changes: 2 additions & 2 deletions SwiftFormat/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ public func indent(_ formatter: Formatter) {
while let token = formatter.tokenAtIndex(i) {
switch token.type {
case .identifier:
if ["if", "else", "for", "while", "do", "catch", "switch"].contains(token.string) {
if ["if", "for", "while", "catch", "switch" /* TODO: get/set/didSet */ ].contains(token.string) {
// Check that it's actually a keyword and not a member property or enum value
return formatter.previousNonWhitespaceOrCommentOrLinebreakToken(fromIndex: i)?.string == "."
}
Expand All @@ -906,7 +906,7 @@ public func indent(_ formatter: Formatter) {
break
}
i = formatter.indexOfPreviousToken(fromIndex: i) {
return !$0.isWhitespaceOrCommentOrLinebreak && $0.type != .endOfScope
return !$0.isWhitespaceOrCommentOrLinebreak && ($0.type != .endOfScope || $0.string == "}")
} ?? -1
}
return true
Expand Down
84 changes: 49 additions & 35 deletions SwiftFormatTests/RulesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1101,41 +1101,6 @@ class RulesTests: XCTestCase {
XCTAssertEqual(try! format(input + "\n", rules: defaultRules), output + "\n")
}

func testNestedWrappedIfIndents() {
let input = "if foo {\nif bar &&\n(baz ||\nquux) {\nfoo()\n}\n}"
let output = "if foo {\n if bar &&\n (baz ||\n quux) {\n foo()\n }\n}"
XCTAssertEqual(try! format(input, rules: [indent]), output)
XCTAssertEqual(try! format(input + "\n", rules: defaultRules), output + "\n")
}

func testWrappedEnumThatLooksLikeIf() {
let input = "foo &&\n bar.if {\nfoo()\n}"
let output = "foo &&\n bar.if {\n foo()\n }"
XCTAssertEqual(try! format(input, rules: [indent]), output)
XCTAssertEqual(try! format(input + "\n", rules: defaultRules), output + "\n")
}

func testChainedClosureIndents() {
let input = "foo\n.bar {\nbaz()\n}\n.bar {\nbaz()\n}"
let output = "foo\n .bar {\n baz()\n }\n .bar {\n baz()\n }"
XCTAssertEqual(try! format(input, rules: [indent]), output)
XCTAssertEqual(try! format(input + "\n", rules: defaultRules), output + "\n")
}

func testChainedFunctionsInsideIf() {
let input = "if foo {\nreturn bar()\n.baz()\n}"
let output = "if foo {\n return bar()\n .baz()\n}"
XCTAssertEqual(try! format(input, rules: [indent]), output)
XCTAssertEqual(try! format(input + "\n", rules: defaultRules), output + "\n")
}

func testChainedFunctionsInsideForLoop() {
let input = "for x in y {\nfoo\n.bar {\nbaz()\n}\n.quux()\n}"
let output = "for x in y {\n foo\n .bar {\n baz()\n }\n .quux()\n}"
XCTAssertEqual(try! format(input, rules: [indent]), output)
XCTAssertEqual(try! format(input + "\n", rules: defaultRules), output + "\n")
}

// MARK: indent switch/case

func testSwitchCaseIndenting() {
Expand Down Expand Up @@ -1391,6 +1356,55 @@ class RulesTests: XCTestCase {
XCTAssertEqual(try! format(input, rules: [indent]), output)
XCTAssertEqual(try! format(input + "\n", rules: defaultRules), output + "\n")
}

func testNestedWrappedIfIndents() {
let input = "if foo {\nif bar &&\n(baz ||\nquux) {\nfoo()\n}\n}"
let output = "if foo {\n if bar &&\n (baz ||\n quux) {\n foo()\n }\n}"
XCTAssertEqual(try! format(input, rules: [indent]), output)
XCTAssertEqual(try! format(input + "\n", rules: defaultRules), output + "\n")
}

func testWrappedEnumThatLooksLikeIf() {
let input = "foo &&\n bar.if {\nfoo()\n}"
let output = "foo &&\n bar.if {\n foo()\n }"
XCTAssertEqual(try! format(input, rules: [indent]), output)
XCTAssertEqual(try! format(input + "\n", rules: defaultRules), output + "\n")
}

func testChainedClosureIndents() {
let input = "foo\n.bar {\nbaz()\n}\n.bar {\nbaz()\n}"
let output = "foo\n .bar {\n baz()\n }\n .bar {\n baz()\n }"
XCTAssertEqual(try! format(input, rules: [indent]), output)
XCTAssertEqual(try! format(input + "\n", rules: defaultRules), output + "\n")
}

func testChainedFunctionsInsideIf() {
let input = "if foo {\nreturn bar()\n.baz()\n}"
let output = "if foo {\n return bar()\n .baz()\n}"
XCTAssertEqual(try! format(input, rules: [indent]), output)
XCTAssertEqual(try! format(input + "\n", rules: defaultRules), output + "\n")
}

func testChainedFunctionsInsideForLoop() {
let input = "for x in y {\nfoo\n.bar {\nbaz()\n}\n.quux()\n}"
let output = "for x in y {\n foo\n .bar {\n baz()\n }\n .quux()\n}"
XCTAssertEqual(try! format(input, rules: [indent]), output)
XCTAssertEqual(try! format(input + "\n", rules: defaultRules), output + "\n")
}

func testChainedFunctionsAfterAnIfStatement() {
let input = "if foo {}\nbar\n.baz {\n}\n.quux()"
let output = "if foo {}\nbar\n .baz {\n }\n .quux()"
XCTAssertEqual(try! format(input, rules: [indent]), output)
XCTAssertEqual(try! format(input + "\n", rules: defaultRules), output + "\n")
}

func testIndentInsideWrappedIfStatementWithClosureCondition() {
let input = "if foo({ 1 }) ||\nbar {\nbaz()\n}"
let output = "if foo({ 1 }) ||\n bar {\n baz()\n}"
XCTAssertEqual(try! format(input, rules: [indent]), output)
XCTAssertEqual(try! format(input + "\n", rules: defaultRules), output + "\n")
}

// MARK: indent comments

Expand Down

0 comments on commit 939506a

Please sign in to comment.