Beispiel #1
0
        private static SemanticTokens ConvertSemanticRangesToSemanticTokens(
            IReadOnlyList <SemanticRange> semanticRanges,
            RazorCodeDocument razorCodeDocument)
        {
            if (semanticRanges is null)
            {
                return(null);
            }

            SemanticRange previousResult = null;

            var data = new List <uint>();

            foreach (var result in semanticRanges)
            {
                var newData = GetData(result, previousResult, razorCodeDocument);
                data.AddRange(newData);

                previousResult = result;
            }

            var resultId = Guid.NewGuid();

            var tokensResult = new SemanticTokens
            {
                Data     = data.ToArray(),
                ResultId = resultId.ToString()
            };

            _semanticTokensCache.Set(resultId.ToString(), data);

            return(tokensResult);
        }
        // Internal for benchmarks
        internal async Task <SemanticTokens?> GetSemanticTokensAsync(
            TextDocumentIdentifier textDocumentIdentifier,
            Range range,
            DocumentSnapshot documentSnapshot,
            int documentVersion,
            CancellationToken cancellationToken)
        {
            var codeDocument = await GetRazorCodeDocumentAsync(documentSnapshot);

            if (codeDocument is null)
            {
                throw new ArgumentNullException(nameof(codeDocument));
            }

            cancellationToken.ThrowIfCancellationRequested();
            var razorSemanticRanges = TagHelperSemanticRangeVisitor.VisitAllNodes(codeDocument, range);
            IReadOnlyList <SemanticRange>?csharpSemanticRanges = null;

            try
            {
                csharpSemanticRanges = await GetCSharpSemanticRangesAsync(
                    codeDocument, textDocumentIdentifier, range, documentVersion, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error thrown while retrieving CSharp semantic range");
            }

            var combinedSemanticRanges = CombineSemanticRanges(razorSemanticRanges, csharpSemanticRanges);

            // We return null when we have an incomplete view of the document.
            // Likely CSharp ahead of us in terms of document versions.
            // We return null (which to the LSP is a no-op) to prevent flashing of CSharp elements.
            if (combinedSemanticRanges is null)
            {
                _logger.LogWarning("Incomplete view of document. C# may be ahead of us in document versions.");
                return(null);
            }

            var data   = ConvertSemanticRangesToSemanticTokensData(combinedSemanticRanges, codeDocument);
            var tokens = new SemanticTokens {
                Data = data
            };

            return(tokens);
        }
 private record SemanticTokensCacheResponse(VersionStamp SemanticVersion, Range Range, SemanticTokens SemanticTokens);
 public ProvideSemanticTokensResponse(SemanticTokens result, long?hostDocumentSyncVersion)
 {
     Result = result;
     HostDocumentSyncVersion = hostDocumentSyncVersion;
 }