public override SemanticTokensOrSemanticTokensEdits GetSemanticTokensEdits(RazorCodeDocument codeDocument, string previousResultId)
        {
            if (codeDocument is null)
            {
                throw new ArgumentNullException(nameof(codeDocument));
            }

            var semanticRanges = TagHelperSemanticRangeVisitor.VisitAllNodes(codeDocument);

            IReadOnlyList <uint> previousResults = null;

            if (previousResultId != null)
            {
                _semanticTokensCache.TryGetValue(previousResultId, out previousResults);
            }
            var newTokens = ConvertSemanticRangesToSemanticTokens(semanticRanges, codeDocument);

            if (previousResults is null)
            {
                return(newTokens);
            }

            var semanticEdits = SemanticTokensEditsDiffer.ComputeSemanticTokensEdits(newTokens, previousResults);

            return(semanticEdits);
        }
コード例 #2
0
        public static IReadOnlyList <SemanticRange> VisitAllNodes(RazorCodeDocument razorCodeDocument, Range range = null)
        {
            var visitor = new TagHelperSemanticRangeVisitor(razorCodeDocument, range);

            visitor.Visit(razorCodeDocument.GetSyntaxTree().Root);

            return(visitor._semanticRanges);
        }
コード例 #3
0
        public override SemanticTokens GetSemanticTokens(RazorCodeDocument codeDocument, Range range)
        {
            if (codeDocument is null)
            {
                throw new ArgumentNullException(nameof(codeDocument));
            }

            var semanticRanges = TagHelperSemanticRangeVisitor.VisitAllNodes(codeDocument, range);

            var semanticTokens = ConvertSemanticRangesToSemanticTokens(semanticRanges, codeDocument);

            return(semanticTokens);
        }
コード例 #4
0
        public static IReadOnlyList <SemanticRange> VisitAllNodes(RazorCodeDocument razorCodeDocument, Range?range = null)
        {
            TextSpan?rangeAsTextSpan = null;

            if (range is not null)
            {
                var sourceText = razorCodeDocument.GetSourceText();
                rangeAsTextSpan = range.AsRazorTextSpan(sourceText);
            }

            var visitor = new TagHelperSemanticRangeVisitor(razorCodeDocument, rangeAsTextSpan);

            visitor.Visit(razorCodeDocument.GetSyntaxTree().Root);

            return(visitor._semanticRanges);
        }
        // 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);
        }
コード例 #6
0
        public override SemanticTokensOrSemanticTokensEdits GetSemanticTokensEdits(RazorCodeDocument codeDocument, string previousResultId)
        {
            if (codeDocument is null)
            {
                throw new ArgumentNullException(nameof(codeDocument));
            }

            var semanticRanges = TagHelperSemanticRangeVisitor.VisitAllNodes(codeDocument);

            var previousResults = previousResultId is null ? null : _semanticTokensCache.Get(previousResultId);
            var newTokens       = ConvertSemanticRangesToSemanticTokens(semanticRanges, codeDocument);

            if (previousResults is null)
            {
                return(newTokens);
            }

            var semanticEdits = SemanticTokensEditsDiffer.ComputeSemanticTokensEdits(newTokens, previousResults);

            return(semanticEdits);
        }