未验证 提交 2644e05c 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #33515 from sharwell/no-fixing-generated

Do not apply Fix All operations to generated code
......@@ -74,12 +74,13 @@ internal static class FixAllContextHelper
}
return await GetDocumentDiagnosticsToFixAsync(
allDiagnostics, projectsToFix, fixAllContext.CancellationToken).ConfigureAwait(false);
allDiagnostics, projectsToFix, isGeneratedCode, fixAllContext.CancellationToken).ConfigureAwait(false);
}
private static async Task<ImmutableDictionary<Document, ImmutableArray<Diagnostic>>> GetDocumentDiagnosticsToFixAsync(
ImmutableArray<Diagnostic> diagnostics,
ImmutableArray<Project> projects,
Func<Document, CancellationToken, bool> isGeneratedCode,
CancellationToken cancellationToken)
{
var treeToDocumentMap = await GetTreeToDocumentMapAsync(projects, cancellationToken).ConfigureAwait(false);
......@@ -89,8 +90,11 @@ internal static class FixAllContextHelper
{
cancellationToken.ThrowIfCancellationRequested();
var document = documentAndDiagnostics.Key;
var diagnosticsForDocument = documentAndDiagnostics.ToImmutableArray();
builder.Add(document, diagnosticsForDocument);
if (!isGeneratedCode(document, cancellationToken))
{
var diagnosticsForDocument = documentAndDiagnostics.ToImmutableArray();
builder.Add(document, diagnosticsForDocument);
}
}
return builder.ToImmutable();
......
......@@ -281,6 +281,146 @@ public int Y2
Assert.Equal(expectedText, VisualStudio.Editor.GetText());
}
[CriticalWpfTheory]
[InlineData(FixAllScope.Project)]
[InlineData(FixAllScope.Solution)]
[Trait(Traits.Feature, Traits.Features.CodeActionsFixAllOccurrences)]
[WorkItem(33507, "https://github.com/dotnet/roslyn/issues/33507")]
public void FixAllOccurrencesIgnoresGeneratedCode(FixAllScope scope)
{
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 generatedSourceMarkup = @"// <auto-generated/>
using System;
using $$System.Threading;
class D
{
public IntPtr X1 { get; set; }
}";
var expectedGeneratedSource = @"// <auto-generated/>
using System;
class D
{
public IntPtr X1 { get; set; }
}";
MarkupTestFile.GetPosition(generatedSourceMarkup, out var generatedSource, out int generatedSourcePosition);
VisualStudio.SolutionExplorer.AddFile(new ProjectUtils.Project(ProjectName), "D.cs", generatedSource, open: false);
// Switch to the main document we'll be editing
VisualStudio.SolutionExplorer.OpenFile(new ProjectUtils.Project(ProjectName), "Class1.cs");
// Verify that applying a Fix All operation does not change generated files.
// This is a regression test for correctness with respect to the design.
SetUpEditor(markup);
VisualStudio.WaitForApplicationIdle(CancellationToken.None);
VisualStudio.Editor.InvokeCodeActionList();
VisualStudio.Editor.Verify.CodeAction(
"Remove Unnecessary Usings",
applyFix: true,
fixAllScope: scope);
Assert.Equal(expectedText, VisualStudio.Editor.GetText());
VisualStudio.SolutionExplorer.OpenFile(new ProjectUtils.Project(ProjectName), "D.cs");
Assert.Equal(generatedSource, VisualStudio.Editor.GetText());
// Verify that a Fix All in Document in the generated file still does nothing.
// ⚠ This is a statement of the current behavior, and not a claim regarding correctness of the design.
// The current behavior is observable; any change to this behavior should be part of an intentional design
// change.
VisualStudio.Editor.MoveCaret(generatedSourcePosition);
VisualStudio.Editor.InvokeCodeActionList();
VisualStudio.Editor.Verify.CodeAction(
"Remove Unnecessary Usings",
applyFix: true,
fixAllScope: FixAllScope.Document);
Assert.Equal(generatedSource, VisualStudio.Editor.GetText());
// 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.
VisualStudio.Editor.MoveCaret(generatedSourcePosition);
VisualStudio.Editor.InvokeCodeActionList();
VisualStudio.Editor.Verify.CodeAction(
"Remove Unnecessary Usings",
applyFix: true,
fixAllScope: null);
Assert.Equal(expectedGeneratedSource, VisualStudio.Editor.GetText());
}
[CriticalWpfTheory]
[InlineData(FixAllScope.Project)]
[InlineData(FixAllScope.Solution)]
[Trait(Traits.Feature, Traits.Features.CodeActionsFixAllOccurrences)]
[WorkItem(33507, "https://github.com/dotnet/roslyn/issues/33507")]
public void FixAllOccurrencesTriggeredFromGeneratedCode(FixAllScope scope)
{
var markup = @"// <auto-generated/>
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; }
}";
VisualStudio.SolutionExplorer.AddFile(new ProjectUtils.Project(ProjectName), "D.cs", secondFile, open: false);
// Switch to the main document we'll be editing
VisualStudio.SolutionExplorer.OpenFile(new ProjectUtils.Project(ProjectName), "Class1.cs");
// Verify that applying a Fix All operation does not change generated file, but does change other files.
// ⚠ This is a statement of the current behavior, and not a claim regarding correctness of the design.
// The current behavior is observable; any change to this behavior should be part of an intentional design
// change.
MarkupTestFile.GetPosition(markup, out var expectedText, out int _);
SetUpEditor(markup);
VisualStudio.WaitForApplicationIdle(CancellationToken.None);
VisualStudio.Editor.InvokeCodeActionList();
VisualStudio.Editor.Verify.CodeAction(
"Remove Unnecessary Usings",
applyFix: true,
fixAllScope: scope);
Assert.Equal(expectedText, VisualStudio.Editor.GetText());
VisualStudio.SolutionExplorer.OpenFile(new ProjectUtils.Project(ProjectName), "D.cs");
Assert.Equal(expectedSecondFile, VisualStudio.Editor.GetText());
}
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)]
public void ClassificationInPreviewPane()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册