Skip to content

Commit

Permalink
Merge pull request #71 from dansiegel/dev/ds/remove-internal-extensions
Browse files Browse the repository at this point in the history
Hide internal extensions
  • Loading branch information
dansiegel authored Dec 15, 2024
2 parents fbdd547 + 2860db6 commit 69671cd
Show file tree
Hide file tree
Showing 19 changed files with 121 additions and 130 deletions.
1 change: 1 addition & 0 deletions src/CodeGenHelpers/AvantiPoint.CodeGenHelpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>true</IsPackable>
<RootNamespace>CodegenHelpers</RootNamespace>
<Description>The AvantiPoint CodeGenHelpers are exactly what you need for your Source Generation tasks. Whether you're writing Code Generators with Roslyn's built in Source Gen or Uno Platform's Source Generator's, this helps you more powerfully generate the code you need. This combines an understanding of some system types, Roslyn Symbol's and strings. The result is beautifully formated code that's easy to read and doesn't require you to track indentations or what's already been added to the class.</Description>
<PackageProjectUrl>https://github.com/dansiegel/CodeGenHelpers</PackageProjectUrl>
<RepositoryUrl>https://github.com/dansiegel/CodeGenHelpers.git</RepositoryUrl>
Expand Down
4 changes: 2 additions & 2 deletions src/CodeGenHelpers/ClassBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public ClassBuilder SetBaseClass(INamedTypeSymbol symbol)
{
if(symbol.Name == Name)
{
BaseClass = $"global::{symbol.GetFullMetadataName()}";
BaseClass = $"global::{SymbolHelpers.GetFullMetadataName(symbol)}";
return this;
}

Expand Down Expand Up @@ -338,7 +338,7 @@ internal override void Write(in CodeWriter writer)
IsAbstract = true;

var classDeclaration = new[] {
AccessModifier.Code(),
AccessibilityHelpers.Code(AccessModifier),
IsStatic ? "static" : null,
IsSealed ? "sealed" : null,
IsAbstract ? "abstract" : null,
Expand Down
2 changes: 1 addition & 1 deletion src/CodeGenHelpers/ConstructorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ internal override void Write(in CodeWriter writer)
var modifier = AccessModifier switch
{
null => Class.AccessModifier.ToString().ToLowerInvariant(),
_ => AccessModifier.Code()
_ => AccessibilityHelpers.Code(AccessModifier)
};
var parameters = _parameters.Any() ? string.Join(", ", _parameters.Select(x => x.ToString())) : string.Empty;
using(writer.Block($"{modifier} {Class.Name}({parameters})", _baseCall.Invoke()))
Expand Down
2 changes: 1 addition & 1 deletion src/CodeGenHelpers/DelegateBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ internal override void Write(in CodeWriter writer)
var parameters = _parameters.Any() ? string.Join(", ", _parameters.Select(x => x.ToString())) : string.Empty;
var parts = new string?[]
{
AccessModifier.Code(),
AccessibilityHelpers.Code(AccessModifier),
"delegate",
ReturnType is null || string.IsNullOrEmpty(ReturnType) ? "void" : ReturnType.Trim(),
$"{Name}{_generics}({parameters})",
Expand Down
2 changes: 1 addition & 1 deletion src/CodeGenHelpers/EnumBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void IBuilder.Write(in CodeWriter writer)

var parts = new[]
{
AccessModifier.Code(),
AccessibilityHelpers.Code(AccessModifier),
"enum",
Name
};
Expand Down
2 changes: 1 addition & 1 deletion src/CodeGenHelpers/EventBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ internal override void Write(in CodeWriter writer)
}

var @static = _static ? "static " : string.Empty;
var eventDeclaration = $"{_declaredAccessibility.Code()} {@static}event {_eventDelegateType} {Name}";
var eventDeclaration = $"{AccessibilityHelpers.Code(_declaredAccessibility)} {@static}event {_eventDelegateType} {Name}";
if(!string.IsNullOrEmpty(_explicitImplementation))
{
eventDeclaration = $"event {@static}{_eventDelegateType} {_explicitImplementation}.{Name}";
Expand Down
2 changes: 1 addition & 1 deletion src/CodeGenHelpers/Extensions/RoslynExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace AvantiPoint.CodeGenHelpers.Extensions;
namespace CodeGenHelpers.Extensions;

#nullable enable
public static class RoslynExtensions
Expand Down
73 changes: 0 additions & 73 deletions src/CodeGenHelpers/Extensions/SymbolExtensions.cs

This file was deleted.

23 changes: 0 additions & 23 deletions src/CodeGenHelpers/Internals/AccessibilityExtensions.cs

This file was deleted.

23 changes: 23 additions & 0 deletions src/CodeGenHelpers/Internals/AccessibilityHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.CodeAnalysis;

#nullable enable
namespace CodeGenHelpers.Internals
{
internal static class AccessibilityHelpers
{
public static string? Code(Accessibility accessModifier) =>
accessModifier switch
{
Accessibility.ProtectedAndInternal => "private protected",
Accessibility.ProtectedOrInternal => "protected internal",
Accessibility.NotApplicable => null,
_ => accessModifier.ToString().ToLowerInvariant()
};

public static string? Code(Accessibility? accessModifier) =>
accessModifier.HasValue ? Code(accessModifier.Value) : null;

public static string? Code(Accessibility? accessModifier, Accessibility defaultValue) =>
accessModifier.HasValue ? Code(accessModifier.Value) : Code(defaultValue);
}
}
2 changes: 1 addition & 1 deletion src/CodeGenHelpers/Internals/CodeBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static string GetTypeName(this ITypeSymbol symbol)
if (symbol.ContainingNamespace.Name == "System" && _mappings.ContainsKey(symbol.Name))
return _mappings[symbol.Name];

return symbol.GetFullMetadataName();
return SymbolHelpers.GetFullMetadataName(symbol);
}

public static string GetTypeName(this Type type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeGenHelpers.Extensions;
using Microsoft.CodeAnalysis;

#pragma warning disable IDE0008
Expand All @@ -11,7 +10,7 @@
#nullable enable
namespace CodeGenHelpers.Internals
{
internal static class ISymbolExtensions
internal static class SymbolHelpers
{
private static readonly Dictionary<string, string> _fullNamesMaping = new Dictionary<string, string>
(StringComparer.OrdinalIgnoreCase)
Expand All @@ -30,7 +29,7 @@ internal static class ISymbolExtensions
{ "bool", typeof(bool).ToString() },
};

public static string GetGloballyQualifiedTypeName(this INamespaceOrTypeSymbol symbol)
public static string GetGloballyQualifiedTypeName(INamespaceOrTypeSymbol symbol)
{
var value = GetFullMetadataName(symbol);
if(_fullNamesMaping.Any(x => x.Value == value))
Expand All @@ -45,7 +44,7 @@ public static string GetGloballyQualifiedTypeName(this INamespaceOrTypeSymbol sy
return value;
}

public static string GetFullMetadataName(this INamespaceOrTypeSymbol symbol)
public static string GetFullMetadataName(INamespaceOrTypeSymbol symbol)
{
ISymbol s = symbol;
var sb = new StringBuilder(s.MetadataName);
Expand All @@ -55,7 +54,7 @@ public static string GetFullMetadataName(this INamespaceOrTypeSymbol symbol)

if (s == null)
{
return symbol.GetFullName();
return GetFullName(symbol);
}

while (!IsRootNamespace(s))
Expand Down Expand Up @@ -89,16 +88,16 @@ private static bool IsRootNamespace(ISymbol s)
return s is INamespaceSymbol symbol && symbol.IsGlobalNamespace;
}

public static string GetFullName(this INamespaceOrTypeSymbol type)
public static string GetFullName(INamespaceOrTypeSymbol type)
{
if (type is IArrayTypeSymbol arrayType)
{
return $"{arrayType.ElementType.GetFullName()}[]";
return $"{GetFullName(arrayType.ElementType)}[]";
}

if (((ITypeSymbol)type).IsNullable(out var t) && t != null)
if (IsNullable((ITypeSymbol)type, out var t) && t != null)
{
return $"System.Nullable`1[{t.GetFullName()}]";
return $"System.Nullable`1[{GetFullName(t)}]";
}

var name = type.ToDisplayString() ?? string.Empty;
Expand All @@ -110,5 +109,68 @@ public static string GetFullName(this INamespaceOrTypeSymbol type)

return output;
}

public static string GetQualifiedTypeName(ITypeSymbol typeSymbol)
{
var type = typeSymbol.GetTypeName();
if (!type.Contains(".") || typeSymbol is not INamedTypeSymbol namedTypeSymbol)
{
return type;
}

var @namespace = namedTypeSymbol.ContainingNamespace.ToString();
var typeName = namedTypeSymbol.Name;

if (typeSymbol.ContainingType is not null)
{
return $"{GetQualifiedTypeName(typeSymbol.ContainingType)}.{typeSymbol.Name}";
}

var fullyQualifiedName = $"{@namespace}.{typeName}";

if (namedTypeSymbol.IsGenericType)
{
var generics = namedTypeSymbol.TypeArguments.Select(x => GetQualifiedTypeName(x));
var baseName = fullyQualifiedName;
var nullable = string.Empty;
if (baseName.EndsWith("?", StringComparison.InvariantCulture))
{
baseName = baseName.Substring(0, baseName.Length - 1);
nullable = "?";
}

fullyQualifiedName = $"{baseName}<{string.Join(", ", generics)}>{nullable}";
}

if (fullyQualifiedName.Contains(".") && !fullyQualifiedName.StartsWith("global::", StringComparison.InvariantCulture))
{
fullyQualifiedName = $"global::{fullyQualifiedName}";
}

return fullyQualifiedName;
}

public static bool IsNullable(ITypeSymbol type)
{
if (type.NullableAnnotation == NullableAnnotation.Annotated)
return true;

return ((type as INamedTypeSymbol)?.IsGenericType ?? false)
&& type.OriginalDefinition.ToDisplayString().Equals("System.Nullable<T>", StringComparison.OrdinalIgnoreCase);
}

public static bool IsNullable(ITypeSymbol type, out ITypeSymbol? nullableType)
{
if (IsNullable(type))
{
nullableType = type.NullableAnnotation == NullableAnnotation.Annotated ? type : ((INamedTypeSymbol)type).TypeArguments.First();
return true;
}
else
{
nullableType = null;
return false;
}
}
}
}
2 changes: 1 addition & 1 deletion src/CodeGenHelpers/MethodBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ internal override void Write(in CodeWriter writer)
output = $"static {output}";

var parameters = string.Join(", ", _parameters.Select(x => x.ToString()));
output = $"{AccessModifier.Code()} {output} {Name}{_generics}({parameters})";
output = $"{AccessibilityHelpers.Code(AccessModifier)} {output} {Name}{_generics}({parameters})";

if(_xmlDoc is ParameterDocumentationComment parameterDocumentation)
parameterDocumentation.RemoveUnusedParameters(_parameters);
Expand Down
2 changes: 1 addition & 1 deletion src/CodeGenHelpers/ParameterizedExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static T AddParameter<T>(this IParameterized<T> parameterized, ITypeSymbo
where T : BuilderBase<T>, IParameterized<T>
{
parameterized.Parent.AddNamespaceImport(typeSymbol);
return parameterized.AddParameter(typeSymbol.GetGloballyQualifiedTypeName(), parameterName);
return parameterized.AddParameter(SymbolHelpers.GetGloballyQualifiedTypeName(typeSymbol), parameterName);
}

public static T AddParameter<T>(this IParameterized<T> parameterized, string typeName, string parameterName, int index)
Expand Down
10 changes: 5 additions & 5 deletions src/CodeGenHelpers/PropertyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,11 @@ void IBuilder.Write(in CodeWriter writer)

var output = (FieldTypeValue switch
{
FieldType.Const => $"{AccessModifier.Code()}{isNew} const {type} {name}",
FieldType.ReadOnly => $"{AccessModifier.Code()}{isNew}{_static} readonly {type} {name}",
FieldType.Const => $"{AccessibilityHelpers.Code(AccessModifier)}{isNew} const {type} {name}",
FieldType.ReadOnly => $"{AccessibilityHelpers.Code(AccessModifier)}{isNew}{_static} readonly {type} {name}",
_ => additionalModifier is null
? $"{AccessModifier.Code()}{isNew}{_static} {type} {name}"
: $"{AccessModifier.Code()} {additionalModifier} {type} {name}"
? $"{AccessibilityHelpers.Code(AccessModifier)}{isNew}{_static} {type} {name}"
: $"{AccessibilityHelpers.Code(AccessModifier)} {additionalModifier} {type} {name}"
}).Trim();

var maxCharacters = (value?.StartsWith("\"") ?? false) ? 9 : 5;
Expand Down Expand Up @@ -312,7 +312,7 @@ void IBuilder.Write(in CodeWriter writer)
return;
}

var set = $"{_setterAccessibility.Code()} set".Trim();
var set = $"{AccessibilityHelpers.Code(_setterAccessibility)} set".Trim();
writer.AppendLine($"{output} {{ get; {set}; }}");
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/CodeGenHelpers/RecordBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ internal override void Write(in CodeWriter writer)

if (PropertyType == RecordPropertyType.Init)
{
using (writer.Block($"{AccessModifier.Code()} record {Name}"))
using (writer.Block($"{AccessibilityHelpers.Code(AccessModifier)} record {Name}"))
{
foreach (RecordPropertyBuilder property in _properties)
{
Expand All @@ -109,7 +109,7 @@ internal override void Write(in CodeWriter writer)
}

var properties = _properties.Any() ? string.Join(", ", _properties.Select(x => x.ToPositionalProperty())) : string.Empty;
writer.AppendLine($"{AccessModifier.Code()} record {Name}({properties});");
writer.AppendLine($"{AccessibilityHelpers.Code(AccessModifier)} record {Name}({properties});");
}

public override RecordBuilder AddNamespaceImport(string importedNamespace)
Expand Down
Loading

0 comments on commit 69671cd

Please sign in to comment.