| 1 |
using System.Collections.Immutable; |
| 2 |
using System.Linq; |
| 3 |
using System.Threading; |
| 4 |
using System.Threading.Tasks; |
| 5 |
using Microsoft.CodeAnalysis; |
| 6 |
using Microsoft.CodeAnalysis.CodeActions; |
| 7 |
using Microsoft.CodeAnalysis.CodeFixes; |
| 8 |
using Microsoft.CodeAnalysis.CSharp; |
| 9 |
using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 10 |
using Microsoft.CodeAnalysis.Diagnostics; |
| 11 |
|
| 12 |
namespace Godot.SourceGenerators |
| 13 |
{ |
| 14 |
[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 15 |
public sealed class ClassPartialModifierAnalyzer : DiagnosticAnalyzer |
| 16 |
{ |
| 17 |
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| 18 |
ImmutableArray.Create(Common.ClassPartialModifierRule, Common.OuterClassPartialModifierRule); |
| 19 |
|
| 20 |
public override void Initialize(AnalysisContext context) |
| 21 |
{ |
| 22 |
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 23 |
context.EnableConcurrentExecution(); |
| 24 |
context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.ClassDeclaration); |
| 25 |
} |
| 26 |
|
| 27 |
private void AnalyzeNode(SyntaxNodeAnalysisContext context) |
| 28 |
{ |
| 29 |
if (context.Node is not ClassDeclarationSyntax classDeclaration) |
| 30 |
return; |
| 31 |
|
| 32 |
if (context.ContainingSymbol is not INamedTypeSymbol typeSymbol) |
| 33 |
return; |
| 34 |
|
| 35 |
if (!typeSymbol.InheritsFrom("GodotSharp", GodotClasses.GodotObject)) |
| 36 |
return; |
| 37 |
|
| 38 |
if (!classDeclaration.IsPartial()) |
| 39 |
context.ReportDiagnostic(Diagnostic.Create( |
| 40 |
Common.ClassPartialModifierRule, |
| 41 |
classDeclaration.Identifier.GetLocation(), |
| 42 |
typeSymbol.ToDisplayString())); |
| 43 |
|
| 44 |
var outerClassDeclaration = context.Node.Parent as ClassDeclarationSyntax; |
| 45 |
while (outerClassDeclaration is not null) |
| 46 |
{ |
| 47 |
var outerClassTypeSymbol = context.SemanticModel.GetDeclaredSymbol(outerClassDeclaration); |
| 48 |
if (outerClassTypeSymbol == null) |
| 49 |
return; |
| 50 |
|
| 51 |
if (!outerClassDeclaration.IsPartial()) |
| 52 |
context.ReportDiagnostic(Diagnostic.Create( |
| 53 |
Common.OuterClassPartialModifierRule, |
| 54 |
outerClassDeclaration.Identifier.GetLocation(), |
| 55 |
outerClassTypeSymbol.ToDisplayString())); |
| 56 |
|
| 57 |
outerClassDeclaration = outerClassDeclaration.Parent as ClassDeclarationSyntax; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
[ExportCodeFixProvider(LanguageNames.CSharp)] |
| 63 |
public sealed class ClassPartialModifierCodeFixProvider : CodeFixProvider |
| 64 |
{ |
| 65 |
public override ImmutableArray<string> FixableDiagnosticIds => |
| 66 |
ImmutableArray.Create(Common.ClassPartialModifierRule.Id); |
| 67 |
|
| 68 |
public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; |
| 69 |
|
| 70 |
public override async Task RegisterCodeFixesAsync(CodeFixContext context) |
| 71 |
{ |
| 72 |
|
| 73 |
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); |
| 74 |
|
| 75 |
|
| 76 |
var diagnostic = context.Diagnostics.First(); |
| 77 |
|
| 78 |
|
| 79 |
var diagnosticSpan = diagnostic.Location.SourceSpan; |
| 80 |
|
| 81 |
|
| 82 |
var classDeclaration = root?.FindToken(diagnosticSpan.Start) |
| 83 |
.Parent? |
| 84 |
.AncestorsAndSelf() |
| 85 |
.OfType<ClassDeclarationSyntax>() |
| 86 |
.First(); |
| 87 |
|
| 88 |
if (classDeclaration == null) |
| 89 |
return; |
| 90 |
|
| 91 |
context.RegisterCodeFix( |
| 92 |
CodeAction.Create( |
| 93 |
"Add partial modifier", |
| 94 |
cancellationToken => AddPartialModifierAsync(context.Document, classDeclaration, cancellationToken), |
| 95 |
classDeclaration.ToFullString()), |
| 96 |
context.Diagnostics); |
| 97 |
} |
| 98 |
|
| 99 |
private static async Task<Document> AddPartialModifierAsync(Document document, |
| 100 |
ClassDeclarationSyntax classDeclaration, CancellationToken cancellationToken) |
| 101 |
{ |
| 102 |
|
| 103 |
var partialModifier = SyntaxFactory.Token(SyntaxKind.PartialKeyword); |
| 104 |
var modifiedClassDeclaration = classDeclaration.AddModifiers(partialModifier); |
| 105 |
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); |
| 106 |
|
| 107 |
var newRoot = root!.ReplaceNode(classDeclaration, modifiedClassDeclaration); |
| 108 |
var newDocument = document.WithSyntaxRoot(newRoot); |
| 109 |
return newDocument; |
| 110 |
} |
| 111 |
} |
| 112 |
} |