diff --git a/Sources/Rules.swift b/Sources/Rules.swift index 5469a126..770e9be0 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -1971,11 +1971,10 @@ public struct _FormatRules { } default: var lastIndex = lastNonSpaceOrLinebreakIndex > -1 ? lastNonSpaceOrLinebreakIndex : i - while formatter.token(at: lastIndex) == .endOfScope("#endif") || - formatter.currentScope(at: lastIndex) == .startOfScope("#if"), - let index = formatter.index(of: .startOfScope, before: lastIndex, if: { - $0 == .startOfScope("#ifdef") - }) + while formatter.token(at: lastIndex) == .endOfScope("#endif"), + let index = formatter.index(of: .startOfScope, before: lastIndex, if: { + $0 == .startOfScope("#if") + }) { lastIndex = formatter.index( of: .nonSpaceOrCommentOrLinebreak, diff --git a/Sources/Tokenizer.swift b/Sources/Tokenizer.swift index 3cc3e48d..5d17c604 100644 --- a/Sources/Tokenizer.swift +++ b/Sources/Tokenizer.swift @@ -1494,7 +1494,12 @@ public func tokenize(_ source: String) -> [Token] { lineStart = index(of: .nonSpaceOrComment, after: lineStart) ?? lineStart switch tokens[lineStart] { case .keyword("#elseif"), .keyword("#else"): - lineStart = index(of: .startOfScope, before: lineStart) ?? lineStart + while let start = index(of: .startOfScope, before: lineStart) { + lineStart = start + if tokens[start] == .startOfScope("#if") { + break + } + } fallthrough case .startOfScope("#if"): guard let prevIndex = index(of: .nonSpaceOrCommentOrLinebreak, before: lineStart) else { diff --git a/Tests/RulesTests+Indentation.swift b/Tests/RulesTests+Indentation.swift index 9ce4bbb2..aa4da6ca 100644 --- a/Tests/RulesTests+Indentation.swift +++ b/Tests/RulesTests+Indentation.swift @@ -3242,6 +3242,20 @@ class IndentTests: RulesTests { testFormatting(for: input, rule: FormatRules.indent, options: options) } + func testNoIndentDotInitInsideIfdef2() { + let input = """ + var title: Font { + #if os(iOS) + .init(style: .title2) + #else + .init(style: .title2, size: 40) + #endif + } + """ + let options = FormatOptions(ifdefIndent: .noIndent) + testFormatting(for: input, rule: FormatRules.indent, options: options) + } + // indent #if/#else/#elseif/#endif (mode: outdent) func testIfEndifOutdenting() {