Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve existing formatting when fully qualifying names #70295

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Microsoft.CodeAnalysis.CodeFixes.FullyQualify;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;

namespace Microsoft.CodeAnalysis.CSharp.CodeFixes.FullyQualify
Expand Down Expand Up @@ -45,8 +44,7 @@ protected override async Task<SyntaxNode> ReplaceNodeAsync(SimpleNameSyntax simp
var newName = simpleName.WithLeadingTrivia(SyntaxTriviaList.Empty);

var qualifiedName = SyntaxFactory.QualifiedName(SyntaxFactory.ParseName(containerName), newName)
.WithLeadingTrivia(leadingTrivia)
.WithAdditionalAnnotations(Formatter.Annotation);
.WithLeadingTrivia(leadingTrivia);
sharwell marked this conversation as resolved.
Show resolved Hide resolved

var syntaxTree = simpleName.SyntaxTree;
var root = await syntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);
Expand Down
44 changes: 43 additions & 1 deletion src/Features/CSharpTest/FullyQualify/FullyQualifyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,48 @@ System.Collections.Generic.IDictionary Method()
index: 1, testHost: testHost);
}

[Theory, CombinatorialData]
[WorkItem("https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1889385")]
public async Task TestPreservesIncorrectIndentation1(TestHost testHost)
{
await TestInRegularAndScriptAsync(
@"class Class
{
[|IDictionary|] Method()
{
Goo();
}
}",
@"class Class
{
System.Collections.IDictionary Method()
{
Goo();
}
}", testHost: testHost);
}

[Theory, CombinatorialData]
[WorkItem("https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1889385")]
public async Task TestPreservesIncorrectIndentation2(TestHost testHost)
{
await TestInRegularAndScriptAsync(
@"class Class
{
\t[|IDictionary|] Method()
{
Goo();
}
}".Replace(@"\t", "\t"),
@"class Class
{
\tSystem.Collections.IDictionary Method()
{
Goo();
}
}".Replace(@"\t", "\t"), testHost: testHost);
}

[Theory, CombinatorialData]
public async Task TestGenericWithNoArgs(TestHost testHost)
{
Expand Down Expand Up @@ -1052,7 +1094,7 @@ public async Task TestAttribute(TestHost testHost)
{
await TestInRegularAndScriptAsync(
@"[ assembly : [|Guid|] ( ""9ed54f84-a89d-4fcd-a854-44251e925f09"" ) ] ",
@"[ assembly : System.Runtime.InteropServices.Guid( ""9ed54f84-a89d-4fcd-a854-44251e925f09"" ) ] ", testHost: testHost);
@"[ assembly : System.Runtime.InteropServices.Guid ( ""9ed54f84-a89d-4fcd-a854-44251e925f09"" ) ] ", testHost: testHost);
}

[Theory, CombinatorialData, WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/546027")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.FullyQualify

Dim qualifiedName = SyntaxFactory.QualifiedName(left:=SyntaxFactory.ParseName(containerName), right:=newName).
WithLeadingTrivia(leadingTrivia).
WithAdditionalAnnotations(Formatter.Annotation, CaseCorrector.Annotation)
WithAdditionalAnnotations(CaseCorrector.Annotation)

Dim tree = simpleName.SyntaxTree
Dim root = Await tree.GetRootAsync(cancellationToken).ConfigureAwait(False)
Expand Down
20 changes: 20 additions & 0 deletions src/Features/VisualBasicTest/FullyQualify/FullyQualifyTests.vb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ Namespace SomeNamespace
End Namespace", testHost:=testHost)
End Function

<Theory, CombinatorialData>
<WorkItem("https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1889385")>
Public Async Function TestPreservesIncorrectIndentation(testHost As TestHost) As Task
Await TestInRegularAndScriptAsync(
"Class Class1
Dim v As [|SomeClass1|]
End Class
Namespace SomeNamespace
Public Class SomeClass1
End Class
End Namespace",
"Class Class1
Dim v As SomeNamespace.SomeClass1
End Class
Namespace SomeNamespace
Public Class SomeClass1
End Class
End Namespace", testHost:=testHost)
End Function

<Theory, CombinatorialData>
Public Async Function TestOrdering(testHost As TestHost) As Task
Dim code = "
Expand Down