Skip to content

Commit

Permalink
fix bug if class is non-static
Browse files Browse the repository at this point in the history
  • Loading branch information
DearVa committed Dec 8, 2023
1 parent a22983e commit 1e90a34
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
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.0</AssemblyVersion>
<FileVersion>1.2.0</FileVersion>
<AssemblyVersion>1.2.1</AssemblyVersion>
<FileVersion>1.2.1</FileVersion>
<AnalysisMode>Recommended</AnalysisMode>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

public static class StringExtension
{
public static string Escape(this string? s)
{
return s == null ? "null" : $"\"{s.Replace("\"", "\\\"")}\"";
}
public static string Escape(this string? s) => s == null ? "null" : $"\"{s.Replace("\"", "\\\"")}\"";

public static string If(this string s, bool condition) => condition ? s : string.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ protected override void GenerateCode(SourceProductionContext context, ImmutableA
modifier.IsKind(SyntaxKind.PublicKeyword) || modifier.IsKind(SyntaxKind.InternalKeyword))
.Text ??
"internal"; // 默认为 internal
var isClassStatic = classDeclarationSyntax.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.StaticKeyword));
var isStaticClass = classDeclarationSyntax.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.StaticKeyword));

IEnumerable<(MethodDeclarationSyntax, AttributeProxy)> GetCandidateMethods()
{
foreach (var method in classDeclarationSyntax.Members.OfType<MethodDeclarationSyntax>())
{
// 如果类是static,那么方法也必须是static
if (method.Modifiers.All(modifier => !modifier.IsKind(SyntaxKind.StaticKeyword)) && isClassStatic) continue;
if (method.Modifiers.All(modifier => !modifier.IsKind(SyntaxKind.StaticKeyword)) && isStaticClass) continue;
if (method.GetSpecifiedAttributes<CommandAttribute>(syntaxContext.SemanticModel, context.CancellationToken)
.FirstOrDefault() is not { } commandAttribute) continue;
yield return (method, commandAttribute);
Expand Down Expand Up @@ -131,10 +131,16 @@ symbol is not IMethodSymbol
$"new {converterType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}()");
}

var isStatic = true;
var isAsync = false;
var caseBuilder = new SourceStringBuilder(initialIndentCount: 3);
foreach (var (method, commandAttribute) in candidateMethods)
{
if (method.Modifiers.All(modifier => !modifier.IsKind(SyntaxKind.StaticKeyword)))
{
isStatic = false;
}

{
var fullName = commandAttribute[nameof(CommandAttribute.FullName)] as string ?? method.Identifier.ValueText;
caseBuilder.AppendLine($"case \"{(caseSensitive ? fullName : fullName.ToLower())}\":");
Expand Down Expand Up @@ -269,13 +275,13 @@ symbol is not IMethodSymbol

sourceBuilder.Append(
$$"""
{{classAccessModifier}} {{(isClassStatic ? "static" : string.Empty)}} partial class {{className}}
{{classAccessModifier}} {{(isStaticClass ? "static" : string.Empty)}} partial class {{className}}
{
private static {{(isAsync ? Global.ValueTask : "void")}} ExecuteInput{{(isAsync ? "Async" : string.Empty)}}({{Global.String}}? input)
private {{"static ".If(isStatic)}}{{(isAsync ? Global.ValueTask : "void")}} ExecuteInput{{"Async".If(isAsync)}}({{Global.String}}? input)
{
if ({{Global.String}}.IsNullOrEmpty(input))
{
return{{(isAsync ? " default" : string.Empty)}};
return{{" default".If(isAsync)}};
}
var arguments = new {{Global.GenericList}}<{{Global.String}}>();
Expand All @@ -286,10 +292,10 @@ symbol is not IMethodSymbol
arguments.Add(part);
}
{{(isAsync ? "return " : string.Empty)}}ExecuteArguments{{(isAsync ? "Async" : string.Empty)}}(arguments);
{{"return ".If(isAsync)}}ExecuteArguments{{"Async".If(isAsync)}}(arguments);
}
private static {{(isAsync ? $"async {Global.ValueTask}" : "void")}} ExecuteArguments{{(isAsync ? "Async" : string.Empty)}}({{Global.GenericIReadonlyList}}<{{Global.String}}> arguments)
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.0</Version>
<Version>1.2.1</Version>
<Company>Antelcat</Company>
<AssemblyVersion>1.2.0</AssemblyVersion>
<FileVersion>1.2.0</FileVersion>
<AssemblyVersion>1.2.1</AssemblyVersion>
<FileVersion>1.2.1</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 1e90a34

Please sign in to comment.