Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: format code with dotnet-format #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 66 additions & 62 deletions Decomposer/src/DecomposerConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,36 @@ internal static class Constants
public const string DecomposedPropertyDeclaration =
GeneratedCodeAttributes
+ """
{{ property_type }}
{{ property_name }}
{ {{ if is_gettable }} get; {{ end }} {{ if is_settable }} set; {{ end }} }
{ {{ if is_gettable }} get; {{ end }} {{ if is_settable }} set; {{ end }} }
""";
{{ property_type
}
}
{ { property_name } }
{ { { if is_gettable } } get; { { end } } { { if is_settable } } set; { { end } } }
{ { { if is_gettable } } get; { { end } } { { if is_settable } } set; { { end } } }
""";

public const string DecomposedMethodDeclaration =
GeneratedCodeAttributes
+ """
{{ return_type }}
{{ method_name }} ({ { for parameter in parameters }}
{{ parameter.type }}
{{ parameter.name }}
{{ if not loop.last }}, {{ endif }}
{{ endfor }})
{{ if has_generic_type_constraints }}
{{ for generic_type_constraint in generic_type_constraints }}
where
{{ generic_type_constraint.name }} : {{ type_constraint.constraint }}
{{ endfor }}
{{ endif }}
""";

public const string DecomposableAttributeFilename = "DecomposableAttribute.g.cs";
{ { return_type } }
{ { method_name } } ({ { for parameter in parameters } }
{ { parameter.type } }
{ { parameter.name } }
{ { if not loop.last } }, { { endif } }
{ { endfor } })
{ { if has_generic_type_constraints } }
{ { for generic_type_constraint in generic_type_constraints } }
where
{ { generic_type_constraint.name } } : { { type_constraint.constraint } }
{ { endfor } }
{ { endif } }
""";

public const string DecomposableAttributeDeclaration =
DecomposedInterfaceHeader
+ """
public const string DecomposableAttributeFilename = "DecomposableAttribute.g.cs";

public const string DecomposableAttributeDeclaration =
DecomposedInterfaceHeader
+ """
[global::System.AttributeUsage(global::System.AttributeTargets.Class | global::System.AttributeTargets.Struct | global::System.AttributeTargets.Interface | global::System.AttributeTargets.Assembly)]
public sealed class DecomposeAttribute : global::System.Attribute
{
Expand All @@ -103,32 +105,34 @@ public DecomposeAttribute(global::System.Type type, string @namespace = null) {
""";

public const string IComposedClassDeclaration = """
public partial class {{ class_name }} : { { for type_member_tuple in decomposed_from } }
IDecomposed <{{ decomposed_from.type }}>
{
{{ for type_member_tuple in decomposed_from }}
{{ if type_member_tuple.member.is_property }}
public
{{~ type_member_tuple.member.type }}
{{~ type_member_tuple.member.name }}
{{ get; }}
{{ elif type_member_tuple.member.is_method }}
public
{{ decomposed_from.type }}
{{ decomposed_from.member }} {{ get; }}
{{ endfor }}


public
{{ class_name }} ({ decomposed_from } decomposed)
{
{ decomposed_from } = decomposed;
}

public
{ decomposed_from }
{ decomposed_from }
{{ get; }}
public partial class {{ class_name }} : { { for type_member_tuple in decomposed_from } }
IDecomposed <{ { decomposed_from.type } }>
{
{ { for type_member_tuple in decomposed_from } }
{ { if type_member_tuple.member.is_property } }
public
{ { ~type_member_tuple.member.type } }
{ { ~type_member_tuple.member.name } }
{ { get; } }
{ { elif type_member_tuple.member.is_method } }
public
{ { decomposed_from.type } }
{ { decomposed_from.member } }
{ { get; } }
{ { endfor } }


public
{ { class_name } } ({ decomposed_from }
decomposed)
{
{ decomposed_from } = decomposed;
}

public
{ decomposed_from }
{ decomposed_from }
{ { get; } }
}
""";

Expand Down Expand Up @@ -160,18 +164,18 @@ public DecomposeAttribute(global::System.Type type, string @namespace = null) {
| SymbolDisplayMiscellaneousOptions.UseErrorTypeSymbolName
| SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier
| SymbolDisplayMiscellaneousOptions.IncludeNotNullableReferenceTypeModifier
);
internal const string AttributeName = "Decompose";
internal const string AttributeFullName = AttributeName + nameof(Attribute);
internal const string InterfaceNamePrefix = "I";
internal const string InterfaceSuffix = "Decomposition";
internal const string NamespaceSuffix = ".Decompositions";
internal const string SourceFileNameSuffix = ".Decompositions.g.cs";
internal const string GeneratedCodeAttributeName = "GeneratedCodeAttribute";
internal const string GeneratedCodeAttributeFullName =
$"System.Diagnostics.CodeAnalysis.{GeneratedCodeAttributeName}";
internal const string CompilerGeneratedAttributeFullName =
"System.Runtime.CompilerServices.CompilerGeneratedAttribute";
);

internal const string AttributeName = "Decompose";
internal const string AttributeFullName = AttributeName + nameof(Attribute);
internal const string InterfaceNamePrefix = "I";
internal const string InterfaceSuffix = "Decomposition";
internal const string NamespaceSuffix = ".Decompositions";
internal const string SourceFileNameSuffix = ".Decompositions.g.cs";
internal const string GeneratedCodeAttributeName = "GeneratedCodeAttribute";
internal const string GeneratedCodeAttributeFullName =
$"System.Diagnostics.CodeAnalysis.{GeneratedCodeAttributeName}";
internal const string CompilerGeneratedAttributeFullName =
"System.Runtime.CompilerServices.CompilerGeneratedAttribute";
}
}
38 changes: 19 additions & 19 deletions InterfaceGenerator/InterfaceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,25 @@ SemanticModel semanticModel
.OfType<IPropertySymbol>()
.Where(p => p.DeclaredAccessibility == Accessibility.Public)
.Select(
p => p.ToDisplayString(Constants.SymbolDisplayFormat)
// PropertyDeclarationTemplate.Render(
// new PropertyDeclarationModel(
// "public",
// p.Type.ToDisplayString(),
// p.Name,
// p.GetMethod != null,
// p.SetMethod != null,
// p.IsIndexer
// ? Join(
// ", ",
// p.Parameters.Select(
// p =>
// $"{p.Type.ToDisplayString()} {p.Name}"
// )
// )
// : ""
// )
// )
p => p.ToDisplayString(Constants.SymbolDisplayFormat)
// PropertyDeclarationTemplate.Render(
// new PropertyDeclarationModel(
// "public",
// p.Type.ToDisplayString(),
// p.Name,
// p.GetMethod != null,
// p.SetMethod != null,
// p.IsIndexer
// ? Join(
// ", ",
// p.Parameters.Select(
// p =>
// $"{p.Type.ToDisplayString()} {p.Name}"
// )
// )
// : ""
// )
// )
)
)
+ Environment.NewLine
Expand Down
158 changes: 81 additions & 77 deletions InterfaceGenerator/InterfaceGeneratorConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@ public static class Constants
using System;

[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Struct)]
internal sealed class {{{GenerateInterfaceAttributeName}}} : Attribute
{
public Type Type { get; }

public {{{GenerateInterfaceAttributeName}}} (Type type = null)
{
{
Type = type;
}
}
internal sealed class {{{GenerateInterfaceAttributeName
}}} : Attribute
{
public Type Type { get; }

public
{ { { GenerateInterfaceAttributeName} } } (Type type = null)
{
{
Type = type;
}
}
}
""";

Expand All @@ -63,74 +65,76 @@ internal sealed class {{{GenerateInterfaceAttributeName}}} : Attribute

namespace {{ namespace }}
{
public partial interface {{ interface_name }}
{
{{ members }}
}
public partial interface {{ interface_name
}}
{
{ { members } }
}
}
""";

public static readonly Scriban.Template InterfaceDeclarationTemplate =
Scriban.Template.Parse(InterfaceDeclaration);

public const string MethodDeclaration = "{{ full_definition }};";

public static readonly Scriban.Template MethodDeclarationTemplate = Scriban.Template.Parse(
MethodDeclaration
);

public const string MethodParameter = """
{{ type }} {{ name }}
""";
Scriban.Template.Parse(InterfaceDeclaration);

public const string MethodDeclaration = "{{ full_definition }};";

public static readonly Scriban.Template MethodDeclarationTemplate = Scriban.Template.Parse(
MethodDeclaration
);

public const string MethodParameter = """
{ { type } }
{ { name } }
""";

public static readonly Scriban.Template MethodParameterTemplate = Scriban.Template.Parse(
MethodParameter
);
public const string PropertyDeclaration =
"{{ type }} {{ if is_indexed }}this[ {{ indexers }}] {{ else }} {{ name }} {{ end }} { {{ if is_gettable }} get; {{ end }} {{ if is_settable }} set; {{ end }} }";
public static readonly Scriban.Template PropertyDeclarationTemplate =
Scriban.Template.Parse(PropertyDeclaration);
public static readonly SymbolDisplayFormat SymbolDisplayFormat =
// new(
// typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
// genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters,
// memberOptions: SymbolDisplayMemberOptions.IncludeParameters | SymbolDisplayMemberOptions.IncludeType,
// parameterOptions: SymbolDisplayParameterOptions.IncludeType | SymbolDisplayParameterOptions.IncludeName,
// propertyStyle: SymbolDisplayPropertyStyle.ShowReadWriteDescriptor,
// miscellaneousOptions: SymbolDisplayMiscellaneousOptions.UseSpecialTypes
// /*
new(
SymbolDisplayGlobalNamespaceStyle.Included,
SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
SymbolDisplayGenericsOptions.IncludeTypeParameters
| SymbolDisplayGenericsOptions.IncludeVariance
| SymbolDisplayGenericsOptions.IncludeTypeConstraints,
SymbolDisplayMemberOptions.IncludeExplicitInterface
| SymbolDisplayMemberOptions.IncludeModifiers
| SymbolDisplayMemberOptions.IncludeParameters
| SymbolDisplayMemberOptions.IncludeRef
| SymbolDisplayMemberOptions.IncludeType
// | SymbolDisplayMemberOptions.IncludeContainingType
| SymbolDisplayMemberOptions.IncludeAccessibility,
SymbolDisplayDelegateStyle.NameAndSignature,
SymbolDisplayExtensionMethodStyle.Default,
SymbolDisplayParameterOptions.IncludeExtensionThis
| SymbolDisplayParameterOptions.IncludeName
| SymbolDisplayParameterOptions.IncludeParamsRefOut
| SymbolDisplayParameterOptions.IncludeType
| SymbolDisplayParameterOptions.IncludeDefaultValue
| SymbolDisplayParameterOptions.IncludeOptionalBrackets,
SymbolDisplayPropertyStyle.ShowReadWriteDescriptor,
miscellaneousOptions: SymbolDisplayMiscellaneousOptions.UseSpecialTypes
| SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers
| SymbolDisplayMiscellaneousOptions.UseAsterisksInMultiDimensionalArrays
| SymbolDisplayMiscellaneousOptions.UseErrorTypeSymbolName
| SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier
| SymbolDisplayMiscellaneousOptions.IncludeNotNullableReferenceTypeModifier
);
);

public const string PropertyDeclaration =
"{{ type }} {{ if is_indexed }}this[ {{ indexers }}] {{ else }} {{ name }} {{ end }} { {{ if is_gettable }} get; {{ end }} {{ if is_settable }} set; {{ end }} }";

public static readonly Scriban.Template PropertyDeclarationTemplate =
Scriban.Template.Parse(PropertyDeclaration);

public static readonly SymbolDisplayFormat SymbolDisplayFormat =
// new(
// typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
// genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters,
// memberOptions: SymbolDisplayMemberOptions.IncludeParameters | SymbolDisplayMemberOptions.IncludeType,
// parameterOptions: SymbolDisplayParameterOptions.IncludeType | SymbolDisplayParameterOptions.IncludeName,
// propertyStyle: SymbolDisplayPropertyStyle.ShowReadWriteDescriptor,
// miscellaneousOptions: SymbolDisplayMiscellaneousOptions.UseSpecialTypes
// /*
new(
SymbolDisplayGlobalNamespaceStyle.Included,
SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
SymbolDisplayGenericsOptions.IncludeTypeParameters
| SymbolDisplayGenericsOptions.IncludeVariance
| SymbolDisplayGenericsOptions.IncludeTypeConstraints,
SymbolDisplayMemberOptions.IncludeExplicitInterface
| SymbolDisplayMemberOptions.IncludeModifiers
| SymbolDisplayMemberOptions.IncludeParameters
| SymbolDisplayMemberOptions.IncludeRef
| SymbolDisplayMemberOptions.IncludeType
// | SymbolDisplayMemberOptions.IncludeContainingType
| SymbolDisplayMemberOptions.IncludeAccessibility,
SymbolDisplayDelegateStyle.NameAndSignature,
SymbolDisplayExtensionMethodStyle.Default,
SymbolDisplayParameterOptions.IncludeExtensionThis
| SymbolDisplayParameterOptions.IncludeName
| SymbolDisplayParameterOptions.IncludeParamsRefOut
| SymbolDisplayParameterOptions.IncludeType
| SymbolDisplayParameterOptions.IncludeDefaultValue
| SymbolDisplayParameterOptions.IncludeOptionalBrackets,
SymbolDisplayPropertyStyle.ShowReadWriteDescriptor,
miscellaneousOptions: SymbolDisplayMiscellaneousOptions.UseSpecialTypes
| SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers
| SymbolDisplayMiscellaneousOptions.UseAsterisksInMultiDimensionalArrays
| SymbolDisplayMiscellaneousOptions.UseErrorTypeSymbolName
| SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier
| SymbolDisplayMiscellaneousOptions.IncludeNotNullableReferenceTypeModifier
);
}

public record struct InterfaceGeneratorModel(
Expand All @@ -139,10 +143,10 @@ public record struct InterfaceGeneratorModel(
string Members,
string TypeParameters,
string TypeConstraints
);
public static class Environment
{
public const string NewLine = "\r\n";
}
);

public static class Environment
{
public const string NewLine = "\r\n";
}
}