Skip to content

Commit c577526

Browse files
committed
Do not affect indentation when formatMode is set to off
Make two changes: - Don’t trim the expansions from a macro if `formatMode` is set to `off`. We want to keep all whitespace just as the macro provided it - Don’t add indentation when collapsing a macro. Again, we want to keep the indentation as the macro provided it. rdar://132597440 Fixes #2757
1 parent f8b6353 commit c577526

File tree

4 files changed

+38
-23
lines changed

4 files changed

+38
-23
lines changed

Sources/SwiftCompilerPluginMessageHandling/Macros.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,18 @@ extension PluginProviderMessageHandler {
163163
in: context
164164
)
165165
if let expansions, hostCapability.hasExpandMacroResult {
166+
let collapseIndentationWidth: Trivia?
167+
switch macroDefinition.formatMode {
168+
case .auto: collapseIndentationWidth = nil
169+
case .disabled: collapseIndentationWidth = []
170+
}
166171
// Make a single element array by collapsing the results into a string.
167172
expandedSources = [
168173
SwiftSyntaxMacroExpansion.collapse(
169174
expansions: expansions,
170175
for: role,
171-
attachedTo: declarationNode
176+
attachedTo: declarationNode,
177+
indentationWidth: collapseIndentationWidth
172178
)
173179
]
174180
} else {

Sources/SwiftSyntaxMacroExpansion/MacroExpansion.swift

+17-6
Original file line numberDiff line numberDiff line change
@@ -398,27 +398,38 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
398398
in: context,
399399
indentationWidth: indentationWidth
400400
)
401-
return expandedSources.map {
402-
collapse(expansions: $0, for: macroRole, attachedTo: declarationNode, indentationWidth: indentationWidth)
401+
if let expandedSources {
402+
// If formatting is disabled we don't want to add any indentation while collapsing
403+
let collapseIndentationWidth: Trivia?
404+
switch definition.formatMode {
405+
case .auto: collapseIndentationWidth = indentationWidth
406+
case .disabled: collapseIndentationWidth = []
407+
}
408+
return collapse(
409+
expansions: expandedSources,
410+
for: macroRole,
411+
attachedTo: declarationNode,
412+
indentationWidth: collapseIndentationWidth
413+
)
403414
}
415+
return nil
404416
}
405417

406418
fileprivate extension SyntaxProtocol {
407419
/// Perform a format if required and then trim any leading/trailing
408420
/// whitespace.
409421
func formattedExpansion(_ mode: FormatMode, indentationWidth: Trivia?) -> String {
410-
let formatted: Syntax
411422
switch mode {
412423
case .auto:
413-
formatted = self.formatted(using: BasicFormat(indentationWidth: indentationWidth))
424+
return self.formatted(using: BasicFormat(indentationWidth: indentationWidth))
425+
.trimmedDescription(matching: \.isWhitespace)
414426
case .disabled:
415-
formatted = Syntax(self)
427+
return Syntax(self).description
416428
#if RESILIENT_LIBRARIES
417429
@unknown default:
418430
fatalError()
419431
#endif
420432
}
421-
return formatted.trimmedDescription(matching: { $0.isWhitespace })
422433
}
423434
}
424435

Tests/SwiftSyntaxMacroExpansionTest/AccessorMacroTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ final class AccessorMacroTests: XCTestCase {
157157
""",
158158
expandedSource: """
159159
var x: Int { // hello
160-
get { 1 }
160+
get { 1 }
161161
}
162162
""",
163163
macros: ["constantOne": ConstantOneSingleLineGetter.self],

Tests/SwiftSyntaxMacroExpansionTest/BodyMacroTests.swift

+13-15
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ final class BodyMacroTests: XCTestCase {
156156
)
157157
}
158158

159-
func testBodyNodeLocationFromContext() {
159+
func testDontAddIndentationWhenCollapsingBody() {
160160
struct SourceLocationMacro: BodyMacro {
161161
public static var formatMode: FormatMode { .disabled }
162162

@@ -168,37 +168,35 @@ final class BodyMacroTests: XCTestCase {
168168
guard let statements = declaration.body?.statements else {
169169
return []
170170
}
171-
let body =
172-
if let location = context.location(of: statements, at: .afterLeadingTrivia, filePathMode: .filePath) {
171+
if let location = context.location(of: statements, at: .afterLeadingTrivia, filePathMode: .filePath) {
172+
return statements.flatMap { statement in
173173
CodeBlockItemListSyntax {
174174
"#sourceLocation(file: \(location.file), line: \(location.line))"
175-
statements
175+
statement.trimmed(matching: \.isNewline)
176176
"#sourceLocation()"
177177
}
178-
} else {
179-
statements
180178
}
181-
return Array(body)
179+
} else {
180+
return Array(statements)
181+
}
182182
}
183183
}
184184

185185
assertMacroExpansion(
186186
"""
187187
@SourceLocationMacro
188188
func f() {
189-
let x: Int = 1
189+
let x: Int = 1
190190
}
191191
""",
192-
expandedSource:
193-
"""
192+
expandedSource: """
194193
func f() {
195-
#sourceLocation(file: "test.swift", line: 3)
196-
let x: Int = 1
197-
#sourceLocation()
194+
#sourceLocation(file: "test.swift", line: 3)
195+
let x: Int = 1
196+
#sourceLocation()
198197
}
199198
""",
200-
macros: ["SourceLocationMacro": SourceLocationMacro.self],
201-
indentationWidth: indentationWidth
199+
macros: ["SourceLocationMacro": SourceLocationMacro.self]
202200
)
203201
}
204202
}

0 commit comments

Comments
 (0)