From 3dcd2361d8a8295da2e69310e8bfcb723fc8cb08 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 9 Oct 2023 10:15:55 -0500 Subject: [PATCH] Preserve existing formatting when fully qualifying names Fixes https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1889385 --- .../FullyQualify/CSharpFullyQualifyService.cs | 4 +- .../FullyQualify/FullyQualifyTests.cs | 44 ++++++++++++++++++- .../VisualBasicFullyQualifyService.vb | 2 +- .../FullyQualify/FullyQualifyTests.vb | 20 +++++++++ 4 files changed, 65 insertions(+), 5 deletions(-) diff --git a/src/Features/CSharp/Portable/FullyQualify/CSharpFullyQualifyService.cs b/src/Features/CSharp/Portable/FullyQualify/CSharpFullyQualifyService.cs index 4522e118cb4fd..a856c4a57fcb1 100644 --- a/src/Features/CSharp/Portable/FullyQualify/CSharpFullyQualifyService.cs +++ b/src/Features/CSharp/Portable/FullyQualify/CSharpFullyQualifyService.cs @@ -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 @@ -45,8 +44,7 @@ protected override async Task ReplaceNodeAsync(SimpleNameSyntax simp var newName = simpleName.WithLeadingTrivia(SyntaxTriviaList.Empty); var qualifiedName = SyntaxFactory.QualifiedName(SyntaxFactory.ParseName(containerName), newName) - .WithLeadingTrivia(leadingTrivia) - .WithAdditionalAnnotations(Formatter.Annotation); + .WithLeadingTrivia(leadingTrivia); var syntaxTree = simpleName.SyntaxTree; var root = await syntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false); diff --git a/src/Features/CSharpTest/FullyQualify/FullyQualifyTests.cs b/src/Features/CSharpTest/FullyQualify/FullyQualifyTests.cs index 9b4cc19b37578..eb86b15a31642 100644 --- a/src/Features/CSharpTest/FullyQualify/FullyQualifyTests.cs +++ b/src/Features/CSharpTest/FullyQualify/FullyQualifyTests.cs @@ -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) { @@ -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")] diff --git a/src/Features/VisualBasic/Portable/FullyQualify/VisualBasicFullyQualifyService.vb b/src/Features/VisualBasic/Portable/FullyQualify/VisualBasicFullyQualifyService.vb index 5b6d84c12f03a..b48bdfc1b8be3 100644 --- a/src/Features/VisualBasic/Portable/FullyQualify/VisualBasicFullyQualifyService.vb +++ b/src/Features/VisualBasic/Portable/FullyQualify/VisualBasicFullyQualifyService.vb @@ -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) diff --git a/src/Features/VisualBasicTest/FullyQualify/FullyQualifyTests.vb b/src/Features/VisualBasicTest/FullyQualify/FullyQualifyTests.vb index e96b83b65557d..4aab53c0790ff 100644 --- a/src/Features/VisualBasicTest/FullyQualify/FullyQualifyTests.vb +++ b/src/Features/VisualBasicTest/FullyQualify/FullyQualifyTests.vb @@ -55,6 +55,26 @@ Namespace SomeNamespace End Namespace", testHost:=testHost) End Function + + + 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 + Public Async Function TestOrdering(testHost As TestHost) As Task Dim code = "