Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only fetch options if needed #73600

Merged
merged 2 commits into from
May 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,8 @@ protected async Task AddDocumentSpanEntriesAsync(
CancellationToken cancellationToken)
{
var document = documentSpan.Document;
var options = _globalOptions.GetClassificationOptions(document.Project.Language);
var sourceText = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var (excerptResult, lineText) = await ExcerptAsync(sourceText, documentSpan, classifiedSpans, options, cancellationToken).ConfigureAwait(false);
var (excerptResult, lineText) = await ExcerptAsync(sourceText, documentSpan, classifiedSpans, cancellationToken).ConfigureAwait(false);

var mappedDocumentSpan = await AbstractDocumentSpanEntry.TryMapAndGetFirstAsync(documentSpan, sourceText, cancellationToken).ConfigureAwait(false);
if (mappedDocumentSpan == null)
Expand All @@ -457,31 +456,42 @@ protected async Task AddDocumentSpanEntriesAsync(
ThreadingContext);
}

private static async Task<(ExcerptResult, SourceText)> ExcerptAsync(
SourceText sourceText, DocumentSpan documentSpan, ClassifiedSpansAndHighlightSpan? classifiedSpans, ClassificationOptions options, CancellationToken cancellationToken)
private async Task<(ExcerptResult, SourceText)> ExcerptAsync(
SourceText sourceText, DocumentSpan documentSpan, ClassifiedSpansAndHighlightSpan? classifiedSpans, CancellationToken cancellationToken)
{
var excerptService = documentSpan.Document.Services.GetService<IDocumentExcerptService>();
var document = documentSpan.Document;
var sourceSpan = documentSpan.SourceSpan;

var excerptService = document.Services.GetService<IDocumentExcerptService>();

// Fetching options is expensive enough to try to avoid it if we can. So only fetch this if absolutely necessary.
ClassificationOptions? options = null;
if (excerptService != null)
{
var result = await excerptService.TryExcerptAsync(documentSpan.Document, documentSpan.SourceSpan, ExcerptMode.SingleLine, options, cancellationToken).ConfigureAwait(false);
options ??= _globalOptions.GetClassificationOptions(document.Project.Language);

var result = await excerptService.TryExcerptAsync(document, sourceSpan, ExcerptMode.SingleLine, options.Value, cancellationToken).ConfigureAwait(false);
if (result != null)
{
return (result.Value, AbstractDocumentSpanEntry.GetLineContainingPosition(result.Value.Content, result.Value.MappedSpan.Start));
}
}

var classificationResult = await ClassifiedSpansAndHighlightSpanFactory.ClassifyAsync(
documentSpan, classifiedSpans, options, cancellationToken).ConfigureAwait(false);
if (classifiedSpans is null)
{
options ??= _globalOptions.GetClassificationOptions(document.Project.Language);

classifiedSpans = await ClassifiedSpansAndHighlightSpanFactory.ClassifyAsync(
documentSpan, classifiedSpans, options.Value, 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);
classifiedSpans.Value.HighlightSpan,
classifiedSpans.Value.ClassifiedSpans,
document,
sourceSpan);

return (excerptResult, AbstractDocumentSpanEntry.GetLineContainingPosition(sourceText, documentSpan.SourceSpan.Start));
return (excerptResult, AbstractDocumentSpanEntry.GetLineContainingPosition(sourceText, sourceSpan.Start));
}

public sealed override async ValueTask OnReferenceFoundAsync(SourceReferenceItem reference, CancellationToken cancellationToken)
Expand Down
Loading