|
| 1 | +// |
| 2 | +// SemanticTokenHighlightProvider.swift |
| 3 | +// CodeEdit |
| 4 | +// |
| 5 | +// Created by Khan Winter on 12/26/24. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import LanguageServerProtocol |
| 10 | +import CodeEditSourceEditor |
| 11 | +import CodeEditTextView |
| 12 | +import CodeEditLanguages |
| 13 | + |
| 14 | +/// Provides semantic token information from a language server for a source editor view. |
| 15 | +/// |
| 16 | +/// This class works in tangent with the ``LanguageServer`` class to ensure we don't unnecessarily request new tokens |
| 17 | +/// if the document isn't updated. The ``LanguageServer`` will call the |
| 18 | +/// ``SemanticTokenHighlightProvider/documentDidChange`` method, which in turn refreshes the semantic token storage. |
| 19 | +/// |
| 20 | +/// That behavior may not be intuitive due to the |
| 21 | +/// ``SemanticTokenHighlightProvider/applyEdit(textView:range:delta:completion:)`` method. One might expect this class |
| 22 | +/// to respond to that method immediately, but it does not. It instead stores the completion passed in that method until |
| 23 | +/// it can respond to the edit with invalidated indices. |
| 24 | +final class SemanticTokenHighlightProvider< |
| 25 | + Storage: GenericSemanticTokenStorage, |
| 26 | + DocumentType: LanguageServerDocument |
| 27 | +>: HighlightProviding { |
| 28 | + enum HighlightError: Error { |
| 29 | + case lspRangeFailure |
| 30 | + } |
| 31 | + |
| 32 | + typealias EditCallback = @MainActor (Result<IndexSet, any Error>) -> Void |
| 33 | + typealias HighlightCallback = @MainActor (Result<[HighlightRange], any Error>) -> Void |
| 34 | + |
| 35 | + private let tokenMap: SemanticTokenMap |
| 36 | + private let documentURI: String |
| 37 | + private weak var languageServer: LanguageServer<DocumentType>? |
| 38 | + private weak var textView: TextView? |
| 39 | + |
| 40 | + private var lastEditCallback: EditCallback? |
| 41 | + private var pendingHighlightCallbacks: [HighlightCallback] = [] |
| 42 | + private var storage: Storage |
| 43 | + |
| 44 | + var documentRange: NSRange { |
| 45 | + textView?.documentRange ?? .zero |
| 46 | + } |
| 47 | + |
| 48 | + init(tokenMap: SemanticTokenMap, languageServer: LanguageServer<DocumentType>, documentURI: String) { |
| 49 | + self.tokenMap = tokenMap |
| 50 | + self.languageServer = languageServer |
| 51 | + self.documentURI = documentURI |
| 52 | + self.storage = Storage() |
| 53 | + } |
| 54 | + |
| 55 | + // MARK: - Language Server Content Lifecycle |
| 56 | + |
| 57 | + /// Called when the language server finishes sending a document update. |
| 58 | + /// |
| 59 | + /// This method first checks if this object has any semantic tokens. If not, requests new tokens and responds to the |
| 60 | + /// `pendingHighlightCallbacks` queue with cancellation errors, causing the highlighter to re-query those indices. |
| 61 | + /// |
| 62 | + /// If this object already has some tokens, it determines whether or not we can request a token delta and |
| 63 | + /// performs the request. |
| 64 | + func documentDidChange() async throws { |
| 65 | + guard let languageServer, let textView else { |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + guard storage.hasReceivedData else { |
| 70 | + // We have no semantic token info, request it! |
| 71 | + try await requestTokens(languageServer: languageServer, textView: textView) |
| 72 | + await MainActor.run { |
| 73 | + for callback in pendingHighlightCallbacks { |
| 74 | + callback(.failure(HighlightProvidingError.operationCancelled)) |
| 75 | + } |
| 76 | + pendingHighlightCallbacks.removeAll() |
| 77 | + } |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + // The document was updated. Update our token cache and send the invalidated ranges for the editor to handle. |
| 82 | + if let lastResultId = storage.lastResultId { |
| 83 | + try await requestDeltaTokens(languageServer: languageServer, textView: textView, lastResultId: lastResultId) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + try await requestTokens(languageServer: languageServer, textView: textView) |
| 88 | + } |
| 89 | + |
| 90 | + // MARK: - LSP Token Requests |
| 91 | + |
| 92 | + /// Requests and applies a token delta. Requires a previous response identifier. |
| 93 | + private func requestDeltaTokens( |
| 94 | + languageServer: LanguageServer<DocumentType>, |
| 95 | + textView: TextView, |
| 96 | + lastResultId: String |
| 97 | + ) async throws { |
| 98 | + guard let response = try await languageServer.requestSemanticTokens( |
| 99 | + for: documentURI, |
| 100 | + previousResultId: lastResultId |
| 101 | + ) else { |
| 102 | + return |
| 103 | + } |
| 104 | + switch response { |
| 105 | + case let .optionA(tokenData): |
| 106 | + await applyEntireResponse(tokenData, callback: lastEditCallback) |
| 107 | + case let .optionB(deltaData): |
| 108 | + await applyDeltaResponse(deltaData, callback: lastEditCallback, textView: textView) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /// Requests and applies tokens for an entire document. This does not require a previous response id, and should be |
| 113 | + /// used in place of `requestDeltaTokens` when that's the case. |
| 114 | + private func requestTokens(languageServer: LanguageServer<DocumentType>, textView: TextView) async throws { |
| 115 | + guard let response = try await languageServer.requestSemanticTokens(for: documentURI) else { |
| 116 | + return |
| 117 | + } |
| 118 | + await applyEntireResponse(response, callback: lastEditCallback) |
| 119 | + } |
| 120 | + |
| 121 | + // MARK: - Apply LSP Response |
| 122 | + |
| 123 | + /// Applies a delta response from the LSP to our storage. |
| 124 | + private func applyDeltaResponse(_ data: SemanticTokensDelta, callback: EditCallback?, textView: TextView?) async { |
| 125 | + let lspRanges = storage.applyDelta(data) |
| 126 | + lastEditCallback = nil // Don't use this callback again. |
| 127 | + await MainActor.run { |
| 128 | + let ranges = lspRanges.compactMap { textView?.nsRangeFrom($0) } |
| 129 | + callback?(.success(IndexSet(ranges: ranges))) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private func applyEntireResponse(_ data: SemanticTokens, callback: EditCallback?) async { |
| 134 | + storage.setData(data) |
| 135 | + lastEditCallback = nil // Don't use this callback again. |
| 136 | + await callback?(.success(IndexSet(integersIn: documentRange))) |
| 137 | + } |
| 138 | + |
| 139 | + // MARK: - Highlight Provider Conformance |
| 140 | + |
| 141 | + func setUp(textView: TextView, codeLanguage: CodeLanguage) { |
| 142 | + // Send off a request to get the initial token data |
| 143 | + self.textView = textView |
| 144 | + Task { |
| 145 | + try await self.documentDidChange() |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + func applyEdit(textView: TextView, range: NSRange, delta: Int, completion: @escaping EditCallback) { |
| 150 | + if let lastEditCallback { |
| 151 | + lastEditCallback(.success(IndexSet())) // Don't throw a cancellation error |
| 152 | + } |
| 153 | + lastEditCallback = completion |
| 154 | + } |
| 155 | + |
| 156 | + func queryHighlightsFor(textView: TextView, range: NSRange, completion: @escaping HighlightCallback) { |
| 157 | + guard storage.hasReceivedData else { |
| 158 | + pendingHighlightCallbacks.append(completion) |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + guard let lspRange = textView.lspRangeFrom(nsRange: range) else { |
| 163 | + completion(.failure(HighlightError.lspRangeFailure)) |
| 164 | + return |
| 165 | + } |
| 166 | + let rawTokens = storage.getTokensFor(range: lspRange) |
| 167 | + let highlights = tokenMap |
| 168 | + .decode(tokens: rawTokens, using: textView) |
| 169 | + .filter({ $0.capture != nil || !$0.modifiers.isEmpty }) |
| 170 | + completion(.success(highlights)) |
| 171 | + } |
| 172 | +} |
0 commit comments