/
UnnecessaryImportsFormatter.cs
79 lines (67 loc) · 3.03 KB
/
UnnecessaryImportsFormatter.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
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Tools.Reflection;
using Microsoft.Extensions.Logging;
namespace Microsoft.CodeAnalysis.Tools.Formatters
{
/// <summary>
/// UnnecessaryImportsFormatter that removes unsused imports when fixing code style errors.
/// </summary>
internal sealed class UnnecessaryImportsFormatter : DocumentFormatter
{
internal const string IDE0005 = nameof(IDE0005);
internal const string Style = nameof(Style);
protected override string FormatWarningDescription => Resources.Remove_unnecessary_import;
public override FixCategory Category => FixCategory.CodeStyle;
internal override async Task<SourceText> FormatFileAsync(
Document document,
SourceText sourceText,
OptionSet optionSet,
AnalyzerConfigOptions analyzerConfigOptions,
FormatOptions formatOptions,
ILogger logger,
CancellationToken cancellationToken)
{
// If we are fixing CodeStyle and the 'IDE0005' diagnostic is configured, then
// see if we can remove unused imports.
// If we are not saving files, do not make changes in this formatter. They will be
// reported by their diagnostic id when the analyzer formatter runs.
if (!formatOptions.SaveFormattedFiles)
{
return sourceText;
}
// If diagnostics are being filtered and IDE0005 isn't specified, then make no changes.
if (!formatOptions.Diagnostics.IsEmpty &&
!formatOptions.Diagnostics.Contains(IDE0005))
{
return sourceText;
}
var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
if (tree is null)
{
return sourceText;
}
var severity = analyzerConfigOptions.GetDiagnosticSeverity(document.Project, tree, IDE0005, Style);
if (severity < formatOptions.CodeStyleSeverity)
{
return sourceText;
}
var formattedDocument = await RemoveUnnecessaryImportsHelper.RemoveUnnecessaryImportsAsync(document, cancellationToken).ConfigureAwait(false);
if (formattedDocument is null)
{
return sourceText;
}
var isSameVersion = await IsSameDocumentAndVersionAsync(document, formattedDocument, cancellationToken).ConfigureAwait(false);
if (isSameVersion)
{
return sourceText;
}
var formattedText = await formattedDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);
return formattedText;
}
}
}