-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create
StringWithTypes
and use it as statement or expression
* Add `StringWithTypes` * Add interpolated string handler that create `StringWithTypes` * Use as initialize in `ParameterBuilder`, `FieldBuilder` -> `PropertyBuilder` * Use as body statement in `MethodLikeBuilder` -> `MethodBuilder`, `ConstructorBuilder` * Update usage
- Loading branch information
1 parent
4c581f4
commit 4eb1c02
Showing
14 changed files
with
155 additions
and
74 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
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
12 changes: 6 additions & 6 deletions
12
TestsHelper.SourceGenerator/CodeBuilding/ParameterBuilder.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 |
---|---|---|
@@ -1,26 +1,26 @@ | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using TestsHelper.SourceGenerator.CodeBuilding.Types; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
|
||
namespace TestsHelper.SourceGenerator.CodeBuilding; | ||
|
||
public class ParameterBuilder | ||
{ | ||
public string Name { get; set; } = null!; | ||
public IType Type { get; set; } = null!; | ||
public string? Initializer { get; set; } = null; | ||
public StringWithTypes Initializer { get; set; } = StringWithTypes.Empty; | ||
|
||
private ParameterBuilder(){} | ||
|
||
public ParameterSyntax Build(BuildContext context) | ||
{ | ||
TypeSyntax type = Type.TryRegisterAlias(context.FileBuilder).Build(); | ||
|
||
return SyntaxFactory.Parameter(SyntaxFactory.Identifier(Name)) | ||
return Parameter(Identifier(Name)) | ||
.WithType(type) | ||
.WithDefault(Initializer == null ? null : SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression(Initializer))); | ||
.WithDefault(Initializer.IsEmpty ? null : EqualsValueClause(ParseExpression(Initializer.ToString(context.FileBuilder)))); | ||
} | ||
|
||
public static ParameterBuilder Create(IType type, string name, string? initializer = null) => | ||
new() {Type = type, Name = name, Initializer = initializer}; | ||
public static ParameterBuilder Create(IType type, string name, StringWithTypes? initializer = null) => | ||
new() {Type = type, Name = name, Initializer = initializer ?? StringWithTypes.Empty}; | ||
} |
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
63 changes: 63 additions & 0 deletions
63
TestsHelper.SourceGenerator/CodeBuilding/StringWithTypes.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,63 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using OneOf; | ||
using TestsHelper.SourceGenerator.CodeBuilding.Types; | ||
|
||
namespace TestsHelper.SourceGenerator.CodeBuilding; | ||
|
||
public sealed class StringWithTypes | ||
{ | ||
public static readonly StringWithTypes Empty = new(EmptyList<OneOf<string, IType, StringWithTypes, MultipleValues<StringWithTypes>>>.Instance); | ||
|
||
public bool IsEmpty => this == Empty; | ||
|
||
private readonly List<OneOf<string, IType, StringWithTypes, MultipleValues<StringWithTypes>>> _components; | ||
|
||
private StringWithTypes(List<OneOf<string, IType, StringWithTypes, MultipleValues<StringWithTypes>>> components) | ||
{ | ||
_components = components; | ||
} | ||
|
||
public void Add(OneOf<string, IType, StringWithTypes, MultipleValues<StringWithTypes>> a) => _components.Add(a); | ||
public StringWithTypes TakeIf(bool condition) => condition ? this : Empty; | ||
|
||
public string ToString(FileBuilder fileBuilder) | ||
{ | ||
StringBuilder builder = new StringBuilder(); | ||
|
||
foreach (OneOf<string, IType, StringWithTypes, MultipleValues<StringWithTypes>> oneOf in _components) | ||
{ | ||
string? text = oneOf.Match( | ||
static s => s, | ||
type => type.TryRegisterAlias(fileBuilder).MakeString(), | ||
stringWithTypes => stringWithTypes.IsEmpty ? null : stringWithTypes.ToString(fileBuilder), | ||
multipleStrings => | ||
string.Join(multipleStrings.Separator, multipleStrings.Values | ||
.Where(types => !types.IsEmpty) | ||
.Select(types => types.ToString(fileBuilder)) | ||
) | ||
); | ||
if (text != null) | ||
{ | ||
builder.Append(text); | ||
} | ||
} | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
public static StringWithTypes Format(StringWithTypesInterpolatedStringHandler stringHandler) => stringHandler.StringWithTypes; | ||
|
||
public static StringWithTypes Create(int capacity) => | ||
new(new List<OneOf<string, IType, StringWithTypes, MultipleValues<StringWithTypes>>>(capacity)); | ||
|
||
public readonly record struct MultipleValues<T>(T[] Values, string Separator); | ||
|
||
public static implicit operator StringWithTypes(string s) | ||
{ | ||
StringWithTypes stringWithTypes = Create(1); | ||
stringWithTypes.Add(s); | ||
return stringWithTypes; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
TestsHelper.SourceGenerator/CodeBuilding/StringWithTypesInterpolatedStringHandler.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,40 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using TestsHelper.SourceGenerator.CodeBuilding.Types; | ||
|
||
namespace TestsHelper.SourceGenerator.CodeBuilding; | ||
|
||
[InterpolatedStringHandler] | ||
public readonly ref struct StringWithTypesInterpolatedStringHandler | ||
{ | ||
public StringWithTypes StringWithTypes { get; } | ||
|
||
public StringWithTypesInterpolatedStringHandler(int literalLength, int formattedCount) | ||
{ | ||
StringWithTypes = StringWithTypes.Create(literalLength + formattedCount); | ||
} | ||
|
||
public void AppendLiteral(string s) => StringWithTypes.Add(s); | ||
|
||
public void AppendFormatted<T>(T t) where T : IType => StringWithTypes.Add(t); | ||
|
||
public void AppendFormatted(FieldBuilder field) => StringWithTypes.Add(field.Name); | ||
|
||
public void AppendFormatted(ParameterBuilder parameterBuilder) => StringWithTypes.Add(parameterBuilder.Name); | ||
|
||
public void AppendFormatted(TypeBuilder typeBuilder) => StringWithTypes.Add(typeBuilder.Name); | ||
|
||
public void AppendFormatted(StringWithTypes stringWithTypes) => StringWithTypes.Add(stringWithTypes); | ||
|
||
public void AppendFormatted(IEnumerable<string> array, string format) => StringWithTypes.Add(string.Join(format, array)); | ||
|
||
public void AppendFormatted(IEnumerable<StringWithTypes> stringWithTypes, string format) | ||
{ | ||
StringWithTypes.Add(new StringWithTypes.MultipleValues<StringWithTypes>(stringWithTypes.ToArray(), format)); | ||
} | ||
|
||
public void AppendFormatted(string s) => StringWithTypes.Add(s); | ||
|
||
internal string GetFormattedText() => string.Empty; | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.