| 1 | using System.Text.Json; |
| 2 | using System.Text.Json.Nodes; |
| 3 | using CascadeIDE.Features.Editor.Application.Monaco; |
| 4 | using CascadeIDE.Services.Lsp; |
| 5 | |
| 6 | #nullable enable |
| 7 | |
| 8 | namespace CascadeIDE.Features.Lsp.DataAcquisition; |
| 9 | |
| 10 | /// <summary>LSP <c>textDocument/semanticTokens</c> (ADR 0163 M8 stretch).</summary> |
| 11 | public sealed partial class CSharpLspDiagnosticsHost |
| 12 | { |
| 13 | private CideEditorSemanticTokensLegend? _semanticLegend; |
| 14 | private volatile bool _supportsSemanticTokens; |
| 15 | |
| 16 | public bool SupportsSemanticTokens => _supportsSemanticTokens && _semanticLegend is not null; |
| 17 | |
| 18 | public CideEditorSemanticTokensLegend? SemanticLegend => _semanticLegend; |
| 19 | |
| 20 | internal void ApplyServerCapabilities(JsonElement initializeResult) |
| 21 | { |
| 22 | _semanticLegend = CSharpLspSemanticTokensParser.TryParseLegend(initializeResult); |
| 23 | _supportsSemanticTokens = _semanticLegend is not null; |
| 24 | } |
| 25 | |
| 26 | private void ClearSemanticTokensState() |
| 27 | { |
| 28 | _semanticLegend = null; |
| 29 | _supportsSemanticTokens = false; |
| 30 | } |
| 31 | |
| 32 | public async Task<CideEditorSemanticTokensData?> RequestSemanticTokensFullAsync( |
| 33 | string filePath, |
| 34 | string text, |
| 35 | CancellationToken ct) |
| 36 | { |
| 37 | if (!SupportsSemanticTokens || !IsCSharpPath(filePath) || _session is null) |
| 38 | return null; |
| 39 | |
| 40 | await SyncFullTextForRequestAsync(filePath, text, ct).ConfigureAwait(false); |
| 41 | var uri = LspFileUri.PathToFileUri(CanonicalFilePath.Normalize(filePath)); |
| 42 | var id = _session.AllocateRequestId(); |
| 43 | var msg = new JsonObject |
| 44 | { |
| 45 | ["jsonrpc"] = "2.0", |
| 46 | ["id"] = id, |
| 47 | ["method"] = "textDocument/semanticTokens/full", |
| 48 | ["params"] = new JsonObject |
| 49 | { |
| 50 | ["textDocument"] = new JsonObject { ["uri"] = uri }, |
| 51 | }, |
| 52 | }; |
| 53 | |
| 54 | try |
| 55 | { |
| 56 | using var doc = await _session.SendRequestAsync(msg, id, TimeSpan.FromSeconds(15), ct).ConfigureAwait(false); |
| 57 | return CSharpLspSemanticTokensParser.TryParseFullResponse(doc); |
| 58 | } |
| 59 | catch |
| 60 | { |
| 61 | return null; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |