Skip to content

Commit

Permalink
Fix indenting of wrapped member after #endif
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Nov 6, 2021
1 parent 27beaba commit ce717a9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1605,15 +1605,15 @@ public struct _FormatRules {
var lineStart = formatter.startOfLine(at: lastNonSpaceOrLinebreakIndex, excludingIndent: true)
let startToken = formatter.token(at: lineStart)
if let startToken = startToken, [
.startOfScope("#if"), .keyword("#else"), .keyword("#elseif")
.startOfScope("#if"), .keyword("#else"), .keyword("#elseif"), .endOfScope("#endif")
].contains(startToken) {
if let index = formatter.index(of: .nonSpaceOrLinebreak, before: lineStart) {
lastNonSpaceOrLinebreakIndex = index
lineStart = formatter.startOfLine(at: lastNonSpaceOrLinebreakIndex, excludingIndent: true)
}
}
if formatter.token(at: lineStart) == .operator(".", .infix),
[.keyword("#else"), .keyword("#elseif")].contains(startToken)
[.keyword("#else"), .keyword("#elseif"), .endOfScope("#endif")].contains(startToken)
{
indent = formatter.indentForLine(at: lineStart)
} else if formatter.tokens[lineStart ..< lastNonSpaceOrLinebreakIndex].allSatisfy({
Expand Down
3 changes: 2 additions & 1 deletion Sources/Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,8 @@ public func tokenize(_ source: String) -> [Token] {
case ":", "=", "->":
type = .infix
case ".":
type = prevNonSpaceToken.isLvalue || prevNonSpaceToken.isAttribute ? .infix : .prefix
type = prevNonSpaceToken.isLvalue || prevNonSpaceToken.isAttribute ||
prevNonSpaceToken == .endOfScope("#endif") ? .infix : .prefix
case "?":
if prevToken.isSpaceOrCommentOrLinebreak {
// ? is a ternary operator, treat it as the start of a scope
Expand Down
15 changes: 15 additions & 0 deletions Tests/RulesTests+Indentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2291,6 +2291,21 @@ class IndentTests: RulesTests {
testFormatting(for: input, output, rule: FormatRules.indent)
}

func testIndentIfDefPostfixMemberSyntax2() {
let input = """
class Bar {
func foo() {
Text("Hello")
#if os(iOS)
.font(.largeTitle)
#endif
.color(.red)
}
}
"""
testFormatting(for: input, rule: FormatRules.indent)
}

// indent #if/#else/#elseif/#endif (mode: noindent)

func testIfEndifNoIndenting() {
Expand Down
33 changes: 33 additions & 0 deletions Tests/TokenizerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3685,6 +3685,39 @@ class TokenizerTests: XCTestCase {
XCTAssertEqual(tokenize(input), output)
}

func testIfdefPrefixDot() {
let input = """
foo
#if bar
.bar
#else
.baz
#endif
.quux
"""
let output: [Token] = [
.identifier("foo"),
.linebreak("\n", 1),
.startOfScope("#if"),
.space(" "),
.identifier("bar"),
.linebreak("\n", 2),
.operator(".", .infix),
.identifier("bar"),
.linebreak("\n", 3),
.keyword("#else"),
.linebreak("\n", 4),
.operator(".", .infix),
.identifier("baz"),
.linebreak("\n", 5),
.endOfScope("#endif"),
.linebreak("\n", 6),
.operator(".", .infix),
.identifier("quux"),
]
XCTAssertEqual(tokenize(input), output)
}

// MARK: linebreaks

func testLF() {
Expand Down

0 comments on commit ce717a9

Please sign in to comment.