Skip to content

Commit 74404ad

Browse files
authored
Merge pull request #1080 from ahoppen/ahoppen/semantic-tokens-fallback
If sourcekitd doesn't support the semantic tokens request, return results from swift-syntax
2 parents 05c8968 + 3d82a89 commit 74404ad

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

Sources/SKTestSupport/SkipUnless.swift

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
import Foundation
1414
import LSPLogging
15+
import LSPTestSupport
1516
import LanguageServerProtocol
1617
import RegexBuilder
1718
@_spi(Testing) import SKCore
19+
import SourceKitLSP
1820
import XCTest
1921

2022
import enum PackageLoading.Platform
@@ -105,13 +107,26 @@ public enum SkipUnless {
105107
) {
106108
let testClient = try await TestSourceKitLSPClient()
107109
let uri = DocumentURI.for(.swift)
108-
testClient.openDocument("func test() {}", uri: uri)
109-
do {
110-
_ = try await testClient.send(DocumentSemanticTokensRequest(textDocument: TextDocumentIdentifier(uri)))
111-
} catch let error as ResponseError {
112-
return !error.message.contains("unknown request: source.request.semantic_tokens")
113-
}
114-
return true
110+
testClient.openDocument("0.bitPattern", uri: uri)
111+
let response = try unwrap(
112+
await testClient.send(DocumentSemanticTokensRequest(textDocument: TextDocumentIdentifier(uri)))
113+
)
114+
let tokens = [SyntaxHighlightingToken](lspEncodedTokens: response.data)
115+
116+
// If we don't have semantic token support in sourcekitd, the second token is an identifier based on the syntax
117+
// tree, not a property.
118+
return tokens != [
119+
SyntaxHighlightingToken(
120+
range: Position(line: 0, utf16index: 0)..<Position(line: 0, utf16index: 1),
121+
kind: .number,
122+
modifiers: []
123+
),
124+
SourceKitLSP.SyntaxHighlightingToken(
125+
range: Position(line: 0, utf16index: 2)..<Position(line: 0, utf16index: 12),
126+
kind: .identifier,
127+
modifiers: []
128+
),
129+
]
115130
}
116131
}
117132

Sources/SourceKitLSP/Swift/SemanticTokens.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extension SwiftLanguageServer {
5151
in range: Range<Position>? = nil
5252
) async throws -> [SyntaxHighlightingToken] {
5353
async let tree = syntaxTreeManager.syntaxTree(for: snapshot)
54-
async let semanticTokens = semanticHighlightingTokens(for: snapshot)
54+
let semanticTokens = await orLog("Loading semantic tokens") { try await semanticHighlightingTokens(for: snapshot) }
5555

5656
let range =
5757
if let range = range.flatMap({ $0.byteSourceRange(in: snapshot) }) {
@@ -63,7 +63,7 @@ extension SwiftLanguageServer {
6363
await tree
6464
.classifications(in: range)
6565
.flatMap({ $0.highlightingTokens(in: snapshot) })
66-
.mergingTokens(with: try semanticTokens ?? [])
66+
.mergingTokens(with: semanticTokens ?? [])
6767
.sorted { $0.start < $1.start }
6868
}
6969

0 commit comments

Comments
 (0)