Skip to content

Commit

Permalink
Improve formatting for property initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Dec 7, 2024
1 parent e988aab commit 4a42758
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -439,35 +439,74 @@ static string GetExpressionWithTrailingSpace(Accessibility accessibility)
{ DefaultValue: DependencyPropertyDefaultValue.Null, IsPropertyChangedCallbackImplemented: false, IsSharedPropertyChangedCallbackImplemented: false }
=> "null",
{ DefaultValue: DependencyPropertyDefaultValue.Callback(string methodName), IsPropertyChangedCallbackImplemented: false, IsSharedPropertyChangedCallbackImplemented: false }
=> $"global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}.Create(new {WellKnownTypeNames.CreateDefaultValueCallback(propertyInfo.UseWindowsUIXaml)}({methodName}))",
=> $"""
global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}.Create(
createDefaultValueCallback: new {WellKnownTypeNames.CreateDefaultValueCallback(propertyInfo.UseWindowsUIXaml)}({methodName}))
""",
{ DefaultValue: { } defaultValue, IsPropertyChangedCallbackImplemented: false, IsSharedPropertyChangedCallbackImplemented: false }
=> $"new global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}({defaultValue})",

// Codegen for legacy UWP
{ IsNet8OrGreater: false } => propertyInfo switch
{
{ DefaultValue: DependencyPropertyDefaultValue.Callback(string methodName), IsPropertyChangedCallbackImplemented: true, IsSharedPropertyChangedCallbackImplemented: false }
=> $"global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}.Create(new {WellKnownTypeNames.CreateDefaultValueCallback(propertyInfo.UseWindowsUIXaml)}({methodName}), static (d, e) => (({typeQualifiedName})d).On{propertyInfo.PropertyName}PropertyChanged(e))",
=> $"""
global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}.Create(
createDefaultValueCallback: new {WellKnownTypeNames.CreateDefaultValueCallback(propertyInfo.UseWindowsUIXaml)}({methodName}),
propertyChangedCallback: static (d, e) => (({typeQualifiedName})d).On{propertyInfo.PropertyName}PropertyChanged(e))
""",
{ DefaultValue: DependencyPropertyDefaultValue.Callback(string methodName), IsPropertyChangedCallbackImplemented: false, IsSharedPropertyChangedCallbackImplemented: true }
=> $"global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}.Create(new {WellKnownTypeNames.CreateDefaultValueCallback(propertyInfo.UseWindowsUIXaml)}({methodName}), static (d, e) => (({typeQualifiedName})d).OnPropertyChanged(e))",
=> $"""
global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}.Create(
createDefaultValueCallback: new {WellKnownTypeNames.CreateDefaultValueCallback(propertyInfo.UseWindowsUIXaml)}({methodName}),
propertyChangedCallback: static (d, e) => (({typeQualifiedName})d).OnPropertyChanged(e))
""",
{ DefaultValue: DependencyPropertyDefaultValue.Callback(string methodName), IsPropertyChangedCallbackImplemented: true, IsSharedPropertyChangedCallbackImplemented: true }
=> $"global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}.Create(new {WellKnownTypeNames.CreateDefaultValueCallback(propertyInfo.UseWindowsUIXaml)}({methodName}), static (d, e) => {{ (({typeQualifiedName})d).On{propertyInfo.PropertyName}PropertyChanged(e); (({typeQualifiedName})d).OnPropertyChanged(e); }})",
=> $$"""
global::{{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}}.Create(
createDefaultValueCallback: new {{WellKnownTypeNames.CreateDefaultValueCallback(propertyInfo.UseWindowsUIXaml)}}({{methodName}}),
propertyChangedCallback: static (d, e) => { (({{typeQualifiedName}})d).On{{propertyInfo.PropertyName}}PropertyChanged(e); (({{typeQualifiedName}})d).OnPropertyChanged(e); })
""",
{ DefaultValue: { } defaultValue, IsPropertyChangedCallbackImplemented: true, IsSharedPropertyChangedCallbackImplemented: false }
=> $"new global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}({defaultValue}, static (d, e) => (({typeQualifiedName})d).On{propertyInfo.PropertyName}PropertyChanged(e))",
=> $"""
new global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}(
defaultValue: {defaultValue},
propertyChangedCallback: static (d, e) => (({typeQualifiedName})d).On{propertyInfo.PropertyName}PropertyChanged(e))
""",
{ DefaultValue: { } defaultValue, IsPropertyChangedCallbackImplemented: false, IsSharedPropertyChangedCallbackImplemented: true }
=> $"new global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}({defaultValue}, static (d, e) => (({typeQualifiedName})d).OnPropertyChanged(e))",
=> $"""
new global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}(
defaultValue: {defaultValue},
propertyChangedCallback: static (d, e) => (({typeQualifiedName})d).OnPropertyChanged(e))
""",
{ DefaultValue: { } defaultValue, IsPropertyChangedCallbackImplemented: true, IsSharedPropertyChangedCallbackImplemented: true }
=> $"new global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}({defaultValue}, static (d, e) => {{ (({typeQualifiedName})d).On{propertyInfo.PropertyName}PropertyChanged(e); (({typeQualifiedName})d).OnPropertyChanged(e); }})",
=> $$"""
new global::{{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}}(
defaultValue: {{defaultValue}},
propertyChangedCallback: static (d, e) => { (({{typeQualifiedName}})d).On{{propertyInfo.PropertyName}}PropertyChanged(e); (({{typeQualifiedName}})d).OnPropertyChanged(e); })
""",
_ => throw new ArgumentException($"Invalid default value '{propertyInfo.DefaultValue}'."),
},

// Codegen for .NET 8 or greater
{ DefaultValue: DependencyPropertyDefaultValue.Null }
=> $"new global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}(null, global::{GeneratorName}.PropertyChangedCallbacks.{propertyInfo.PropertyName}())",
=> $"""
new global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}(
defaultValue: null,
propertyChangedCallback: global::{GeneratorName}.PropertyChangedCallbacks.{propertyInfo.PropertyName}())
""",
{ DefaultValue: DependencyPropertyDefaultValue.Callback(string methodName) }
=> $"global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}.Create(new {WellKnownTypeNames.CreateDefaultValueCallback(propertyInfo.UseWindowsUIXaml)}({methodName}), global::{GeneratorName}.PropertyChangedCallbacks.{propertyInfo.PropertyName}())",
=> $"""
global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}.Create(
createDefaultValueCallback: new {WellKnownTypeNames.CreateDefaultValueCallback(propertyInfo.UseWindowsUIXaml)}({methodName}),
propertyChangedCallback: global::{GeneratorName}.PropertyChangedCallbacks.{propertyInfo.PropertyName}())
""",
{ DefaultValue: { } defaultValue } and ({ IsPropertyChangedCallbackImplemented: true } or { IsSharedPropertyChangedCallbackImplemented: true })
=> $"new global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}({defaultValue}, global::{GeneratorName}.PropertyChangedCallbacks.{propertyInfo.PropertyName}())",
=> $"""
new global::{WellKnownTypeNames.PropertyMetadata(propertyInfo.UseWindowsUIXaml)}(
defaultValue: {defaultValue},
propertyChangedCallback: global::{GeneratorName}.PropertyChangedCallbacks.{propertyInfo.PropertyName}())
""",
_ => throw new ArgumentException($"Invalid default value '{propertyInfo.DefaultValue}'."),
};

Expand All @@ -477,13 +516,16 @@ static string GetExpressionWithTrailingSpace(Accessibility accessibility)
/// </summary>
""", isMultiline: true);
writer.WriteGeneratedAttributes(GeneratorName, includeNonUserCodeAttributes: false);
writer.WriteLine($$"""
writer.Write($$"""
public static readonly global::{{WellKnownTypeNames.DependencyProperty(propertyInfo.UseWindowsUIXaml)}} {{propertyInfo.PropertyName}}Property = global::{{WellKnownTypeNames.DependencyProperty(propertyInfo.UseWindowsUIXaml)}}.Register(
name: "{{propertyInfo.PropertyName}}",
propertyType: typeof({{propertyInfo.TypeName}}),
ownerType: typeof({{typeQualifiedName}}),
typeMetadata: {{typeMetadata}});
typeMetadata:
""", isMultiline: true);
writer.IncreaseIndent();
writer.WriteLine($"{typeMetadata});", isMultiline: true);
writer.DecreaseIndent();
writer.WriteLine();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ partial class MyControl
name: "Number",
propertyType: typeof(int),
ownerType: typeof(MyControl),
typeMetadata: new global::Windows.UI.Xaml.PropertyMetadata(default(int), global::CommunityToolkit.WinUI.DependencyPropertyGenerator.PropertyChangedCallbacks.Number()));
typeMetadata: new global::Windows.UI.Xaml.PropertyMetadata(
defaultValue: default(int),
propertyChangedCallback: global::CommunityToolkit.WinUI.DependencyPropertyGenerator.PropertyChangedCallbacks.Number()));
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
Expand Down Expand Up @@ -698,7 +700,9 @@ partial class MyControl
name: "Number",
propertyType: typeof(int),
ownerType: typeof(MyControl),
typeMetadata: new global::Windows.UI.Xaml.PropertyMetadata(default(int), global::CommunityToolkit.WinUI.DependencyPropertyGenerator.PropertyChangedCallbacks.Number()));
typeMetadata: new global::Windows.UI.Xaml.PropertyMetadata(
defaultValue: default(int),
propertyChangedCallback: global::CommunityToolkit.WinUI.DependencyPropertyGenerator.PropertyChangedCallbacks.Number()));
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
Expand Down Expand Up @@ -969,7 +973,9 @@ partial class MyControl
name: "Number",
propertyType: typeof(int),
ownerType: typeof(MyControl),
typeMetadata: new global::Windows.UI.Xaml.PropertyMetadata(42, global::CommunityToolkit.WinUI.DependencyPropertyGenerator.PropertyChangedCallbacks.Number()));
typeMetadata: new global::Windows.UI.Xaml.PropertyMetadata(
defaultValue: 42,
propertyChangedCallback: global::CommunityToolkit.WinUI.DependencyPropertyGenerator.PropertyChangedCallbacks.Number()));
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
Expand Down Expand Up @@ -1124,7 +1130,9 @@ partial class MyControl
name: "Number",
propertyType: typeof(int),
ownerType: typeof(MyControl),
typeMetadata: new global::Windows.UI.Xaml.PropertyMetadata(default(int), global::CommunityToolkit.WinUI.DependencyPropertyGenerator.PropertyChangedCallbacks.Number()));
typeMetadata: new global::Windows.UI.Xaml.PropertyMetadata(
defaultValue: default(int),
propertyChangedCallback: global::CommunityToolkit.WinUI.DependencyPropertyGenerator.PropertyChangedCallbacks.Number()));
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
Expand Down Expand Up @@ -1283,7 +1291,9 @@ partial class MyControl
name: "Number",
propertyType: typeof(int),
ownerType: typeof(MyControl),
typeMetadata: new global::Windows.UI.Xaml.PropertyMetadata(default(int), global::CommunityToolkit.WinUI.DependencyPropertyGenerator.PropertyChangedCallbacks.Number()));
typeMetadata: new global::Windows.UI.Xaml.PropertyMetadata(
defaultValue: default(int),
propertyChangedCallback: global::CommunityToolkit.WinUI.DependencyPropertyGenerator.PropertyChangedCallbacks.Number()));
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
Expand Down Expand Up @@ -2359,7 +2369,8 @@ partial class MyControl
name: "Number",
propertyType: typeof(int),
ownerType: typeof(MyControl),
typeMetadata: global::Windows.UI.Xaml.PropertyMetadata.Create(new Windows.UI.Xaml.CreateDefaultValueCallback(CreateNumber)));
typeMetadata: global::Windows.UI.Xaml.PropertyMetadata.Create(
createDefaultValueCallback: new Windows.UI.Xaml.CreateDefaultValueCallback(CreateNumber)));
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
Expand Down Expand Up @@ -2481,7 +2492,8 @@ partial class MyControl
name: "Number",
propertyType: typeof(int?),
ownerType: typeof(MyControl),
typeMetadata: global::Windows.UI.Xaml.PropertyMetadata.Create(new Windows.UI.Xaml.CreateDefaultValueCallback(CreateNumber)));
typeMetadata: global::Windows.UI.Xaml.PropertyMetadata.Create(
createDefaultValueCallback: new Windows.UI.Xaml.CreateDefaultValueCallback(CreateNumber)));
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
Expand Down Expand Up @@ -2605,7 +2617,8 @@ partial class MyControl
name: "Name",
propertyType: typeof(string),
ownerType: typeof(MyControl),
typeMetadata: global::Windows.UI.Xaml.PropertyMetadata.Create(new Windows.UI.Xaml.CreateDefaultValueCallback(CreateName)));
typeMetadata: global::Windows.UI.Xaml.PropertyMetadata.Create(
createDefaultValueCallback: new Windows.UI.Xaml.CreateDefaultValueCallback(CreateName)));
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
Expand Down

0 comments on commit 4a42758

Please sign in to comment.