forked from dotnet/format
/
SolutionCodeFixApplier.cs
126 lines (109 loc) · 5.22 KB
/
SolutionCodeFixApplier.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.Extensions.Logging;
namespace Microsoft.CodeAnalysis.Tools.Analyzers
{
internal class SolutionCodeFixApplier : ICodeFixApplier
{
public async Task<Solution> ApplyCodeFixesAsync(
Solution solution,
CodeAnalysisResult result,
CodeFixProvider codeFix,
string diagnosticId,
ILogger logger,
CancellationToken cancellationToken)
{
var fixAllProvider = codeFix.GetFixAllProvider();
if (fixAllProvider?.GetSupportedFixAllScopes()?.Contains(FixAllScope.Solution) != true)
{
logger.LogWarning(Resources.Unable_to_fix_0_Code_fix_1_doesnt_support_Fix_All_in_Solution, diagnosticId, codeFix.GetType().Name);
return solution;
}
var diagnostic = result.Diagnostics
.SelectMany(kvp => kvp.Value)
.Where(diagnostic => diagnostic.Location.SourceTree != null)
.FirstOrDefault();
if (diagnostic is null)
{
return solution;
}
var document = solution.GetDocument(diagnostic.Location.SourceTree);
if (document is null)
{
return solution;
}
CodeAction? action = null;
var context = new CodeFixContext(document, diagnostic,
(a, _) =>
{
if (action == null)
{
action = a;
}
},
cancellationToken);
await codeFix.RegisterCodeFixesAsync(context).ConfigureAwait(false);
var fixAllContext = new FixAllContext(
document: document,
codeFixProvider: codeFix,
scope: FixAllScope.Solution,
codeActionEquivalenceKey: action?.EquivalenceKey!, // FixAllState supports null equivalence key. This should still be supported.
diagnosticIds: new[] { diagnosticId },
fixAllDiagnosticProvider: new DiagnosticProvider(result),
cancellationToken: cancellationToken);
try
{
var fixAllAction = await fixAllProvider.GetFixAsync(fixAllContext).ConfigureAwait(false);
if (fixAllAction is null)
{
logger.LogWarning(Resources.Unable_to_fix_0_Code_fix_1_didnt_return_a_Fix_All_action, diagnosticId, codeFix.GetType().Name);
return solution;
}
var operations = await fixAllAction.GetOperationsAsync(cancellationToken).ConfigureAwait(false);
var applyChangesOperation = operations.OfType<ApplyChangesOperation>().SingleOrDefault();
if (applyChangesOperation is null)
{
logger.LogWarning(Resources.Unable_to_fix_0_Code_fix_1_returned_an_unexpected_operation, diagnosticId, codeFix.GetType().Name);
return solution;
}
return applyChangesOperation.ChangedSolution;
}
catch (Exception ex)
{
logger.LogWarning(Resources.Failed_to_apply_code_fix_0_for_1_2, codeFix?.GetType().Name, diagnosticId, ex.Message);
return solution;
}
}
private class DiagnosticProvider : FixAllContext.DiagnosticProvider
{
private static Task<IEnumerable<Diagnostic>> EmptyDignosticResult => Task.FromResult(Enumerable.Empty<Diagnostic>());
private readonly IReadOnlyDictionary<Project, List<Diagnostic>> _diagnosticsByProject;
internal DiagnosticProvider(CodeAnalysisResult analysisResult)
{
_diagnosticsByProject = analysisResult.Diagnostics;
}
public override Task<IEnumerable<Diagnostic>> GetAllDiagnosticsAsync(Project project, CancellationToken cancellationToken)
{
return GetProjectDiagnosticsAsync(project, cancellationToken);
}
public override async Task<IEnumerable<Diagnostic>> GetDocumentDiagnosticsAsync(Document document, CancellationToken cancellationToken)
{
var projectDiagnostics = await GetProjectDiagnosticsAsync(document.Project, cancellationToken);
return projectDiagnostics.Where(diagnostic => diagnostic.Location.SourceTree?.FilePath == document.FilePath).ToImmutableArray();
}
public override Task<IEnumerable<Diagnostic>> GetProjectDiagnosticsAsync(Project project, CancellationToken cancellationToken)
{
return _diagnosticsByProject.ContainsKey(project)
? Task.FromResult<IEnumerable<Diagnostic>>(_diagnosticsByProject[project])
: EmptyDignosticResult;
}
}
}
}