Fix interpolated strings in ScriptPropertyDefVal

This commit is contained in:
Paul Joannon 2024-02-29 12:57:27 +01:00
parent df78c0636d
commit 42233284b1
No known key found for this signature in database
GPG key ID: C12F69B0AD0390DD
5 changed files with 105 additions and 1 deletions

View file

@ -0,0 +1,26 @@
using Godot;
namespace Godot.SourceGenerators.Sample
{
public partial class ExportedComplexStrings : Node
{
[Export]
private string _fieldInterpolated1 = $"The quick brown fox jumps over ({Engine.GetVersionInfo()})";
[Export]
private string _fieldInterpolated2 = $"The quick brown fox jumps over ({Engine.GetVersionInfo()["major"],0:G}) the lazy dog.";
[Export]
private string _fieldInterpolated3 = $"{((int)Engine.GetVersionInfo()["major"]) * -1 * -1:G} the lazy dog.";
[Export]
private string _fieldInterpolated4 = $"{":::fff,,}<,<}},,}]"}";
[Export]
public string PropertyInterpolated1
{
get;
private set;
} = $"The quick brown fox jumps over {GD.VarToStr($"the lazy {Engine.GetVersionInfo()} do")}g.";
}
}

View file

@ -21,4 +21,13 @@ public class ScriptPropertyDefValGeneratorTests
"ExportedProperties_ScriptPropertyDefVal.generated.cs"
);
}
[Fact]
public async void ExportedComplexStrings()
{
await CSharpSourceGeneratorVerifier<ScriptPropertyDefValGenerator>.Verify(
"ExportedComplexStrings.cs",
"ExportedComplexStrings_ScriptPropertyDefVal.generated.cs"
);
}
}

View file

@ -0,0 +1,29 @@
partial class ExportedComplexStrings
{
#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword
#if TOOLS
/// <summary>
/// Get the default values for all properties declared in this class.
/// This method is used by Godot to determine the value that will be
/// used by the inspector when resetting properties.
/// Do not call this method.
/// </summary>
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
internal new static global::System.Collections.Generic.Dictionary<global::Godot.StringName, global::Godot.Variant> GetGodotPropertyDefaultValues()
{
var values = new global::System.Collections.Generic.Dictionary<global::Godot.StringName, global::Godot.Variant>(5);
string __PropertyInterpolated1_default_value = $"The quick brown fox jumps over {(global::Godot.GD.VarToStr($"the lazy {(global::Godot.Engine.GetVersionInfo())} do"))}g.";
values.Add(PropertyName.PropertyInterpolated1, global::Godot.Variant.From<string>(__PropertyInterpolated1_default_value));
string ___fieldInterpolated1_default_value = $"The quick brown fox jumps over ({(global::Godot.Engine.GetVersionInfo())})";
values.Add(PropertyName._fieldInterpolated1, global::Godot.Variant.From<string>(___fieldInterpolated1_default_value));
string ___fieldInterpolated2_default_value = $"The quick brown fox jumps over ({(global::Godot.Engine.GetVersionInfo()["major"]),0:G}) the lazy dog.";
values.Add(PropertyName._fieldInterpolated2, global::Godot.Variant.From<string>(___fieldInterpolated2_default_value));
string ___fieldInterpolated3_default_value = $"{(((int)global::Godot.Engine.GetVersionInfo()["major"]) * -1 * -1):G} the lazy dog.";
values.Add(PropertyName._fieldInterpolated3, global::Godot.Variant.From<string>(___fieldInterpolated3_default_value));
string ___fieldInterpolated4_default_value = $"{(":::fff,,}<,<}},,}]")}";
values.Add(PropertyName._fieldInterpolated4, global::Godot.Variant.From<string>(___fieldInterpolated4_default_value));
return values;
}
#endif // TOOLS
#pragma warning restore CS0109
}

View file

@ -0,0 +1,23 @@
using Godot;
public partial class ExportedComplexStrings : Node
{
[Export]
private string _fieldInterpolated1 = $"The quick brown fox jumps over ({Engine.GetVersionInfo()})";
[Export]
private string _fieldInterpolated2 = $"The quick brown fox jumps over ({Engine.GetVersionInfo()["major"],0:G}) the lazy dog.";
[Export]
private string _fieldInterpolated3 = $"{((int)Engine.GetVersionInfo()["major"]) * -1 * -1:G} the lazy dog.";
[Export]
private string _fieldInterpolated4 = $"{":::fff,,}<,<}},,}]"}";
[Export]
public string PropertyInterpolated1
{
get;
private set;
} = $"The quick brown fox jumps over {GD.VarToStr($"the lazy {Engine.GetVersionInfo()} do")}g.";
}

View file

@ -208,7 +208,17 @@ namespace Godot.SourceGenerators
if (child.IsNode)
{
FullQualifiedSyntax(child.AsNode()!, sm, sb, isFirstNode: innerIsFirstNode);
var childNode = child.AsNode()!;
if (node is InterpolationSyntax && childNode is ExpressionSyntax)
{
ParenEnclosedFullQualifiedSyntax(childNode, sm, sb, isFirstNode: innerIsFirstNode);
}
else
{
FullQualifiedSyntax(childNode, sm, sb, isFirstNode: innerIsFirstNode);
}
innerIsFirstNode = false;
}
else
@ -221,6 +231,13 @@ namespace Godot.SourceGenerators
sb.Append(child.GetTrailingTrivia());
}
}
static void ParenEnclosedFullQualifiedSyntax(SyntaxNode node, SemanticModel sm, StringBuilder sb, bool isFirstNode)
{
sb.Append(SyntaxFactory.Token(SyntaxKind.OpenParenToken));
FullQualifiedSyntax(node, sm, sb, isFirstNode);
sb.Append(SyntaxFactory.Token(SyntaxKind.CloseParenToken));
}
}
public static string SanitizeQualifiedNameForUniqueHint(this string qualifiedName)