@@ -21,11 +21,18 @@ final class SwiftCodeLensScanner: SyntaxVisitor {
21
21
/// The collection of CodeLenses found in the document.
22
22
private var result : [ CodeLens ] = [ ]
23
23
24
+ private let targetName : String ?
25
+
24
26
/// The map of supported commands and their client side command names
25
27
private let supportedCommands : [ SupportedCodeLensCommand : String ]
26
28
27
- private init ( snapshot: DocumentSnapshot , supportedCommands: [ SupportedCodeLensCommand : String ] ) {
29
+ private init (
30
+ snapshot: DocumentSnapshot ,
31
+ targetName: String ? ,
32
+ supportedCommands: [ SupportedCodeLensCommand : String ]
33
+ ) {
28
34
self . snapshot = snapshot
35
+ self . targetName = targetName
29
36
self . supportedCommands = supportedCommands
30
37
super. init ( viewMode: . fixedUp)
31
38
}
@@ -35,6 +42,7 @@ final class SwiftCodeLensScanner: SyntaxVisitor {
35
42
public static func findCodeLenses(
36
43
in snapshot: DocumentSnapshot ,
37
44
syntaxTreeManager: SyntaxTreeManager ,
45
+ targetName: String ? = nil ,
38
46
supportedCommands: [ SupportedCodeLensCommand : String ]
39
47
) async -> [ CodeLens ] {
40
48
guard snapshot. text. contains ( " @main " ) && !supportedCommands. isEmpty else {
@@ -43,7 +51,7 @@ final class SwiftCodeLensScanner: SyntaxVisitor {
43
51
}
44
52
45
53
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)
47
55
visitor. walk ( syntaxTree)
48
56
return visitor. result
49
57
}
@@ -61,14 +69,20 @@ final class SwiftCodeLensScanner: SyntaxVisitor {
61
69
private func captureLensFromAttribute( attribute: AttributeListSyntax . Element ) {
62
70
if attribute. trimmedDescription == " @main " {
63
71
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
+ }
64
78
65
79
if let runCommand = supportedCommands [ SupportedCodeLensCommand . run] {
66
80
// Return commands for running/debugging the executable.
67
81
// These command names must be recognized by the client and so should not be chosen arbitrarily.
68
82
self . result. append (
69
83
CodeLens (
70
84
range: range,
71
- command: Command ( title: " Run " , command: runCommand, arguments: nil )
85
+ command: Command ( title: " Run " + targetNameToAppend , command: runCommand, arguments: arguments )
72
86
)
73
87
)
74
88
}
@@ -77,7 +91,7 @@ final class SwiftCodeLensScanner: SyntaxVisitor {
77
91
self . result. append (
78
92
CodeLens (
79
93
range: range,
80
- command: Command ( title: " Debug " , command: debugCommand, arguments: nil )
94
+ command: Command ( title: " Debug " + targetNameToAppend , command: debugCommand, arguments: arguments )
81
95
)
82
96
)
83
97
}
0 commit comments