提交 7c652067 编写于 作者: C CyrusNajmabadi

Add support for removing unused local functions.

上级 87e10254
......@@ -62,6 +62,7 @@
<Compile Include="AddImport\CSharpAddImportFeatureService.cs" />
<Compile Include="AddPackage\CSharpAddSpecificPackageCodeFixProvider.cs" />
<Compile Include="AddParameter\CSharpAddParameterCodeFixProvider.cs" />
<Compile Include="RemoveUnusedLocalFunction\CSharpRemoveUnusedLocalFunctionCodeFixProvider.cs" />
<Compile Include="RemoveUnusedVariable\CSharpRemoveUnusedVariableCodeFixProvider.cs" />
<Compile Include="Completion\CompletionProviders\DeclarationNameCompletionProvider.NameGenerator.cs" />
<Compile Include="Completion\CompletionProviders\DeclarationNameCompletionProvider.DeclarationInfo.cs" />
......
......@@ -845,6 +845,15 @@ internal class CSharpFeaturesResources {
}
}
/// <summary>
/// Looks up a localized string similar to Remove unused function.
/// </summary>
internal static string Remove_unused_function {
get {
return ResourceManager.GetString("Remove_unused_function", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Replace return with yield return.
/// </summary>
......
......@@ -524,4 +524,7 @@
<data name="Suggested_name" xml:space="preserve">
<value>(Suggested name)</value>
</data>
<data name="Remove_unused_function" xml:space="preserve">
<value>Remove unused function</value>
</data>
</root>
\ No newline at end of file
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp.RemoveUnusedLocalFunction
{
[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.RemoveUnusedLocalFunction), Shared]
[ExtensionOrder(After = PredefinedCodeFixProviderNames.AddImport)]
internal class CSharpRemoveUnusedLocalFunctionCodeFixProvider : SyntaxEditorBasedCodeFixProvider
{
private const string CS8321 = nameof(CS8321); // The local function 'X' is declared but never used
public sealed override ImmutableArray<string> FixableDiagnosticIds
=> ImmutableArray.Create(CS8321);
public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
context.RegisterCodeFix(
new MyCodeAction(c => FixAsync(context.Document, context.Diagnostics.First(), c)),
context.Diagnostics);
return SpecializedTasks.EmptyTask;
}
protected override Task FixAllAsync(Document document, ImmutableArray<Diagnostic> diagnostics, SyntaxEditor editor, CancellationToken cancellationToken)
{
var root = editor.OriginalRoot;
var localFunctions = diagnostics.Select(d => root.FindToken(d.Location.SourceSpan.Start))
.Select(t => t.GetAncestor<LocalFunctionStatementSyntax>())
.WhereNotNull();
foreach (var localFunction in localFunctions)
{
editor.RemoveNode(localFunction);
}
return SpecializedTasks.EmptyTask;
}
private class MyCodeAction : CodeAction.DocumentChangeAction
{
public MyCodeAction(Func<CancellationToken, Task<Document>> createChangedDocument) :
base(CSharpFeaturesResources.Remove_unused_function, createChangedDocument, CSharpFeaturesResources.Remove_unused_function)
{
}
}
}
}
\ No newline at end of file
......@@ -38,6 +38,7 @@ internal static class PredefinedCodeFixProviderNames
public const string RemoveUnnecessaryCast = nameof(RemoveUnnecessaryCast);
public const string RemoveUnnecessaryImports = nameof(RemoveUnnecessaryImports);
public const string RemoveUnreachableCode = nameof(RemoveUnreachableCode);
public const string RemoveUnusedLocalFunction = nameof(RemoveUnusedLocalFunction);
public const string RemoveUnusedVariable = nameof(RemoveUnusedVariable);
public const string RenameTracking = nameof(RenameTracking);
public const string SimplifyNames = nameof(SimplifyNames);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册