@@ -173,6 +173,24 @@ public struct FunctionMacro: ExpressionMacro {
173173 }
174174}
175175
176+ public struct MultilineFunctionMacro : ExpressionMacro {
177+ public static func expansion<
178+ Node: FreestandingMacroExpansionSyntax ,
179+ Context: MacroExpansionContext
180+ > (
181+ of node: Node ,
182+ in context: Context
183+ ) -> ExprSyntax {
184+ guard let lexicalContext = context. lexicalContext. first,
185+ let name = lexicalContext. functionName ( in: context)
186+ else {
187+ return #""<unknown>""#
188+ }
189+
190+ return ExprSyntax ( " { \n \( literal: name) \n } " )
191+ }
192+ }
193+
176194public struct AllLexicalContextsMacro : DeclarationMacro {
177195 public static func expansion(
178196 of node: some FreestandingMacroExpansionSyntax ,
@@ -194,7 +212,7 @@ final class LexicalContextTests: XCTestCase {
194212 """ ,
195213 expandedSource: """
196214 func f(a: Int, _: Double, c: Int) {
197- print( " f(a:_:c:) " )
215+ print( " f(a:_:c:) " )
198216 }
199217 """ ,
200218 macros: [ " function " : FunctionMacro . self] ,
@@ -263,12 +281,182 @@ final class LexicalContextTests: XCTestCase {
263281 """ ,
264282 expandedSource: """
265283 extension A {
266- static var staticProp: String = " staticProp "
284+ static var staticProp: String = " staticProp "
267285 }
268286 """ ,
269287 macros: [ " function " : FunctionMacro . self] ,
270288 indentationWidth: indentationWidth
271289 )
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+ )
324+ }
325+
326+ func testPoundMultilineFunction( ) {
327+ assertMacroExpansion (
328+ """
329+ func f(a: Int, _: Double, c: Int) {
330+ print(#function)
331+ }
332+ """ ,
333+ expandedSource: """
334+ func f(a: Int, _: Double, c: Int) {
335+ print({
336+ " f(a:_:c:) "
337+ })
338+ }
339+ """ ,
340+ macros: [ " function " : MultilineFunctionMacro . self] ,
341+ indentationWidth: indentationWidth
342+ )
343+
344+ assertMacroExpansion (
345+ """
346+ struct X {
347+ init(from: String) {
348+ #function
349+ }
350+
351+ subscript(a: Int) -> String {
352+ #function
353+ }
354+
355+ subscript(a a: Int) -> String {
356+ #function
357+ }
358+ }
359+ """ ,
360+ expandedSource: """
361+ struct X {
362+ init(from: String) {
363+ {
364+ " init(from:) "
365+ }
366+ }
367+
368+ subscript(a: Int) -> String {
369+ {
370+ " subscript(_:) "
371+ }
372+ }
373+
374+ subscript(a a: Int) -> String {
375+ {
376+ " subscript(a:) "
377+ }
378+ }
379+ }
380+ """ ,
381+ macros: [ " function " : MultilineFunctionMacro . self] ,
382+ indentationWidth: indentationWidth
383+ )
384+
385+ assertMacroExpansion (
386+ """
387+ var computed: String {
388+ get {
389+ #function
390+ }
391+ }
392+ """ ,
393+ expandedSource: """
394+ var computed: String {
395+ get {
396+ {
397+ " computed "
398+ }
399+ }
400+ }
401+ """ ,
402+ macros: [ " function " : MultilineFunctionMacro . self] ,
403+ indentationWidth: indentationWidth
404+ )
405+
406+ assertMacroExpansion (
407+ """
408+ extension A {
409+ static var staticProp: String = #function
410+ }
411+ """ ,
412+ expandedSource: """
413+ extension A {
414+ static var staticProp: String = {
415+ " staticProp "
416+ }
417+ }
418+ """ ,
419+ macros: [ " function " : MultilineFunctionMacro . self] ,
420+ indentationWidth: indentationWidth
421+ )
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+ )
272460 }
273461
274462 func testAllLexicalContexts( ) {
0 commit comments