-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[generator] Refactor enum writing to use SourceWriters (#1063)
Context: #1037 Refactor `EnumGenerator` to use `SourceWriters` instead of `TextWriter`. This will make it possible to use our existing code to apply `[PlatformOSSupported]` attributes to enums in a future PR. This is a separate PR to help verify there was no output changed from the refactor.
- Loading branch information
Showing
9 changed files
with
205 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Xamarin.SourceWriter | ||
{ | ||
public class EnumMemberWriter : ISourceWriter | ||
{ | ||
public string Name { get; set; } | ||
public List<string> Comments { get; } = new List<string> (); | ||
public List<AttributeWriter> Attributes { get; } = new List<AttributeWriter> (); | ||
public string Value { get; set; } | ||
public int Priority { get; set; } | ||
|
||
public virtual void Write (CodeWriter writer) | ||
{ | ||
WriteComments (writer); | ||
WriteAttributes (writer); | ||
WriteSignature (writer); | ||
} | ||
|
||
public virtual void WriteComments (CodeWriter writer) | ||
{ | ||
foreach (var c in Comments) | ||
writer.WriteLine (c); | ||
} | ||
|
||
public virtual void WriteAttributes (CodeWriter writer) | ||
{ | ||
foreach (var att in Attributes) | ||
att.WriteAttribute (writer); | ||
} | ||
|
||
public virtual void WriteSignature (CodeWriter writer) | ||
{ | ||
if (Value.HasValue ()) { | ||
writer.Write (Name + " = "); | ||
writer.Write (Value); | ||
writer.WriteLine (","); | ||
} else { | ||
writer.WriteLine ($"{Name},"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Xamarin.SourceWriter | ||
{ | ||
public class EnumWriter : ISourceWriter | ||
{ | ||
Visibility visibility; | ||
|
||
public string Name { get; set; } | ||
public bool IsPublic { get => visibility.HasFlag (Visibility.Public); set => visibility = value ? Visibility.Public : Visibility.Default; } | ||
public bool IsInternal { get => visibility.HasFlag (Visibility.Internal); set => visibility = value ? Visibility.Internal : Visibility.Default; } | ||
public bool IsPrivate { get => visibility.HasFlag (Visibility.Private); set => visibility = value ? Visibility.Private : Visibility.Default; } | ||
public bool IsProtected { get => visibility.HasFlag (Visibility.Protected); set => visibility = value ? Visibility.Protected : Visibility.Default; } | ||
public List<string> Comments { get; } = new List<string> (); | ||
public List<AttributeWriter> Attributes { get; } = new List<AttributeWriter> (); | ||
public List<EnumMemberWriter> Members { get; } = new List<EnumMemberWriter> (); | ||
|
||
public int Priority { get; set; } | ||
|
||
public void Write (CodeWriter writer) | ||
{ | ||
WriteComments (writer); | ||
WriteAttributes (writer); | ||
WriteSignature (writer); | ||
WriteMembers (writer); | ||
WriteTypeClose (writer); | ||
} | ||
|
||
public virtual void WriteComments (CodeWriter writer) | ||
{ | ||
foreach (var c in Comments) | ||
writer.WriteLine (c); | ||
} | ||
|
||
public virtual void WriteAttributes (CodeWriter writer) | ||
{ | ||
foreach (var att in Attributes) | ||
att.WriteAttribute (writer); | ||
} | ||
|
||
public virtual void WriteSignature (CodeWriter writer) | ||
{ | ||
if (IsPublic) | ||
writer.Write ("public "); | ||
if (IsProtected) | ||
writer.Write ("protected "); | ||
if (IsInternal) | ||
writer.Write ("internal "); | ||
if (IsPrivate) | ||
writer.Write ("private "); | ||
|
||
writer.Write ("enum "); | ||
writer.WriteLine (Name + " "); | ||
|
||
writer.WriteLine ("{"); | ||
writer.Indent (); | ||
} | ||
|
||
public virtual void WriteMembers (CodeWriter writer) | ||
{ | ||
foreach (var member in Members) { | ||
member.Write (writer); | ||
writer.WriteLine (); | ||
} | ||
} | ||
|
||
public virtual void WriteTypeClose (CodeWriter writer) | ||
{ | ||
writer.Unindent (); | ||
writer.WriteLine ("}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using Xamarin.SourceWriter; | ||
|
||
namespace generator.SourceWriters | ||
{ | ||
public class FlagsAttr : AttributeWriter | ||
{ | ||
public override void WriteAttribute (CodeWriter writer) | ||
{ | ||
writer.WriteLine ("[System.Flags]"); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
tools/generator/SourceWriters/Attributes/IntDefinitionAttr.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using Xamarin.SourceWriter; | ||
|
||
namespace generator.SourceWriters | ||
{ | ||
public class IntDefinitionAttr : AttributeWriter | ||
{ | ||
public string ManagedMember { get; set; } | ||
public string JniField { get; set; } | ||
|
||
public IntDefinitionAttr (string managedMember, string jniField) | ||
{ | ||
ManagedMember = managedMember; | ||
JniField = jniField; | ||
} | ||
|
||
public override void WriteAttribute (CodeWriter writer) | ||
{ | ||
var member = ManagedMember is null ? "null" : "\"" + ManagedMember + "\""; | ||
writer.WriteLine ($"[global::Android.Runtime.IntDefinition ({member}, JniField = \"{JniField}\")]"); | ||
} | ||
} | ||
} |