Skip to content

Commit 6badbad

Browse files
Fix VB remove-imports not showing up (#78806)
2 parents 2489a20 + fc59bd4 commit 6badbad

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/Analyzers/VisualBasic/Tests/RemoveUnnecessaryImports/RemoveUnnecessaryImportsTests.vb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,5 +1323,17 @@ End Namespace
13231323
parseOptions:=TestOptions.Regular,
13241324
compilationOptions:=TestOptions.ReleaseExe.WithGlobalImports({GlobalImport.Parse("System"), GlobalImport.Parse("Goo"), GlobalImport.Parse("Bar")}))
13251325
End Function
1326+
1327+
<Fact, WorkItem("https://github.com/dotnet/roslyn/issues/78799")>
1328+
Public Async Function TestExplicitSelectionOfFullImportStatement() As Task
1329+
Await TestAsync(
1330+
"[|Imports System.Collections.Generic|]
1331+
1332+
Class C
1333+
End Class",
1334+
"Class C
1335+
End Class",
1336+
TestOptions.Regular)
1337+
End Function
13261338
End Class
13271339
End Namespace

src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Helpers/RemoveUnnecessaryImports/AbstractUnnecessaryImportsProvider.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
using System.Collections.Generic;
77
using System.Collections.Immutable;
88
using System.Diagnostics.CodeAnalysis;
9+
using System.Linq;
910
using System.Threading;
10-
using Microsoft.CodeAnalysis.Shared.Extensions;
1111
using Microsoft.CodeAnalysis.Text;
1212

1313
namespace Microsoft.CodeAnalysis.RemoveUnnecessaryImports;
@@ -26,15 +26,21 @@ public ImmutableArray<TSyntaxNode> GetUnnecessaryImports(SemanticModel model, Te
2626
public ImmutableArray<TSyntaxNode> GetUnnecessaryImports(
2727
SemanticModel model, TextSpan? span, Func<SyntaxNode, bool>? predicate, CancellationToken cancellationToken)
2828
{
29-
if (span.HasValue)
30-
{
31-
// Bail out if there are no usings/imports in the filter span.
32-
var node = model.SyntaxTree.FindNode(span, findInTrivia: false, getInnermostNodeForTie: false, cancellationToken);
33-
if (node.FirstAncestorOrSelf<TSyntaxNode>() is null)
34-
return [];
35-
}
29+
// Bail out if there are no usings/imports in the filter span.
30+
if (span.HasValue && !HasImportThatIntersectsWithSpan(span.Value))
31+
return [];
3632

3733
return GetUnnecessaryImports(model, predicate, cancellationToken);
34+
35+
bool HasImportThatIntersectsWithSpan(TextSpan span)
36+
{
37+
var root = model.SyntaxTree.GetRoot(cancellationToken);
38+
return root
39+
.DescendantNodes(n => n.FullSpan.IntersectsWith(span))
40+
.Where(n => n.FullSpan.IntersectsWith(span))
41+
.OfType<TSyntaxNode>()
42+
.Any();
43+
}
3844
}
3945

4046
bool IEqualityComparer<TSyntaxNode>.Equals([AllowNull] TSyntaxNode x, [AllowNull] TSyntaxNode y)

0 commit comments

Comments
 (0)