-
-
Notifications
You must be signed in to change notification settings - Fork 51
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
3 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
src/Meziantou.Analyzer.CodeFixers/Rules/UseLangwordInXmlCommentFixer.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,54 @@ | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.Editing; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
|
||
namespace Meziantou.Analyzer.Rules; | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp), Shared] | ||
public sealed class UseLangwordInXmlCommentFixer : CodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(RuleIdentifiers.UseLangwordInXmlComment); | ||
|
||
public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; | ||
|
||
public override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | ||
var nodeToFix = root?.FindNode(context.Span, getInnermostNodeForTie: true, findInsideTrivia: true); | ||
if (nodeToFix is null) | ||
return; | ||
|
||
if (!context.Diagnostics[0].Properties.TryGetValue("keyword", out var keyword) || keyword is null) | ||
return; | ||
|
||
var title = $"Use <see langword=\"{keyword}\" />"; | ||
var codeAction = CodeAction.Create( | ||
title, | ||
cancellationToken => Fix(context.Document, nodeToFix, keyword, cancellationToken), | ||
equivalenceKey: title); | ||
|
||
context.RegisterCodeFix(codeAction, context.Diagnostics); | ||
} | ||
|
||
private static async Task<Document> Fix(Document document, SyntaxNode nodeToFix, string keyword, CancellationToken cancellationToken) | ||
{ | ||
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false); | ||
|
||
var newNode = XmlNullKeywordElement() | ||
.WithLessThanToken(Token(SyntaxKind.LessThanToken)) | ||
.WithAttributes( | ||
SingletonList<XmlAttributeSyntax>( | ||
XmlTextAttribute("langword", keyword))); | ||
|
||
editor.ReplaceNode(nodeToFix, newNode); | ||
return editor.GetChangedDocument(); | ||
} | ||
} |
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