Skip to content

Commit

Permalink
Fix interpolated strings in ScriptPropertyDefVal
Browse files Browse the repository at this point in the history
  • Loading branch information
paulloz committed Mar 1, 2024
1 parent df78c06 commit 4223328
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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.";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,13 @@ await CSharpSourceGeneratorVerifier<ScriptPropertyDefValGenerator>.Verify(
"ExportedProperties_ScriptPropertyDefVal.generated.cs"
);
}

[Fact]
public async void ExportedComplexStrings()
{
await CSharpSourceGeneratorVerifier<ScriptPropertyDefValGenerator>.Verify(
"ExportedComplexStrings.cs",
"ExportedComplexStrings_ScriptPropertyDefVal.generated.cs"
);
}
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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.";
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,17 @@ private static void FullQualifiedSyntax(SyntaxNode node, SemanticModel sm, Strin

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
Expand All @@ -221,6 +231,13 @@ private static void FullQualifiedSyntax(SyntaxNode node, SemanticModel sm, Strin
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)
Expand Down

0 comments on commit 4223328

Please sign in to comment.