|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Immutable; |
| 7 | +using System.Composition; |
| 8 | +using System.Diagnostics.CodeAnalysis; |
| 9 | +using System.Linq; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using Microsoft.CodeAnalysis.CodeActions; |
| 13 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 14 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 15 | +using Microsoft.CodeAnalysis.Editing; |
| 16 | +using Microsoft.CodeAnalysis.Host.Mef; |
| 17 | +using Microsoft.CodeAnalysis.Shared.Extensions; |
| 18 | + |
| 19 | +namespace Microsoft.CodeAnalysis.CSharp.CodeFixes.TransposeRecordKeyword |
| 20 | +{ |
| 21 | + [ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.TransposeRecordKeyword), Shared] |
| 22 | + internal class CSharpTransposeRecordKeywordCodeFixProvider : SyntaxEditorBasedCodeFixProvider |
| 23 | + { |
| 24 | + private const string CS9012 = nameof(CS9012); // Unexpected keyword 'record'. Did you mean 'record struct' or 'record class'? |
| 25 | + |
| 26 | + [ImportingConstructor] |
| 27 | + [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] |
| 28 | + public CSharpTransposeRecordKeywordCodeFixProvider() |
| 29 | + { |
| 30 | + } |
| 31 | + |
| 32 | + public override ImmutableArray<string> FixableDiagnosticIds |
| 33 | + => ImmutableArray.Create(CS9012); |
| 34 | + |
| 35 | + internal override CodeFixCategory CodeFixCategory |
| 36 | + => CodeFixCategory.Compile; |
| 37 | + |
| 38 | + private static bool TryGetRecordDeclaration( |
| 39 | + Diagnostic diagnostic, CancellationToken cancellationToken, [NotNullWhen(true)] out RecordDeclarationSyntax? recordDeclaration) |
| 40 | + { |
| 41 | + recordDeclaration = diagnostic.Location.FindNode(cancellationToken) as RecordDeclarationSyntax; |
| 42 | + return recordDeclaration != null; |
| 43 | + } |
| 44 | + |
| 45 | + private static bool TryGetTokens( |
| 46 | + RecordDeclarationSyntax recordDeclaration, |
| 47 | + out SyntaxToken classOrStructKeyword, |
| 48 | + out SyntaxToken recordKeyword) |
| 49 | + { |
| 50 | + recordKeyword = recordDeclaration.Keyword; |
| 51 | + if (!recordKeyword.IsMissing) |
| 52 | + { |
| 53 | + var leadingTrivia = recordKeyword.LeadingTrivia; |
| 54 | + var skippedTriviaIndex = leadingTrivia.IndexOf(SyntaxKind.SkippedTokensTrivia); |
| 55 | + if (skippedTriviaIndex >= 0) |
| 56 | + { |
| 57 | + var skippedTrivia = leadingTrivia[skippedTriviaIndex]; |
| 58 | + var structure = (SkippedTokensTriviaSyntax)skippedTrivia.GetStructure()!; |
| 59 | + var tokens = structure.Tokens; |
| 60 | + if (tokens.Count == 1) |
| 61 | + { |
| 62 | + classOrStructKeyword = tokens.Single(); |
| 63 | + if (classOrStructKeyword.Kind() is SyntaxKind.ClassKeyword or SyntaxKind.StructKeyword) |
| 64 | + { |
| 65 | + // Because the class/struct keyword is skipped trivia on the record keyword, it will |
| 66 | + // not have trivia of it's own. So we need to move the other trivia appropriate trivia |
| 67 | + // on the record keyword to it. |
| 68 | + var remainingLeadingTrivia = SyntaxFactory.TriviaList(leadingTrivia.Skip(skippedTriviaIndex + 1)); |
| 69 | + var trailingTriviaTakeUntil = remainingLeadingTrivia.IndexOf(SyntaxKind.EndOfLineTrivia) is >= 0 and var eolIndex |
| 70 | + ? eolIndex + 1 |
| 71 | + : remainingLeadingTrivia.Count; |
| 72 | + |
| 73 | + classOrStructKeyword = classOrStructKeyword |
| 74 | + .WithLeadingTrivia(SyntaxFactory.TriviaList(remainingLeadingTrivia.Skip(trailingTriviaTakeUntil))) |
| 75 | + .WithTrailingTrivia(recordKeyword.TrailingTrivia); |
| 76 | + recordKeyword = recordKeyword |
| 77 | + .WithLeadingTrivia(leadingTrivia.Take(skippedTriviaIndex)) |
| 78 | + .WithTrailingTrivia(SyntaxFactory.TriviaList(remainingLeadingTrivia.Take(trailingTriviaTakeUntil))); |
| 79 | + |
| 80 | + return true; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + classOrStructKeyword = default; |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + public override Task RegisterCodeFixesAsync(CodeFixContext context) |
| 91 | + { |
| 92 | + var document = context.Document; |
| 93 | + var cancellationToken = context.CancellationToken; |
| 94 | + |
| 95 | + var diagnostic = context.Diagnostics.First(); |
| 96 | + if (TryGetRecordDeclaration(diagnostic, cancellationToken, out var recordDeclaration) && |
| 97 | + TryGetTokens(recordDeclaration, out _, out _)) |
| 98 | + { |
| 99 | + context.RegisterCodeFix( |
| 100 | + new MyCodeAction(c => this.FixAsync(document, diagnostic, c)), |
| 101 | + diagnostic); |
| 102 | + } |
| 103 | + |
| 104 | + return Task.CompletedTask; |
| 105 | + } |
| 106 | + |
| 107 | + protected override Task FixAllAsync( |
| 108 | + Document document, ImmutableArray<Diagnostic> diagnostics, |
| 109 | + SyntaxEditor editor, CancellationToken cancellationToken) |
| 110 | + { |
| 111 | + foreach (var diagnostic in diagnostics) |
| 112 | + { |
| 113 | + if (TryGetRecordDeclaration(diagnostic, cancellationToken, out var recordDeclaration)) |
| 114 | + { |
| 115 | + editor.ReplaceNode( |
| 116 | + recordDeclaration, |
| 117 | + (current, _) => |
| 118 | + { |
| 119 | + var currentRecordDeclaration = (RecordDeclarationSyntax)current; |
| 120 | + if (!TryGetTokens(currentRecordDeclaration, out var classOrStructKeyword, out var recordKeyword)) |
| 121 | + return currentRecordDeclaration; |
| 122 | + |
| 123 | + return currentRecordDeclaration |
| 124 | + .WithClassOrStructKeyword(classOrStructKeyword) |
| 125 | + .WithKeyword(recordKeyword); |
| 126 | + }); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return Task.CompletedTask; |
| 131 | + } |
| 132 | + |
| 133 | + private class MyCodeAction : CustomCodeActions.DocumentChangeAction |
| 134 | + { |
| 135 | + public MyCodeAction( |
| 136 | + Func<CancellationToken, Task<Document>> createChangedDocument) |
| 137 | + : base(CSharpCodeFixesResources.Fix_record_declaration, createChangedDocument, nameof(CSharpTransposeRecordKeywordCodeFixProvider)) |
| 138 | + { |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments