From eea79f89c7dd25566991912aa6ccc141726f143d Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Wed, 14 Aug 2024 13:49:52 -0700 Subject: [PATCH 1/5] Do not run 'remove unnecessary imports' on generated code --- .../RemoveUnnecessaryImportsTests.cs | 26 ++----------------- ...oveUnnecessaryImportsDiagnosticAnalyzer.cs | 2 -- 2 files changed, 2 insertions(+), 26 deletions(-) diff --git a/src/Analyzers/CSharp/Tests/RemoveUnnecessaryImports/RemoveUnnecessaryImportsTests.cs b/src/Analyzers/CSharp/Tests/RemoveUnnecessaryImports/RemoveUnnecessaryImportsTests.cs index d06499cad9610..c538aa68e5bf9 100644 --- a/src/Analyzers/CSharp/Tests/RemoveUnnecessaryImports/RemoveUnnecessaryImportsTests.cs +++ b/src/Analyzers/CSharp/Tests/RemoveUnnecessaryImports/RemoveUnnecessaryImportsTests.cs @@ -186,22 +186,9 @@ public async Task TestGeneratedCode() var source = """ // - [|{|IDE0005_gen:using System;|} - using System.Collections.Generic; - {|IDE0005_gen:using System.Linq;|}|] - - class Program - { - static void Main(string[] args) - { - List d; - } - } - """; - var fixedSource = """ - // - + using System; using System.Collections.Generic; + using System.Linq; class Program { @@ -212,18 +199,9 @@ static void Main(string[] args) } """; - // Fix All operations in generated code do not apply changes - var batchFixedSource = source; - await new VerifyCS.Test { TestCode = source, - FixedCode = fixedSource, - BatchFixedState = - { - Sources = { batchFixedSource }, - MarkupHandling = MarkupMode.Allow, - }, }.RunAsync(); } diff --git a/src/Analyzers/Core/Analyzers/RemoveUnnecessaryImports/AbstractRemoveUnnecessaryImportsDiagnosticAnalyzer.cs b/src/Analyzers/Core/Analyzers/RemoveUnnecessaryImports/AbstractRemoveUnnecessaryImportsDiagnosticAnalyzer.cs index 6c860d117931f..72325a9dc6379 100644 --- a/src/Analyzers/Core/Analyzers/RemoveUnnecessaryImports/AbstractRemoveUnnecessaryImportsDiagnosticAnalyzer.cs +++ b/src/Analyzers/Core/Analyzers/RemoveUnnecessaryImports/AbstractRemoveUnnecessaryImportsDiagnosticAnalyzer.cs @@ -71,8 +71,6 @@ private static ImmutableArray GetDescriptors(LocalizableSt protected abstract bool IsRegularCommentOrDocComment(SyntaxTrivia trivia); protected abstract IUnnecessaryImportsProvider UnnecessaryImportsProvider { get; } - protected override GeneratedCodeAnalysisFlags GeneratedCodeAnalysisFlags => GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics; - protected abstract SyntaxToken? TryGetLastToken(SyntaxNode node); protected override void InitializeWorker(AnalysisContext context) From 78759a281f53b34ac213588778db7cb3aa14cb29 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Wed, 13 Nov 2024 09:54:35 -0800 Subject: [PATCH 2/5] Switch to primary constructor --- .../New.IntegrationTests/CSharp/CSharpCodeActions.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/VisualStudio/IntegrationTest/New.IntegrationTests/CSharp/CSharpCodeActions.cs b/src/VisualStudio/IntegrationTest/New.IntegrationTests/CSharp/CSharpCodeActions.cs index 08bd9fc336185..039163c07229a 100644 --- a/src/VisualStudio/IntegrationTest/New.IntegrationTests/CSharp/CSharpCodeActions.cs +++ b/src/VisualStudio/IntegrationTest/New.IntegrationTests/CSharp/CSharpCodeActions.cs @@ -25,13 +25,8 @@ namespace Roslyn.VisualStudio.NewIntegrationTests.CSharp; -public class CSharpCodeActions : AbstractEditorTest +public sealed class CSharpCodeActions() : AbstractEditorTest(nameof(CSharpCodeActions)) { - public CSharpCodeActions() - : base(nameof(CSharpCodeActions)) - { - } - protected override string LanguageName => LanguageNames.CSharp; [IdeFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)] From 8abea8b76b6aa5c17303507bfbacf8d2d9175549 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Wed, 13 Nov 2024 09:55:47 -0800 Subject: [PATCH 3/5] Switch to raw strings --- .../CSharp/CSharpCodeActions.cs | 1315 +++++++++-------- 1 file changed, 680 insertions(+), 635 deletions(-) diff --git a/src/VisualStudio/IntegrationTest/New.IntegrationTests/CSharp/CSharpCodeActions.cs b/src/VisualStudio/IntegrationTest/New.IntegrationTests/CSharp/CSharpCodeActions.cs index 039163c07229a..e3b7b6dee553e 100644 --- a/src/VisualStudio/IntegrationTest/New.IntegrationTests/CSharp/CSharpCodeActions.cs +++ b/src/VisualStudio/IntegrationTest/New.IntegrationTests/CSharp/CSharpCodeActions.cs @@ -33,38 +33,38 @@ public sealed class CSharpCodeActions() : AbstractEditorTest(nameof(CSharpCodeAc public async Task GenerateMethodInClosedFile() { var project = ProjectName; - await TestServices.SolutionExplorer.AddFileAsync(project, "Foo.cs", contents: @" -public class Foo -{ -} -", cancellationToken: HangMitigatingCancellationToken); + await TestServices.SolutionExplorer.AddFileAsync(project, "Foo.cs", contents: """ + public class Foo + { + } + """, cancellationToken: HangMitigatingCancellationToken); - await SetUpEditorAsync(@" -using System; + await SetUpEditorAsync(""" + using System; -public class Program -{ - public static void Main(string[] args) - { - Foo f = new Foo(); - f.Bar()$$ - } -} -", HangMitigatingCancellationToken); + public class Program + { + public static void Main(string[] args) + { + Foo f = new Foo(); + f.Bar()$$ + } + } + """, HangMitigatingCancellationToken); await TestServices.Editor.InvokeCodeActionListAsync(HangMitigatingCancellationToken); await TestServices.EditorVerifier.CodeActionAsync("Generate method 'Bar'", applyFix: true, cancellationToken: HangMitigatingCancellationToken); - await TestServices.SolutionVerifier.FileContentsAsync(project, "Foo.cs", @" -using System; + await TestServices.SolutionVerifier.FileContentsAsync(project, "Foo.cs", """ + using System; -public class Foo -{ - internal void Bar() - { - throw new NotImplementedException(); - } -} -", HangMitigatingCancellationToken); + public class Foo + { + internal void Bar() + { + throw new NotImplementedException(); + } + } + """, HangMitigatingCancellationToken); } [IdeFact, Trait(Traits.Feature, Traits.Features.CodeActionsAddImport)] @@ -72,12 +72,12 @@ public async Task AddUsingOnIncompleteMember() { // Need to ensure that incomplete member diagnostics run at high pri so that add-using can be // triggered by them. - await SetUpEditorAsync(@" -class Program -{ - DateTime$$ -} -", HangMitigatingCancellationToken); + await SetUpEditorAsync(""" + class Program + { + DateTime$$ + } + """, HangMitigatingCancellationToken); await TestServices.Editor.InvokeCodeActionListAsync(HangMitigatingCancellationToken); await TestServices.EditorVerifier.CodeActionAsync("using System;", cancellationToken: HangMitigatingCancellationToken); } @@ -90,15 +90,15 @@ public async Task FastDoubleInvoke() // to get it to invoke without any sort of waiting to happen. This helps address a bug // we had where our asynchronous smart tags interfered with asynchrony in VS, which caused // the second smart tag to not expand if you tried invoking it too quickly - await SetUpEditorAsync(@" -class Program -{ - static void Main(string[] args) - { - Exception $$ex = new System.ArgumentException(); - } -} -", HangMitigatingCancellationToken); + await SetUpEditorAsync(""" + class Program + { + static void Main(string[] args) + { + Exception $$ex = new System.ArgumentException(); + } + } + """, HangMitigatingCancellationToken); // Suspend file change notification during code action application, since spurious file change notifications // can cause silent failure to apply the code action if they occur within this block. @@ -117,44 +117,46 @@ static void Main(string[] args) } await TestServices.EditorVerifier.TextContainsAsync( - @" -using System; + """ + using System; -class Program -{ - static void Main(string[] args) - { - Exception ex = new ArgumentException(); - } -}", cancellationToken: HangMitigatingCancellationToken); + class Program + { + static void Main(string[] args) + { + Exception ex = new ArgumentException(); + } + } + """, cancellationToken: HangMitigatingCancellationToken); } [IdeFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvokeDelegateWithConditionalAccess)] public async Task InvokeDelegateWithConditionalAccessMultipleTimes() { - var markup = @" -using System; -class C -{ - public event EventHandler First; - public event EventHandler Second; - void RaiseFirst() - { - var temp1 = First; - if (temp1 != null) - { - temp1$$(this, EventArgs.Empty); - } - } - void RaiseSecond() - { - var temp2 = Second; - if (temp2 != null) - { - temp2(this, EventArgs.Empty); - } - } -}"; + var markup = """ + using System; + class C + { + public event EventHandler First; + public event EventHandler Second; + void RaiseFirst() + { + var temp1 = First; + if (temp1 != null) + { + temp1$$(this, EventArgs.Empty); + } + } + void RaiseSecond() + { + var temp2 = Second; + if (temp2 != null) + { + temp2(this, EventArgs.Empty); + } + } + } + """; MarkupTestFile.GetSpans(markup, out _, out var _); await SetUpEditorAsync(markup, HangMitigatingCancellationToken); @@ -174,40 +176,42 @@ void RaiseSecond() [WorkItem("https://github.com/dotnet/roslyn/issues/19089")] public async Task ApplyEditorConfigAndFixAllOccurrences() { - var markup = @" -class C -{ - public int X1 - { - get - { - $$return 3; - } - } - - public int Y1 => 5; - - public int X2 - { - get - { - return 3; - } - } - - public int Y2 => 5; -}"; - var expectedText = @" -class C -{ - public int X1 => 3; + var markup = """ + class C + { + public int X1 + { + get + { + $$return 3; + } + } + + public int Y1 => 5; + + public int X2 + { + get + { + return 3; + } + } + + public int Y2 => 5; + } + """; + var expectedText = """ + class C + { + public int X1 => 3; - public int Y1 => 5; + public int Y1 => 5; - public int X2 => 3; + public int X2 => 3; - public int Y2 => 5; -}"; + public int Y2 => 5; + } + """; await TestServices.SolutionExplorer.OpenFileAsync(ProjectName, "Class1.cs", HangMitigatingCancellationToken); @@ -229,11 +233,12 @@ await TestServices.Workspace.WaitForAllAsyncOperationsAsync( HangMitigatingCancellationToken); await TestServices.EditorVerifier.CodeActionsNotShowingAsync(HangMitigatingCancellationToken); - var editorConfig = @"root = true + var editorConfig = """ + root = true -[*.cs] -csharp_style_expression_bodied_properties = true:warning -"; + [*.cs] + csharp_style_expression_bodied_properties = true:warning + """; await TestServices.SolutionExplorer.AddFileAsync(ProjectName, ".editorconfig", editorConfig, open: false, HangMitigatingCancellationToken); @@ -278,41 +283,42 @@ await TestServices.EditorVerifier.CodeActionAsync( fixAllScope: FixAllScope.Project, cancellationToken: HangMitigatingCancellationToken); - expectedText = @" -class C -{ - public int X1 - { - get - { - return 3; - } - } - - public int Y1 - { - get - { - return 5; - } - } - - public int X2 - { - get - { - return 3; - } - } - - public int Y2 - { - get - { - return 5; - } - } -}"; + expectedText = """ + class C + { + public int X1 + { + get + { + return 3; + } + } + + public int Y1 + { + get + { + return 5; + } + } + + public int X2 + { + get + { + return 3; + } + } + + public int Y2 + { + get + { + return 5; + } + } + } + """; AssertEx.EqualOrDiff(expectedText, await TestServices.Editor.GetTextAsync(HangMitigatingCancellationToken)); } @@ -324,36 +330,42 @@ public int Y2 [WorkItem("https://github.com/dotnet/roslyn/issues/33507")] public async Task FixAllOccurrencesIgnoresGeneratedCode(FixAllScope scope) { - var markup = @" -using System; -using $$System.Threading; + var markup = """ + using System; + using $$System.Threading; -class C -{ - public IntPtr X1 { get; set; } -}"; - var expectedText = @" -using System; + class C + { + public IntPtr X1 { get; set; } + } + """; + var expectedText = """ + using System; -class C -{ - public IntPtr X1 { get; set; } -}"; - var generatedSourceMarkup = @"// -using System; -using $$System.Threading; + class C + { + public IntPtr X1 { get; set; } + } + """; + var generatedSourceMarkup = """ + // + using System; + using $$System.Threading; -class D -{ - public IntPtr X1 { get; set; } -}"; - var expectedGeneratedSource = @"// -using System; + class D + { + public IntPtr X1 { get; set; } + } + """; + var expectedGeneratedSource = """ + // + using System; -class D -{ - public IntPtr X1 { get; set; } -}"; + class D + { + public IntPtr X1 { get; set; } + } + """; MarkupTestFile.GetPosition(generatedSourceMarkup, out var generatedSource, out int generatedSourcePosition); @@ -411,29 +423,33 @@ await TestServices.EditorVerifier.CodeActionAsync( [WorkItem("https://github.com/dotnet/roslyn/issues/33507")] public async Task FixAllOccurrencesTriggeredFromGeneratedCode(FixAllScope scope) { - var markup = @"// -using System; -using $$System.Threading; + var markup = """ + // + using System; + using $$System.Threading; -class C -{ - public IntPtr X1 { get; set; } -}"; - var secondFile = @" -using System; -using System.Threading; + class C + { + public IntPtr X1 { get; set; } + } + """; + var secondFile = """ + using System; + using System.Threading; -class D -{ - public IntPtr X1 { get; set; } -}"; - var expectedSecondFile = @" -using System; + class D + { + public IntPtr X1 { get; set; } + } + """; + var expectedSecondFile = """ + using System; -class D -{ - public IntPtr X1 { get; set; } -}"; + class D + { + public IntPtr X1 { get; set; } + } + """; await TestServices.SolutionExplorer.AddFileAsync(ProjectName, "D.cs", secondFile, open: false, HangMitigatingCancellationToken); @@ -462,14 +478,15 @@ await TestServices.EditorVerifier.CodeActionAsync( [IdeFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)] public async Task ClassificationInPreviewPane() { - await SetUpEditorAsync(@" -class Program -{ - int Main() - { - Foo$$(); - } -}", HangMitigatingCancellationToken); + await SetUpEditorAsync(""" + class Program + { + int Main() + { + Foo$$(); + } + } + """, HangMitigatingCancellationToken); await TestServices.Editor.InvokeCodeActionListAsync(HangMitigatingCancellationToken); var classifiedTokens = await TestServices.Editor.GetLightBulbPreviewClassificationsAsync("Generate method 'Foo'", HangMitigatingCancellationToken); Assert.True(classifiedTokens.Any(c => c.Span.GetText().ToString() == "void" && c.ClassificationType.Classification == "keyword")); @@ -478,16 +495,17 @@ int Main() [IdeFact, Trait(Traits.Feature, Traits.Features.CodeActionsAddImport)] public async Task AddUsingExactMatchBeforeRenameTracking() { - await SetUpEditorAsync(@" -public class Program -{ - static void Main(string[] args) - { - P2$$ p; - } -} + await SetUpEditorAsync(""" + public class Program + { + static void Main(string[] args) + { + P2$$ p; + } + } -public class P2 { }", HangMitigatingCancellationToken); + public class P2 { } + """, HangMitigatingCancellationToken); await TestServices.Input.SendAsync([VirtualKeyCode.BACK, VirtualKeyCode.BACK, "Stream"], HangMitigatingCancellationToken); await TestServices.Workspace.WaitForAllAsyncOperationsAsync( @@ -538,22 +556,23 @@ await TestServices.Workspace.WaitForAllAsyncOperationsAsync( [IdeFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateType)] public async Task GFUFuzzyMatchAfterRenameTrackingAndAfterGenerateType() { - await SetUpEditorAsync(@" -namespace N -{ - class Goober { } -} + await SetUpEditorAsync(""" + namespace N + { + class Goober { } + } -namespace NS -{ - public class P2 - { - static void Main(string[] args) - { - P2$$ p; - } - } -}", HangMitigatingCancellationToken); + namespace NS + { + public class P2 + { + static void Main(string[] args) + { + P2$$ p; + } + } + } + """, HangMitigatingCancellationToken); await TestServices.Input.SendAsync([VirtualKeyCode.BACK, VirtualKeyCode.BACK, "Foober"], HangMitigatingCancellationToken); await TestServices.Workspace.WaitForAllAsyncOperationsAsync( [ @@ -594,18 +613,19 @@ await TestServices.Workspace.WaitForAllAsyncOperationsAsync( [IdeFact, Trait(Traits.Feature, Traits.Features.CodeGeneration)] public async Task SuppressionAfterRefactorings() { - await SetUpEditorAsync(@" -[System.Obsolete] -class C -{ -} -class Program -{ - static void Main(string[] args) - { - C p = $$2; - } -}", HangMitigatingCancellationToken); + await SetUpEditorAsync(""" + [System.Obsolete] + class C + { + } + class Program + { + static void Main(string[] args) + { + C p = $$2; + } + } + """, HangMitigatingCancellationToken); await TestServices.Editor.SelectTextInCurrentDocumentAsync("2", HangMitigatingCancellationToken); await TestServices.Editor.InvokeCodeActionListAsync(HangMitigatingCancellationToken); @@ -637,16 +657,17 @@ static void Main(string[] args) [IdeFact, Trait(Traits.Feature, Traits.Features.CodeActionsAddImport)] public async Task OrderFixesByCursorProximityLeft() { - await SetUpEditorAsync(@" -using System; -public class Program -{ - static void Main(string[] args) - { - Byte[] bytes = null; - GCHandle$$ handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); - } -}", HangMitigatingCancellationToken); + await SetUpEditorAsync(""" + using System; + public class Program + { + static void Main(string[] args) + { + Byte[] bytes = null; + GCHandle$$ handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); + } + } + """, HangMitigatingCancellationToken); await TestServices.Editor.InvokeCodeActionListAsync(HangMitigatingCancellationToken); var expectedItems = new[] { @@ -661,16 +682,17 @@ static void Main(string[] args) [IdeFact, Trait(Traits.Feature, Traits.Features.CodeActionsAddImport)] public async Task OrderFixesByCursorProximityRight() { - await SetUpEditorAsync(@" -using System; -public class Program -{ - static void Main(string[] args) - { - Byte[] bytes = null; - GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.$$Pinned); - } -}", HangMitigatingCancellationToken); + await SetUpEditorAsync(""" + using System; + public class Program + { + static void Main(string[] args) + { + Byte[] bytes = null; + GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.$$Pinned); + } + } + """, HangMitigatingCancellationToken); await TestServices.Editor.InvokeCodeActionListAsync(HangMitigatingCancellationToken); var expectedItems = new[] { @@ -685,15 +707,16 @@ static void Main(string[] args) [IdeFact, Trait(Traits.Feature, Traits.Features.CodeActionsConfiguration)] public async Task ConfigureCodeStyleOptionValueAndSeverity() { - await SetUpEditorAsync(@" -using System; -public class Program -{ - static void Main(string[] args) - { - var $$x = new Program(); - } -}", HangMitigatingCancellationToken); + await SetUpEditorAsync(""" + using System; + public class Program + { + static void Main(string[] args) + { + var $$x = new Program(); + } + } + """, HangMitigatingCancellationToken); await TestServices.Editor.InvokeCodeActionListAsync(HangMitigatingCancellationToken); await TestServices.EditorVerifier.CodeActionsAsync([ @@ -709,15 +732,16 @@ await TestServices.EditorVerifier.CodeActionsAsync([ [WorkItem("https://github.com/dotnet/roslyn/issues/46784")] public async Task ConfigureSeverity() { - var markup = @" -class C -{ - public static void Main() - { - // CS0168: The variable 'x' is declared but never used - int $$x; - } -}"; + var markup = """ + class C + { + public static void Main() + { + // CS0168: The variable 'x' is declared but never used + int $$x; + } + } + """; await SetUpEditorAsync(markup, HangMitigatingCancellationToken); await TestServices.Workspace.WaitForAllAsyncOperationsAsync( @@ -797,15 +821,16 @@ static async Task VerifyDiagnosticInErrorListAsync(string expectedSeverity, Test [WorkItem("https://github.com/dotnet/roslyn/issues/46784")] public async Task ConfigureSeverityWithManualEditsToEditorconfig() { - var markup = @" -class C -{ - public static void Main() - { - // CS0168: The variable 'x' is declared but never used - int $$x; - } -}"; + var markup = """ + class C + { + public static void Main() + { + // CS0168: The variable 'x' is declared but never used + int $$x; + } + } + """; await SetUpEditorAsync(markup, HangMitigatingCancellationToken); await TestServices.Workspace.WaitForAllAsyncOperationsAsync( @@ -822,9 +847,10 @@ await TestServices.Workspace.WaitForAllAsyncOperationsAsync( // Add an .editorconfig file to the project to change severity to error. await TestServices.SolutionExplorer.AddFileAsync(ProjectName, ".editorconfig", open: true, cancellationToken: HangMitigatingCancellationToken); - await TestServices.Input.SendAsync(@" -[*.cs] -dotnet_diagnostic.CS0168.severity = ", HangMitigatingCancellationToken); + await TestServices.Input.SendAsync(""" + [*.cs] + dotnet_diagnostic.CS0168.severity = + """, HangMitigatingCancellationToken); // NOTE: Below wait is a critical step in repro-ing the original regression. await TestServices.Workspace.WaitForAllAsyncOperationsAsync( @@ -879,27 +905,29 @@ static async Task VerifyDiagnosticInErrorListAsync(string expectedSeverity, Test [InlineData(BackgroundAnalysisScope.FullSolution, CompilerDiagnosticsScope.FullSolution)] internal async Task ConfigureSeverityWithManualEditsToEditorconfig_CurrentDocumentScope(BackgroundAnalysisScope analyzerScope, CompilerDiagnosticsScope compilerScope) { - var markup1 = @" -class C -{ - public static void Main() - { - // CS0219: The variable 'x' is assigned but its value is never used - // IDE0059: Unnecessary assignment of a value to 'x' - int x = 0; - } -}"; + var markup1 = """ + class C + { + public static void Main() + { + // CS0219: The variable 'x' is assigned but its value is never used + // IDE0059: Unnecessary assignment of a value to 'x' + int x = 0; + } + } + """; - var markup2 = @" -class C2 -{ - public static void M() - { - // CS0219: The variable 'y' is assigned but its value is never used - // IDE0059: Unnecessary assignment of a value to 'y' - int $$y = 0; - } -}"; + var markup2 = """ + class C2 + { + public static void M() + { + // CS0219: The variable 'y' is assigned but its value is never used + // IDE0059: Unnecessary assignment of a value to 'y' + int $$y = 0; + } + } + """; await TestServices.Workspace.SetBackgroundAnalysisOptionsAsync(analyzerScope, compilerScope, HangMitigatingCancellationToken); await SetUpEditorAsync(markup2, HangMitigatingCancellationToken); @@ -929,10 +957,11 @@ await TestServices.Workspace.WaitForAllAsyncOperationsAsync( // Add an .editorconfig file to the project to change severities to error. await TestServices.SolutionExplorer.AddFileAsync(ProjectName, ".editorconfig", open: true, cancellationToken: HangMitigatingCancellationToken); - await TestServices.Editor.SetTextAsync(@" -[*.cs] -dotnet_diagnostic.CS0219.severity = error -dotnet_diagnostic.IDE0059.severity = error", HangMitigatingCancellationToken); + await TestServices.Editor.SetTextAsync(""" + [*.cs] + dotnet_diagnostic.CS0219.severity = error + dotnet_diagnostic.IDE0059.severity = error + """, HangMitigatingCancellationToken); await TestServices.Workspace.WaitForAllAsyncOperationsAsync( [ @@ -947,10 +976,11 @@ await TestServices.Workspace.WaitForAllAsyncOperationsAsync( await VerifyDiagnosticsInErrorListAsync("error", "error", TestServices, HangMitigatingCancellationToken); // Edit editorconfig file to disable both compiler and analyzer diagnostics. - await TestServices.Editor.SetTextAsync(@" -[*.cs] -dotnet_diagnostic.CS0219.severity = none -dotnet_diagnostic.IDE0059.severity = none", HangMitigatingCancellationToken); + await TestServices.Editor.SetTextAsync(""" + [*.cs] + dotnet_diagnostic.CS0219.severity = none + dotnet_diagnostic.IDE0059.severity = none + """, HangMitigatingCancellationToken); await TestServices.Workspace.WaitForAllAsyncOperationsAsync( [ @@ -1005,64 +1035,66 @@ static async Task VerifyDiagnosticsInErrorListAsync(string expectedCompilerDiagn [Trait(Traits.Feature, Traits.Features.CodeActionsFixAllOccurrences)] public async Task TestFixAllOccurrences_CodeFix_ContainingMember() { - var markup = @" -class Program1 -{ - static void Main() - { - $$if (true) if (true) return; + var markup = """ + class Program1 + { + static void Main() + { + $$if (true) if (true) return; - if (false) if (false) return; - } + if (false) if (false) return; + } - void OtherMethod() - { - if (true) if (true) return; - } -} + void OtherMethod() + { + if (true) if (true) return; + } + } -class OtherType -{ - void OtherMethod() - { - if (true) if (true) return; - } -}"; - var expectedText = @" -class Program1 -{ - static void Main() - { - if (true) - { - if (true) + class OtherType { - return; + void OtherMethod() + { + if (true) if (true) return; + } } - } - - if (false) - { - if (false) + """; + var expectedText = """ + class Program1 { - return; + static void Main() + { + if (true) + { + if (true) + { + return; + } + } + + if (false) + { + if (false) + { + return; + } + } + } + + void OtherMethod() + { + if (true) if (true) return; + } } - } - } - - void OtherMethod() - { - if (true) if (true) return; - } -} -class OtherType -{ - void OtherMethod() - { - if (true) if (true) return; - } -}"; + class OtherType + { + void OtherMethod() + { + if (true) if (true) return; + } + } + """; await TestServices.SolutionExplorer.OpenFileAsync(ProjectName, "Class1.cs", HangMitigatingCancellationToken); @@ -1092,141 +1124,145 @@ await TestServices.EditorVerifier.CodeActionAsync( [Trait(Traits.Feature, Traits.Features.CodeActionsFixAllOccurrences)] public async Task TestFixAllOccurrences_CodeFix_ContainingType() { - var markup1 = @" -partial class Program1 -{ - static void Main() - { - $$if (true) if (true) return; - - if (false) if (false) return; - } + var markup1 = """ + partial class Program1 + { + static void Main() + { + $$if (true) if (true) return; - void M1() - { - if (true) if (true) return; - } -} + if (false) if (false) return; + } -class OtherType1 -{ - void OtherMethod() - { - if (true) if (true) return; - } -} + void M1() + { + if (true) if (true) return; + } + } -partial class Program1 -{ - void M2() - { - if (true) if (true) return; - } -}"; - var expectedText1 = @" -partial class Program1 -{ - static void Main() - { - if (true) - { - if (true) + class OtherType1 { - return; + void OtherMethod() + { + if (true) if (true) return; + } } - } - if (false) - { - if (false) + partial class Program1 { - return; + void M2() + { + if (true) if (true) return; + } } - } - } - - void M1() - { - if (true) - { - if (true) + """; + var expectedText1 = """ + partial class Program1 { - return; + static void Main() + { + if (true) + { + if (true) + { + return; + } + } + + if (false) + { + if (false) + { + return; + } + } + } + + void M1() + { + if (true) + { + if (true) + { + return; + } + } + } } - } - } -} -class OtherType1 -{ - void OtherMethod() - { - if (true) if (true) return; - } -} + class OtherType1 + { + void OtherMethod() + { + if (true) if (true) return; + } + } -partial class Program1 -{ - void M2() - { - if (true) - { - if (true) + partial class Program1 { - return; + void M2() + { + if (true) + { + if (true) + { + return; + } + } + } } - } - } -}"; + """; - var markup2 = @" -partial class Program1 -{ - void OtherFileMethod() - { - if (true) if (true) return; + var markup2 = """ + partial class Program1 + { + void OtherFileMethod() + { + if (true) if (true) return; - if (false) if (false) return; - } -} + if (false) if (false) return; + } + } -class OtherType2 -{ - void OtherMethod() - { - if (true) if (true) return; - } -}"; - var expectedText2 = @" -partial class Program1 -{ - void OtherFileMethod() - { - if (true) - { - if (true) + class OtherType2 { - return; + void OtherMethod() + { + if (true) if (true) return; + } } - } - - if (false) - { - if (false) + """; + var expectedText2 = """ + partial class Program1 { - return; + void OtherFileMethod() + { + if (true) + { + if (true) + { + return; + } + } + + if (false) + { + if (false) + { + return; + } + } + } } - } - } -} -class OtherType2 -{ - void OtherMethod() - { - if (true) if (true) return; - } -}"; + class OtherType2 + { + void OtherMethod() + { + if (true) if (true) return; + } + } + """; await TestServices.SolutionExplorer.AddFileAsync(ProjectName, "Class2.cs", markup2, cancellationToken: HangMitigatingCancellationToken); await TestServices.SolutionExplorer.OpenFileAsync(ProjectName, "Class1.cs", HangMitigatingCancellationToken); @@ -1259,54 +1295,56 @@ await TestServices.EditorVerifier.CodeActionAsync( [Trait(Traits.Feature, Traits.Features.CodeActionsFixAllOccurrences)] public async Task TestFixAllOccurrences_CodeRefactoring_ContainingMember() { - var markup = @" -class C1 -{ - void M() - { - var singleLine1 = $$""a""; - var singleLine2 = @""goo""""bar""; - } - - void M2() - { - var singleLine1 = ""a""; - var singleLine2 = @""goo""""bar""; - } -} - -class C2 -{ - void M3() - { - var singleLine1 = ""a""; - var singleLine2 = @""goo""""bar""; - } -}"; - var expectedText = @" -class C1 -{ - void M() - { - var singleLine1 = """"""a""""""; - var singleLine2 = """"""goo""bar""""""; - } + var markup = """ + class C1 + { + void M() + { + var singleLine1 = $$"a"; + var singleLine2 = @"goo""bar"; + } + + void M2() + { + var singleLine1 = "a"; + var singleLine2 = @"goo""bar"; + } + } - void M2() - { - var singleLine1 = ""a""; - var singleLine2 = @""goo""""bar""; - } -} + class C2 + { + void M3() + { + var singleLine1 = "a"; + var singleLine2 = @"goo""bar"; + } + } + """; + var expectedText = """" + class C1 + { + void M() + { + var singleLine1 = """a"""; + var singleLine2 = """goo"bar"""; + } + + void M2() + { + var singleLine1 = "a"; + var singleLine2 = @"goo""bar"; + } + } -class C2 -{ - void M3() - { - var singleLine1 = ""a""; - var singleLine2 = @""goo""""bar""; - } -}"; + class C2 + { + void M3() + { + var singleLine1 = "a"; + var singleLine2 = @"goo""bar"; + } + } + """"; await TestServices.SolutionExplorer.OpenFileAsync(ProjectName, "Class1.cs", HangMitigatingCancellationToken); @@ -1336,109 +1374,113 @@ await TestServices.EditorVerifier.CodeActionAsync( [Trait(Traits.Feature, Traits.Features.CodeActionsFixAllOccurrences)] public async Task TestFixAllOccurrences_CodeRefactoring_ContainingType() { - var markup1 = @" -partial class C1 -{ - void M() - { - var singleLine1 = $$""a""; - var singleLine2 = @""goo""""bar""; - } - - void M2() - { - var singleLine1 = ""a""; - var singleLine2 = @""goo""""bar""; - } -} - -class C2 -{ - void M3() - { - var singleLine1 = ""a""; - var singleLine2 = @""goo""""bar""; - } -} + var markup1 = """ + partial class C1 + { + void M() + { + var singleLine1 = $$"a"; + var singleLine2 = @"goo""bar"; + } + + void M2() + { + var singleLine1 = "a"; + var singleLine2 = @"goo""bar"; + } + } -partial class C1 -{ - void M4() - { - var singleLine1 = ""a""; - var singleLine2 = @""goo""""bar""; - } -}"; - var expectedText1 = @" -partial class C1 -{ - void M() - { - var singleLine1 = """"""a""""""; - var singleLine2 = """"""goo""bar""""""; - } + class C2 + { + void M3() + { + var singleLine1 = "a"; + var singleLine2 = @"goo""bar"; + } + } - void M2() - { - var singleLine1 = """"""a""""""; - var singleLine2 = """"""goo""bar""""""; - } -} + partial class C1 + { + void M4() + { + var singleLine1 = "a"; + var singleLine2 = @"goo""bar"; + } + } + """; + var expectedText1 = """" + partial class C1 + { + void M() + { + var singleLine1 = """a"""; + var singleLine2 = """goo"bar"""; + } + + void M2() + { + var singleLine1 = """a"""; + var singleLine2 = """goo"bar"""; + } + } -class C2 -{ - void M3() - { - var singleLine1 = ""a""; - var singleLine2 = @""goo""""bar""; - } -} + class C2 + { + void M3() + { + var singleLine1 = "a"; + var singleLine2 = @"goo""bar"; + } + } -partial class C1 -{ - void M4() - { - var singleLine1 = """"""a""""""; - var singleLine2 = """"""goo""bar""""""; - } -}"; + partial class C1 + { + void M4() + { + var singleLine1 = """a"""; + var singleLine2 = """goo"bar"""; + } + } + """"; - var markup2 = @" -partial class C1 -{ - void M5() - { - var singleLine1 = ""a""; - var singleLine2 = @""goo""""bar""; - } -} + var markup2 = """ + partial class C1 + { + void M5() + { + var singleLine1 = "a"; + var singleLine2 = @"goo""bar"; + } + } -class C2 -{ - void M6() - { - var singleLine1 = ""a""; - var singleLine2 = @""goo""""bar""; - } -}"; - var expectedText2 = @" -partial class C1 -{ - void M5() - { - var singleLine1 = """"""a""""""; - var singleLine2 = """"""goo""bar""""""; - } -} + class C2 + { + void M6() + { + var singleLine1 = "a"; + var singleLine2 = @"goo""bar"; + } + } + """; + var expectedText2 = """" + partial class C1 + { + void M5() + { + var singleLine1 = """a"""; + var singleLine2 = """goo"bar"""; + } + } -class C2 -{ - void M6() - { - var singleLine1 = ""a""; - var singleLine2 = @""goo""""bar""; - } -}"; + class C2 + { + void M6() + { + var singleLine1 = "a"; + var singleLine2 = @"goo""bar"; + } + } + """"; await TestServices.SolutionExplorer.AddFileAsync(ProjectName, "Class2.cs", markup2, cancellationToken: HangMitigatingCancellationToken); await TestServices.SolutionExplorer.OpenFileAsync(ProjectName, "Class1.cs", HangMitigatingCancellationToken); @@ -1471,15 +1513,15 @@ await TestServices.EditorVerifier.CodeActionAsync( [WorkItem("https://github.com/dotnet/roslyn/issues/61334")] public async Task UseExpressionBodyBeforeExtractBaseClass() { - await SetUpEditorAsync(@" -public class Program -{ - $$public void M() - { - System.Console.WriteLine(0); - } -} -", HangMitigatingCancellationToken); + await SetUpEditorAsync(""" + public class Program + { + $$public void M() + { + System.Console.WriteLine(0); + } + } + """, HangMitigatingCancellationToken); await TestServices.Workspace.WaitForAllAsyncOperationsAsync( [ @@ -1512,8 +1554,10 @@ await TestServices.Workspace.WaitForAllAsyncOperationsAsync( public async Task TestNonSourceDocumentRefactoring() { var markup = @"$$# Editorconfig File"; - var expectedText = @"# Editorconfig File -# Refactored"; + var expectedText = """ + # Editorconfig File + # Refactored + """; await TestServices.SolutionExplorer.OpenFileAsync(ProjectName, "Class1.cs", HangMitigatingCancellationToken); await TestServices.SolutionExplorer.AddAnalyzerReferenceAsync(ProjectName, typeof(NonSourceFileRefactoring).Assembly.Location, HangMitigatingCancellationToken); @@ -1543,14 +1587,15 @@ await TestServices.EditorVerifier.CodeActionAsync( [IdeFact, Trait(Traits.Feature, Traits.Features.CodeGeneration)] public async Task TestRefactoringsAreSortedByPriority() { - var codeFormat = @" -#pragma warning disable IDE0060 // Remove unused parameter -class C -{ - public C(int x1, int x2, int x3) - { - } -};"; + var codeFormat = """ + #pragma warning disable IDE0060 // Remove unused parameter + class C + { + public C(int x1, int x2, int x3) + { + } + }; + """; for (var i = 1; i <= 3; i++) { var code = codeFormat.Replace($"x{i}", $"$$x{i}"); From 6121e9b4e74c7a0789065f383dbf697c7311017f Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Wed, 13 Nov 2024 10:09:26 -0800 Subject: [PATCH 4/5] Fix test --- .../CSharp/CSharpCodeActions.cs | 31 ++----------------- 1 file changed, 2 insertions(+), 29 deletions(-) diff --git a/src/VisualStudio/IntegrationTest/New.IntegrationTests/CSharp/CSharpCodeActions.cs b/src/VisualStudio/IntegrationTest/New.IntegrationTests/CSharp/CSharpCodeActions.cs index e3b7b6dee553e..f2cc6b6731668 100644 --- a/src/VisualStudio/IntegrationTest/New.IntegrationTests/CSharp/CSharpCodeActions.cs +++ b/src/VisualStudio/IntegrationTest/New.IntegrationTests/CSharp/CSharpCodeActions.cs @@ -328,6 +328,7 @@ public int Y2 [InlineData(FixAllScope.Solution)] [Trait(Traits.Feature, Traits.Features.CodeActionsFixAllOccurrences)] [WorkItem("https://github.com/dotnet/roslyn/issues/33507")] + [WorkItem("https://github.com/dotnet/roslyn/issues/74761")] public async Task FixAllOccurrencesIgnoresGeneratedCode(FixAllScope scope) { var markup = """ @@ -352,15 +353,6 @@ class C using System; using $$System.Threading; - class D - { - public IntPtr X1 { get; set; } - } - """; - var expectedGeneratedSource = """ - // - using System; - class D { public IntPtr X1 { get; set; } @@ -394,26 +386,7 @@ await TestServices.EditorVerifier.CodeActionAsync( // The current behavior is observable; any change to this behavior should be part of an intentional design // change. await TestServices.Editor.MoveCaretAsync(generatedSourcePosition, HangMitigatingCancellationToken); - await TestServices.Editor.InvokeCodeActionListAsync(HangMitigatingCancellationToken); - await TestServices.EditorVerifier.CodeActionAsync( - "Remove unnecessary usings", - applyFix: true, - fixAllScope: FixAllScope.Document, - cancellationToken: HangMitigatingCancellationToken); - - AssertEx.EqualOrDiff(generatedSource, await TestServices.Editor.GetTextAsync(HangMitigatingCancellationToken)); - - // Verify that the code action can still be applied manually from within the generated file. - // This is a regression test for correctness with respect to the design. - await TestServices.Editor.MoveCaretAsync(generatedSourcePosition, HangMitigatingCancellationToken); - await TestServices.Editor.InvokeCodeActionListAsync(HangMitigatingCancellationToken); - await TestServices.EditorVerifier.CodeActionAsync( - "Remove unnecessary usings", - applyFix: true, - fixAllScope: null, - cancellationToken: HangMitigatingCancellationToken); - - AssertEx.EqualOrDiff(expectedGeneratedSource, await TestServices.Editor.GetTextAsync(HangMitigatingCancellationToken)); + await TestServices.EditorVerifier.CodeActionsNotShowingAsync(HangMitigatingCancellationToken); } [CriticalIdeTheory] From 3afc07b87f467c1a053d0934dd5f2f1d671ec54f Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Wed, 13 Nov 2024 10:22:42 -0800 Subject: [PATCH 5/5] Fix test --- .../CSharp/CSharpCodeActions.cs | 24 ++++--------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/src/VisualStudio/IntegrationTest/New.IntegrationTests/CSharp/CSharpCodeActions.cs b/src/VisualStudio/IntegrationTest/New.IntegrationTests/CSharp/CSharpCodeActions.cs index f2cc6b6731668..e7ea9428208d1 100644 --- a/src/VisualStudio/IntegrationTest/New.IntegrationTests/CSharp/CSharpCodeActions.cs +++ b/src/VisualStudio/IntegrationTest/New.IntegrationTests/CSharp/CSharpCodeActions.cs @@ -389,12 +389,11 @@ await TestServices.EditorVerifier.CodeActionAsync( await TestServices.EditorVerifier.CodeActionsNotShowingAsync(HangMitigatingCancellationToken); } - [CriticalIdeTheory] - [InlineData(FixAllScope.Project)] - [InlineData(FixAllScope.Solution)] + [IdeFact] [Trait(Traits.Feature, Traits.Features.CodeActionsFixAllOccurrences)] [WorkItem("https://github.com/dotnet/roslyn/issues/33507")] - public async Task FixAllOccurrencesTriggeredFromGeneratedCode(FixAllScope scope) + [WorkItem("https://github.com/dotnet/roslyn/issues/74761")] + public async Task FixAllOccurrencesNotTriggeredFromGeneratedCode() { var markup = """ // @@ -410,14 +409,6 @@ class C using System; using System.Threading; - class D - { - public IntPtr X1 { get; set; } - } - """; - var expectedSecondFile = """ - using System; - class D { public IntPtr X1 { get; set; } @@ -435,17 +426,12 @@ class D // change. MarkupTestFile.GetPosition(markup, out var expectedText, out int _); await SetUpEditorAsync(markup, HangMitigatingCancellationToken); - await TestServices.Editor.InvokeCodeActionListAsync(HangMitigatingCancellationToken); - await TestServices.EditorVerifier.CodeActionAsync( - "Remove unnecessary usings", - applyFix: true, - fixAllScope: scope, - cancellationToken: HangMitigatingCancellationToken); + await TestServices.EditorVerifier.CodeActionsNotShowingAsync(HangMitigatingCancellationToken); AssertEx.EqualOrDiff(expectedText, await TestServices.Editor.GetTextAsync(HangMitigatingCancellationToken)); await TestServices.SolutionExplorer.OpenFileAsync(ProjectName, "D.cs", HangMitigatingCancellationToken); - AssertEx.EqualOrDiff(expectedSecondFile, await TestServices.Editor.GetTextAsync(HangMitigatingCancellationToken)); + AssertEx.EqualOrDiff(secondFile, await TestServices.Editor.GetTextAsync(HangMitigatingCancellationToken)); } [IdeFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)]