|
| 1 | +using System.Collections.Immutable; |
| 2 | +using System.Composition; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +using Microsoft.CodeAnalysis; |
| 8 | +using Microsoft.CodeAnalysis.CodeActions; |
| 9 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 10 | +using Microsoft.CodeAnalysis.CSharp; |
| 11 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 12 | + |
| 13 | +namespace DisCatSharp.Analyzer; |
| 14 | + |
| 15 | +[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(DisCatSharpBanDeleteMessageDaysMigrationCodeFix))] |
| 16 | +[Shared] |
| 17 | +public sealed class DisCatSharpBanDeleteMessageDaysMigrationCodeFix : SingleDiagnosticCodeFixProvider |
| 18 | +{ |
| 19 | + protected override string FixableDiagnosticId => DisCatSharpDiagnosticIds.BanDeleteMessageDaysMigration; |
| 20 | + |
| 21 | + protected override Task RegisterCodeFixesAsync(CodeFixContext context, ImmutableArray<Diagnostic> diagnostics) |
| 22 | + { |
| 23 | + context.RegisterCodeFix( |
| 24 | + CodeAction.Create( |
| 25 | + "Rename to 'deleteMessageSeconds' (convert literal days -> seconds)", |
| 26 | + ct => ApplyFixAsync(context.Document, diagnostics[0], ct), |
| 27 | + equivalenceKey: nameof(DisCatSharpBanDeleteMessageDaysMigrationCodeFix)), |
| 28 | + diagnostics[0]); |
| 29 | + |
| 30 | + return Task.CompletedTask; |
| 31 | + } |
| 32 | + |
| 33 | + internal static async Task<Document> ApplyFixAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) |
| 34 | + { |
| 35 | + var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); |
| 36 | + if (root is null) |
| 37 | + return document; |
| 38 | + |
| 39 | + var nameColonNode = root.FindNode(diagnostic.Location.SourceSpan) as NameColonSyntax; |
| 40 | + if (nameColonNode is null) |
| 41 | + return document; |
| 42 | + |
| 43 | + var argument = nameColonNode.Parent as ArgumentSyntax; |
| 44 | + if (argument is null) |
| 45 | + return document; |
| 46 | + |
| 47 | + // New name colon: deleteMessageSeconds: |
| 48 | + var newNameColon = SyntaxFactory.NameColon( |
| 49 | + SyntaxFactory.IdentifierName("deleteMessageSeconds")) |
| 50 | + .WithTriviaFrom(nameColonNode); |
| 51 | + |
| 52 | + // If the argument expression is a numeric literal, multiply by 86400 |
| 53 | + ArgumentSyntax newArgument; |
| 54 | + if (argument.Expression is LiteralExpressionSyntax { Token.Value: int days }) |
| 55 | + { |
| 56 | + var seconds = days * 86400; |
| 57 | + var newLiteral = SyntaxFactory.LiteralExpression( |
| 58 | + SyntaxKind.NumericLiteralExpression, |
| 59 | + SyntaxFactory.Literal(seconds)) |
| 60 | + .WithTriviaFrom(argument.Expression); |
| 61 | + newArgument = argument.WithNameColon(newNameColon).WithExpression(newLiteral); |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + // Non-literal: rename only, add TODO comment |
| 66 | + var todoComment = SyntaxFactory.Comment("/* TODO: was days, now seconds - multiply by 86400 if needed */"); |
| 67 | + var newExpression = argument.Expression.WithLeadingTrivia( |
| 68 | + argument.Expression.GetLeadingTrivia().Add(todoComment).Add(SyntaxFactory.Space)); |
| 69 | + newArgument = argument.WithNameColon(newNameColon).WithExpression(newExpression); |
| 70 | + } |
| 71 | + |
| 72 | + var newRoot = root.ReplaceNode(argument, newArgument); |
| 73 | + return document.WithSyntaxRoot(newRoot); |
| 74 | + } |
| 75 | +} |
0 commit comments