diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Editing/ImportAdderService.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Editing/ImportAdderService.cs index b836a2d0d8f01..91ee326561e99 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Editing/ImportAdderService.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Editing/ImportAdderService.cs @@ -2,14 +2,12 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.AddImport; -using Microsoft.CodeAnalysis.CodeGeneration; using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Host; using Microsoft.CodeAnalysis.PooledObjects; @@ -38,7 +36,6 @@ public async Task AddImportsAsync( { var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var addImportsService = document.GetRequiredLanguageService(); - var codeGenerator = document.GetRequiredLanguageService(); var generator = document.GetRequiredLanguageService(); // Create a simple interval tree for simplification spans. @@ -53,13 +50,20 @@ public async Task AddImportsAsync( // // We'll dive under the parent because it overlaps with the span. But we only want to include (and dive // into) B and C not A and D. - var nodes = root.DescendantNodesAndSelf(OverlapsWithSpan).Where(OverlapsWithSpan); - if (strategy == Strategy.AddImportsFromSymbolAnnotations) - return await AddImportDirectivesFromSymbolAnnotationsAsync(document, nodes, addImportsService, generator, options, cancellationToken).ConfigureAwait(false); + { + var nodes = root.DescendantNodesAndSelf(n => OverlapsWithSpan(n) && n.ContainsAnnotations).Where(OverlapsWithSpan); + var annotatedNodes = nodes.Where(x => x.HasAnnotations(SymbolAnnotation.Kind)); + + return await AddImportDirectivesFromSymbolAnnotationsAsync(document, annotatedNodes, addImportsService, generator, options, cancellationToken).ConfigureAwait(false); + } if (strategy == Strategy.AddImportsFromSyntaxes) + { + var nodes = root.DescendantNodesAndSelf(OverlapsWithSpan).Where(OverlapsWithSpan); + return await AddImportDirectivesFromSyntaxesAsync(document, nodes, addImportsService, generator, options, cancellationToken).ConfigureAwait(false); + } throw ExceptionUtilities.UnexpectedValue(strategy); @@ -165,7 +169,7 @@ private async Task AddImportDirectivesFromSyntaxesAsync( private async Task AddImportDirectivesFromSymbolAnnotationsAsync( Document document, - IEnumerable syntaxNodes, + IEnumerable annotatedNodes, IAddImportsService addImportsService, SyntaxGenerator generator, AddImportPlacementOptions options, @@ -182,7 +186,6 @@ private async Task AddImportDirectivesFromSymbolAnnotationsAsync( #endif SyntaxNode? first = null, last = null; - var annotatedNodes = syntaxNodes.Where(x => x.HasAnnotations(SymbolAnnotation.Kind)); foreach (var annotatedNode in annotatedNodes) {