Skip to content
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 @@ -15,17 +15,12 @@

namespace Microsoft.CodeAnalysis.CSharp.Completion.Providers;

[ExportCompletionProvider(nameof(ExtensionMethodImportCompletionProvider), LanguageNames.CSharp)]
[ExportCompletionProvider(nameof(ExtensionMethodImportCompletionProvider), LanguageNames.CSharp), Shared]
[ExtensionOrder(After = nameof(TypeImportCompletionProvider))]
[Shared]
internal sealed class ExtensionMethodImportCompletionProvider : AbstractExtensionMethodImportCompletionProvider
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class ExtensionMethodImportCompletionProvider() : AbstractExtensionMethodImportCompletionProvider
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public ExtensionMethodImportCompletionProvider()
{
}

internal override string Language => LanguageNames.CSharp;

protected override string GenericSuffix => "<>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,6 @@ internal static void LogTypeImportCompletionCacheMiss()
internal static void LogCommitOfTypeImportCompletionItem()
=> s_countLogAggregator.IncreaseCount(ActionInfo.CommitsOfTypeImportCompletionItem);

internal static void LogExtensionMethodCompletionTicksDataPoint(TimeSpan total, TimeSpan getSymbols, TimeSpan createItems, TimeSpan? remoteAssetSync)
{
s_histogramLogAggregator.LogTime(ActionInfo.ExtensionMethodCompletionTicks, total);
s_statisticLogAggregator.AddDataPoint(ActionInfo.ExtensionMethodCompletionTicks, total);

if (remoteAssetSync.HasValue)
{
s_statisticLogAggregator.AddDataPoint(ActionInfo.ExtensionMethodCompletionRemoteAssetSyncTicks, remoteAssetSync.Value);
s_statisticLogAggregator.AddDataPoint(ActionInfo.ExtensionMethodCompletionRemoteTicks, total - remoteAssetSync.Value - getSymbols - createItems);
}

s_statisticLogAggregator.AddDataPoint(ActionInfo.ExtensionMethodCompletionGetSymbolsTicks, getSymbols);
s_statisticLogAggregator.AddDataPoint(ActionInfo.ExtensionMethodCompletionCreateItemsTicks, createItems);
}

internal static void LogExtensionMethodCompletionMethodsProvidedDataPoint(int count)
=> s_statisticLogAggregator.AddDataPoint(ActionInfo.ExtensionMethodCompletionMethodsProvided, count);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Extensions.ContextQuery;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Completion.Providers;

Expand Down Expand Up @@ -43,12 +44,13 @@ protected override async Task AddCompletionItemsAsync(
var syntaxFacts = completionContext.Document.GetRequiredLanguageService<ISyntaxFactsService>();
if (TryGetReceiverTypeSymbol(syntaxContext, syntaxFacts, cancellationToken, out var receiverTypeSymbol))
{

var inferredTypes = completionContext.CompletionOptions.TargetTypedCompletionFilter
? syntaxContext.InferredTypes
: [];

var result = await ExtensionMethodImportCompletionHelper.GetUnimportedExtensionMethodsAsync(
var totalTime = SharedStopwatch.StartNew();

var completionItems = await ExtensionMethodImportCompletionHelper.GetUnimportedExtensionMethodsAsync(
syntaxContext,
receiverTypeSymbol,
namespaceInScope,
Expand All @@ -57,10 +59,10 @@ protected override async Task AddCompletionItemsAsync(
hideAdvancedMembers: completionContext.CompletionOptions.MemberDisplayOptions.HideAdvancedMembers,
cancellationToken).ConfigureAwait(false);

if (result is not null)
if (!completionItems.IsDefault)
{
var receiverTypeKey = SymbolKey.CreateString(receiverTypeSymbol, cancellationToken);
completionContext.AddItems(result.CompletionItems.Select(i => Convert(i, receiverTypeKey)));
completionContext.AddItems(completionItems.Select(i => Convert(i, receiverTypeKey)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ private sealed class SymbolComputer
private readonly ITypeSymbol _receiverTypeSymbol;
private readonly ImmutableArray<string> _receiverTypeNames;
private readonly ISet<string> _namespaceInScope;
private readonly IImportCompletionCacheService<ExtensionMethodImportCompletionCacheEntry, object> _cacheService;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never used.


// This dictionary is used as cache among all projects and PE references.
// The key is the receiver type as in the extension method declaration (symbol retrived from originating compilation).
Expand All @@ -54,7 +53,6 @@ public SymbolComputer(

var receiverTypeNames = GetReceiverTypeNames(receiverTypeSymbol);
_receiverTypeNames = AddComplexTypes(receiverTypeNames);
_cacheService = GetCacheService(document.Project);
}

private static IImportCompletionCacheService<ExtensionMethodImportCompletionCacheEntry, object> GetCacheService(Project project)
Expand All @@ -71,16 +69,15 @@ public static void QueueCacheWarmUpTask(Project project)
public static async ValueTask UpdateCacheAsync(Project project, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var cacheService = GetCacheService(project);

foreach (var relevantProject in GetAllRelevantProjects(project))
await GetUpToDateCacheEntryAsync(relevantProject, cacheService, cancellationToken).ConfigureAwait(false);
await GetUpToDateCacheEntryAsync(relevantProject, cancellationToken).ConfigureAwait(false);

foreach (var peReference in GetAllRelevantPeReferences(project))
await SymbolTreeInfo.GetInfoForMetadataReferenceAsync(project.Solution, peReference, checksum: null, cancellationToken).ConfigureAwait(false);
}

public async Task<(ImmutableArray<IMethodSymbol> symbols, bool isPartialResult)> GetExtensionMethodSymbolsAsync(bool forceCacheCreation, bool hideAdvancedMembers, CancellationToken cancellationToken)
public async Task<ImmutableArray<IMethodSymbol>> GetExtensionMethodSymbolsAsync(bool forceCacheCreation, bool hideAdvancedMembers, CancellationToken cancellationToken)
{
try
{
Expand All @@ -101,31 +98,18 @@ public static async ValueTask UpdateCacheAsync(Project project, CancellationToke

var results = await Task.WhenAll(peReferenceMethodSymbolsTask, projectMethodSymbolsTask).ConfigureAwait(false);

var isPartialResult = false;

using var _ = ArrayBuilder<IMethodSymbol>.GetInstance(results[0].Length + results[1].Length, out var symbols);
foreach (var methodArray in results)
{
foreach (var method in methodArray)
{
// `null` indicates we don't have the index ready for the corresponding project/PE.
// returns what we have even it means we only show partial results.
if (method is null)
{
isPartialResult = true;
}
else
{
symbols.Add(method);
}
}
symbols.AddIfNotNull(method);
}

var browsableSymbols = symbols
.ToImmutable()
.FilterToVisibleAndBrowsableSymbols(hideAdvancedMembers, _originatingSemanticModel.Compilation, inclusionFilter: static s => true);

return (browsableSymbols, isPartialResult);
return browsableSymbols;
}
finally
{
Expand Down Expand Up @@ -156,9 +140,9 @@ private async Task GetExtensionMethodSymbolsFromProjectAsync(
ExtensionMethodImportCompletionCacheEntry? cacheEntry;
if (forceCacheCreation)
{
cacheEntry = await GetUpToDateCacheEntryAsync(project, _cacheService, cancellationToken).ConfigureAwait(false);
cacheEntry = await GetUpToDateCacheEntryAsync(project, cancellationToken).ConfigureAwait(false);
}
else if (!s_projectItemsCache.TryGetValue(project.State, out cacheEntry))
else if (!s_projectItemsCache.TryGetValue(project.Id, out cacheEntry))
{
// Use cached data if available, even checksum doesn't match. otherwise, returns null indicating cache not ready.
callback(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
Expand All @@ -26,7 +27,12 @@ namespace Microsoft.CodeAnalysis.Completion.Providers;
/// <remarks>It runs out-of-proc if it's enabled</remarks>
internal static partial class ExtensionMethodImportCompletionHelper
{
private static readonly ConditionalWeakTable<ProjectState, ExtensionMethodImportCompletionCacheEntry> s_projectItemsCache = new();
/// <summary>
/// Technically never gets cleared out. However, as we're only really storing information about extension
/// methods in a project, this should not be too bad. It's only really a leak if the project is truly
/// unloaded, and not too bad in that case.
/// </summary>
private static readonly ConcurrentDictionary<ProjectId, ExtensionMethodImportCompletionCacheEntry> s_projectItemsCache = new();

public static async Task WarmUpCacheAsync(Project project, CancellationToken cancellationToken)
{
Expand All @@ -48,7 +54,7 @@ public static async Task WarmUpCacheAsync(Project project, CancellationToken can
public static void WarmUpCacheInCurrentProcess(Project project)
=> SymbolComputer.QueueCacheWarmUpTask(project);

public static async Task<SerializableUnimportedExtensionMethods?> GetUnimportedExtensionMethodsAsync(
public static async Task<ImmutableArray<SerializableImportCompletionItem>> GetUnimportedExtensionMethodsAsync(
SyntaxContext syntaxContext,
ITypeSymbol receiverTypeSymbol,
ISet<string> namespaceInScope,
Expand All @@ -57,13 +63,10 @@ public static void WarmUpCacheInCurrentProcess(Project project)
bool hideAdvancedMembers,
CancellationToken cancellationToken)
{
SerializableUnimportedExtensionMethods? result = null;
var document = syntaxContext.Document;
var position = syntaxContext.Position;
var project = document.Project;

var totalTime = SharedStopwatch.StartNew();

var client = await RemoteHostClient.TryGetClientAsync(project, cancellationToken).ConfigureAwait(false);
if (client != null)
{
Expand All @@ -72,36 +75,24 @@ public static void WarmUpCacheInCurrentProcess(Project project)

// Call the project overload. Add-import-for-extension-method doesn't search outside of the current
// project cone.
var remoteResult = await client.TryInvokeAsync<IRemoteExtensionMethodImportCompletionService, SerializableUnimportedExtensionMethods?>(
var remoteResult = await client.TryInvokeAsync<IRemoteExtensionMethodImportCompletionService, ImmutableArray<SerializableImportCompletionItem>>(
project,
(service, solutionInfo, cancellationToken) => service.GetUnimportedExtensionMethodsAsync(
solutionInfo, document.Id, position, receiverTypeSymbolKeyData, [.. namespaceInScope],
targetTypesSymbolKeyData, forceCacheCreation, hideAdvancedMembers, cancellationToken),
cancellationToken).ConfigureAwait(false);

result = remoteResult.HasValue ? remoteResult.Value : null;
return remoteResult.HasValue ? remoteResult.Value : default;
}
else
{
result = await GetUnimportedExtensionMethodsInCurrentProcessAsync(
document, syntaxContext.SemanticModel, position, receiverTypeSymbol, namespaceInScope, targetTypesSymbols, forceCacheCreation, hideAdvancedMembers, remoteAssetSyncTime: null, cancellationToken)
return await GetUnimportedExtensionMethodsInCurrentProcessAsync(
document, syntaxContext.SemanticModel, position, receiverTypeSymbol, namespaceInScope, targetTypesSymbols, forceCacheCreation, hideAdvancedMembers, cancellationToken)
.ConfigureAwait(false);
}

if (result is not null)
{
// report telemetry:
CompletionProvidersLogger.LogExtensionMethodCompletionTicksDataPoint(
totalTime.Elapsed, result.GetSymbolsTime, result.CreateItemsTime, result.RemoteAssetSyncTime);

if (result.IsPartialResult)
CompletionProvidersLogger.LogExtensionMethodCompletionPartialResultCount();
}

return result;
}

public static async Task<SerializableUnimportedExtensionMethods> GetUnimportedExtensionMethodsInCurrentProcessAsync(
public static async Task<ImmutableArray<SerializableImportCompletionItem>> GetUnimportedExtensionMethodsInCurrentProcessAsync(
Document document,
SemanticModel? semanticModel,
int position,
Expand All @@ -110,27 +101,19 @@ public static async Task<SerializableUnimportedExtensionMethods> GetUnimportedEx
ImmutableArray<ITypeSymbol> targetTypes,
bool forceCacheCreation,
bool hideAdvancedMembers,
TimeSpan? remoteAssetSyncTime,
CancellationToken cancellationToken)
{
var stopwatch = SharedStopwatch.StartNew();

// First find symbols of all applicable extension methods.
// Workspace's syntax/symbol index is used to avoid iterating every method symbols in the solution.
semanticModel ??= await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var symbolComputer = new SymbolComputer(
document, semanticModel, receiverTypeSymbol, position, namespaceInScope);
var (extensionMethodSymbols, isPartialResult) = await symbolComputer.GetExtensionMethodSymbolsAsync(forceCacheCreation, hideAdvancedMembers, cancellationToken).ConfigureAwait(false);

var getSymbolsTime = stopwatch.Elapsed;
stopwatch = SharedStopwatch.StartNew();
var extensionMethodSymbols = await symbolComputer.GetExtensionMethodSymbolsAsync(forceCacheCreation, hideAdvancedMembers, cancellationToken).ConfigureAwait(false);

var compilation = await document.Project.GetRequiredCompilationAsync(cancellationToken).ConfigureAwait(false);
var items = ConvertSymbolsToCompletionItems(compilation, extensionMethodSymbols, targetTypes, cancellationToken);

var createItemsTime = stopwatch.Elapsed;

return new SerializableUnimportedExtensionMethods(items, isPartialResult, getSymbolsTime, createItemsTime, remoteAssetSyncTime);
return items;
}

public static async ValueTask BatchUpdateCacheAsync(ImmutableSegmentedList<Project> projects, CancellationToken cancellationToken)
Expand Down Expand Up @@ -250,15 +233,16 @@ private static string GetFullyQualifiedNamespaceName(INamespaceSymbol symbol, Di

private static async Task<ExtensionMethodImportCompletionCacheEntry> GetUpToDateCacheEntryAsync(
Project project,
IImportCompletionCacheService<ExtensionMethodImportCompletionCacheEntry, object> cacheService,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not used.

CancellationToken cancellationToken)
{
var projectId = project.Id;

// While we are caching data from SyntaxTreeInfo, all the things we cared about here are actually based on sources symbols.
// So using source symbol checksum would suffice.
var checksum = await SymbolTreeInfo.GetSourceSymbolsChecksumAsync(project, cancellationToken).ConfigureAwait(false);

// Cache miss, create all requested items.
if (!s_projectItemsCache.TryGetValue(project.State, out var cacheEntry) ||
if (!s_projectItemsCache.TryGetValue(projectId, out var cacheEntry) ||
cacheEntry.Checksum != checksum ||
cacheEntry.Language != project.Language)
{
Expand All @@ -281,12 +265,7 @@ private static async Task<ExtensionMethodImportCompletionCacheEntry> GetUpToDate
}

cacheEntry = builder.ToCacheEntry();
#if NET
s_projectItemsCache.AddOrUpdate(project.State, cacheEntry);
#else
s_projectItemsCache.Remove(project.State);
s_projectItemsCache.GetValue(project.State, _ => cacheEntry);
#endif
s_projectItemsCache[projectId] = cacheEntry;
}

return cacheEntry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.CodeAnalysis.Completion.Providers;

internal interface IRemoteExtensionMethodImportCompletionService
{
ValueTask<SerializableUnimportedExtensionMethods?> GetUnimportedExtensionMethodsAsync(
ValueTask<ImmutableArray<SerializableImportCompletionItem>> GetUnimportedExtensionMethodsAsync(
Checksum solutionChecksum,
DocumentId documentId,
int position,
Expand Down

This file was deleted.

Loading
Loading