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

Cache the getnode/gettoken classifiers retrieived from the extension manager. #73712

Merged
merged 6 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
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 @@ -7,6 +7,7 @@
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Classification.Classifiers;
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.Extensions;
using Microsoft.CodeAnalysis.Host;
Expand All @@ -24,6 +25,9 @@ internal abstract class AbstractClassificationService(ISyntaxClassificationServi
{
private readonly ISyntaxClassificationService _syntaxClassificationService = syntaxClassificationService;

private Func<SyntaxNode, ImmutableArray<ISyntaxClassifier>>? _getNodeClassifiers;
private Func<SyntaxToken, ImmutableArray<ISyntaxClassifier>>? _getTokenClassifiers;

public abstract void AddLexicalClassifications(SourceText text, TextSpan textSpan, SegmentedList<ClassifiedSpan> result, CancellationToken cancellationToken);
public abstract ClassifiedSpan AdjustStaleClassification(SourceText text, ClassifiedSpan classifiedSpan);

Expand All @@ -39,7 +43,7 @@ public Task AddEmbeddedLanguageClassificationsAsync(
return AddClassificationsAsync(document, textSpans, options, ClassificationType.EmbeddedLanguage, result, cancellationToken);
}

private static async Task AddClassificationsAsync(
public async Task AddClassificationsAsync(
Document document,
ImmutableArray<TextSpan> textSpans,
ClassificationOptions options,
Expand Down Expand Up @@ -130,7 +134,7 @@ private static async Task<bool> TryGetCachedClassificationsAsync(
return true;
}

public static async Task AddClassificationsInCurrentProcessAsync(
private async Task AddClassificationsInCurrentProcessAsync(
Document document,
ImmutableArray<TextSpan> textSpans,
ClassificationType type,
Expand All @@ -141,27 +145,23 @@ public static async Task AddClassificationsInCurrentProcessAsync(
if (type == ClassificationType.Semantic)
{
var classificationService = document.GetRequiredLanguageService<ISyntaxClassificationService>();
var reassignedVariableService = document.GetRequiredLanguageService<IReassignedVariableService>();
var obsoleteSymbolService = document.GetRequiredLanguageService<IObsoleteSymbolService>();

var extensionManager = document.Project.Solution.Services.GetRequiredService<IExtensionManager>();
var classifiers = classificationService.GetDefaultSyntaxClassifiers();

var getNodeClassifiers = extensionManager.CreateNodeExtensionGetter(classifiers, c => c.SyntaxNodeTypes);
var getTokenClassifiers = extensionManager.CreateTokenExtensionGetter(classifiers, c => c.SyntaxTokenKinds);
var (getNodeClassifiers, getTokenClassifiers) = GetExtensionClassifiers(document, classificationService);

await classificationService.AddSemanticClassificationsAsync(
document, textSpans, options, getNodeClassifiers, getTokenClassifiers, result, cancellationToken).ConfigureAwait(false);

if (options.ClassifyReassignedVariables)
{
var reassignedVariableService = document.GetRequiredLanguageService<IReassignedVariableService>();
var reassignedVariableSpans = await reassignedVariableService.GetLocationsAsync(document, textSpans, cancellationToken).ConfigureAwait(false);
foreach (var span in reassignedVariableSpans)
result.Add(new ClassifiedSpan(span, ClassificationTypeNames.ReassignedVariable));
}

if (options.ClassifyObsoleteSymbols)
{
var obsoleteSymbolService = document.GetRequiredLanguageService<IObsoleteSymbolService>();
var obsoleteSymbolSpans = await obsoleteSymbolService.GetLocationsAsync(document, textSpans, cancellationToken).ConfigureAwait(false);
foreach (var span in obsoleteSymbolSpans)
result.Add(new ClassifiedSpan(span, ClassificationTypeNames.ObsoleteSymbol));
Expand All @@ -180,6 +180,23 @@ await embeddedLanguageService.AddEmbeddedLanguageClassificationsAsync(
{
throw ExceptionUtilities.UnexpectedValue(type);
}

return;

(Func<SyntaxNode, ImmutableArray<ISyntaxClassifier>>, Func<SyntaxToken, ImmutableArray<ISyntaxClassifier>>) GetExtensionClassifiers(
Document document, ISyntaxClassificationService classificationService)
{
if (_getNodeClassifiers == null || _getTokenClassifiers == null)
{
var extensionManager = document.Project.Solution.Services.GetRequiredService<IExtensionManager>();
var classifiers = classificationService.GetDefaultSyntaxClassifiers();

_getNodeClassifiers = extensionManager.CreateNodeExtensionGetter(classifiers, static c => c.SyntaxNodeTypes);
_getTokenClassifiers = extensionManager.CreateTokenExtensionGetter(classifiers, static c => c.SyntaxTokenKinds);
}

return (_getNodeClassifiers, _getTokenClassifiers);
}
}

public async Task AddSyntacticClassificationsAsync(Document document, ImmutableArray<TextSpan> textSpans, SegmentedList<ClassifiedSpan> result, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

Expand Down Expand Up @@ -38,8 +39,12 @@ public ValueTask<SerializableClassifiedSpans> GetClassificationsAsync(
solution = document.Project.Solution;

using var _ = Classifier.GetPooledList(out var temp);
await AbstractClassificationService.AddClassificationsInCurrentProcessAsync(
document, spans, type, options, temp, cancellationToken).ConfigureAwait(false);

// Safe to do this. The remote classification service only runs for C#/VB. So we know we'll always
// have this service and it will always be this type.
var classificationService = (AbstractClassificationService)document.GetRequiredLanguageService<IClassificationService>();
await classificationService.AddClassificationsAsync(
document, spans, options, type, temp, cancellationToken).ConfigureAwait(false);

if (isFullyLoaded)
{
Expand Down
Loading