@@ -21,11 +21,18 @@ final class SwiftCodeLensScanner: SyntaxVisitor {
2121 /// The collection of CodeLenses found in the document.
2222 private var result : [ CodeLens ] = [ ]
2323
24+ private let targetName : String ?
25+
2426 /// The map of supported commands and their client side command names
2527 private let supportedCommands : [ SupportedCodeLensCommand : String ]
2628
27- private init ( snapshot: DocumentSnapshot , supportedCommands: [ SupportedCodeLensCommand : String ] ) {
29+ private init (
30+ snapshot: DocumentSnapshot ,
31+ targetName: String ? ,
32+ supportedCommands: [ SupportedCodeLensCommand : String ]
33+ ) {
2834 self . snapshot = snapshot
35+ self . targetName = targetName
2936 self . supportedCommands = supportedCommands
3037 super. init ( viewMode: . fixedUp)
3138 }
@@ -35,6 +42,7 @@ final class SwiftCodeLensScanner: SyntaxVisitor {
3542 public static func findCodeLenses(
3643 in snapshot: DocumentSnapshot ,
3744 syntaxTreeManager: SyntaxTreeManager ,
45+ targetName: String ? = nil ,
3846 supportedCommands: [ SupportedCodeLensCommand : String ]
3947 ) async -> [ CodeLens ] {
4048 guard snapshot. text. contains ( " @main " ) && !supportedCommands. isEmpty else {
@@ -43,7 +51,7 @@ final class SwiftCodeLensScanner: SyntaxVisitor {
4351 }
4452
4553 let syntaxTree = await syntaxTreeManager. syntaxTree ( for: snapshot)
46- let visitor = SwiftCodeLensScanner ( snapshot: snapshot, supportedCommands: supportedCommands)
54+ let visitor = SwiftCodeLensScanner ( snapshot: snapshot, targetName : targetName , supportedCommands: supportedCommands)
4755 visitor. walk ( syntaxTree)
4856 return visitor. result
4957 }
@@ -61,14 +69,20 @@ final class SwiftCodeLensScanner: SyntaxVisitor {
6169 private func captureLensFromAttribute( attribute: AttributeListSyntax . Element ) {
6270 if attribute. trimmedDescription == " @main " {
6371 let range = self . snapshot. absolutePositionRange ( of: attribute. trimmedRange)
72+ var targetNameToAppend : String = " "
73+ var arguments : [ LSPAny ] = [ ]
74+ if let targetName {
75+ targetNameToAppend = " \( targetName) "
76+ arguments. append ( . string( targetName) )
77+ }
6478
6579 if let runCommand = supportedCommands [ SupportedCodeLensCommand . run] {
6680 // Return commands for running/debugging the executable.
6781 // These command names must be recognized by the client and so should not be chosen arbitrarily.
6882 self . result. append (
6983 CodeLens (
7084 range: range,
71- command: Command ( title: " Run " , command: runCommand, arguments: nil )
85+ command: Command ( title: " Run " + targetNameToAppend , command: runCommand, arguments: arguments )
7286 )
7387 )
7488 }
@@ -77,7 +91,7 @@ final class SwiftCodeLensScanner: SyntaxVisitor {
7791 self . result. append (
7892 CodeLens (
7993 range: range,
80- command: Command ( title: " Debug " , command: debugCommand, arguments: nil )
94+ command: Command ( title: " Debug " + targetNameToAppend , command: debugCommand, arguments: arguments )
8195 )
8296 )
8397 }
0 commit comments