Skip to content

Commit

Permalink
Auto-format source code
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHub Actions Autoformatter committed Jan 27, 2025
1 parent 7cfbb0e commit b8e9cb1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void Initialize (IncrementalGeneratorInitializationContext context)
}

static string [] GetFlagsForTarget (Dictionary<string, (string AttributeFullName, AttributeTargets Targets)> flags,
AttributeTargets[] targets)
AttributeTargets [] targets)
=> flags.Where (kv => targets.Any (t => kv.Value.Targets.HasFlag (t)))
.Select (kv => kv.Key)
.ToArray ();
Expand All @@ -80,7 +80,7 @@ static string [] GetFlagsForTarget (Dictionary<string, (string AttributeFullName
=> dataAttribute.Where (kv => targets.Any (t => kv.Value.Data.Target.HasFlag (t)))
.Select (kv => kv.Value)
.ToArray ();


static void WriteFlagProperty (TabbedStringBuilder sb, string flagName)
{
Expand All @@ -96,23 +96,23 @@ static void WriteAttributeProperty (TabbedStringBuilder sb,
(string AttributeFullName, string AttributeName, BindingAttributeData Data) attrData)
{
// add a property that will state if we have the attr, this will help with nullability
sb.AppendLine($"readonly bool _has{attrData.AttributeName} = false;");
sb.AppendLine ($"readonly bool _has{attrData.AttributeName} = false;");
sb.AppendLine ($"[MemberNotNullWhen (true, nameof ({attrData.AttributeName}))]");
using (var flagPropertyBlock = sb.CreateBlock ($"public bool Has{attrData.AttributeName}", block: true)) {
flagPropertyBlock.AppendLine ($"get => _has{attrData.AttributeName};");
flagPropertyBlock.AppendLine ($"private init => _has{attrData.AttributeName} = value;");
}
sb.AppendLine ();
sb.AppendLine($"readonly {attrData.Data.DataModelType}? _{attrData.AttributeName} = null;");
sb.AppendLine ();
sb.AppendLine ($"readonly {attrData.Data.DataModelType}? _{attrData.AttributeName} = null;");
// decorate to help with nullability
using (var attributePropertyBlock = sb.CreateBlock ($"public {attrData.Data.DataModelType}? {attrData.AttributeName}", block: true)) {
attributePropertyBlock.AppendLine ($"get => _{attrData.AttributeName};");
attributePropertyBlock.AppendLine ($"private init => _{attrData.AttributeName} = value;");
}
}

static void WriteDataModelExtension (TabbedStringBuilder sb, string dataModel, string [] flags,
(string AttributeFullName, string AttributeName, BindingAttributeData Data)[] attributes)
static void WriteDataModelExtension (TabbedStringBuilder sb, string dataModel, string [] flags,
(string AttributeFullName, string AttributeName, BindingAttributeData Data) [] attributes)
{
sb.Clear ();
sb.AppendLine ("// <auto-generated/>");
Expand Down Expand Up @@ -142,8 +142,8 @@ static void WriteDataModelExtension (TabbedStringBuilder sb, string dataModel, s
modelBlock.AppendLine ();
modelBlock.AppendLine ("readonly Dictionary<string, List<AttributeData>>? _attributesDictionary = null;");
using (var dictionaryPropertyBlock =
modelBlock.CreateBlock ("public Dictionary<string, List<AttributeData>>? AttributesDictionary",
block: true)) {
modelBlock.CreateBlock ("public Dictionary<string, List<AttributeData>>? AttributesDictionary",
block: true)) {
dictionaryPropertyBlock.AppendLine ("get => _attributesDictionary;");
using (var initBlock = dictionaryPropertyBlock.CreateBlock ("private init", block: true)) {
initBlock.AppendLine ("_attributesDictionary = value;");
Expand All @@ -154,7 +154,7 @@ static void WriteDataModelExtension (TabbedStringBuilder sb, string dataModel, s

foreach (var attributeData in attributes) {
// check if the attribute is present, if it is, set the value
ifBlock.AppendLine($"Has{attributeData.AttributeName} = _attributesDictionary.Has{attributeData.AttributeName} ();");
ifBlock.AppendLine ($"Has{attributeData.AttributeName} = _attributesDictionary.Has{attributeData.AttributeName} ();");
using (var attrIfBlock = ifBlock.CreateBlock ($"if (Has{attributeData.AttributeName})", block: true)) {
attrIfBlock.AppendLine ($"{attributeData.AttributeName} = _attributesDictionary.Get{attributeData.AttributeName} ();");
}
Expand All @@ -166,9 +166,9 @@ static void WriteDataModelExtension (TabbedStringBuilder sb, string dataModel, s
}

static void GenerateModelExtension (TabbedStringBuilder sb, string dataModel,
Dictionary<string, (string AttributeFullName, AttributeTargets Targets)> flags,
Dictionary<string, (string AttributeFullName, string AttributeName, BindingAttributeData Data)> attributes,
AttributeTargets[] targets,
Dictionary<string, (string AttributeFullName, AttributeTargets Targets)> flags,
Dictionary<string, (string AttributeFullName, string AttributeName, BindingAttributeData Data)> attributes,
AttributeTargets [] targets,
SourceProductionContext context)
{
var methodFlags = GetFlagsForTarget (flags, targets);
Expand All @@ -184,7 +184,7 @@ static AttributeTargets GetTarget (ISymbol symbol)
// loop over attrs, if we find the BindingFlagAttribute, return the target
foreach (var attr in attrData) {
if (attr.AttributeClass?.Name == BindingFlagData.Name
&& BindingFlagData.TryParse (attr, out var data)) {
&& BindingFlagData.TryParse (attr, out var data)) {
return data.Value.Target;
}
}
Expand All @@ -198,7 +198,7 @@ static AttributeTargets GetTarget (ISymbol symbol)
// loop over attrs, if we find the BindingFlagAttribute, return the target
foreach (var attr in attrData) {
if (attr.AttributeClass?.Name == BindingAttributeData.Name
&& BindingAttributeData.TryParse (attr, out var data)) {
&& BindingAttributeData.TryParse (attr, out var data)) {
return data;
}
}
Expand Down Expand Up @@ -230,7 +230,7 @@ void GenerateCode (SourceProductionContext context, Compilation compilation,
}

// all flags are collected, generate the code
var sb = new TabbedStringBuilder (new());
var sb = new TabbedStringBuilder (new ());
GenerateDictionaryExtension (sb, flags, dataAttributes);

// Add the source code to the compilation.
Expand Down Expand Up @@ -271,8 +271,8 @@ static void GenerateDictionaryExtension (TabbedStringBuilder sb,
// loop over the flags and generate a helper static method to retrieve it from a attribute data dict
foreach (var (methodName, attributeName) in flags) {
using (var methodBlock = classBlock.CreateBlock (
$"public static bool {methodName} (this Dictionary<string, List<AttributeData>> self)",
block: true)) {
$"public static bool {methodName} (this Dictionary<string, List<AttributeData>> self)",
block: true)) {
methodBlock.AppendLine ($"return self.ContainsKey ({attributeName.AttributeFullName});");
}

Expand All @@ -283,16 +283,16 @@ static void GenerateDictionaryExtension (TabbedStringBuilder sb,
foreach (var (methodName, attributeInfo) in dataAttributes) {
// property to check if the attribute is present
using (var methodBlock = classBlock.CreateBlock (
$"public static bool {methodName} (this Dictionary<string, List<AttributeData>> self)",
block: true)) {
$"public static bool {methodName} (this Dictionary<string, List<AttributeData>> self)",
block: true)) {
methodBlock.AppendLine ($"return self.ContainsKey ({attributeInfo.AttributeFullName});");
}

classBlock.AppendLine ();
// property to access the attribute
using (var methodBlock = classBlock.CreateBlock (
$"public static {attributeInfo.Data.DataModelType}? Get{attributeInfo.AttributeName} (this Dictionary<string, List<AttributeData>> self)",
block: true)) {
$"public static {attributeInfo.Data.DataModelType}? Get{attributeInfo.AttributeName} (this Dictionary<string, List<AttributeData>> self)",
block: true)) {
methodBlock.AppendRaw (
$@"if (self.{methodName} ()) {{
var data = self.GetAttribute<{attributeInfo.Data.DataModelType}> ({attributeInfo.AttributeFullName}, {attributeInfo.Data.DataModelType}.TryParse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ namespace Microsoft.Macios.Generator.DataModel;
readonly partial struct Accessor {

public ExportData? ExportPropertyData { get; init; }
public Accessor () {}

public Accessor () { }
}

0 comments on commit b8e9cb1

Please sign in to comment.