From 79a0832f110a5e3d338373bcd929dc149a7e6560 Mon Sep 17 00:00:00 2001 From: PTKu Date: Fri, 24 Feb 2023 17:50:26 +0100 Subject: [PATCH 1/2] Create draft PR for #96 From 5060030dc5fdd3505f0fb601e9b55d9c4b5f6e55 Mon Sep 17 00:00:00 2001 From: Peter <61538034+PTKu@users.noreply.github.com> Date: Sat, 25 Feb 2023 08:37:29 +0100 Subject: [PATCH 2/2] [connector] temporarily removes localization tags from string attributes --- .../Ast/AddedPropertyDeclarationAstNode.cs | 23 ++++++++++++++++++- .../Ast/AddedPropertySetterAstNode.cs | 1 + ...Node.cs => AttributeDeclarationAstNode.cs} | 2 +- .../Pragmas/PragmaParser/PragmaGrammar.cs | 2 +- .../Cs/PragmasExtensionsTests.cs | 2 +- .../Onliners/types_with_name_attributes.g.cs | 14 ++++++++++- .../types_with_property_attributes.g.cs | 14 ++++++++++- .../Localizations/LocalizationHelper.cs | 2 +- 8 files changed, 53 insertions(+), 7 deletions(-) rename src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/Ast/{DeclarationAttributeAstNode.cs => AttributeDeclarationAstNode.cs} (94%) diff --git a/src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/Ast/AddedPropertyDeclarationAstNode.cs b/src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/Ast/AddedPropertyDeclarationAstNode.cs index 21679269..5fe0d7ac 100644 --- a/src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/Ast/AddedPropertyDeclarationAstNode.cs +++ b/src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/Ast/AddedPropertyDeclarationAstNode.cs @@ -26,6 +26,27 @@ public override void Init(AstContext context, ParseTreeNode treeNode) public override void AcceptVisitor(IAstVisitor visitor) { - if (visitor is PragmaVisitor v) v.Product = $"{AccessQualifier} {Type} {Identifier} {{ get; set; }}"; + if (visitor is PragmaVisitor v) + { + if (Type.ToUpperInvariant() == "STRING") + { + v.Product = $"private {Type} _{Identifier};" + + $"\n{AccessQualifier} {Type} {Identifier} " + + $"{{ " + + $"get" + + $"{{ " + + $"return Ix.Localizations.LocalizationHelper.CleanUpLocalizationTokens(_{Identifier}); " + + $"}} " + + $"set; " + + $"{{_{Identifier} = value;" + + $"}} " + + $"}}"; + } + else + { + v.Product = $"{AccessQualifier} {Type} {Identifier} {{ get; set; }}"; + } + + } } } \ No newline at end of file diff --git a/src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/Ast/AddedPropertySetterAstNode.cs b/src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/Ast/AddedPropertySetterAstNode.cs index 1757f994..e3bc2a29 100644 --- a/src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/Ast/AddedPropertySetterAstNode.cs +++ b/src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/Ast/AddedPropertySetterAstNode.cs @@ -30,6 +30,7 @@ public override void Init(AstContext context, ParseTreeNode treeNode) public override void AcceptVisitor(IAstVisitor visitor) { + if (visitor is PragmaVisitor v) v.Product = MemberName != null ? $"{MemberName}.{PropertyName} = {InitValue};" diff --git a/src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/Ast/DeclarationAttributeAstNode.cs b/src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/Ast/AttributeDeclarationAstNode.cs similarity index 94% rename from src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/Ast/DeclarationAttributeAstNode.cs rename to src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/Ast/AttributeDeclarationAstNode.cs index 419f58fe..c704a55f 100644 --- a/src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/Ast/DeclarationAttributeAstNode.cs +++ b/src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/Ast/AttributeDeclarationAstNode.cs @@ -11,7 +11,7 @@ namespace Ix.Compiler.Cs.Pragmas.PragmaParser; -internal class DeclarationAttributeAstNode : AstNode +internal class AttributeDeclarationAstNode : AstNode { public string? AttributeLiteral { get; set; } diff --git a/src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/PragmaGrammar.cs b/src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/PragmaGrammar.cs index be625d48..b5949552 100644 --- a/src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/PragmaGrammar.cs +++ b/src/ix.compiler/src/IX.Cs.Compiler/Pragmas/PragmaParser/PragmaGrammar.cs @@ -34,7 +34,7 @@ internal class PragmaGrammar : Grammar public readonly NonTerminal AddedPropertySetter = new(nameof(AddedPropertySetter), typeof(AddedPropertySetterAstNode)); public readonly Terminal assing = new(nameof(assing)); - public readonly NonTerminal DeclarationAttribute = new(nameof(DeclarationAttribute), typeof(DeclarationAttributeAstNode)); + public readonly NonTerminal DeclarationAttribute = new(nameof(DeclarationAttribute), typeof(AttributeDeclarationAstNode)); public readonly NonTerminal ClrAttribute = new(nameof(ClrAttribute)); public readonly FreeTextLiteral ClrAttributeContent = diff --git a/src/ix.compiler/tests/Ix.Compiler.CsTests/Cs/PragmasExtensionsTests.cs b/src/ix.compiler/tests/Ix.Compiler.CsTests/Cs/PragmasExtensionsTests.cs index c85150c3..3f74e19e 100644 --- a/src/ix.compiler/tests/Ix.Compiler.CsTests/Cs/PragmasExtensionsTests.cs +++ b/src/ix.compiler/tests/Ix.Compiler.CsTests/Cs/PragmasExtensionsTests.cs @@ -58,7 +58,7 @@ public void should_get_attribute_source() [Fact] public void should_declare_property() { - var expected = "public string AttributeName { get; set; }"; + var expected = "private string _AttributeName;\npublic string AttributeName { get{ return Ix.Localizations.LocalizationHelper.CleanUpLocalizationTokens(_AttributeName); } set; {_AttributeName = value;} }"; var field = new TypeMock("someField", new ReadOnlyCollection(new IPragma[] { diff --git a/src/ix.compiler/tests/Ix.Compiler.CsTests/samples/units/expected/.g/Onliners/types_with_name_attributes.g.cs b/src/ix.compiler/tests/Ix.Compiler.CsTests/samples/units/expected/.g/Onliners/types_with_name_attributes.g.cs index 11189ed9..ec01e1b1 100644 --- a/src/ix.compiler/tests/Ix.Compiler.CsTests/samples/units/expected/.g/Onliners/types_with_name_attributes.g.cs +++ b/src/ix.compiler/tests/Ix.Compiler.CsTests/samples/units/expected/.g/Onliners/types_with_name_attributes.g.cs @@ -258,7 +258,19 @@ public Ix.Connector.ITwinObject GetParent() public partial class NoAccessModifierClass : Ix.Connector.ITwinObject { - public string AttributeName { get; set; } + private string _AttributeName; + public string AttributeName + { + get + { + return Ix.Localizations.LocalizationHelper.CleanUpLocalizationTokens(_AttributeName); + } + + set; + { + _AttributeName = value; + } + } public OnlinerString SomeClassVariable { get; } diff --git a/src/ix.compiler/tests/Ix.Compiler.CsTests/samples/units/expected/.g/Onliners/types_with_property_attributes.g.cs b/src/ix.compiler/tests/Ix.Compiler.CsTests/samples/units/expected/.g/Onliners/types_with_property_attributes.g.cs index 4724fd51..3db32e0e 100644 --- a/src/ix.compiler/tests/Ix.Compiler.CsTests/samples/units/expected/.g/Onliners/types_with_property_attributes.g.cs +++ b/src/ix.compiler/tests/Ix.Compiler.CsTests/samples/units/expected/.g/Onliners/types_with_property_attributes.g.cs @@ -7,7 +7,19 @@ namespace TypesWithPropertyAttributes { public partial class SomeAddedProperties : Ix.Connector.ITwinObject { - public string Description { get; set; } + private string _Description; + public string Description + { + get + { + return Ix.Localizations.LocalizationHelper.CleanUpLocalizationTokens(_Description); + } + + set; + { + _Description = value; + } + } public OnlinerInt Counter { get; } diff --git a/src/ix.connectors/src/Ix.Connector/Localizations/LocalizationHelper.cs b/src/ix.connectors/src/Ix.Connector/Localizations/LocalizationHelper.cs index cea9aba2..5a3e5c5c 100644 --- a/src/ix.connectors/src/Ix.Connector/Localizations/LocalizationHelper.cs +++ b/src/ix.connectors/src/Ix.Connector/Localizations/LocalizationHelper.cs @@ -81,7 +81,7 @@ internal static IEnumerable GetTranslatable(string input, return localizables; } - internal static string CleanUpLocalizationTokens(string localized) + public static string CleanUpLocalizationTokens(this string localized) { if (localized != null) return localized.Replace("<#", string.Empty).Replace("#>", string.Empty);