From 100234cabe950ca07250f2dd571548882a10fc47 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Sat, 5 Dec 2015 13:54:51 -0800 Subject: [PATCH] We should never match with a namespace if the user has provided generic type args. --- .../Diagnostics/AddImport/AddImportTests.vb | 11 +++++++++-- .../AbstractAddImportCodeFixProvider.cs | 5 +++++ .../VisualBasicAddImportCodeFixProvider.vb | 17 ++++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/EditorFeatures/VisualBasicTest/Diagnostics/AddImport/AddImportTests.vb b/src/EditorFeatures/VisualBasicTest/Diagnostics/AddImport/AddImportTests.vb index 94720eb23ad..ddfb0fa352b 100644 --- a/src/EditorFeatures/VisualBasicTest/Diagnostics/AddImport/AddImportTests.vb +++ b/src/EditorFeatures/VisualBasicTest/Diagnostics/AddImport/AddImportTests.vb @@ -159,9 +159,16 @@ NewLines("Imports System.Collections.Generic \n Class Foo \n Function F() As Lis End Function - Public Async Function TestGenericWithWrongArgs() As Task + Public Async Function TestGenericWithWrongArgs1() As Task Await TestMissingAsync( -NewLines("Class Foo \n Function F() As [|List(Of Integer, String)|] \n End Function \n End Class")) +NewLines("Class Foo \n Function F() As [|List(Of Integer, String, Boolean)|] \n End Function \n End Class")) + End Function + + + Public Async Function TestGenericWithWrongArgs2() As Task + Await TestAsync( +NewLines("Class Foo \n Function F() As [|List(Of Integer, String)|] \n End Function \n End Class"), +NewLines("Imports System.Collections.Generic \n Class Foo \n Function F() As SortedList(Of Integer, String) \n End Function \n End Class")) End Function diff --git a/src/Features/Core/Portable/CodeFixes/AddImport/AbstractAddImportCodeFixProvider.cs b/src/Features/Core/Portable/CodeFixes/AddImport/AbstractAddImportCodeFixProvider.cs index c6669693238..e81ca3f3803 100644 --- a/src/Features/Core/Portable/CodeFixes/AddImport/AbstractAddImportCodeFixProvider.cs +++ b/src/Features/Core/Portable/CodeFixes/AddImport/AbstractAddImportCodeFixProvider.cs @@ -508,6 +508,11 @@ private async Task> GetNamespacesForMatchingNamespacesAsy int arity; _syntaxFacts.GetNameAndArityOfSimpleName(_node, out name, out arity); + if (arity > 0) + { + return null; + } + if (ExpressionBinds(checkForExtensionMethods: false)) { return null; diff --git a/src/Features/VisualBasic/Portable/CodeFixes/AddImport/VisualBasicAddImportCodeFixProvider.vb b/src/Features/VisualBasic/Portable/CodeFixes/AddImport/VisualBasicAddImportCodeFixProvider.vb index 3f98022f168..e1abddd24c8 100644 --- a/src/Features/VisualBasic/Portable/CodeFixes/AddImport/VisualBasicAddImportCodeFixProvider.vb +++ b/src/Features/VisualBasic/Portable/CodeFixes/AddImport/VisualBasicAddImportCodeFixProvider.vb @@ -293,13 +293,28 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.AddImport placeSystemNamespaceFirst As Boolean, cancellationToken As CancellationToken) As Task(Of Document) + Dim originalDocument = document + Dim originalContextNode = contextNode + Dim originalRoot = DirectCast(contextNode.SyntaxTree.GetRoot(cancellationToken), CompilationUnitSyntax) + + Dim root = originalRoot + + If Not String.IsNullOrEmpty(desiredName) Then + Dim firstToken = contextNode.GetFirstToken() + If firstToken.IsKind(SyntaxKind.IdentifierToken) AndAlso firstToken.ValueText <> desiredName Then + Dim annotation = New SyntaxAnnotation() + root = root.ReplaceToken(firstToken, SyntaxFactory.Identifier(desiredName).WithTriviaFrom(firstToken).WithAdditionalAnnotations(annotation)) + document = document.WithSyntaxRoot(root) + contextNode = root.GetAnnotatedTokens(annotation).First().Parent + End If + End If + Dim memberImportsClause = SyntaxFactory.SimpleImportsClause(name:=DirectCast(symbol.GenerateTypeSyntax(addGlobal:=False), NameSyntax).WithAdditionalAnnotations(Simplifier.Annotation)) Dim newImport = SyntaxFactory.ImportsStatement( importsClauses:=SyntaxFactory.SingletonSeparatedList(Of ImportsClauseSyntax)(memberImportsClause)) Dim syntaxTree = contextNode.SyntaxTree - Dim root = DirectCast(syntaxTree.GetRoot(cancellationToken), CompilationUnitSyntax) Return Task.FromResult( document.WithSyntaxRoot( root.AddImportsStatement(newImport, placeSystemNamespaceFirst, -- GitLab