Fix indenting dot-prefixed expressions inside #elseif

This commit is contained in:
Nick Lockwood
2023-04-11 13:20:04 +01:00
parent 92c2397fcb
commit d755cc5b03
2 changed files with 30 additions and 4 deletions
+9 -4
View File
@@ -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 ||
+21
View File
@@ -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
}
"""