Skip to content

Commit

Permalink
feat(UxmlTraitAttribute): use a kebab-case version of member name by …
Browse files Browse the repository at this point in the history
…default

BREAKING CHANGE: UxmlNameAttribute now uses a kebab-case version of the member name by default.
  • Loading branch information
jonisavo committed Oct 21, 2022
1 parent a484ec4 commit 7bfb5e0
Show file tree
Hide file tree
Showing 20 changed files with 101 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ ExportedObj/
*.booproj
*.svd
*.pdb
!/Assets/UIComponents/Roslyn/*.pdb
*.mdb
*.opendb
*.VC.db

# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
!/Assets/UIComponents/Roslyn/*.pdb.meta
*.mdb.meta

# Unity3D generated file on crash reports
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<UXML xmlns:test="UIComponents.Tests.Roslyn">
<test:RoslynTestComponent current-time="123.456" text-color="#FF0000" greeting="Morning" enabled="false" />
<test:RoslynTestComponent time="123.456" text-color="#FF0000" greeting="Morning" enabled="false" />
</UXML>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<UXML xmlns:test="UIComponents.Tests.Roslyn">
<test:RoslynTestVisualElement age="20" music-volume="0.5" unix-timestamp="1234567890" />
<test:RoslynTestVisualElement age="20" music-volume="0.5" timestamp="1234567890" />
</UXML>
4 changes: 2 additions & 2 deletions Assets/UIComponents.Tests/Roslyn/RoslynTestComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public enum Greetings
Morning
}

[UxmlTrait(Name = "text-color")]
[UxmlTrait]
public Color TextColor;

[UxmlTrait(Name = "current-time")]
[UxmlTrait(Name = "time")]
public double CurrentTime;

[UxmlTrait(DefaultValue = Greetings.Morning)]
Expand Down
4 changes: 2 additions & 2 deletions Assets/UIComponents.Tests/Roslyn/RoslynTestVisualElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public partial class RoslynTestVisualElement : VisualElement
[UxmlTrait]
public int Age;

[UxmlTrait(Name = "unix-timestamp")]
[UxmlTrait(Name = "timestamp", DefaultValue = -1)]
public long UnixTimestamp;

[UxmlTrait(Name = "music-volume", DefaultValue = 1.0f)]
[UxmlTrait(DefaultValue = 1.0f)]
public float MusicVolume { get; set; }
}
}
Binary file modified Assets/UIComponents/Roslyn/UIComponents.Roslyn.Generation.dll
Binary file not shown.
Binary file modified Assets/UIComponents/Roslyn/UIComponents.Roslyn.Generation.pdb
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public new partial class UxmlFactory : UxmlFactory<BoolComponentWithUsing, UxmlT
[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public new partial class UxmlTraits : VisualElement.UxmlTraits
{
UxmlBoolAttributeDescription m_FieldTrait = new UxmlBoolAttributeDescription { name = "fieldtrait" };
UxmlBoolAttributeDescription m_PropertyTrait = new UxmlBoolAttributeDescription { name = "propertytrait" };
UxmlBoolAttributeDescription m_FieldTrait = new UxmlBoolAttributeDescription { name = "field-trait" };
UxmlBoolAttributeDescription m_PropertyTrait = new UxmlBoolAttributeDescription { name = "property-trait" };

[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public new partial class UxmlFactory : UxmlFactory<ColorComponentWithUsing, Uxml
[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public new partial class UxmlTraits : VisualElement.UxmlTraits
{
UxmlColorAttributeDescription m_FieldTrait = new UxmlColorAttributeDescription { name = "fieldtrait" };
UxmlColorAttributeDescription m_PropertyTrait = new UxmlColorAttributeDescription { name = "propertytrait" };
UxmlColorAttributeDescription m_FieldTrait = new UxmlColorAttributeDescription { name = "field-trait" };
UxmlColorAttributeDescription m_PropertyTrait = new UxmlColorAttributeDescription { name = "property-trait" };

[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public new partial class UxmlFactory : UxmlFactory<DoubleComponentWithUsing, Uxm
[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public new partial class UxmlTraits : VisualElement.UxmlTraits
{
UxmlDoubleAttributeDescription m_FieldTrait = new UxmlDoubleAttributeDescription { name = "fieldtrait" };
UxmlDoubleAttributeDescription m_PropertyTrait = new UxmlDoubleAttributeDescription { name = "propertytrait" };
UxmlDoubleAttributeDescription m_FieldTrait = new UxmlDoubleAttributeDescription { name = "field-trait" };
UxmlDoubleAttributeDescription m_PropertyTrait = new UxmlDoubleAttributeDescription { name = "property-trait" };

[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public new partial class UxmlFactory : UxmlFactory<OwnEnumComponent, UxmlTraits>
[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public new partial class UxmlTraits : VisualElement.UxmlTraits
{
UxmlEnumAttributeDescription<OwnEnumComponent.OwnEnum> m_FieldTrait = new UxmlEnumAttributeDescription<OwnEnumComponent.OwnEnum> { name = "fieldtrait" };
UxmlEnumAttributeDescription<OwnEnumComponent.OwnEnum> m_PropertyTrait = new UxmlEnumAttributeDescription<OwnEnumComponent.OwnEnum> { name = "propertytrait" };
UxmlEnumAttributeDescription<OwnEnumComponent.OwnEnum> m_FieldTrait = new UxmlEnumAttributeDescription<OwnEnumComponent.OwnEnum> { name = "field-trait" };
UxmlEnumAttributeDescription<OwnEnumComponent.OwnEnum> m_PropertyTrait = new UxmlEnumAttributeDescription<OwnEnumComponent.OwnEnum> { name = "property-trait" };

[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public new partial class UxmlFactory : UxmlFactory<MyEnumComponentWithUsing, Uxm
[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public new partial class UxmlTraits : VisualElement.UxmlTraits
{
UxmlEnumAttributeDescription<MyEnum> m_FieldTrait = new UxmlEnumAttributeDescription<MyEnum> { name = "fieldtrait" };
UxmlEnumAttributeDescription<MyEnum> m_PropertyTrait = new UxmlEnumAttributeDescription<MyEnum> { name = "propertytrait" };
UxmlEnumAttributeDescription<MyEnum> m_FieldTrait = new UxmlEnumAttributeDescription<MyEnum> { name = "field-trait" };
UxmlEnumAttributeDescription<MyEnum> m_PropertyTrait = new UxmlEnumAttributeDescription<MyEnum> { name = "property-trait" };

[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public new partial class UxmlFactory : UxmlFactory<FloatComponentWithUsing, Uxml
[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public new partial class UxmlTraits : VisualElement.UxmlTraits
{
UxmlFloatAttributeDescription m_FieldTrait = new UxmlFloatAttributeDescription { name = "fieldtrait" };
UxmlFloatAttributeDescription m_PropertyTrait = new UxmlFloatAttributeDescription { name = "propertytrait" };
UxmlFloatAttributeDescription m_FieldTrait = new UxmlFloatAttributeDescription { name = "field-trait" };
UxmlFloatAttributeDescription m_PropertyTrait = new UxmlFloatAttributeDescription { name = "property-trait" };

[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public new partial class UxmlFactory : UxmlFactory<CustomNamespaceComponent, Uxm
[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public new partial class UxmlTraits : VisualElement.UxmlTraits
{
UxmlEnumAttributeDescription<Custom.Components.CustomNamespaceComponent.OwnEnum> m_FieldTrait = new UxmlEnumAttributeDescription<Custom.Components.CustomNamespaceComponent.OwnEnum> { name = "fieldtrait" };
UxmlEnumAttributeDescription<Custom.Components.CustomNamespaceComponent.OwnEnum> m_PropertyTrait = new UxmlEnumAttributeDescription<Custom.Components.CustomNamespaceComponent.OwnEnum> { name = "propertytrait" };
UxmlEnumAttributeDescription<Custom.Components.CustomNamespaceComponent.OwnEnum> m_FieldTrait = new UxmlEnumAttributeDescription<Custom.Components.CustomNamespaceComponent.OwnEnum> { name = "field-trait" };
UxmlEnumAttributeDescription<Custom.Components.CustomNamespaceComponent.OwnEnum> m_PropertyTrait = new UxmlEnumAttributeDescription<Custom.Components.CustomNamespaceComponent.OwnEnum> { name = "property-trait" };

[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public new partial class UxmlFactory : UxmlFactory<IntComponentWithUsing, UxmlTr
[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public new partial class UxmlTraits : VisualElement.UxmlTraits
{
UxmlIntAttributeDescription m_FieldTrait = new UxmlIntAttributeDescription { name = "fieldtrait" };
UxmlIntAttributeDescription m_PropertyTrait = new UxmlIntAttributeDescription { name = "propertytrait" };
UxmlIntAttributeDescription m_FieldTrait = new UxmlIntAttributeDescription { name = "field-trait" };
UxmlIntAttributeDescription m_PropertyTrait = new UxmlIntAttributeDescription { name = "property-trait" };

[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public new partial class UxmlFactory : UxmlFactory<LongComponentWithUsing, UxmlT
[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public new partial class UxmlTraits : VisualElement.UxmlTraits
{
UxmlLongAttributeDescription m_FieldTrait = new UxmlLongAttributeDescription { name = "fieldtrait" };
UxmlLongAttributeDescription m_PropertyTrait = new UxmlLongAttributeDescription { name = "propertytrait" };
UxmlLongAttributeDescription m_FieldTrait = new UxmlLongAttributeDescription { name = "field-trait" };
UxmlLongAttributeDescription m_PropertyTrait = new UxmlLongAttributeDescription { name = "property-trait" };

[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public new partial class UxmlFactory : UxmlFactory<StringComponentWithUsing, Uxm
[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public new partial class UxmlTraits : VisualElement.UxmlTraits
{
UxmlStringAttributeDescription m_FieldTrait = new UxmlStringAttributeDescription { name = "fieldtrait" };
UxmlStringAttributeDescription m_PropertyTrait = new UxmlStringAttributeDescription { name = "propertytrait" };
UxmlStringAttributeDescription m_FieldTrait = new UxmlStringAttributeDescription { name = "field-trait" };
UxmlStringAttributeDescription m_PropertyTrait = new UxmlStringAttributeDescription { name = "property-trait" };

[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//HintName: LongTraitNameComponent.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
// </auto-generated>

using System.CodeDom.Compiler;
using UnityEngine.UIElements;

public partial class LongTraitNameComponent
{
[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public new partial class UxmlFactory : UxmlFactory<LongTraitNameComponent, UxmlTraits> {}

[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public new partial class UxmlTraits : VisualElement.UxmlTraits
{
UxmlIntAttributeDescription m_HereIsALongMemberNameWithALotOfComplexity123Test_Hello___WorldA = new UxmlIntAttributeDescription { name = "here-is-a-long-member-name-with-a-lot-of-complexity123-test-hello-world-a" };

[GeneratedCode("UIComponents.Roslyn.Generation", "0.26.0")]
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
((LongTraitNameComponent)ve).HereIsALongMemberNameWithALotOfComplexity123Test_Hello___WorldA = m_HereIsALongMemberNameWithALotOfComplexity123Test_Hello___WorldA.GetValueFromBag(bag, cc);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,22 @@ public partial class ComponentWithUxmlNameAndTraits : UIComponent
[UxmlTrait(DefaultValue = true)]
public bool Value;
}
";
return GeneratorTester.Verify<UxmlAugmentGenerator>(source);
}

[Fact]
public Task Handles_Long_Member_Name_For_Trait()
{
var source = @"
using UIComponents;
using UIComponents.Experimental;
public partial class LongTraitNameComponent : UIComponent
{
[UxmlTrait]
public int HereIsALongMemberNameWithALotOfComplexity123Test_Hello___WorldA;
}
";
return GeneratorTester.Verify<UxmlAugmentGenerator>(source);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.CodeAnalysis;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using UIComponents.Roslyn.Generation.Utilities;

namespace UIComponents.Roslyn.Generation.Generators.Uxml
Expand Down Expand Up @@ -54,6 +56,33 @@ public TraitDescription(string classMemberName, string uxmlName, INamedTypeSymbo
}
}

private static string StringToKebabCase(string inputString)
{
var stringWithoutUnderscores = inputString.Replace("_", "-");
var compactStringBuilder = new StringBuilder();

for (var i = 0; i < stringWithoutUnderscores.Length; i++)
{
var character = stringWithoutUnderscores[i];

if (i > 0)
{
var previousCharacter = stringWithoutUnderscores[i - 1];

if (character == '-' && previousCharacter == '-')
continue;
}

compactStringBuilder.Append(character);
}

var compactString = compactStringBuilder.ToString();
// https://gist.github.com/nblackburn/875e6ff75bc8ce171c758bf75f304707?permalink_comment_id=3677597#gistcomment-3677597
var kebabCaseStringPartOne = Regex.Replace(compactString, @"\B([A-Z])(?=[a-z])", "-$1");
var finalKebabCaseString = Regex.Replace(kebabCaseStringPartOne, @"\B([a-z0-9])([A-Z])", "$1-$2");
return finalKebabCaseString.ToLower();
}

private static (string, string) GetTraitUxmlNameAndDefaultValue(
Dictionary<string, TypedConstant> traitArguments,
ITypeSymbol typeSymbol,
Expand All @@ -66,7 +95,7 @@ public TraitDescription(string classMemberName, string uxmlName, INamedTypeSymbo
uxmlName = traitArguments[NameArgumentName].Value?.ToString();

if (string.IsNullOrEmpty(uxmlName))
uxmlName = memberName.ToLower();
uxmlName = StringToKebabCase(memberName);

if (traitArguments.ContainsKey(DefaultValueArgumentName))
{
Expand Down

0 comments on commit 7bfb5e0

Please sign in to comment.