Skip to content

Commit 4ae8ae7

Browse files
committed
Merge pull request dotnet#210 from srivatsn/addusing
The rule that makes types explicit should add a using when it needs to qualify type.
2 parents 04189a6 + ea21daf commit 4ae8ae7

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

src/Microsoft.DotNet.CodeFormatter.Analyzers.Tests/ProvideExplicitVariableTypeAnalyzerTests.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void M()
3232
int[][] z = new[] { new[] { 1, 2, 3 }, new[] { 4, 5, 6 } };
3333
}
3434
}";
35-
const string expected = input;
35+
const string expected = input;
3636
Verify(input, expected, runFormatter: false);
3737
}
3838

@@ -511,6 +511,64 @@ void M()
511511
List<int>.Enumerator x = (new List<int>()).GetEnumerator();
512512
var locationBuilder = ImmutableArray.CreateBuilder<C2>();
513513
}
514+
}";
515+
Verify(input, expected, runFormatter: false);
516+
}
517+
518+
[Fact]
519+
public void TestAddingUsing()
520+
{
521+
const string input = @"
522+
class C1
523+
{
524+
System.Collections.Generic.List<int> f() { return null; }
525+
526+
void M()
527+
{
528+
var x = f();
529+
}
530+
}";
531+
const string expected =
532+
@"using System.Collections.Generic;
533+
534+
class C1
535+
{
536+
System.Collections.Generic.List<int> f() { return null; }
537+
538+
void M()
539+
{
540+
List<int> x = f();
541+
}
542+
}";
543+
Verify(input, expected, runFormatter: false);
544+
}
545+
546+
[Fact]
547+
public void TestNotAddingDuplicateUsing()
548+
{
549+
const string input = @"
550+
using System.Collections.Generic;
551+
552+
class C1
553+
{
554+
System.Collections.Generic.List<int> f() { return null; }
555+
556+
void M()
557+
{
558+
var x = f();
559+
}
560+
}";
561+
const string expected = @"
562+
using System.Collections.Generic;
563+
564+
class C1
565+
{
566+
System.Collections.Generic.List<int> f() { return null; }
567+
568+
void M()
569+
{
570+
List<int> x = f();
571+
}
514572
}";
515573
Verify(input, expected, runFormatter: false);
516574
}

src/Microsoft.DotNet.CodeFormatter.Analyzers/ProvideExplicitVariableTypeFixer.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,21 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
8080
private async Task<Document> ReplaceVarWithExplicitType(Document document, SyntaxNode varNode, ITypeSymbol explicitTypeSymbol, CancellationToken cancellationToken)
8181
{
8282
DocumentEditor documentEditor = await DocumentEditor.CreateAsync(document, cancellationToken);
83+
84+
// Create an annotation and tag the type node with it so that we can find it again in a new tree.
85+
var explicitTypeAnnotation = new SyntaxAnnotation();
8386
SyntaxNode explicitTypeNode = documentEditor.Generator.TypeExpression(explicitTypeSymbol)
84-
.WithAdditionalAnnotations(Simplifier.Annotation)
87+
.WithAdditionalAnnotations(Simplifier.Annotation, explicitTypeAnnotation)
8588
.WithTriviaFrom(varNode);
8689
documentEditor.ReplaceNode(varNode, explicitTypeNode);
87-
return documentEditor.GetChangedDocument();
90+
var newDocument = documentEditor.GetChangedDocument();
91+
92+
// We don't want the explicit type to be fully qualified. So add an using for this node and the simplifier will
93+
// take care of removing it if it isn't necessary.
94+
// The second parmaeter to AddImportsAsync is an annotation that is used to locate the span
95+
// for which an using should be added.
96+
newDocument = await ImportAdder.AddImportsAsync(newDocument, explicitTypeAnnotation).ConfigureAwait(false);
97+
return newDocument;
8898
}
8999

90100
private static RuleType GetRuleType(Diagnostic diagnostic)

0 commit comments

Comments
 (0)