mirror of
https://github.com/spectreconsole/spectre.console.git
synced 2025-10-25 15:19:23 +00:00
35 lines
1.3 KiB
C#
35 lines
1.3 KiB
C#
using System.Collections.Immutable;
|
|
using System.Composition;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.CodeAnalysis;
|
|
using Microsoft.CodeAnalysis.CodeFixes;
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
using Spectre.Console.Analyzer.CodeActions;
|
|
|
|
namespace Spectre.Console.Analyzer.FixProviders;
|
|
|
|
/// <summary>
|
|
/// Fix provider to change System.Console calls to AnsiConsole calls.
|
|
/// </summary>
|
|
[ExportCodeFixProvider(LanguageNames.CSharp)]
|
|
[Shared]
|
|
public class SystemConsoleToAnsiConsoleFix : CodeFixProvider
|
|
{
|
|
/// <inheritdoc />
|
|
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(
|
|
Descriptors.S1000_UseAnsiConsoleOverSystemConsole.Id);
|
|
|
|
/// <inheritdoc />
|
|
public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
|
|
|
|
/// <inheritdoc />
|
|
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
|
|
{
|
|
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
|
|
var methodDeclaration = root.FindNode(context.Span).FirstAncestorOrSelf<InvocationExpressionSyntax>();
|
|
context.RegisterCodeFix(
|
|
new SwitchToAnsiConsoleAction(context.Document, methodDeclaration, "Convert static call to AnsiConsole to Spectre.Console.AnsiConsole"),
|
|
context.Diagnostics);
|
|
}
|
|
}
|