Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CustomGeneratorTests/CustomGeneratorTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GodotSharp.SourceGenerators" Version="2.6.0-250606-1000.Release" />
<PackageReference Include="GodotSharp.SourceGenerators" Version="2.6.0-250606-1122.Release" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="[4.8.0]" PrivateAssets="all" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Godot 3 Tests/Godot 3 Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="[7.2.0]" />
<PackageReference Include="GodotSharp.SourceGenerators" Version="2.6.0-250606-1000.Release" />
<PackageReference Include="GodotSharp.SourceGenerators" Version="2.6.0-250606-1122.Release" />
<ProjectReference Include="..\CustomGeneratorTests\CustomGeneratorTests.csproj" OutputItemType="analyzer" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Godot 4 Tests/Godot 4 Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="[7.2.0]" />
<PackageReference Include="GodotSharp.SourceGenerators" Version="2.6.0-250606-1000.Release" />
<PackageReference Include="GodotSharp.SourceGenerators" Version="2.6.0-250606-1122.Release" />
<ProjectReference Include="..\CustomGeneratorTests\CustomGeneratorTests.csproj" OutputItemType="analyzer" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion NRT.Tests/GD3.NRT/GD3.NRT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="[7.2.0]" />
<PackageReference Include="GodotSharp.SourceGenerators" Version="2.6.0-250606-1000.Release" />
<PackageReference Include="GodotSharp.SourceGenerators" Version="2.6.0-250606-1122.Release" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion NRT.Tests/GD4.NRT/GD4.NRT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="[7.2.0]" />
<PackageReference Include="GodotSharp.SourceGenerators" Version="2.6.0-250606-1000.Release" />
<PackageReference Include="GodotSharp.SourceGenerators" Version="2.6.0-250606-1122.Release" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion SourceGenerators/AutoloadExtensions/AutoloadAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Godot;

[AttributeUsage(AttributeTargets.Class)]
public class AutoloadAttribute([CallerFilePath] string ClassPath = null) : Attribute
public class AutoloadAttribute([CallerFilePath] string ClassPath = null!) : Attribute
{
public string ClassPath { get; } = ClassPath;
}
Expand Down
4 changes: 2 additions & 2 deletions SourceGenerators/AutoloadExtensions/AutoloadDataModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ namespace GodotSharp.SourceGenerators.AutoloadExtensions;

internal class AutoloadDataModel : ClassDataModel
{
public record AutoloadData(string Type, string GodotName, string DisplayName);
public record AutoloadData(string Type, string? GodotName, string? DisplayName);

public IList<AutoloadData> Autoloads { get; }

public AutoloadDataModel(Compilation compilation, INamedTypeSymbol symbol, string csPath, string gdRoot, IDictionary<string, string> lookup)
public AutoloadDataModel(Compilation compilation, INamedTypeSymbol symbol, string csPath, string? gdRoot, IDictionary<string, string> lookup)
: base(symbol)
{
Autoloads = AutoloadScraper.GetAutoloads(compilation, csPath, gdRoot)
Expand Down
33 changes: 17 additions & 16 deletions SourceGenerators/AutoloadExtensions/AutoloadScraper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;

Expand All @@ -11,7 +12,7 @@ internal static class AutoloadScraper
private const string AutoloadRegexStr = @"^(?<Name>.+?)=""\*res:\/\/(?<Path>.+?)""$";
private static readonly Regex AutoloadRegex = new(AutoloadRegexStr, RegexOptions.Compiled | RegexOptions.ExplicitCapture);

public static IEnumerable<Autoload> GetAutoloads(Compilation compilation, string csFile, string gdRoot)
public static IEnumerable<Autoload> GetAutoloads(Compilation compilation, string csFile, string? gdRoot)
{
var gdFile = GD.GetProjectFile(csFile, gdRoot);
Log.Debug($"Scraping {gdFile} [Compiling {csFile}]");
Expand Down Expand Up @@ -42,7 +43,7 @@ IEnumerable<Autoload> MatchAutoloads(string gdFile)
}
}

bool TryMatchAutoload(string line, out string name, out string path)
bool TryMatchAutoload(string line, [NotNullWhen(true)] out string? name, [NotNullWhen(true)] out string? path)
{
var match = AutoloadRegex.Match(line);
if (match.Success)
Expand All @@ -58,13 +59,13 @@ bool TryMatchAutoload(string line, out string name, out string path)
return false;
}

string TryGetType(string path)
string? TryGetType(string? path)
{
return Path.GetExtension(path) switch
{
".gd" => MiniGdScraper.TryGetType(compilation, gdRoot, path),
".cs" => MiniCsScraper.TryGetType(compilation, gdRoot, path),
".tscn" => MiniTscnScraper.TryGetType(compilation, gdRoot, path),
".gd" => MiniGdScraper.TryGetType(compilation, gdRoot, path!),
".cs" => MiniCsScraper.TryGetType(compilation, gdRoot, path!),
".tscn" => MiniTscnScraper.TryGetType(compilation, gdRoot, path!),
_ => null,
};
}
Expand All @@ -84,7 +85,7 @@ public static string TryGetType(Compilation compilation, string gdRoot, string p
var file = Path.Combine(gdRoot, path);
Log.Debug($">>> Scraping type from gd: {file}");

string type = null;
string? type = null;
foreach (var line in File.ReadLines(file)
.Where(line => line is not "" && !line.StartsWith("#")))
{
Expand All @@ -100,7 +101,7 @@ public static string TryGetType(Compilation compilation, string gdRoot, string p
Log.Debug($"<<< {type}");
return type;

bool TryMatchExtends(string line, ref string type)
bool TryMatchExtends(string line, ref string? type)
{
var match = ExtendsRegex1.Match(line);
if (match.Success)
Expand All @@ -124,7 +125,7 @@ bool TryMatchExtends(string line, ref string type)
return false;
}

string GetValidType(string type)
string GetValidType(string? type)
{
if (type is null)
{
Expand Down Expand Up @@ -164,7 +165,7 @@ public static string TryGetType(Compilation compilation, string gdRoot, string p
Log.Debug($"<<< {type}");
return type;

static string GetValidType(string type)
static string GetValidType(string? type)
{
if (type is null)
{
Expand Down Expand Up @@ -201,7 +202,7 @@ public static string TryGetType(Compilation compilation, string gdRoot, string t
var file = Path.Combine(gdRoot, tscn);
Log.Debug($">>> Scraping type from tscn: {file}");

string type = null;
string? type = null;
var doneRes = false;
var doneRoot = false;
var doneValues = false;
Expand Down Expand Up @@ -254,7 +255,7 @@ bool TryMatchRes(string line)
return false;
}

bool TryMatchRoot(string line, ref string type)
bool TryMatchRoot(string line, ref string? type)
{
if (doneRoot) return false;

Expand Down Expand Up @@ -298,7 +299,7 @@ bool TryMatchRoot(string line, ref string type)
return true;
}

bool TryMatchValues(string line, ref string type)
bool TryMatchValues(string line, ref string? type)
{
if (doneValues) return false;

Expand All @@ -316,7 +317,7 @@ bool TryMatchValues(string line, ref string type)

return true;

bool TryMatchScript(string line, ref string type)
bool TryMatchScript(string line, ref string? type)
{
var match = ScriptRegexGD4.Match(line);
if (match.Success)
Expand All @@ -338,7 +339,7 @@ bool TryMatchScript(string line, ref string type)

return false;

string TryGetType(string path)
string? TryGetType(string path)
{
return Path.GetExtension(path) switch
{
Expand All @@ -350,7 +351,7 @@ string TryGetType(string path)
}
}

string GetValidType(string type)
string GetValidType(string? type)
{
if (type is null)
{
Expand Down
14 changes: 8 additions & 6 deletions SourceGenerators/AutoloadExtensions/AutoloadSourceGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Scriban;

Expand All @@ -7,6 +8,7 @@ namespace GodotSharp.SourceGenerators.AutoloadExtensions;
[Generator]
internal class AutoloadSourceGenerator : SourceGeneratorForDeclaredTypeWithAttribute<Godot.AutoloadAttribute>
{
[field: MaybeNull]
private static Template AutoloadTemplate => field ??= Template.Parse(Resources.AutoloadTemplate);

protected override IEnumerable<(string Name, string Source)> StaticSources
Expand All @@ -17,7 +19,7 @@ internal class AutoloadSourceGenerator : SourceGeneratorForDeclaredTypeWithAttri
}
}

protected override (string GeneratedCode, DiagnosticDetail Error) GenerateCode(Compilation compilation, SyntaxNode _, INamedTypeSymbol symbol, AttributeData attribute, AnalyzerConfigOptions options)
protected override (string? GeneratedCode, DiagnosticDetail? Error) GenerateCode(Compilation compilation, SyntaxNode _, INamedTypeSymbol symbol, AttributeData attribute, AnalyzerConfigOptions options)
{
var model = new AutoloadDataModel(compilation, symbol, ReconstructAttribute().ClassPath, options.TryGetGodotProjectDir(), GetAutoloadRenameLookup());
Log.Debug($"--- MODEL ---\n{model}\n");
Expand All @@ -28,15 +30,15 @@ protected override (string GeneratedCode, DiagnosticDetail Error) GenerateCode(C
return (output, null);

Godot.AutoloadAttribute ReconstructAttribute()
=> new((string)attribute.ConstructorArguments[0].Value);
=> new((string)attribute.ConstructorArguments[0].Value!);

IDictionary<string, string> GetAutoloadRenameLookup()
{
return symbol.GetAttributes()
.Where(x => x.AttributeClass.Name == RenameAttribute)
.Where(x => x.AttributeClass?.Name == RenameAttribute)
.ToDictionary(
x => (string)x.ConstructorArguments[1].Value, // GodotName
x => (string)x.ConstructorArguments[0].Value); // DisplayName
x => (string)x.ConstructorArguments[1].Value!, // GodotName
x => (string)x.ConstructorArguments[0].Value!); // DisplayName
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Scriban;

Expand All @@ -7,9 +8,10 @@ namespace GodotSharp.SourceGenerators.CodeCommentsExtensions;
[Generator]
internal class CodeCommentsSourceGenerator : SourceGeneratorForDeclaredTypeWithAttribute<Godot.CodeCommentsAttribute>
{
[field: MaybeNull]
private static Template CodeCommentsTemplate => field ??= Template.Parse(Resources.CodeCommentsTemplate);

protected override (string GeneratedCode, DiagnosticDetail Error) GenerateCode(Compilation compilation, SyntaxNode node, INamedTypeSymbol symbol, AttributeData attribute, AnalyzerConfigOptions options)
protected override (string? GeneratedCode, DiagnosticDetail? Error) GenerateCode(Compilation compilation, SyntaxNode node, INamedTypeSymbol symbol, AttributeData attribute, AnalyzerConfigOptions options)
{
var model = new CodeCommentsDataModel(symbol, node, ReconstructAttribute().Strip);
Log.Debug($"--- MODEL ---\n{model}\n");
Expand All @@ -20,6 +22,6 @@ protected override (string GeneratedCode, DiagnosticDetail Error) GenerateCode(C
return (output, null);

Godot.CodeCommentsAttribute ReconstructAttribute()
=> new((string)attribute.ConstructorArguments[0].Value);
=> new((string)attribute.ConstructorArguments[0].Value!);
}
}
8 changes: 4 additions & 4 deletions SourceGenerators/DiagnosticDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

public record DiagnosticDetail
{
public string Id { get; init; }
public string Category { get; init; }
public string Title { get; init; }
public string Message { get; init; }
public string? Id { get; init; }
public string? Category { get; init; }
public required string Title { get; init; }
public required string Message { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ internal class GodotNotifyDataModel : MemberDataModel
public string Field { get; }

public string Modifiers { get; }
public string GetAccess { get; }
public string SetAccess { get; }
public string? GetAccess { get; }
public string? SetAccess { get; }

public bool ClassIsResource { get; }
public bool ValueIsResource { get; }
Expand All @@ -36,7 +36,7 @@ public GodotNotifyDataModel(IPropertySymbol symbol, SyntaxNode node)
static bool IsResource(ITypeSymbol type)
=> type.InheritsFrom("Resource");

static string GetAccessibility(IMethodSymbol accessor, string @default)
static string? GetAccessibility(IMethodSymbol? accessor, string @default)
{
var accessibility = accessor?.GetDeclaredAccessibility();
return accessibility is null ? null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Scriban;

Expand All @@ -7,9 +8,10 @@ namespace GodotSharp.SourceGenerators.GodotNotifyExtensions;
[Generator]
internal class GodotNotifySourceGenerator : SourceGeneratorForDeclaredPropertyWithAttribute<Godot.NotifyAttribute>
{
[field: MaybeNull]
private static Template GodotNotifyTemplate => field ??= Template.Parse(Resources.GodotNotifyTemplate);

protected override (string GeneratedCode, DiagnosticDetail Error) GenerateCode(Compilation compilation, SyntaxNode node, IPropertySymbol symbol, AttributeData attribute, AnalyzerConfigOptions options)
protected override (string? GeneratedCode, DiagnosticDetail? Error) GenerateCode(Compilation compilation, SyntaxNode node, IPropertySymbol symbol, AttributeData attribute, AnalyzerConfigOptions options)
{
var model = new GodotNotifyDataModel(symbol, node);
Log.Debug($"--- MODEL ---\n{model}\n");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Scriban;

Expand All @@ -7,9 +8,10 @@ namespace GodotSharp.SourceGenerators.GodotOverrideExtensions;
[Generator]
internal class GodotOverrideSourceGenerator : SourceGeneratorForDeclaredMethodWithAttribute<Godot.GodotOverrideAttribute>
{
[field: MaybeNull]
private static Template GodotOverrideTemplate => field ??= Template.Parse(Resources.GodotOverrideTemplate);

protected override (string GeneratedCode, DiagnosticDetail Error) GenerateCode(Compilation compilation, SyntaxNode node, IMethodSymbol symbol, AttributeData attribute, AnalyzerConfigOptions options)
protected override (string? GeneratedCode, DiagnosticDetail? Error) GenerateCode(Compilation compilation, SyntaxNode node, IMethodSymbol symbol, AttributeData attribute, AnalyzerConfigOptions options)
{
var model = new GodotOverrideDataModel(symbol, ReconstructAttribute().Replace);
Log.Debug($"--- MODEL ---\n{model}\n");
Expand All @@ -20,6 +22,6 @@ protected override (string GeneratedCode, DiagnosticDetail Error) GenerateCode(C
return (output, null);

Godot.GodotOverrideAttribute ReconstructAttribute()
=> new((bool)attribute.ConstructorArguments[0].Value);
=> new((bool)attribute.ConstructorArguments[0].Value!);
}
}
2 changes: 1 addition & 1 deletion SourceGenerators/InputMapExtensions/InputMapAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Godot;
[AttributeUsage(AttributeTargets.Class)]
public sealed class InputMapAttribute : Attribute
{
public InputMapAttribute([CallerFilePath] string classPath = null)
public InputMapAttribute([CallerFilePath] string classPath = null!)
=> ClassPath = classPath;

public string ClassPath { get; }
Expand Down
2 changes: 1 addition & 1 deletion SourceGenerators/InputMapExtensions/InputMapDataModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class InputMapDataModel : ClassDataModel
public IList<InputAction> Actions { get; }
public ILookup<string, InputAction> NestedActions { get; }

public InputMapDataModel(INamedTypeSymbol symbol, string csPath, string gdRoot) : base(symbol)
public InputMapDataModel(INamedTypeSymbol symbol, string csPath, string? gdRoot) : base(symbol)
{
var actions = InputMapScraper
.GetInputActions(csPath, gdRoot)
Expand Down
7 changes: 4 additions & 3 deletions SourceGenerators/InputMapExtensions/InputMapScraper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;

namespace GodotSharp.SourceGenerators.InputMapExtensions;

Expand All @@ -7,7 +8,7 @@ internal static class InputMapScraper
private const string InputRegexStr = @"^""?(?<Input>.+?)""?=.*$";
private static readonly Regex InputRegex = new(InputRegexStr, RegexOptions.Compiled | RegexOptions.ExplicitCapture);

public static IEnumerable<string> GetInputActions(string csFile, string gdRoot)
public static IEnumerable<string> GetInputActions(string csFile, string? gdRoot)
{
var gdFile = GD.GetProjectFile(csFile, gdRoot);
Log.Debug($"Scraping {gdFile} [Compiling {csFile}]");
Expand Down Expand Up @@ -36,7 +37,7 @@ static IEnumerable<string> MatchInputActions(string gdFile)
}
}

static bool TryMatchInput(string line, out string action)
static bool TryMatchInput(string line, [NotNullWhen(true)] out string? action)
{
var match = InputRegex.Match(line);
if (match.Success)
Expand Down
Loading