private async Task <(ExcerptResult, SourceText)> ExcerptAsync(SourceText sourceText, DocumentSpan documentSpan) { var excerptService = documentSpan.Document.Services.GetService <IDocumentExcerptService>(); if (excerptService != null) { var result = await excerptService.TryExcerptAsync(documentSpan.Document, documentSpan.SourceSpan, ExcerptMode.SingleLine, CancellationToken).ConfigureAwait(false); if (result != null) { return(result.Value, AbstractDocumentSpanEntry.GetLineContainingPosition(result.Value.Content, result.Value.MappedSpan.Start)); } } var classificationResult = await ClassifiedSpansAndHighlightSpanFactory.ClassifyAsync(documentSpan, CancellationToken).ConfigureAwait(false); // need to fix the span issue tracking here - https://github.com/dotnet/roslyn/issues/31001 var excerptResult = new ExcerptResult( sourceText, classificationResult.HighlightSpan, classificationResult.ClassifiedSpans, documentSpan.Document, documentSpan.SourceSpan); return(excerptResult, AbstractDocumentSpanEntry.GetLineContainingPosition(sourceText, documentSpan.SourceSpan.Start)); }
static async Task <ClassifiedTextElement?> ComputeTextAsync( int id, int?definitionId, DocumentSpan documentSpan, ClassifiedTextElement?definitionText, bool isWrittenTo, CancellationToken cancellationToken) { // General case if (documentSpan != default) { var classifiedSpansAndHighlightSpan = await ClassifiedSpansAndHighlightSpanFactory.ClassifyAsync( documentSpan, cancellationToken).ConfigureAwait(false); var classifiedSpans = classifiedSpansAndHighlightSpan.ClassifiedSpans; var docText = await documentSpan.Document.GetTextAsync(cancellationToken).ConfigureAwait(false); var classifiedTextRuns = GetClassifiedTextRuns(id, definitionId, documentSpan, isWrittenTo, classifiedSpans, docText); return(new ClassifiedTextElement(classifiedTextRuns.ToArray())); } // Certain definitions may not have a DocumentSpan, such as namespace and metadata definitions else if (id == definitionId) { return(definitionText); } return(null);
public async ValueTask OnReferenceFoundAsync(Document document, TextSpan span, CancellationToken cancellationToken) { var options = await _context.GetOptionsAsync(document.Project.Language, cancellationToken).ConfigureAwait(false); var documentSpan = await ClassifiedSpansAndHighlightSpanFactory.GetClassifiedDocumentSpanAsync( document, span, options.ClassificationOptions, cancellationToken).ConfigureAwait(false); await _context.OnReferenceFoundAsync( new SourceReferenceItem(_definition, documentSpan, SymbolUsageInfo.None), cancellationToken).ConfigureAwait(false); }
private async Task <LSP.ReferenceGroup[]> GetReferenceGroupsAsync(LSP.ReferenceParams request, SimpleFindUsagesContext context, CancellationToken cancellationToken) { var definitionMap = new Dictionary <DefinitionItem, List <SourceReferenceItem> >(); foreach (var reference in context.GetReferences()) { if (!definitionMap.ContainsKey(reference.Definition)) { definitionMap.Add(reference.Definition, new List <SourceReferenceItem>()); } definitionMap[reference.Definition].Add(reference); } var referenceGroups = ArrayBuilder <LSP.ReferenceGroup> .GetInstance(); foreach (var keyValuePair in definitionMap) { var definition = keyValuePair.Key; var references = keyValuePair.Value; var referenceGroup = new LSP.ReferenceGroup(); var text = definition.GetClassifiedText(); referenceGroup.Definition = await ProtocolConversions.DocumentSpanToLocationWithTextAsync(definition.SourceSpans.First(), text, cancellationToken).ConfigureAwait(false); referenceGroup.DefinitionIcon = new ImageElement(definition.Tags.GetFirstGlyph().GetImageId()); var locationWithTexts = new ArrayBuilder <LSP.LocationWithText>(); foreach (var reference in references) { var classifiedSpansAndHighlightSpan = await ClassifiedSpansAndHighlightSpanFactory.ClassifyAsync(reference.SourceSpan, context.CancellationToken).ConfigureAwait(false); var classifiedSpans = classifiedSpansAndHighlightSpan.ClassifiedSpans; var referenceLocation = await ProtocolConversions.DocumentSpanToLocationAsync(reference.SourceSpan, cancellationToken).ConfigureAwait(false); var docText = await reference.SourceSpan.Document.GetTextAsync(context.CancellationToken).ConfigureAwait(false); var classifiedText = new ClassifiedTextElement(classifiedSpans.Select(cspan => new ClassifiedTextRun(cspan.ClassificationType, docText.ToString(cspan.TextSpan)))); var locationWithText = new LSP.LocationWithText { Range = referenceLocation.Range, Uri = referenceLocation.Uri, Text = classifiedText }; locationWithTexts.Add(locationWithText); } referenceGroup.References = locationWithTexts.ToArrayAndFree(); referenceGroups.Add(referenceGroup); } return(referenceGroups.ToArrayAndFree()); }
protected async Task <Entry> CreateDocumentSpanEntryAsync( RoslynDefinitionBucket definitionBucket, DocumentSpan documentSpan, HighlightSpanKind spanKind) { var document = documentSpan.Document; var(guid, projectName, sourceText) = await GetGuidAndProjectNameAndSourceTextAsync(document).ConfigureAwait(false); var classifiedSpansAndHighlightSpan = await ClassifiedSpansAndHighlightSpanFactory.ClassifyAsync(documentSpan, CancellationToken).ConfigureAwait(false); return(new DocumentSpanEntry( this, definitionBucket, documentSpan, spanKind, projectName, guid, sourceText, classifiedSpansAndHighlightSpan)); }
protected async Task <Entry> CreateDocumentSpanEntryAsync( RoslynDefinitionBucket definitionBucket, DocumentSpan documentSpan, HighlightSpanKind spanKind, ImmutableDictionary <string, ImmutableArray <string> > customColumnsDataOpt) { var document = documentSpan.Document; var(guid, projectName, sourceText) = await GetGuidAndProjectNameAndSourceTextAsync(document).ConfigureAwait(false); var classifiedSpansAndHighlightSpan = await ClassifiedSpansAndHighlightSpanFactory.ClassifyAsync(documentSpan, CancellationToken).ConfigureAwait(false); return(new DocumentSpanEntry( this, definitionBucket, documentSpan, spanKind, projectName, guid, sourceText, classifiedSpansAndHighlightSpan, GetAggregatedCustomColumnsData(customColumnsDataOpt))); }
public static async Task <ValueTrackedItem?> TryCreateAsync(Document document, TextSpan textSpan, ISymbol symbol, ValueTrackedItem?parent = null, CancellationToken cancellationToken = default) { var excerptService = document.Services.GetService <IDocumentExcerptService>(); SourceText?sourceText = null; ImmutableArray <ClassifiedSpan> classifiedSpans = default; if (excerptService != null) { var result = await excerptService.TryExcerptAsync(document, textSpan, ExcerptMode.SingleLine, cancellationToken).ConfigureAwait(false); if (result.HasValue) { var value = result.Value; sourceText = value.Content; } } if (sourceText is null) { var options = ClassificationOptions.From(document.Project); var documentSpan = await ClassifiedSpansAndHighlightSpanFactory.GetClassifiedDocumentSpanAsync(document, textSpan, options, cancellationToken).ConfigureAwait(false); var classificationResult = await ClassifiedSpansAndHighlightSpanFactory.ClassifyAsync(documentSpan, options, cancellationToken).ConfigureAwait(false); classifiedSpans = classificationResult.ClassifiedSpans; var syntaxTree = await document.GetRequiredSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); sourceText = await syntaxTree.GetTextAsync(cancellationToken).ConfigureAwait(false); } return(new ValueTrackedItem( SymbolKey.Create(symbol, cancellationToken), sourceText, classifiedSpans, textSpan, document.Id, symbol.GetGlyph(), parent: parent)); }
static async Task <object?> ComputeTextAsync( int id, int?definitionId, DocumentSpan documentSpan, ClassifiedTextElement?definitionText, CancellationToken cancellationToken) { if (id == definitionId) { return(definitionText); } else if (documentSpan != default) { var classifiedSpansAndHighlightSpan = await ClassifiedSpansAndHighlightSpanFactory.ClassifyAsync( documentSpan, cancellationToken).ConfigureAwait(false); var classifiedSpans = classifiedSpansAndHighlightSpan.ClassifiedSpans; var docText = await documentSpan.Document.GetTextAsync(cancellationToken).ConfigureAwait(false); return(new ClassifiedTextElement( classifiedSpans.Select(cspan => new ClassifiedTextRun(cspan.ClassificationType, docText.ToString(cspan.TextSpan))))); } return(null); }