From 44a5287f1ead631ff2b2e12694a55d574b0b47b6 Mon Sep 17 00:00:00 2001 From: Gen Lu Date: Thu, 25 Jun 2020 16:26:24 -0700 Subject: [PATCH] Use loop instead of enumerator --- .../ExtensionMethodImportCompletionHelper.cs | 56 +++++++++---------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/src/Features/Core/Portable/Completion/Providers/ImportCompletionProvider/ExtensionMethodImportCompletionHelper.cs b/src/Features/Core/Portable/Completion/Providers/ImportCompletionProvider/ExtensionMethodImportCompletionHelper.cs index b67177f3aa4..16e203431c3 100644 --- a/src/Features/Core/Portable/Completion/Providers/ImportCompletionProvider/ExtensionMethodImportCompletionHelper.cs +++ b/src/Features/Core/Portable/Completion/Providers/ImportCompletionProvider/ExtensionMethodImportCompletionHelper.cs @@ -303,45 +303,39 @@ static ImmutableArray AttachComplexTypes(ImmutableArray receiver // This is also affected by the symbol resolving issue mentioned above, which means in case referenced projects // are targeting different framework, we will miss extension methods with any framework type in their signature from those projects. - var methodsInCurrentCompilation = methodSymbols.Select(s => SymbolFinder.FindSimilarSymbols(s, semanticModel.Compilation).FirstOrDefault()).WhereNotNull(); - - var enumerator = methodsInCurrentCompilation.GetEnumerator(); - if (!enumerator.MoveNext()) - { - continue; - } - - var methodInCurrentCompilation = enumerator.Current; - - // We haven't seen this receiver type yet. Try to check by reducing one extension method - // to the given receiver type and save the result. - if (!cachedResult) - { - // If this is the first symbol we retrived from current compilation, - // try to check if we can apply it to given receiver type, and save result to our cache. - var reducedMethodSymbol = methodInCurrentCompilation.ReduceExtensionMethod(receiverTypeSymbol); - cachedResult = reducedMethodSymbol != null; - checkedReceiverTypes[declaredReceiverTypeInCurrentCompilation] = cachedResult; - } - - // Now, cachedResult being false means method doesn't match the receiver type, - // stop processing methods from this group. - if (!cachedResult) + var isFirstMethod = true; + foreach (var methodInCurrentCompilation in methodSymbols.Select(s => SymbolFinder.FindSimilarSymbols(s, semanticModel.Compilation).FirstOrDefault()).WhereNotNull()) { - continue; - } + if (isFirstMethod) + { + isFirstMethod = false; - // Add all methods to item list - do - { - methodInCurrentCompilation = enumerator.Current; + // We haven't seen this receiver type yet. Try to check by reducing one extension method + // to the given receiver type and save the result. + if (!cachedResult) + { + // If this is the first symbol we retrived from current compilation, + // try to check if we can apply it to given receiver type, and save result to our cache. + // Since method symbols are grouped by their declared receiver type, they are either all matches to the receiver type + // or all mismatches. So we only need to call ReduceExtensionMethod on one of them. + var reducedMethodSymbol = methodInCurrentCompilation.ReduceExtensionMethod(receiverTypeSymbol); + cachedResult = reducedMethodSymbol != null; + checkedReceiverTypes[declaredReceiverTypeInCurrentCompilation] = cachedResult; + + // Now, cachedResult being false means method doesn't match the receiver type, + // stop processing methods from this group. + if (!cachedResult) + { + break; + } + } + } if (semanticModel.IsAccessible(position, methodInCurrentCompilation)) { CreateAndAddItem(methodInCurrentCompilation, builder, stringCache); } } - while (enumerator.MoveNext()); } } -- GitLab