mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
Fix indent inside #ifdef
This commit is contained in:
+4
-5
@@ -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,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user