@@ -100,6 +100,25 @@ public struct ImageLiteralMacro: ExpressionMacro {
100
100
}
101
101
}
102
102
103
+ public struct ColumnMacro : ExpressionMacro {
104
+ public static func expansion(
105
+ of macro: MacroExpansionExprSyntax ,
106
+ in context: any MacroExpansionContext
107
+ ) throws -> ExprSyntax {
108
+ guard let sourceLoc = context. location ( of: macro) ,
109
+ let column = sourceLoc. column else {
110
+ throw CustomError . message ( " can't find location for macro " )
111
+ }
112
+
113
+
114
+ let fileLiteral : ExprSyntax = " \( literal: column) "
115
+ if let leadingTrivia = macro. leadingTrivia {
116
+ return fileLiteral. with ( \. leadingTrivia, leadingTrivia)
117
+ }
118
+ return fileLiteral
119
+ }
120
+ }
121
+
103
122
public struct FileIDMacro : ExpressionMacro {
104
123
public static func expansion(
105
124
of macro: MacroExpansionExprSyntax ,
@@ -602,6 +621,7 @@ public func AssertMacroExpansion(
602
621
public let testMacros : [ String : Macro . Type ] = [
603
622
" checkContext " : CheckContextIndependenceMacro . self,
604
623
" colorLiteral " : ColorLiteralMacro . self,
624
+ " column " : ColumnMacro . self,
605
625
" fileID " : FileIDMacro . self,
606
626
" imageLiteral " : ImageLiteralMacro . self,
607
627
" stringify " : StringifyMacro . self,
@@ -648,16 +668,18 @@ final class MacroSystemTests: XCTestCase {
648
668
)
649
669
}
650
670
651
- func testFileExpansions ( ) {
671
+ func testLocationExpansions ( ) {
652
672
AssertMacroExpansion (
653
673
macros: testMacros,
654
674
testModuleName: " MyModule " ,
655
675
testFileName: " taylor.swift " ,
656
676
"""
657
677
let b = #fileID
678
+ let c = #column
658
679
""" ,
659
680
"""
660
681
let b = " MyModule/taylor.swift "
682
+ let c = 9
661
683
"""
662
684
)
663
685
}
0 commit comments