diff --git a/Sources/Tokenizer.swift b/Sources/Tokenizer.swift index 41063b49..d9eb5b89 100644 --- a/Sources/Tokenizer.swift +++ b/Sources/Tokenizer.swift @@ -1492,12 +1492,17 @@ public func tokenize(_ source: String) -> [Token] { if prevNonSpaceToken.isLvalue { var lineStart = index(of: .linebreak, before: prevNonSpaceIndex) ?? 0 lineStart = index(of: .nonSpaceOrComment, after: lineStart) ?? lineStart - if [.startOfScope("#if"), .keyword("#elseif")].contains(tokens[lineStart]), - let prevIndex = index(of: .nonSpaceOrCommentOrLinebreak, before: lineStart) - { + switch tokens[lineStart] { + case .keyword("#elseif"), .keyword("#else"): + lineStart = index(of: .startOfScope, before: lineStart) ?? lineStart + fallthrough + case .startOfScope("#if"): + guard let prevIndex = index(of: .nonSpaceOrCommentOrLinebreak, before: lineStart) else { + fallthrough + } prevNonSpaceIndex = prevIndex continue - } else { + default: _type = .infix } } else if prevNonSpaceToken.isAttribute || diff --git a/Tests/RulesTests+Indentation.swift b/Tests/RulesTests+Indentation.swift index 7ca11464..d0cae06c 100644 --- a/Tests/RulesTests+Indentation.swift +++ b/Tests/RulesTests+Indentation.swift @@ -2902,6 +2902,23 @@ class IndentTests: RulesTests { testFormatting(for: input, rule: FormatRules.indent) } + func testNoIndentDotExpressionInsideIfdef() { + let input = """ + let current: Platform = { + #if os(macOS) + .mac + #elseif os(Linux) + .linux + #elseif os(Windows) + .windows + #else + fatalError("Unknown OS not supported") + #endif + }() + """ + testFormatting(for: input, rule: FormatRules.indent) + } + // indent #if/#else/#elseif/#endif (mode: noindent) func testIfEndifNoIndenting() { @@ -3070,6 +3087,10 @@ class IndentTests: RulesTests { func myFunc() -> String { #if DEBUG .init("foo") + #elseif PROD + .init("bar") + #else + .init("baz") #endif } """