Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #749 from JoeRobich/remove-unnecessary-imports
Remove unnecessary imports
- Loading branch information
Showing
31 changed files
with
472 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// 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; | ||
|
||
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 (!formatOptions.FixCodeStyle) | ||
{ | ||
return sourceText; | ||
} | ||
|
||
var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); | ||
if (tree is null) | ||
{ | ||
return sourceText; | ||
} | ||
|
||
var severity = analyzerConfigOptions.GetDiagnosticSeverity(tree, IDE0005, Style); | ||
if (severity < formatOptions.CodeStyleSeverity) | ||
{ | ||
return sourceText; | ||
} | ||
|
||
var formattedDocument = await RemoveUnnecessaryImportsHelper.RemoveUnnecessaryImportsAsync(document, cancellationToken).ConfigureAwait(false); | ||
|
||
var isSameVersion = await IsSameDocumentAndVersionAsync(document, formattedDocument, cancellationToken).ConfigureAwait(false); | ||
if (isSameVersion) | ||
{ | ||
return sourceText; | ||
} | ||
|
||
var formattedText = await formattedDocument.GetTextAsync(cancellationToken).ConfigureAwait(false); | ||
return formattedText; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// 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.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.CodeAnalysis.Tools.Reflection | ||
{ | ||
internal static class RemoveUnnecessaryImportsHelper | ||
{ | ||
private static readonly Assembly? s_microsoftCodeAnalysisFeaturesAssembly = Assembly.Load(new AssemblyName("Microsoft.CodeAnalysis.Features")); | ||
private static readonly Type? s_abstractRemoveUnnecessaryImportsCodeFixProviderType = s_microsoftCodeAnalysisFeaturesAssembly?.GetType("Microsoft.CodeAnalysis.RemoveUnnecessaryImports.AbstractRemoveUnnecessaryImportsCodeFixProvider"); | ||
private static readonly MethodInfo? s_removeUnnecessaryImportsAsyncMethod = s_abstractRemoveUnnecessaryImportsCodeFixProviderType?.GetMethod("RemoveUnnecessaryImportsAsync", BindingFlags.Static | BindingFlags.NonPublic); | ||
|
||
public static async Task<Document> RemoveUnnecessaryImportsAsync(Document document, CancellationToken cancellationToken) | ||
{ | ||
if (s_removeUnnecessaryImportsAsyncMethod is null) | ||
{ | ||
return document; | ||
} | ||
|
||
return await (Task<Document>)s_removeUnnecessaryImportsAsyncMethod.Invoke(obj: null, new object[] { document, cancellationToken }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.