Skip to content

Commit b508e4b

Browse files
committed
fix: comments with # macros should also be appropriately indented
1 parent 32c6662 commit b508e4b

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed

Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@ private func adjustIndentationOfFreestandingMacro(expandedCode: String, node: so
137137
var indentedSource =
138138
expandedCode
139139
.indented(by: indentationOfFirstLine)
140-
.wrappingInTrivia(from: node)
141140

142-
// if the experssion started in middle of the line, then remove indentation of the first line
143-
if !node.leadingTrivia.contains(where: \.isNewline) {
141+
if indentedSource.count >= indentationOfFirstLine.sourceLength.utf8Length {
144142
indentedSource.removeFirst(indentationOfFirstLine.sourceLength.utf8Length)
145143
}
146144

145+
indentedSource = indentedSource.wrappingInTrivia(from: node)
146+
147147
return indentedSource
148148
}
149149

@@ -1274,9 +1274,7 @@ private extension String {
12741274
/// user should think about it as just replacing the `#...` expression without
12751275
/// any trivia.
12761276
func wrappingInTrivia(from node: some SyntaxProtocol) -> String {
1277-
// We need to remove the indentation from the last line because the macro
1278-
// expansion buffer already contains the indentation.
1279-
return node.leadingTrivia.removingIndentationOnLastLine.description
1277+
return node.leadingTrivia.description
12801278
+ self
12811279
+ node.trailingTrivia.description
12821280
}

Tests/SwiftSyntaxMacroExpansionTest/LexicalContextTests.swift

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,40 @@ final class LexicalContextTests: XCTestCase {
287287
macros: ["function": FunctionMacro.self],
288288
indentationWidth: indentationWidth
289289
)
290+
291+
assertMacroExpansion(
292+
"""
293+
func f(a: Int, _: Double, c: Int) {
294+
print(/*comment*/#function)
295+
}
296+
""",
297+
expandedSource: """
298+
func f(a: Int, _: Double, c: Int) {
299+
print(/*comment*/"f(a:_:c:)")
300+
}
301+
""",
302+
macros: ["function": FunctionMacro.self],
303+
indentationWidth: indentationWidth
304+
)
305+
306+
assertMacroExpansion(
307+
"""
308+
var computed: String {
309+
get {
310+
/*comment*/#function
311+
}
312+
}
313+
""",
314+
expandedSource: """
315+
var computed: String {
316+
get {
317+
/*comment*/"computed"
318+
}
319+
}
320+
""",
321+
macros: ["function": FunctionMacro.self],
322+
indentationWidth: indentationWidth
323+
)
290324
}
291325

292326
func testPoundMultilineFunction() {
@@ -385,6 +419,44 @@ final class LexicalContextTests: XCTestCase {
385419
macros: ["function": MultilineFunctionMacro.self],
386420
indentationWidth: indentationWidth
387421
)
422+
423+
assertMacroExpansion(
424+
"""
425+
func f(a: Int, _: Double, c: Int) {
426+
print(/*comment*/#function)
427+
}
428+
""",
429+
expandedSource: """
430+
func f(a: Int, _: Double, c: Int) {
431+
print(/*comment*/{
432+
"f(a:_:c:)"
433+
})
434+
}
435+
""",
436+
macros: ["function": MultilineFunctionMacro.self],
437+
indentationWidth: indentationWidth
438+
)
439+
440+
assertMacroExpansion(
441+
"""
442+
var computed: String {
443+
get {
444+
/*comment*/#function // another comment
445+
}
446+
}
447+
""",
448+
expandedSource: """
449+
var computed: String {
450+
get {
451+
/*comment*/{
452+
"computed"
453+
} // another comment
454+
}
455+
}
456+
""",
457+
macros: ["function": MultilineFunctionMacro.self],
458+
indentationWidth: indentationWidth
459+
)
388460
}
389461

390462
func testAllLexicalContexts() {

0 commit comments

Comments
 (0)