Skip to content

Commit c5c605d

Browse files
authored
Merge pull request #912 from ahoppen/ahoppen/semantic-tokens-request
Load semantic tokens from a document using a sourcekitd request instead of the 0,0 edit
2 parents 83519ea + 1d10f82 commit c5c605d

File tree

9 files changed

+46
-301
lines changed

9 files changed

+46
-301
lines changed

Sources/SourceKitD/sourcekitd_uids.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public struct sourcekitd_keys {
5959
public let request: sourcekitd_uid_t
6060
public let results: sourcekitd_uid_t
6161
public let retrieve_refactor_actions: sourcekitd_uid_t
62+
public let semantic_tokens: sourcekitd_uid_t
6263
public let severity: sourcekitd_uid_t
6364
public let sourcefile: sourcekitd_uid_t
6465
public let sourcetext: sourcekitd_uid_t
@@ -135,6 +136,7 @@ public struct sourcekitd_keys {
135136
request = api.uid_get_from_cstr("key.request")!
136137
results = api.uid_get_from_cstr("key.results")!
137138
retrieve_refactor_actions = api.uid_get_from_cstr("key.retrieve_refactor_actions")!
139+
semantic_tokens = api.uid_get_from_cstr("key.semantic_tokens")!
138140
severity = api.uid_get_from_cstr("key.severity")!
139141
sourcefile = api.uid_get_from_cstr("key.sourcefile")!
140142
sourcetext = api.uid_get_from_cstr("key.sourcetext")!
@@ -178,6 +180,7 @@ public struct sourcekitd_requests {
178180
public let codecomplete_close: sourcekitd_uid_t
179181
public let cursorinfo: sourcekitd_uid_t
180182
public let diagnostics: sourcekitd_uid_t
183+
public let semantic_tokens: sourcekitd_uid_t
181184
public let expression_type: sourcekitd_uid_t
182185
public let find_usr: sourcekitd_uid_t
183186
public let variable_type: sourcekitd_uid_t
@@ -196,6 +199,7 @@ public struct sourcekitd_requests {
196199
codecomplete_close = api.uid_get_from_cstr("source.request.codecomplete.close")!
197200
cursorinfo = api.uid_get_from_cstr("source.request.cursorinfo")!
198201
diagnostics = api.uid_get_from_cstr("source.request.diagnostics")!
202+
semantic_tokens = api.uid_get_from_cstr("source.request.semantic_tokens")!
199203
expression_type = api.uid_get_from_cstr("source.request.expression.type")!
200204
find_usr = api.uid_get_from_cstr("source.request.editor.find_usr")!
201205
variable_type = api.uid_get_from_cstr("source.request.variable.type")!

Sources/SourceKitLSP/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ add_library(SourceKitLSP STATIC
33
CapabilityRegistry.swift
44
DocumentManager.swift
55
IndexStoreDB+MainFilesProvider.swift
6-
RangeAdjuster.swift
76
ResponseError+Init.swift
87
Sequence+AsyncMap.swift
98
SourceKitIndexDelegate.swift
@@ -25,7 +24,6 @@ target_sources(SourceKitLSP PRIVATE
2524
Swift/SemanticRefactorCommand.swift
2625
Swift/SemanticRefactoring.swift
2726
Swift/SemanticTokens.swift
28-
Swift/SemanticTokensManager.swift
2927
Swift/SourceKitD+ResponseError.swift
3028
Swift/SwiftCommand.swift
3129
Swift/SwiftLanguageServer.swift

Sources/SourceKitLSP/CapabilityRegistry.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ public final class CapabilityRegistry {
7575
clientCapabilities.workspace?.didChangeWatchedFiles?.dynamicRegistration == true
7676
}
7777

78-
public var clientHasSemanticTokenRefreshSupport: Bool {
79-
clientCapabilities.workspace?.semanticTokens?.refreshSupport == true
80-
}
81-
8278
public var clientHasDiagnosticsCodeDescriptionSupport: Bool {
8379
clientCapabilities.textDocument?.publishDiagnostics?.codeDescriptionSupport == true
8480
}

Sources/SourceKitLSP/RangeAdjuster.swift

Lines changed: 0 additions & 71 deletions
This file was deleted.

Sources/SourceKitLSP/Swift/SemanticTokens.swift

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,33 @@
1212

1313
import LSPLogging
1414
import LanguageServerProtocol
15+
import SourceKitD
1516
import SwiftIDEUtils
1617
import SwiftParser
1718
import SwiftSyntax
1819

1920
extension SwiftLanguageServer {
21+
/// Requests the semantic highlighting tokens for the given snapshot from sourcekitd.
22+
private func semanticHighlightingTokens(for snapshot: DocumentSnapshot) async throws -> [SyntaxHighlightingToken]? {
23+
guard let buildSettings = await self.buildSettings(for: snapshot.uri), !buildSettings.isFallback else {
24+
return nil
25+
}
26+
27+
let skreq = SKDRequestDictionary(sourcekitd: self.sourcekitd)
28+
skreq[keys.request] = requests.semantic_tokens
29+
skreq[keys.sourcefile] = snapshot.uri.pseudoPath
30+
31+
// FIXME: SourceKit should probably cache this for us.
32+
skreq[keys.compilerargs] = buildSettings.compilerArgs
33+
34+
let dict = try await sourcekitd.send(skreq)
35+
36+
guard let skTokens: SKDResponseArray = dict[keys.semantic_tokens] else {
37+
return nil
38+
}
39+
return SyntaxHighlightingTokenParser(sourcekitd: sourcekitd).parseTokens(skTokens, in: snapshot)
40+
}
41+
2042
/// Computes an array of syntax highlighting tokens from the syntax tree that
2143
/// have been merged with any semantic tokens from SourceKit. If the provided
2244
/// range is non-empty, this function restricts its output to only those
@@ -29,13 +51,17 @@ extension SwiftLanguageServer {
2951
for snapshot: DocumentSnapshot,
3052
in range: Range<Position>? = nil
3153
) async -> [SyntaxHighlightingToken] {
32-
let tree = await syntaxTreeManager.syntaxTree(for: snapshot)
33-
let semanticTokens = await semanticTokensManager.semanticTokens(for: snapshot.id)
54+
async let tree = syntaxTreeManager.syntaxTree(for: snapshot)
55+
async let semanticTokens = await orLog { try await semanticHighlightingTokens(for: snapshot) }
56+
3457
let range =
35-
range.flatMap({ $0.byteSourceRange(in: snapshot) })
36-
?? ByteSourceRange(offset: 0, length: tree.totalLength.utf8Length)
58+
if let range = range.flatMap({ $0.byteSourceRange(in: snapshot) }) {
59+
range
60+
} else {
61+
ByteSourceRange(offset: 0, length: await tree.totalLength.utf8Length)
62+
}
3763
return
38-
tree
64+
await tree
3965
.classifications(in: range)
4066
.flatMap({ $0.highlightingTokens(in: snapshot) })
4167
.mergingTokens(with: semanticTokens ?? [])

Sources/SourceKitLSP/Swift/SemanticTokensManager.swift

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)