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 694a975 commit c35a6bd
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 47 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 @@ -270,8 +270,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 @@ -282,16 +282,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
10 changes: 5 additions & 5 deletions src/rgen/Microsoft.Macios.Transformer/Attributes/SnippetData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
namespace Microsoft.Macios.Transformer.Attributes;

readonly struct SnippetData : IEquatable<SnippetData> {

public string Code { get; }

public bool Optimizable { get; }

public SnippetData (string code)
{
Code = code;
}

public SnippetData (string code, bool optimizable)
{
Code = code;
Expand Down Expand Up @@ -61,10 +61,10 @@ public static bool TryParse (AttributeData attributeData,
}
}

data = new (code, optimizable);
data = new (code, optimizable);
return true;
}

public bool Equals (SnippetData other)
{
if (Code != other.Code)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
namespace Microsoft.Macios.Transformer.Attributes;

readonly struct StrongDictionaryData : IEquatable<StrongDictionaryData> {

public string TypeWithKeys { get; }
public string? Suffix { get; }

public StrongDictionaryData (string typeWithKeys)
{
TypeWithKeys = typeWithKeys;
}

public StrongDictionaryData (string typeWithKeys, string? suffix)
{
TypeWithKeys = typeWithKeys;
Expand Down Expand Up @@ -60,7 +60,7 @@ public static bool TryParse (AttributeData attributeData,
data = new (typeWithKeys, suffix);
return true;
}

public bool Equals (StrongDictionaryData other)
{
if (TypeWithKeys != other.TypeWithKeys)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
namespace Microsoft.Macios.Transformer.Attributes;

readonly struct ThreadSafeData : IEquatable<ThreadSafeData> {

public bool Safe { get; } = true;
public ThreadSafeData () : this (true) {}

public ThreadSafeData () : this (true) { }

public ThreadSafeData (bool safe)
{
Safe = safe;
Expand All @@ -23,7 +23,7 @@ public static bool TryParse (AttributeData attributeData,
data = null;
var count = attributeData.ConstructorArguments.Length;
if (count == 0) {
data = new();
data = new ();
return true;
}
bool safe = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.Macios.Transformer.Tests.Attributes;

public class SnippetDataTests : BaseTransformerTestClass {
class TestDataTryCreate : IEnumerable<object []> {

public IEnumerator<object []> GetEnumerator ()
{
const string path = "/some/random/path.cs";
Expand All @@ -30,8 +30,8 @@ namespace Test;
[Dispose (""dispatcher = null;"", Optimizable = true)]
interface NSButton { }
";
yield return [(Source: disposeAttribute, Path: path), new SnippetData("dispatcher = null;", true)];

yield return [(Source: disposeAttribute, Path: path), new SnippetData ("dispatcher = null;", true)];
}

IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Microsoft.Macios.Transformer.Tests.Attributes;

public class StrongDictionaryDataTests : BaseTransformerTestClass {

class TestDataTryCreate : IEnumerable<object []> {
public IEnumerator<object []> GetEnumerator ()
{
Expand All @@ -33,8 +33,8 @@ interface AVCapturePhotoSettingsThumbnailFormat {
NSNumber Height { get; set; }
}
";
yield return [(Source: strongDictionary, Path: "/some/random/path.cs"), new StrongDictionaryData("AVCapturePhotoSettingsThumbnailFormatKeys")];

yield return [(Source: strongDictionary, Path: "/some/random/path.cs"), new StrongDictionaryData ("AVCapturePhotoSettingsThumbnailFormatKeys")];
}

IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Microsoft.Macios.Transformer.Tests.Attributes;

public class ThreadSafeDataTests : BaseTransformerTestClass {

class TestDataTryCreate : IEnumerable<object []> {
public IEnumerator<object []> GetEnumerator ()
{
Expand All @@ -37,8 +37,8 @@ interface UIFeedbackGenerator : UIInteraction {
void Prepare ();
}
";
yield return [(Source: threadSageMethod, Path: path), new ThreadSafeData()];

yield return [(Source: threadSageMethod, Path: path), new ThreadSafeData ()];

const string notThreadSafeMethod = @"
using System;
Expand All @@ -59,8 +59,8 @@ interface UIFeedbackGenerator : UIInteraction {
void Prepare ();
}
";
yield return [(Source: notThreadSafeMethod, Path: path), new ThreadSafeData(false)];

yield return [(Source: notThreadSafeMethod, Path: path), new ThreadSafeData (false)];

}

Expand Down

0 comments on commit c35a6bd

Please sign in to comment.