mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
Fix indenting dot-prefixed expressions inside #elseif
This commit is contained in:
@@ -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 ||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user