Skip to content

Skip semantic tokens tests if sourcekitd doesn't support the semantic tokens request #947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions Sources/SourceKitLSP/Swift/SemanticTokens.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ extension SwiftLanguageServer {
private func mergedAndSortedTokens(
for snapshot: DocumentSnapshot,
in range: Range<Position>? = nil
) async -> [SyntaxHighlightingToken] {
) async throws -> [SyntaxHighlightingToken] {
async let tree = syntaxTreeManager.syntaxTree(for: snapshot)
async let semanticTokens = await orLog("Loading semantic tokens") { try await semanticHighlightingTokens(for: snapshot) }
async let semanticTokens = semanticHighlightingTokens(for: snapshot)

let range =
if let range = range.flatMap({ $0.byteSourceRange(in: snapshot) }) {
Expand All @@ -64,7 +64,7 @@ extension SwiftLanguageServer {
await tree
.classifications(in: range)
.flatMap({ $0.highlightingTokens(in: snapshot) })
.mergingTokens(with: semanticTokens ?? [])
.mergingTokens(with: try semanticTokens ?? [])
.sorted { $0.start < $1.start }
}

Expand All @@ -73,7 +73,7 @@ extension SwiftLanguageServer {
) async throws -> DocumentSemanticTokensResponse? {
let snapshot = try self.documentManager.latestSnapshot(req.textDocument.uri)

let tokens = await mergedAndSortedTokens(for: snapshot)
let tokens = try await mergedAndSortedTokens(for: snapshot)
let encodedTokens = tokens.lspEncoded

return DocumentSemanticTokensResponse(data: encodedTokens)
Expand All @@ -89,8 +89,7 @@ extension SwiftLanguageServer {
_ req: DocumentSemanticTokensRangeRequest
) async throws -> DocumentSemanticTokensResponse? {
let snapshot = try self.documentManager.latestSnapshot(req.textDocument.uri)

let tokens = await mergedAndSortedTokens(for: snapshot, in: req.range)
let tokens = try await mergedAndSortedTokens(for: snapshot, in: req.range)
let encodedTokens = tokens.lspEncoded

return DocumentSemanticTokensResponse(data: encodedTokens)
Expand Down
14 changes: 12 additions & 2 deletions Tests/SourceKitLSPTests/SemanticTokensTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import LSPTestSupport
import LanguageServerProtocol
import SKTestSupport
import SourceKitLSP
import SourceKitD
import XCTest

private typealias Token = SyntaxHighlightingToken
Expand All @@ -29,7 +30,7 @@ final class SemanticTokensTests: XCTestCase {
/// - Note: This URI is set to a unique value before each test case in `setUp`.
private var uri: DocumentURI!

/// The current verion of the document being opened.
/// The current version of the document being opened.
///
/// - Note: This gets reset to 0 in `setUp` and incremented on every call to
/// `openDocument` and `editDocument`.
Expand Down Expand Up @@ -141,7 +142,16 @@ final class SemanticTokensTests: XCTestCase {
range: Range<Position>? = nil
) async throws -> [Token] {
openDocument(text: text)
return try await performSemanticTokensRequest(range: range)
do {
return try await performSemanticTokensRequest(range: range)
} catch let error as ResponseError {
// FIXME: Remove when the semantic tokens request is widely available in sourcekitd
if error.message.contains("unknown request: source.request.semantic_tokens") {
throw XCTSkip("semantic tokens request not supported by sourcekitd")
} else {
throw error
}
}
}

func testIntArrayCoding() {
Expand Down