-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
95 additions
and
3 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
src/Menees.Analyzers.CodeFixes/Men017RemoveUnusedPrivateSetterFixer.cs
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,80 @@ | ||
namespace Menees.Analyzers.CodeFixes; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading; | ||
using Microsoft.CodeAnalysis; | ||
|
||
[Shared] | ||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(Men017RemoveUnusedPrivateSetterFixer))] | ||
public sealed class Men017RemoveUnusedPrivateSetterFixer : CodeFixProvider | ||
{ | ||
#region Private Data Members | ||
|
||
private static readonly ImmutableArray<string> FixableDiagnostics | ||
= ImmutableArray.Create(Men017RemoveUnusedPrivateSetter.DiagnosticId); | ||
|
||
#endregion | ||
|
||
#region Public Properties | ||
|
||
public sealed override ImmutableArray<string> FixableDiagnosticIds => FixableDiagnostics; | ||
|
||
#endregion | ||
|
||
#region Public Methods | ||
|
||
public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; | ||
|
||
public sealed override Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
foreach (Diagnostic diagnostic in context.Diagnostics.Where(d => FixableDiagnostics.Contains(d.Id))) | ||
{ | ||
context.RegisterCodeFix( | ||
CodeAction.Create( | ||
Resources.Men017CodeFix, | ||
cancellation => GetTransformedDocumentAsync(context.Document, diagnostic, cancellation), | ||
equivalenceKey: nameof(Men017RemoveUnusedPrivateSetterFixer)), | ||
diagnostic); | ||
} | ||
|
||
return Task.FromResult(true); | ||
} | ||
|
||
#endregion | ||
|
||
#region Private Methods | ||
|
||
private async Task<Document> GetTransformedDocumentAsync( | ||
Document document, | ||
Diagnostic diagnostic, | ||
CancellationToken cancellation) | ||
{ | ||
Document result = document; | ||
|
||
SyntaxNode? root = await document.GetSyntaxRootAsync(cancellation); | ||
if (root != null) | ||
{ | ||
AccessorDeclarationSyntax? accessorDeclaration = root.FindNode(diagnostic.Location.SourceSpan) | ||
?.FirstAncestorOrSelf<AccessorDeclarationSyntax>(); | ||
AccessorListSyntax? accessorList = accessorDeclaration?.FirstAncestorOrSelf<AccessorListSyntax>(); | ||
|
||
if (accessorList != null && accessorDeclaration != null) | ||
{ | ||
AccessorListSyntax? newAccessorList = accessorList | ||
.RemoveNode(accessorDeclaration, SyntaxRemoveOptions.KeepExteriorTrivia)? | ||
.WithAdditionalAnnotations(Formatter.Annotation); | ||
if (newAccessorList != null) | ||
{ | ||
SyntaxNode newRoot = root.ReplaceNode(accessorList, newAccessorList); | ||
result = document.WithSyntaxRoot(newRoot); | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
#endregion | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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