internal async Task <ExcerptResultInternal?> TryGetExcerptInternalAsync(
            Document document,
            TextSpan span,
            ExcerptModeInternal mode,
            SourceText razorDocumentText,
            LinePositionSpan mappedLinePosition,
            RazorClassificationOptionsWrapper options,
            CancellationToken cancellationToken)
        {
            var razorDocumentSpan = razorDocumentText.Lines.GetTextSpan(mappedLinePosition);

            var generatedDocument = document;

            // First compute the range of text we want to we to display relative to the razor document.
            var excerptSpan = ChooseExcerptSpan(razorDocumentText, razorDocumentSpan, mode);

            // Then we'll classify the spans based on the razor document, since that's the coordinate
            // space that our output mappings use.
            var classifiedSpans = await ClassifyPreviewAsync(
                razorDocumentSpan,
                excerptSpan,
                span,
                generatedDocument,
                options,
                cancellationToken).ConfigureAwait(false);

            var excerptText = GetTranslatedExcerptText(razorDocumentText, ref razorDocumentSpan, ref excerptSpan, classifiedSpans);

            return(new ExcerptResultInternal(excerptText, razorDocumentSpan, classifiedSpans.ToImmutable(), document, span));
        }
Example #2
0
        internal override async Task <ExcerptResultInternal?> TryGetExcerptInternalAsync(
            Document document,
            TextSpan span,
            ExcerptModeInternal mode,
            RazorClassificationOptionsWrapper options,
            CancellationToken cancellationToken)
        {
            if (_document is null)
            {
                return(null);
            }

            var mappedSpans = await _mappingService.MapSpansAsync(document, new[] { span }, cancellationToken).ConfigureAwait(false);

            if (mappedSpans.Length == 0 || mappedSpans[0].Equals(default(RazorMappedSpanResult)))
            {
                return(null);
            }

            var project       = _document.Project;
            var razorDocument = project.GetDocument(mappedSpans[0].FilePath);

            if (razorDocument is null)
            {
                return(null);
            }

            var razorDocumentText = await razorDocument.GetTextAsync().ConfigureAwait(false);

            var razorDocumentSpan = razorDocumentText.Lines.GetTextSpan(mappedSpans[0].LinePositionSpan);

            var generatedDocument = document;

            // First compute the range of text we want to we to display relative to the primary document.
            var excerptSpan = ChooseExcerptSpan(razorDocumentText, razorDocumentSpan, mode);

            // Then we'll classify the spans based on the primary document, since that's the coordinate
            // space that our output mappings use.
            var output = await _document.GetGeneratedOutputAsync().ConfigureAwait(false);

            var mappings        = output.GetCSharpDocument().SourceMappings;
            var classifiedSpans = await ClassifyPreviewAsync(
                excerptSpan,
                generatedDocument,
                mappings,
                options,
                cancellationToken).ConfigureAwait(false);

            var excerptText = GetTranslatedExcerptText(razorDocumentText, ref razorDocumentSpan, ref excerptSpan, classifiedSpans);

            return(new ExcerptResultInternal(excerptText, razorDocumentSpan, classifiedSpans.ToImmutable(), document, span));
        }
Example #3
0
        protected TextSpan ChooseExcerptSpan(SourceText text, TextSpan span, ExcerptModeInternal mode)
        {
            var startLine = text.Lines.GetLineFromPosition(span.Start);
            var endLine   = text.Lines.GetLineFromPosition(span.End);

            // If we're showing a single line then this will do. Otherwise expand the range by 3 in
            // each direction (if possible).
            if (mode == ExcerptModeInternal.Tooltip)
            {
                var startIndex = Math.Max(startLine.LineNumber - 3, 0);
                startLine = text.Lines[startIndex];

                var endIndex = Math.Min(endLine.LineNumber + 3, text.Lines.Count - 1);
                endLine = text.Lines[endIndex];
            }

            return(new TextSpan(startLine.Start, endLine.End - startLine.Start));
        }
        internal override async Task <ExcerptResultInternal?> TryGetExcerptInternalAsync(
            Document document,
            TextSpan span,
            ExcerptModeInternal mode,
            CancellationToken cancellationToken)
        {
            var mappedSpans = await _mappingService.MapSpansAsync(document, new[] { span }, cancellationToken).ConfigureAwait(false);

            if (mappedSpans.Length == 0 || mappedSpans[0].Equals(default(RazorMappedSpanResult)))
            {
                return(null);
            }

            return(await TryGetExcerptInternalAsync(
                       document,
                       span,
                       mode,
                       _documentSnapshot.Snapshot.AsText(),
                       mappedSpans[0].LinePositionSpan,
                       cancellationToken).ConfigureAwait(false));
        }
Example #5
0
        protected TextSpan ChooseExcerptSpan(SourceText text, TextSpan span, ExcerptModeInternal mode)
        {
            var startLine = text.Lines.GetLineFromPosition(span.Start);
            var endLine   = text.Lines.GetLineFromPosition(span.End);

            if (mode == ExcerptModeInternal.Tooltip)
            {
                // Expand the range by 3 in each direction (if possible).
                var startIndex = Math.Max(startLine.LineNumber - 3, 0);
                startLine = text.Lines[startIndex];

                var endIndex = Math.Min(endLine.LineNumber + 3, text.Lines.Count - 1);
                endLine = text.Lines[endIndex];
                return(CreateTextSpan(startLine, endLine));
            }
            else
            {
                // Trim leading whitespace in a single line excerpt
                var excerptSpan        = CreateTextSpan(startLine, endLine);
                var trimmedExcerptSpan = excerptSpan.TrimLeadingWhitespace(text);
                return(trimmedExcerptSpan);
            }
Example #6
0
 internal abstract Task <ExcerptResultInternal?> TryGetExcerptInternalAsync(
     Document document,
     TextSpan span,
     ExcerptModeInternal mode,
     CancellationToken cancellationToken);
        public async Task <ExcerptResultInternal?> TryGetExcerptInternalAsync(
            Document document,
            TextSpan span,
            ExcerptModeInternal mode,
            CancellationToken cancellationToken)
        {
            if (_document == null)
            {
                return(null);
            }

            var mapped = await _mapper.MapSpansAsync(document, new[] { span }, cancellationToken).ConfigureAwait(false);

            if (mapped.Length == 0 || mapped[0].Equals(default(RazorMappedSpanResult)))
            {
                return(null);
            }

            var project         = _document.Project;
            var primaryDocument = project.GetDocument(mapped[0].FilePath);

            if (primaryDocument == null)
            {
                return(null);
            }

            var primaryText = await primaryDocument.GetTextAsync().ConfigureAwait(false);

            var primarySpan = primaryText.Lines.GetTextSpan(mapped[0].LinePositionSpan);

            var secondaryDocument = document;
            var secondarySpan     = span;

            // First compute the range of text we want to we to display relative to the primary document.
            var excerptSpan = ChooseExcerptSpan(primaryText, primarySpan, mode);

            // Then we'll classify the spans based on the primary document, since that's the coordinate
            // space that our output mappings use.
            var output = await _document.GetGeneratedOutputAsync().ConfigureAwait(false);

            var mappings        = output.GetCSharpDocument().SourceMappings;
            var classifiedSpans = await ClassifyPreviewAsync(
                primaryText,
                excerptSpan,
                secondaryDocument,
                mappings,
                cancellationToken).ConfigureAwait(false);

            // Now translate everything to be relative to the excerpt
            var offset      = 0 - excerptSpan.Start;
            var excerptText = primaryText.GetSubText(excerptSpan);

            excerptSpan = new TextSpan(0, excerptSpan.Length);
            primarySpan = new TextSpan(primarySpan.Start + offset, primarySpan.Length);

            for (var i = 0; i < classifiedSpans.Count; i++)
            {
                var classifiedSpan = classifiedSpans[i];
                var updated        = new TextSpan(classifiedSpan.TextSpan.Start + offset, classifiedSpan.TextSpan.Length);
                Debug.Assert(excerptSpan.Contains(updated));

                classifiedSpans[i] = new ClassifiedSpan(classifiedSpan.ClassificationType, updated);
            }

            return(new ExcerptResultInternal(excerptText, primarySpan, classifiedSpans.ToImmutable(), document, span));
        }
Example #8
0
 internal abstract Task <ExcerptResultInternal?> TryGetExcerptInternalAsync(
     Document document,
     TextSpan span,
     ExcerptModeInternal mode,
     RazorClassificationOptionsWrapper options,
     CancellationToken cancellationToken);