-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provide analyser corresponding to the GD0001 and GD0002, add ClassPar…
…tialModifierAnalyzerFix, and tests Co-authored-by: Raul Santos <[email protected]> Co-authored-by: A Thousand Ships <[email protected]>
- Loading branch information
1 parent
0246230
commit 00dc195
Showing
15 changed files
with
231 additions
and
77 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpCodeFixVerifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Testing; | ||
using Microsoft.CodeAnalysis.Testing.Verifiers; | ||
|
||
namespace Godot.SourceGenerators.Tests; | ||
|
||
public static class CSharpCodeFixVerifier<TCodeFix, TAnalyzer> | ||
where TCodeFix : CodeFixProvider, new() | ||
where TAnalyzer : DiagnosticAnalyzer, new() | ||
{ | ||
public class Test : CSharpCodeFixTest<TAnalyzer, TCodeFix, XUnitVerifier> | ||
{ | ||
public Test() | ||
{ | ||
ReferenceAssemblies = ReferenceAssemblies.Net.Net60; | ||
SolutionTransforms.Add((Solution solution, ProjectId projectId) => | ||
{ | ||
Project project = solution.GetProject(projectId)! | ||
.AddMetadataReference(Constants.GodotSharpAssembly.CreateMetadataReference()); | ||
return project.Solution; | ||
}); | ||
} | ||
} | ||
|
||
public static Task Verify(string sources, string fixedSources) | ||
{ | ||
return MakeVerifier(sources, fixedSources).RunAsync(); | ||
} | ||
|
||
public static Test MakeVerifier(string source, string results) | ||
{ | ||
var verifier = new Test(); | ||
|
||
verifier.TestCode = File.ReadAllText(Path.Combine(Constants.SourceFolderPath, source)); | ||
verifier.FixedCode = File.ReadAllText(Path.Combine(Constants.GeneratedSourceFolderPath, results)); | ||
|
||
verifier.TestState.AnalyzerConfigFiles.Add(("/.globalconfig", $""" | ||
is_global = true | ||
build_property.GodotProjectDir = {Constants.ExecutingAssemblyPath} | ||
""")); | ||
|
||
return verifier; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/ClassPartialModifierAnalyzerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Godot.SourceGenerators.Tests; | ||
|
||
public class ClassPartialModifierTest | ||
{ | ||
[Fact] | ||
public async Task ClassPartialModifierCodeFixTest() | ||
{ | ||
await CSharpCodeFixVerifier<ClassPartialModifierCodeFixProvider, ClassPartialModifierAnalyzer> | ||
.Verify("ClassPartialModifier.GD0001.cs", "ClassPartialModifier.GD0001.fixed.cs"); | ||
} | ||
|
||
[Fact] | ||
public async void OuterClassPartialModifierAnalyzerTest() | ||
{ | ||
await CSharpAnalyzerVerifier<ClassPartialModifierAnalyzer>.Verify("OuterClassPartialModifierAnalyzer.GD0002.cs"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...dot.SourceGenerators.Tests/TestData/GeneratedSources/ClassPartialModifier.GD0001.fixed.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using Godot; | ||
|
||
public partial class ClassPartialModifier : Node | ||
{ | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
...odot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/ClassPartialModifier.GD0001.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using Godot; | ||
|
||
public class {|GD0001:ClassPartialModifier|} : Node | ||
{ | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...Godot.SourceGenerators.Tests/TestData/Sources/OuterClassPartialModifierAnalyzer.GD0002.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Godot; | ||
|
||
public class {|GD0002:OuterOuterClassPartialModifierAnalyzer|} | ||
{ | ||
public class {|GD0002:OuterClassPartialModifierAnalyzer|} | ||
{ | ||
// MyNode is contained in a non-partial type so the source generators | ||
// can't enhance this type to work with Godot. | ||
public partial class MyNode : Node { } | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ClassPartialModifierAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Godot.SourceGenerators | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class ClassPartialModifierAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => | ||
ImmutableArray.Create(Common.ClassPartialModifierRule, Common.OuterClassPartialModifierRule); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
context.EnableConcurrentExecution(); | ||
context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.ClassDeclaration); | ||
} | ||
|
||
private void AnalyzeNode(SyntaxNodeAnalysisContext context) | ||
{ | ||
if (context.Node is not ClassDeclarationSyntax classDeclaration) | ||
return; | ||
|
||
if (context.ContainingSymbol is not INamedTypeSymbol typeSymbol) | ||
return; | ||
|
||
if (!typeSymbol.InheritsFrom("GodotSharp", GodotClasses.GodotObject)) | ||
return; | ||
|
||
if (!classDeclaration.IsPartial()) | ||
context.ReportDiagnostic(Diagnostic.Create( | ||
Common.ClassPartialModifierRule, | ||
classDeclaration.Identifier.GetLocation(), | ||
typeSymbol.ToDisplayString())); | ||
|
||
var outerClassDeclaration = context.Node.Parent as ClassDeclarationSyntax; | ||
while (outerClassDeclaration is not null) | ||
{ | ||
var outerClassTypeSymbol = context.SemanticModel.GetDeclaredSymbol(outerClassDeclaration); | ||
if (outerClassTypeSymbol == null) | ||
return; | ||
|
||
if (!outerClassDeclaration.IsPartial()) | ||
context.ReportDiagnostic(Diagnostic.Create( | ||
Common.OuterClassPartialModifierRule, | ||
outerClassDeclaration.Identifier.GetLocation(), | ||
outerClassTypeSymbol.ToDisplayString())); | ||
|
||
outerClassDeclaration = outerClassDeclaration.Parent as ClassDeclarationSyntax; | ||
} | ||
} | ||
} | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp)] | ||
public sealed class ClassPartialModifierCodeFixProvider : CodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds => | ||
ImmutableArray.Create(Common.ClassPartialModifierRule.Id); | ||
|
||
public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; | ||
|
||
public override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
// Get the syntax root of the document. | ||
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | ||
|
||
// Get the diagnostic to fix. | ||
var diagnostic = context.Diagnostics.First(); | ||
|
||
// Get the location of code issue. | ||
var diagnosticSpan = diagnostic.Location.SourceSpan; | ||
|
||
// Use that location to find the containing class declaration. | ||
var classDeclaration = root?.FindToken(diagnosticSpan.Start) | ||
.Parent? | ||
.AncestorsAndSelf() | ||
.OfType<ClassDeclarationSyntax>() | ||
.First(); | ||
|
||
if (classDeclaration == null) | ||
return; | ||
|
||
context.RegisterCodeFix( | ||
CodeAction.Create( | ||
"Add partial modifier", | ||
cancellationToken => AddPartialModifierAsync(context.Document, classDeclaration, cancellationToken), | ||
classDeclaration.ToFullString()), | ||
context.Diagnostics); | ||
} | ||
|
||
private static async Task<Document> AddPartialModifierAsync(Document document, | ||
ClassDeclarationSyntax classDeclaration, CancellationToken cancellationToken) | ||
{ | ||
// Create a new partial modifier. | ||
var partialModifier = SyntaxFactory.Token(SyntaxKind.PartialKeyword); | ||
var modifiedClassDeclaration = classDeclaration.AddModifiers(partialModifier); | ||
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | ||
// Replace the old class declaration with the modified one in the syntax root. | ||
var newRoot = root!.ReplaceNode(classDeclaration, modifiedClassDeclaration); | ||
var newDocument = document.WithSyntaxRoot(newRoot); | ||
return newDocument; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.