Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
DearVa committed Dec 23, 2023
1 parent 6bc8a27 commit 2d38dd8
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// using System.Linq;
// using Microsoft.CodeAnalysis;
// using Microsoft.CodeAnalysis.Diagnostics;
//
// namespace Antelcat.Parameterization.SourceGenerators.Analyzers;
//
// public abstract class ClassAttributeBaseAnalyzer : DiagnosticAnalyzer
// {
// public override void Initialize(AnalysisContext context)
// {
// context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
// context.EnableConcurrentExecution();
//
// context.RegisterCompilationStartAction(static context =>
// {
// if (context.Compilation.GetTypeByMetadataName($"{Global.Namespace}.{nameof(ParameterizationAttribute)}") is not { } attribute)
// {
// return;
// }
//
// context.RegisterSymbolAction(context =>
// {
// if (context.Symbol is not INamedTypeSymbol typeSymbol) return;
// typeSymbol.GetMembers().OfType<IMethodSymbol>()
// .Where(m => m.GetAttributes().Any(a => a.))
//
// }, SymbolKind.NamedType);
// });
// }
// }
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<Nullable>enable</Nullable>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<Company>Antelcat</Company>
<AssemblyVersion>1.2.2</AssemblyVersion>
<FileVersion>1.2.2</FileVersion>
<AssemblyVersion>1.2.3</AssemblyVersion>
<FileVersion>1.2.3</FileVersion>
<AnalysisMode>Recommended</AnalysisMode>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,21 @@ public static bool IsDerivedFrom<T>(this ITypeSymbol? typeSymbol)
return false;
}

public static string? ToDisplayName(this SyntaxNode syntax, SemanticModel semanticModel, SymbolDisplayFormat? symbolDisplayFormat = null)
public static string ToDisplayName(this SyntaxNode syntax, SemanticModel semanticModel, SymbolDisplayFormat? symbolDisplayFormat = null)
{
return semanticModel.GetSymbolInfo(syntax).Symbol?.ToDisplayString(symbolDisplayFormat);
return semanticModel.GetSymbolInfo(syntax).Symbol?.ToDisplayString(symbolDisplayFormat) + "?".If(syntax is NullableTypeSyntax);
}

public static bool IsArray(this SyntaxNode syntax, SemanticModel semanticModel)
{
return semanticModel.GetSymbolInfo(syntax).Symbol is IArrayTypeSymbol;
}

public static ITypeSymbol? GetArrayElementType(this SyntaxNode syntax, SemanticModel semanticModel)
{
return (semanticModel.GetSymbolInfo(syntax).Symbol as IArrayTypeSymbol)?.ElementType;
}

public static bool IsAwaitable(this TypeSyntax type, SemanticModel semanticModel)
{
if (semanticModel.GetSymbolInfo(type).Symbol is not ITypeSymbol symbol) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ public virtual void SetValue(object? value)
}
}
public class ParsedArrayArgument : ParsedArgument
public class ParsedArrayArgument<T> : ParsedArgument
{
public ParsedArrayArgument(string name) : base(name)
{
value = new List<object?>(1);
value = new List<T>(1);
}
public object?[] ToArray() => ((List<object?>)value!).ToArray();
public T[] ToArray() => ((List<T>)value!).ToArray();
public override void SetValue(object? value)
{
((List<object?>)this.value!).Add(value);
((List<T>)this.value!).Add(((T)value!)!);
hasValue = true;
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ public static T ConvertArgument<T>(ParsedArgument parsed)
{
if (!parsed.HasValue) throw new ArgumentException($"Argument \"{parsed.Name}\" is not specified.");
object? value;
if (parsed is ParsedArrayArgument parsedArray)
if (parsed is ParsedArrayArgument<T> parsedArray)
{
value = parsedArray.ToArray();
}
Expand All @@ -173,7 +173,7 @@ public static T ConvertArgument<T>(ParsedArgument parsed, T defaultValue)
{
if (!parsed.HasValue) return defaultValue;
object? value;
if (parsed is ParsedArrayArgument parsedArray)
if (parsed is ParsedArrayArgument<T> parsedArray)
{
value = parsedArray.ToArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ symbol is not IMethodSymbol
.AppendLine($"var parsedArguments = new {Global.GlobalNamespace}.ParsedArgument[] {{ {string.Join(", ",
methodParameters
.Select(p => p.Type.NotNull().IsArray(syntaxContext.SemanticModel) ?
$"new {Global.GlobalNamespace}.ParsedArrayArgument(\"{p.Identifier.ValueText}\")" :
$"new {Global.GlobalNamespace}.ParsedArrayArgument<{p.Type.NotNull().GetArrayElementType(syntaxContext.SemanticModel)}>(\"{p.Identifier.ValueText}\")" :
$"new {Global.GlobalNamespace}.ParsedArgument(\"{p.Identifier.ValueText}\")"))} }};")
.AppendLine($"var argumentNames = new {Global.ValueTuple}<{Global.String}, {Global.String}?>[] {{ {string.Join(", ",
parameterNames
Expand Down Expand Up @@ -253,8 +253,8 @@ symbol is not IMethodSymbol
))
.WithIndex()
.Select(x =>
$"{Global.GlobalNamespace}.Common.ConvertArgument<{x.value.typeName}>(parsedArguments[{x.index}]{(
x.value.defaultValue == null ? string.Empty : $", {x.value.defaultValue}")})"))});");
$"{Global.GlobalNamespace}.Common.ConvertArgument<{x.value.typeName}>(parsedArguments[{x.index}]{
$", {x.value.defaultValue}".If(x.value.defaultValue != null)})"))});");
}
else
{
Expand Down Expand Up @@ -295,7 +295,6 @@ symbol is not IMethodSymbol
{{"return ".If(isAsync)}}ExecuteArguments{{"Async".If(isAsync)}}(arguments);
}
[global::System.Diagnostics.CodeAnalysis.SuppressMessage("RuleCategory", "CS8625:Cannot convert null literal to non-nullable reference type", Justification = "Generated Code")]
private {{"static ".If(isStatic)}}{{(isAsync ? $"async {Global.ValueTask}" : "void")}} ExecuteArguments{{"Async".If(isAsync)}}({{Global.GenericIReadonlyList}}<{{Global.String}}> arguments)
{
if (arguments.Count == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<Version>1.2.2</Version>
<Version>1.2.3</Version>
<Company>Antelcat</Company>
<AssemblyVersion>1.2.2</AssemblyVersion>
<FileVersion>1.2.2</FileVersion>
<AssemblyVersion>1.2.3</AssemblyVersion>
<FileVersion>1.2.3</FileVersion>
<Title>Effortless Command-Line Application Builder</Title>
<Description>A powerful source generator designed to revolutionize the way you create command-line applications. This tool simplifies the process of building CLI applications by automatically generating parsing methods with just attribute marking on classes and methods.</Description>
<PackageProjectUrl>https://github.com/Antelcat/Antelcat.Parameterization</PackageProjectUrl>
Expand Down

0 comments on commit 2d38dd8

Please sign in to comment.