Fix indent inside #ifdef

This commit is contained in:
Nick Lockwood
2023-05-20 16:37:02 +01:00
parent 7ff506897a
commit f5c988e3f4
3 changed files with 24 additions and 6 deletions
+4 -5
View File
@@ -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,
+6 -1
View File
@@ -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 {
+14
View File
@@ -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() {