|
2 | 2 | using Apollo.Contracts.Analysis; |
3 | 3 | using Apollo.Infrastructure; |
4 | 4 | using Apollo.Infrastructure.Workers; |
| 5 | +using Microsoft.CodeAnalysis.Classification; |
5 | 6 | using Microsoft.CodeAnalysis.Completion; |
6 | 7 | using Microsoft.CodeAnalysis.Tags; |
7 | 8 | using Microsoft.Extensions.Logging; |
@@ -231,23 +232,41 @@ public async Task<byte[]> GetSignatureHelpAsync(string code, string signatureHel |
231 | 232 | return Encoding.UTF8.GetBytes(JsonSerializer.Serialize(payload, _jsonOptions)); |
232 | 233 | } |
233 | 234 |
|
234 | | - public async Task<byte[]> GetQuickInfoAsync(string quickInfoRequestString) |
| 235 | + public async Task<byte[]> GetQuickInfoAsync(string code, string quickInfoRequestString) |
235 | 236 | { |
236 | | - var quickInfoRequest = JsonSerializer.Deserialize<QuickInfoRequest>(quickInfoRequestString); |
237 | | - if (quickInfoRequest == null || _quickInfoProvider == null) |
| 237 | + try |
238 | 238 | { |
239 | | - return []; |
240 | | - } |
| 239 | + using var doc = JsonDocument.Parse(quickInfoRequestString); |
| 240 | + var root = doc.RootElement; |
| 241 | + var path = root.GetProperty("FileName").GetString(); |
| 242 | + var line = root.GetProperty("Line").GetInt32(); |
| 243 | + var column = root.GetProperty("Column").GetInt32(); |
241 | 244 |
|
242 | | - var document = _projectService.GetCurrentDocument(); |
243 | | - if (document == null) |
| 245 | + if (string.IsNullOrEmpty(path) || _quickInfoProvider == null) |
| 246 | + return []; |
| 247 | + |
| 248 | + _workerLogger.LogTrace($"QuickInfo request for {path} at {line}:{column}"); |
| 249 | + _projectService.UpdateDocument(path, code); |
| 250 | + _projectService.SetCurrentDocument(path); |
| 251 | + |
| 252 | + var document = _projectService.GetDocument(path); |
| 253 | + if (document == null) |
| 254 | + return []; |
| 255 | + |
| 256 | + var sourceText = await document.GetTextAsync(); |
| 257 | + if (line < 0 || line >= sourceText.Lines.Count) |
| 258 | + return []; |
| 259 | + |
| 260 | + var quickInfoRequest = new QuickInfoRequest { FileName = path, Line = line, Column = column }; |
| 261 | + var quickInfoResponse = await _quickInfoProvider.Handle(quickInfoRequest, document); |
| 262 | + var payload = new ResponsePayload(quickInfoResponse, "GetQuickInfoAsync"); |
| 263 | + return Encoding.UTF8.GetBytes(JsonSerializer.Serialize(payload, _jsonOptions)); |
| 264 | + } |
| 265 | + catch (Exception ex) |
244 | 266 | { |
| 267 | + _workerLogger.LogError($"Error getting quick info: {ex.Message}"); |
245 | 268 | return []; |
246 | 269 | } |
247 | | - |
248 | | - var quickInfoResponse = await _quickInfoProvider.Handle(quickInfoRequest, document); |
249 | | - var payload = new ResponsePayload(quickInfoResponse, "GetQuickInfoAsync"); |
250 | | - return Encoding.UTF8.GetBytes(JsonSerializer.Serialize(payload, _jsonOptions)); |
251 | 270 | } |
252 | 271 |
|
253 | 272 | public async Task<byte[]> GetDiagnosticsAsync(string uri, Solution solution) |
@@ -385,51 +404,121 @@ public async Task<byte[]> GetSemanticTokensAsync(string requestJson) |
385 | 404 |
|
386 | 405 | _workerLogger.LogTrace($"Semantic tokens request for {request.DocumentUri}"); |
387 | 406 |
|
388 | | - // Check if this is a Razor file |
389 | 407 | var isRazorFile = request.DocumentUri.EndsWith(".razor", StringComparison.OrdinalIgnoreCase) || |
390 | 408 | request.DocumentUri.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase); |
391 | 409 |
|
392 | | - if (!isRazorFile) |
393 | | - { |
394 | | - _workerLogger.LogTrace($"Not a Razor file: {request.DocumentUri}"); |
395 | | - return Encoding.UTF8.GetBytes(JsonSerializer.Serialize( |
396 | | - new ResponsePayload(SemanticTokensResult.Empty, "GetSemanticTokensAsync"), |
397 | | - _jsonOptions)); |
398 | | - } |
| 410 | + SemanticTokensResult result; |
399 | 411 |
|
400 | | - if (_razorSemanticTokenService == null) |
| 412 | + if (isRazorFile) |
401 | 413 | { |
402 | | - _workerLogger.LogError("Razor semantic token service not initialized"); |
403 | | - return Encoding.UTF8.GetBytes(JsonSerializer.Serialize( |
404 | | - new ResponsePayload(SemanticTokensResult.Empty, "GetSemanticTokensAsync"), |
405 | | - _jsonOptions)); |
406 | | - } |
| 414 | + if (_razorSemanticTokenService == null || string.IsNullOrEmpty(request.RazorContent)) |
| 415 | + { |
| 416 | + return SerializeTokensResponse(SemanticTokensResult.Empty); |
| 417 | + } |
407 | 418 |
|
408 | | - if (string.IsNullOrEmpty(request.RazorContent)) |
| 419 | + result = await _razorSemanticTokenService.GetSemanticTokensAsync( |
| 420 | + request.RazorContent, |
| 421 | + request.DocumentUri); |
| 422 | + } |
| 423 | + else |
409 | 424 | { |
410 | | - _workerLogger.LogTrace("No Razor content provided"); |
411 | | - return Encoding.UTF8.GetBytes(JsonSerializer.Serialize( |
412 | | - new ResponsePayload(SemanticTokensResult.Empty, "GetSemanticTokensAsync"), |
413 | | - _jsonOptions)); |
| 425 | + result = await GetCSharpSemanticTokensAsync(request.DocumentUri); |
414 | 426 | } |
415 | 427 |
|
416 | | - var result = await _razorSemanticTokenService.GetSemanticTokensAsync( |
417 | | - request.RazorContent, |
418 | | - request.DocumentUri); |
419 | | - |
420 | 428 | _workerLogger.LogTrace($"Returning {result.Data.Length / 5} semantic tokens"); |
421 | | - |
422 | | - return Encoding.UTF8.GetBytes(JsonSerializer.Serialize( |
423 | | - new ResponsePayload(result, "GetSemanticTokensAsync"), |
424 | | - _jsonOptions)); |
| 429 | + return SerializeTokensResponse(result); |
425 | 430 | } |
426 | 431 | catch (Exception ex) |
427 | 432 | { |
428 | 433 | _workerLogger.LogError($"Error getting semantic tokens: {ex.Message}"); |
429 | 434 | _workerLogger.LogTrace(ex.StackTrace ?? string.Empty); |
430 | | - return Encoding.UTF8.GetBytes(JsonSerializer.Serialize( |
431 | | - new ResponsePayload(SemanticTokensResult.Empty, "GetSemanticTokensAsync"), |
432 | | - _jsonOptions)); |
| 435 | + return SerializeTokensResponse(SemanticTokensResult.Empty); |
| 436 | + } |
| 437 | + } |
| 438 | + |
| 439 | + private byte[] SerializeTokensResponse(SemanticTokensResult result) |
| 440 | + { |
| 441 | + return Encoding.UTF8.GetBytes(JsonSerializer.Serialize( |
| 442 | + new ResponsePayload(result, "GetSemanticTokensAsync"), |
| 443 | + _jsonOptions)); |
| 444 | + } |
| 445 | + |
| 446 | + private async Task<SemanticTokensResult> GetCSharpSemanticTokensAsync(string documentUri) |
| 447 | + { |
| 448 | + try |
| 449 | + { |
| 450 | + var document = _projectService.GetDocument(documentUri) |
| 451 | + ?? _projectService.GetCurrentDocument(); |
| 452 | + |
| 453 | + if (document == null) |
| 454 | + { |
| 455 | + _workerLogger.LogTrace($"No document found for C# semantic tokens: {documentUri}"); |
| 456 | + return SemanticTokensResult.Empty; |
| 457 | + } |
| 458 | + |
| 459 | + var text = await document.GetTextAsync(); |
| 460 | + var textSpan = TextSpan.FromBounds(0, text.Length); |
| 461 | + |
| 462 | + var classifiedSpans = await Classifier.GetClassifiedSpansAsync( |
| 463 | + document, textSpan); |
| 464 | + |
| 465 | + var semanticSpans = classifiedSpans |
| 466 | + .Where(s => RazorSemanticTokenService.IsCSharpSemanticClassification(s.ClassificationType)) |
| 467 | + .OrderBy(s => s.TextSpan.Start) |
| 468 | + .ToList(); |
| 469 | + |
| 470 | + if (semanticSpans.Count == 0) |
| 471 | + return SemanticTokensResult.Empty; |
| 472 | + |
| 473 | + var tokens = new int[semanticSpans.Count * 5]; |
| 474 | + var tokenIndex = 0; |
| 475 | + var previousLine = 0; |
| 476 | + var previousStartChar = 0; |
| 477 | + var previousSpanEnd = 0; |
| 478 | + |
| 479 | + foreach (var span in semanticSpans) |
| 480 | + { |
| 481 | + if (previousSpanEnd > span.TextSpan.Start) |
| 482 | + continue; |
| 483 | + |
| 484 | + var tokenType = RazorSemanticTokenService.MapCSharpClassificationToTokenType(span.ClassificationType); |
| 485 | + if (tokenType < 0) |
| 486 | + continue; |
| 487 | + |
| 488 | + var linePosition = text.Lines.GetLinePositionSpan(span.TextSpan); |
| 489 | + var line = linePosition.Start.Line; |
| 490 | + var startChar = linePosition.Start.Character; |
| 491 | + var length = span.TextSpan.Length; |
| 492 | + |
| 493 | + var deltaLine = line - previousLine; |
| 494 | + var deltaStartChar = deltaLine == 0 |
| 495 | + ? startChar - previousStartChar |
| 496 | + : startChar; |
| 497 | + |
| 498 | + tokens[tokenIndex++] = deltaLine; |
| 499 | + tokens[tokenIndex++] = deltaStartChar; |
| 500 | + tokens[tokenIndex++] = length; |
| 501 | + tokens[tokenIndex++] = tokenType; |
| 502 | + tokens[tokenIndex++] = 0; |
| 503 | + |
| 504 | + previousLine = line; |
| 505 | + previousStartChar = startChar; |
| 506 | + previousSpanEnd = span.TextSpan.End; |
| 507 | + } |
| 508 | + |
| 509 | + if (tokenIndex < tokens.Length) |
| 510 | + tokens = tokens[..tokenIndex]; |
| 511 | + |
| 512 | + return new SemanticTokensResult |
| 513 | + { |
| 514 | + Data = tokens, |
| 515 | + ResultId = Guid.NewGuid().ToString() |
| 516 | + }; |
| 517 | + } |
| 518 | + catch (Exception ex) |
| 519 | + { |
| 520 | + _workerLogger.LogError($"Error getting C# semantic tokens: {ex.Message}"); |
| 521 | + return SemanticTokensResult.Empty; |
433 | 522 | } |
434 | 523 | } |
435 | 524 |
|
|
0 commit comments