1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-05 12:10:55 +00:00

C#: Various fixes to generic scripts

- Report a diagnostic when there are multiple classes that match the script file name in the same script since that will result in a duplicate path key in the bimap and it's not allowed.
- Fix InspectorPlugin to handle empty paths in case the project was built with a previous version of Godot that used empty paths for generic scripts.
- Add tests for the new diagnostic GD0003.
This commit is contained in:
Raul Santos
2024-02-15 18:18:33 +01:00
parent ae51db75e7
commit fe280ef9ae
12 changed files with 126 additions and 28 deletions

View File

@@ -58,9 +58,10 @@ namespace Godot.SourceGenerators
.GroupBy<(ClassDeclarationSyntax cds, INamedTypeSymbol symbol), INamedTypeSymbol>(x => x.symbol, SymbolEqualityComparer.Default)
.ToDictionary<IGrouping<INamedTypeSymbol, (ClassDeclarationSyntax cds, INamedTypeSymbol symbol)>, INamedTypeSymbol, IEnumerable<ClassDeclarationSyntax>>(g => g.Key, g => g.Select(x => x.cds), SymbolEqualityComparer.Default);
var usedPaths = new HashSet<string>();
foreach (var godotClass in godotClasses)
{
VisitGodotScriptClass(context, godotProjectDir,
VisitGodotScriptClass(context, godotProjectDir, usedPaths,
symbol: godotClass.Key,
classDeclarations: godotClass.Value);
}
@@ -74,6 +75,7 @@ namespace Godot.SourceGenerators
private static void VisitGodotScriptClass(
GeneratorExecutionContext context,
string godotProjectDir,
HashSet<string> usedPaths,
INamedTypeSymbol symbol,
IEnumerable<ClassDeclarationSyntax> classDeclarations
)
@@ -93,8 +95,19 @@ namespace Godot.SourceGenerators
if (attributes.Length != 0)
attributes.Append("\n");
string scriptPath = RelativeToDir(cds.SyntaxTree.FilePath, godotProjectDir);
if (!usedPaths.Add(scriptPath))
{
context.ReportDiagnostic(Diagnostic.Create(
Common.MultipleClassesInGodotScriptRule,
cds.Identifier.GetLocation(),
symbol.Name
));
return;
}
attributes.Append(@"[ScriptPathAttribute(""res://");
attributes.Append(RelativeToDir(cds.SyntaxTree.FilePath, godotProjectDir));
attributes.Append(scriptPath);
attributes.Append(@""")]");
}