Skip to content

Commit 82bafea

Browse files
Merge pull request #1960 from matthewbastien/improved-codelens
Add target display name to run and debug code lenses
2 parents 4151b78 + a1a92c9 commit 82bafea

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

Sources/SourceKitLSP/Swift/SwiftCodeLensScanner.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Sources/SourceKitLSP/Swift/SwiftLanguageService.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#if compiler(>=6)
1414
package import BuildSystemIntegration
15+
import BuildServerProtocol
1516
import Csourcekitd
1617
import Dispatch
1718
import Foundation
@@ -30,6 +31,7 @@ package import SwiftSyntax
3031
package import ToolchainRegistry
3132
#else
3233
import BuildSystemIntegration
34+
import BuildServerProtocol
3335
import Csourcekitd
3436
import Dispatch
3537
import Foundation
@@ -1051,9 +1053,17 @@ extension SwiftLanguageService {
10511053

10521054
package func codeLens(_ req: CodeLensRequest) async throws -> [CodeLens] {
10531055
let snapshot = try documentManager.latestSnapshot(req.textDocument.uri)
1056+
var targetDisplayName: String? = nil
1057+
if let workspace = await sourceKitLSPServer?.workspaceForDocument(uri: req.textDocument.uri),
1058+
let target = await workspace.buildSystemManager.canonicalTarget(for: req.textDocument.uri),
1059+
let buildTarget = await workspace.buildSystemManager.buildTarget(named: target)
1060+
{
1061+
targetDisplayName = buildTarget.displayName
1062+
}
10541063
return await SwiftCodeLensScanner.findCodeLenses(
10551064
in: snapshot,
10561065
syntaxTreeManager: self.syntaxTreeManager,
1066+
targetName: targetDisplayName,
10571067
supportedCommands: self.capabilityRegistry.supportedCodeLensCommands
10581068
)
10591069
}

Tests/SourceKitLSPTests/CodeLensTests.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,23 @@ final class CodeLensTests: XCTestCase {
7373

7474
let project = try await SwiftPMTestProject(
7575
files: [
76-
"Test.swift": """
76+
"Sources/MyApp/Test.swift": """
7777
1️⃣@main2️⃣
7878
struct MyApp {
7979
public static func main() {}
8080
}
8181
"""
8282
],
83+
manifest: """
84+
// swift-tools-version: 5.7
85+
86+
import PackageDescription
87+
88+
let package = Package(
89+
name: "MyApp",
90+
targets: [.executableTarget(name: "MyApp")]
91+
)
92+
""",
8393
capabilities: capabilities
8494
)
8595

@@ -94,11 +104,11 @@ final class CodeLensTests: XCTestCase {
94104
[
95105
CodeLens(
96106
range: positions["1️⃣"]..<positions["2️⃣"],
97-
command: Command(title: "Run", command: "swift.run", arguments: nil)
107+
command: Command(title: "Run MyApp", command: "swift.run", arguments: [.string("MyApp")])
98108
),
99109
CodeLens(
100110
range: positions["1️⃣"]..<positions["2️⃣"],
101-
command: Command(title: "Debug", command: "swift.debug", arguments: nil)
111+
command: Command(title: "Debug MyApp", command: "swift.debug", arguments: [.string("MyApp")])
102112
),
103113
]
104114
)

0 commit comments

Comments
 (0)