-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
3,791 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# GeneratedIdentifier | ||
|
||
A Roslyn source generator that provides strongly-typed identifiers using LightResults. | ||
|
||
[![main](https://img.shields.io/github/actions/workflow/status/jscarle/LightResults.Extensions/main.yml?logo=github)](https://github.com/jscarle/LightResults.Extensions) | ||
[![nuget](https://img.shields.io/nuget/v/LightResults.Extensions.GeneratedIdentifier)](https://www.nuget.org/packages/LightResults.Extensions.GeneratedIdentifier) | ||
[![downloads](https://img.shields.io/nuget/dt/LightResults.Extensions.GeneratedIdentifier)](https://www.nuget.org/packages/LightResults.Extensions.GeneratedIdentifier) |
59 changes: 59 additions & 0 deletions
59
src/LightResults.Extensions.GeneratedIdentifier/Common/Declaration.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,59 @@ | ||
namespace LightResults.Extensions.GeneratedIdentifier.Common; | ||
|
||
/// <summary>Represents a declaration.</summary> | ||
public sealed record Declaration | ||
{ | ||
/// <summary>Initializes a new instance of the <see cref="Declaration"/> class with the specified type, name, and generic parameters.</summary> | ||
/// <param name="type">The type of declaration.</param> | ||
/// <param name="name">The name of the declaration.</param> | ||
/// <param name="genericParameters">A read-only list of generic parameter names, or an empty list if not generic.</param> | ||
internal Declaration(DeclarationType type, string name, EquatableImmutableArray<string> genericParameters) | ||
{ | ||
Type = type; | ||
Name = name; | ||
GenericParameters = genericParameters; | ||
} | ||
|
||
/// <summary>Gets the type of declaration.</summary> | ||
public DeclarationType Type { get; } | ||
|
||
/// <summary>Gets the name of the declaration.</summary> | ||
public string Name { get; } | ||
|
||
/// <summary>Gets a read-only list of generic parameter names for generic declarations, or an empty list otherwise.</summary> | ||
public EquatableImmutableArray<string> GenericParameters { get; } | ||
|
||
/// <summary>Returns a string representation of the declaration in the appropriate format for its type.</summary> | ||
/// <returns>A string representation of the declaration.</returns> | ||
public override string ToString() | ||
{ | ||
switch (Type) | ||
{ | ||
case DeclarationType.Namespace: | ||
return $"namespace {Name}"; | ||
case DeclarationType.Interface: | ||
case DeclarationType.Class: | ||
case DeclarationType.Record: | ||
case DeclarationType.Struct: | ||
case DeclarationType.RecordStruct: | ||
var keyword = ToKeyword(Type); | ||
return GenericParameters.Count == 0 ? $"{keyword} {Name}" : $"{keyword} {Name}<{string.Join(", ", GenericParameters)}>"; | ||
default: | ||
return base.ToString(); | ||
} | ||
} | ||
|
||
private static string ToKeyword(DeclarationType declarationType) | ||
{ | ||
return declarationType switch | ||
{ | ||
DeclarationType.Namespace => "namespace", | ||
DeclarationType.Interface => "interface", | ||
DeclarationType.Class => "class", | ||
DeclarationType.Record => "record", | ||
DeclarationType.Struct => "struct", | ||
DeclarationType.RecordStruct => "record struct", | ||
_ => throw new InvalidOperationException(), | ||
}; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/LightResults.Extensions.GeneratedIdentifier/Common/DeclarationExtensions.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,53 @@ | ||
using System.Text; | ||
|
||
namespace LightResults.Extensions.GeneratedIdentifier.Common; | ||
|
||
/// <summary>Provides extension methods for working with declarations.</summary> | ||
internal static class DeclarationExtensions | ||
{ | ||
/// <summary>Converts a list of declarations to their corresponding namespace.</summary> | ||
/// <param name="declarations">The list of declarations to convert.</param> | ||
/// <returns>The namespace represented by the declarations.</returns> | ||
public static string ToNamespace(this EquatableImmutableArray<Declaration> declarations) | ||
{ | ||
var builder = new StringBuilder(); | ||
|
||
for (var index = 0; index < declarations.Count; index++) | ||
{ | ||
var declaration = declarations[index]; | ||
if (declaration.Type != DeclarationType.Namespace) | ||
continue; | ||
|
||
if (builder.Length > 0) | ||
builder.Append('.'); | ||
builder.Append(declaration.Name); | ||
} | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
/// <summary>Converts a list of declarations to their fully qualified name.</summary> | ||
/// <param name="declarations">The list of declarations to convert.</param> | ||
/// <returns>The fully qualified name represented by the declarations.</returns> | ||
public static string ToFullyQualifiedName(this EquatableImmutableArray<Declaration> declarations) | ||
{ | ||
var builder = new StringBuilder(); | ||
|
||
for (var index = 0; index < declarations.Count; index++) | ||
{ | ||
var declaration = declarations[index]; | ||
|
||
if (builder.Length > 0) | ||
builder.Append('.'); | ||
builder.Append(declaration.Name); | ||
|
||
if (declaration.GenericParameters.Count == 0) | ||
continue; | ||
|
||
builder.Append('`'); | ||
builder.Append(declaration.GenericParameters.Count); | ||
} | ||
|
||
return builder.ToString(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/LightResults.Extensions.GeneratedIdentifier/Common/DeclarationType.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 @@ | ||
namespace LightResults.Extensions.GeneratedIdentifier.Common; | ||
|
||
/// <summary>Specifies the kind of declaration.</summary> | ||
public enum DeclarationType | ||
{ | ||
/// <summary>Represents a namespace declaration.</summary> | ||
Namespace = 0, | ||
|
||
/// <summary>Represents an interface declaration.</summary> | ||
Interface = 1, | ||
|
||
/// <summary>Represents a class declaration.</summary> | ||
Class = 2, | ||
|
||
/// <summary>Represents a record declaration.</summary> | ||
Record = 3, | ||
|
||
/// <summary>Represents a struct declaration.</summary> | ||
Struct = 4, | ||
|
||
/// <summary>Represents a record struct declaration.</summary> | ||
RecordStruct = 5, | ||
} |
15 changes: 15 additions & 0 deletions
15
src/LightResults.Extensions.GeneratedIdentifier/Common/EquatableImmutableArray.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,15 @@ | ||
using System.Collections.Immutable; | ||
|
||
namespace LightResults.Extensions.GeneratedIdentifier.Common; | ||
|
||
/// <summary>Provides extension methods to convert various collections to an <see cref="EquatableImmutableArray{T}"/>.</summary> | ||
internal static class EquatableImmutableArray | ||
{ | ||
/// <summary>Converts an <see cref="IEnumerable{T}"/> to an <see cref="EquatableImmutableArray{T}"/>.</summary> | ||
/// <param name="enumerable">The <see cref="IEnumerable{T}"/> to convert.</param> | ||
/// <returns>An <see cref="EquatableImmutableArray{T}"/> containing the same elements as the original enumerable.</returns> | ||
public static EquatableImmutableArray<T> ToEquatableImmutableArray<T>(this IEnumerable<T> enumerable) | ||
{ | ||
return new EquatableImmutableArray<T>(enumerable.ToImmutableArray()); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/LightResults.Extensions.GeneratedIdentifier/Common/EquatableImmutableArray`1.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,87 @@ | ||
using System.Collections; | ||
using System.Collections.Immutable; | ||
|
||
namespace LightResults.Extensions.GeneratedIdentifier.Common; | ||
|
||
/// <summary>Represents an immutable array that implements <see cref="IEquatable{T}"/>.</summary> | ||
/// <typeparam name="T">The type of elements in the array.</typeparam> | ||
public readonly struct EquatableImmutableArray<T> : IEquatable<EquatableImmutableArray<T>>, IReadOnlyList<T> | ||
{ | ||
/// <summary>Gets an empty <see cref="EquatableImmutableArray{T}"/>.</summary> | ||
internal static EquatableImmutableArray<T> Empty { get; } = new(ImmutableArray<T>.Empty); | ||
|
||
/// <summary>Gets the number of elements in the array.</summary> | ||
public int Count => Array.Length; | ||
|
||
/// <summary>Gets the element at the specified index.</summary> | ||
/// <param name="index">The zero-based index of the element to get.</param> | ||
/// <returns>The element at the specified index.</returns> | ||
public T this[int index] => Array[index]; | ||
|
||
private ImmutableArray<T> Array => _array ?? ImmutableArray<T>.Empty; | ||
private readonly ImmutableArray<T>? _array; | ||
|
||
internal EquatableImmutableArray(ImmutableArray<T>? array) | ||
{ | ||
_array = array; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public bool Equals(EquatableImmutableArray<T> other) | ||
{ | ||
return this.SequenceEqual(other); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override bool Equals(object? obj) | ||
{ | ||
return obj is EquatableImmutableArray<T> other && Equals(other); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override int GetHashCode() | ||
{ | ||
var hashCode = new HashCode(); | ||
|
||
foreach (var item in Array) | ||
hashCode.Add(item); | ||
|
||
return hashCode.ToHashCode(); | ||
} | ||
|
||
/// <summary>Determines whether two <see cref="EquatableImmutableArray{T}"/> instances are equal.</summary> | ||
/// <param name="left">The first <see cref="EquatableImmutableArray{T}"/> to compare.</param> | ||
/// <param name="right">The second <see cref="EquatableImmutableArray{T}"/> to compare.</param> | ||
/// <returns><see langword="true"/> if the two <see cref="EquatableImmutableArray{T}"/> instances are equal; otherwise, <see langword="false"/>.</returns> | ||
public static bool operator ==(EquatableImmutableArray<T> left, EquatableImmutableArray<T> right) | ||
{ | ||
return left.Equals(right); | ||
} | ||
|
||
/// <summary>Determines whether two <see cref="EquatableImmutableArray{T}"/> instances are not equal.</summary> | ||
/// <param name="left">The first <see cref="EquatableImmutableArray{T}"/> to compare.</param> | ||
/// <param name="right">The second <see cref="EquatableImmutableArray{T}"/> to compare.</param> | ||
/// <returns><see langword="true"/> if the two <see cref="EquatableImmutableArray{T}"/> instances are not equal; otherwise, <see langword="false"/>.</returns> | ||
public static bool operator !=(EquatableImmutableArray<T> left, EquatableImmutableArray<T> right) | ||
{ | ||
return !left.Equals(right); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public IEnumerator<T> GetEnumerator() | ||
{ | ||
// ReSharper disable once ForCanBeConvertedToForeach | ||
// ReSharper disable once LoopCanBeConvertedToQuery | ||
for (var index = 0; index < Array.Length; index++) | ||
{ | ||
var item = Array[index]; | ||
yield return item; | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/LightResults.Extensions.GeneratedIdentifier/Common/IncrementalValueProviderExtensions.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,13 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace LightResults.Extensions.GeneratedIdentifier.Common; | ||
|
||
internal static class IncrementalValueProviderExtensions | ||
{ | ||
public static IncrementalValuesProvider<TSource> WhereNotNull<TSource>(this IncrementalValuesProvider<TSource?> source) | ||
where TSource : struct | ||
{ | ||
return source.Where(x => x is not null) | ||
.Select((x, _) => x!.Value); | ||
} | ||
} |
Oops, something went wrong.