|
| 1 | +using System.Collections.Immutable; |
| 2 | +using System.Composition; |
| 3 | +using System.Runtime.CompilerServices; |
| 4 | +using Microsoft.CodeAnalysis; |
| 5 | +using Microsoft.CodeAnalysis.CodeActions; |
| 6 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 7 | +using Microsoft.CodeAnalysis.CSharp; |
| 8 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 9 | + |
| 10 | +namespace Architect.DomainModeling.CodeFixProviders; |
| 11 | + |
| 12 | +[Shared] |
| 13 | +[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(MissingStringComparisonCodeFixProvider))] |
| 14 | +public sealed class MissingStringComparisonCodeFixProvider : CodeFixProvider |
| 15 | +{ |
| 16 | + private static readonly ImmutableArray<string> FixableDiagnosticIdConstant = ["ValueObjectGeneratorMissingStringComparison", "WrapperValueObjectGeneratorMissingStringComparison"]; |
| 17 | + |
| 18 | + public override ImmutableArray<string> FixableDiagnosticIds => FixableDiagnosticIdConstant; |
| 19 | + |
| 20 | + public override FixAllProvider? GetFixAllProvider() |
| 21 | + { |
| 22 | + return null; |
| 23 | + } |
| 24 | + |
| 25 | + public override async Task RegisterCodeFixesAsync(CodeFixContext context) |
| 26 | + { |
| 27 | + var diagnostic = context.Diagnostics.First(); |
| 28 | + var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); |
| 29 | + |
| 30 | + if (root is null) |
| 31 | + return; |
| 32 | + |
| 33 | + var token = root.FindToken(diagnostic.Location.SourceSpan.Start); |
| 34 | + var tds = token.Parent?.AncestorsAndSelf() |
| 35 | + .OfType<TypeDeclarationSyntax>() |
| 36 | + .FirstOrDefault(); |
| 37 | + |
| 38 | + if (tds is null) |
| 39 | + return; |
| 40 | + |
| 41 | + var ordinalFix = CodeAction.Create( |
| 42 | + title: "Implement StringComparison { get; } with StringComparison.Ordinal", |
| 43 | + createChangedDocument: ct => AddStringComparisonMemberAsync(context.Document, root, tds, stringComparisonExpression: "StringComparison.Ordinal", ct), |
| 44 | + equivalenceKey: "ImplementStringComparisonOrdinalGetter"); |
| 45 | + context.RegisterCodeFix(ordinalFix, context.Diagnostics.First()); |
| 46 | + |
| 47 | + var ordinalIgnoreCaseFix = CodeAction.Create( |
| 48 | + title: "Implement StringComparison { get; } with StringComparison.OrdinalIgnoreCase", |
| 49 | + createChangedDocument: ct => AddStringComparisonMemberAsync(context.Document, root, tds, stringComparisonExpression: "StringComparison.OrdinalIgnoreCase", ct), |
| 50 | + equivalenceKey: "ImplementStringComparisonOrdinalIgnoreCaseGetter"); |
| 51 | + context.RegisterCodeFix(ordinalIgnoreCaseFix, context.Diagnostics.First()); |
| 52 | + } |
| 53 | + |
| 54 | + private static Task<Document> AddStringComparisonMemberAsync( |
| 55 | + Document document, |
| 56 | + SyntaxNode root, |
| 57 | + TypeDeclarationSyntax tds, |
| 58 | + string stringComparisonExpression, |
| 59 | + CancellationToken _) |
| 60 | + { |
| 61 | + var newlineTrivia = GetNewlineTrivia(tds); |
| 62 | + |
| 63 | + var property = SyntaxFactory.PropertyDeclaration( |
| 64 | + SyntaxFactory.ParseTypeName("StringComparison"), |
| 65 | + SyntaxFactory.Identifier("StringComparison")) |
| 66 | + .AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword)) |
| 67 | + .WithExpressionBody( |
| 68 | + SyntaxFactory.ArrowExpressionClause( |
| 69 | + SyntaxFactory.ParseExpression(stringComparisonExpression))) |
| 70 | + .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)) |
| 71 | + .WithLeadingTrivia(newlineTrivia) |
| 72 | + .WithTrailingTrivia(newlineTrivia) |
| 73 | + .WithTrailingTrivia(newlineTrivia); |
| 74 | + |
| 75 | + var updatedTds = tds.WithMembers(tds.Members.Insert(0, property)); |
| 76 | + var updatedRoot = root.ReplaceNode(tds, updatedTds); |
| 77 | + return Task.FromResult(document.WithSyntaxRoot(updatedRoot)); |
| 78 | + } |
| 79 | + |
| 80 | + private static SyntaxTrivia GetNewlineTrivia(SyntaxNode node) |
| 81 | + { |
| 82 | + var allTrivia = node.DescendantTrivia(descendIntoTrivia: true); |
| 83 | + |
| 84 | + var (nCount, rnCount) = (0, 0); |
| 85 | + |
| 86 | + foreach (var trivia in allTrivia) |
| 87 | + { |
| 88 | + if (!trivia.IsKind(SyntaxKind.EndOfLineTrivia)) |
| 89 | + continue; |
| 90 | + |
| 91 | + var length = trivia.Span.Length; |
| 92 | + var lengthIsOne = length == 1; |
| 93 | + var lengthIsTwo = length == 2; |
| 94 | + nCount += Unsafe.As<bool, int>(ref lengthIsOne); |
| 95 | + rnCount += Unsafe.As<bool, int>(ref lengthIsTwo); |
| 96 | + } |
| 97 | + |
| 98 | + return rnCount > nCount |
| 99 | + ? SyntaxFactory.ElasticCarriageReturnLineFeed |
| 100 | + : SyntaxFactory.ElasticLineFeed; |
| 101 | + } |
| 102 | +} |
0 commit comments