From 065430a52f50d7741811572e3a574a6bac782591 Mon Sep 17 00:00:00 2001 From: Rikitav Date: Mon, 18 Aug 2025 21:15:34 +0400 Subject: [PATCH] Code cleanup --- .../DeveloperHelperAnalyzer.cs | 4 +- .../GeneratedKeyboardMarkupGenerator.cs | 5 - Telegrator.Hosting.Web/README.md | 1 - .../ApiMarkdownGenerator.cs | 9 +- ...plicitHandlerBuilderExtensionsGenerator.cs | 134 ------------------ 5 files changed, 5 insertions(+), 148 deletions(-) diff --git a/Telegrator.Analyzers/DeveloperHelperAnalyzer.cs b/Telegrator.Analyzers/DeveloperHelperAnalyzer.cs index 0e12cbc..f9cfbc0 100644 --- a/Telegrator.Analyzers/DeveloperHelperAnalyzer.cs +++ b/Telegrator.Analyzers/DeveloperHelperAnalyzer.cs @@ -30,7 +30,7 @@ namespace Telegrator.Analyzers return true; } - + private static HandlerDeclarationModel Transform(GeneratorSyntaxContext context, CancellationToken cancellationToken) { ClassDeclarationSyntax classSyntax = (ClassDeclarationSyntax)context.Node; @@ -72,7 +72,7 @@ namespace Telegrator.Analyzers sourceBuilder.AppendLine("\t}\n}"); sourceBuilder.Insert(0, string.Join("\n", usingDirectives.OrderBy(use => use)) + "\n\n"); - //context.AddSource("DeveloperHelperAnalyzer.cs", sourceBuilder.ToString()); + context.AddSource("DeveloperHelperAnalyzer.cs", sourceBuilder.ToString()); } private static void ParseHandlerDeclaration(SourceProductionContext context, StringBuilder sourceBuilder, HandlerDeclarationModel handler, CancellationToken cancellationToken) diff --git a/Telegrator.Analyzers/GeneratedKeyboardMarkupGenerator.cs b/Telegrator.Analyzers/GeneratedKeyboardMarkupGenerator.cs index 7b96d3e..d7cf91a 100644 --- a/Telegrator.Analyzers/GeneratedKeyboardMarkupGenerator.cs +++ b/Telegrator.Analyzers/GeneratedKeyboardMarkupGenerator.cs @@ -4,11 +4,6 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using System.Collections.Immutable; using Telegrator.Analyzers.RoslynExtensions; - -#if DEBUG -using System.Diagnostics; -#endif - namespace Telegrator.Analyzers { [Generator(LanguageNames.CSharp)] diff --git a/Telegrator.Hosting.Web/README.md b/Telegrator.Hosting.Web/README.md index 1a18ae7..94be16d 100644 --- a/Telegrator.Hosting.Web/README.md +++ b/Telegrator.Hosting.Web/README.md @@ -45,7 +45,6 @@ TelegramBotWebHostBuilder builder = TelegramBotWebHost.CreateBuilder(new Telegra // Register handlers builder.Handlers.CollectHandlersAssemblyWide(); -builder.Services.AddHandlersFromAssembly(typeof(Program).Assembly); // Register your services builder.Services.AddSingleton(); diff --git a/dev/Telegrator.RoslynGenerators/ApiMarkdownGenerator.cs b/dev/Telegrator.RoslynGenerators/ApiMarkdownGenerator.cs index 039d4b8..bf36eb5 100644 --- a/dev/Telegrator.RoslynGenerators/ApiMarkdownGenerator.cs +++ b/dev/Telegrator.RoslynGenerators/ApiMarkdownGenerator.cs @@ -5,6 +5,7 @@ using System.Text; using System.Xml.Linq; using Telegrator.RoslynExtensions; +#if DEBUG namespace Telegrator.RoslynGenerators { /// @@ -15,11 +16,6 @@ namespace Telegrator.RoslynGenerators { public void Initialize(IncrementalGeneratorInitializationContext context) { -#if RELEASE - // DEBUG ONLY GENERATOR - return; -#endif - IncrementalValueProvider> typeDeclarations = context.SyntaxProvider .CreateSyntaxProvider( predicate: (node, _) => node is ClassDeclarationSyntax || node is InterfaceDeclarationSyntax || node is StructDeclarationSyntax || node is EnumDeclarationSyntax, @@ -285,4 +281,5 @@ namespace Telegrator.RoslynGenerators return type.Name; } } -} \ No newline at end of file +} +#endif diff --git a/dev/Telegrator.RoslynGenerators/ImplicitHandlerBuilderExtensionsGenerator.cs b/dev/Telegrator.RoslynGenerators/ImplicitHandlerBuilderExtensionsGenerator.cs index 1f85be5..a910eb5 100644 --- a/dev/Telegrator.RoslynGenerators/ImplicitHandlerBuilderExtensionsGenerator.cs +++ b/dev/Telegrator.RoslynGenerators/ImplicitHandlerBuilderExtensionsGenerator.cs @@ -25,15 +25,11 @@ namespace Telegrator.RoslynGenerators private static readonly MemberAccessExpressionSyntax BuilderAdderMethodAccessExpression = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("builder"), SyntaxFactory.IdentifierName("AddTargetedFilters")); private static readonly IEqualityComparer UsingEqualityComparer = new UsingDirectiveEqualityComparer(); - private static SyntaxTrivia TabulationTrivia => SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, "\t"); private static SyntaxTrivia WhitespaceTrivia => SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, " "); private static SyntaxTrivia NewLineTrivia => SyntaxFactory.SyntaxTrivia(SyntaxKind.EndOfLineTrivia, "\n"); public void Initialize(IncrementalGeneratorInitializationContext context) { -#if DEBUG - Debugger.Launch(); -#endif IncrementalValueProvider> pipeline = context.SyntaxProvider .CreateSyntaxProvider(SyntaxPredicate, SyntaxTransform) .Where(declaration => declaration != null) @@ -68,24 +64,6 @@ namespace Telegrator.RoslynGenerators StringBuilder debugExport = new StringBuilder("/*"); List usings = ParseUsings(DefaultUsings).ToList(); - /* - Dictionary targeters = []; - List usingDirectives = - [ - "using Telegrator.Handlers.Building;", - "using Telegrator.Handlers.Building.Components;" - ]; - */ - - /* - StringBuilder sourceBuilder = new StringBuilder() - .AppendLine("namespace Telegrator") - .AppendLine("{") - .Append("\t//").Append(string.Join(", ", declarations.Select(decl => decl.Identifier.ToString()))).AppendLine() - .AppendLine("\tpublic static partial class HandlerBuilderExtensions") - .AppendLine("\t{"); - */ - Dictionary targetters = []; foreach (ClassDeclarationSyntax classDeclaration in declarations) { @@ -303,117 +281,5 @@ namespace Telegrator.RoslynGenerators return obj.GetHashCode(); } } - - /* - private static void ParseClassDeclaration(StringBuilder sourceBuilder, ClassDeclarationSyntax classDeclaration, Dictionary targeters) - { - string className = classDeclaration.Identifier.ToString(); - if (className == "FilterAnnotation") - return; - - IEnumerable methods = classDeclaration.Members.OfType(); - MethodDeclarationSyntax? targeterMethod = methods.FirstOrDefault(method => method.Identifier.ToString() == "GetFilterringTarget"); - - string filterName = className.Replace("Attribute", string.Empty); - string classTargetterMethodName = filterName + "_GetFilterringTarget"; - - if (targeterMethod != null) - { - targeters.Add(className, classTargetterMethodName); - RenderTargeterMethod(sourceBuilder, classTargetterMethodName, targeterMethod); - sourceBuilder.AppendLine(); - } - else - { - if (classDeclaration.BaseList == null) - throw new Exception(); - - string baseClassName = classDeclaration.BaseList.Types - .ElementAt(0).GetBaseTypeSyntaxName(); - - if (!targeters.ContainsKey(baseClassName)) - throw new TargteterNotFoundException(); - - classTargetterMethodName = targeters[baseClassName]; - } - - if (classDeclaration.Modifiers.HasModifiers("abstract")) - return; - - if (classDeclaration.ParameterList != null) - { - if (classDeclaration.BaseList != null) - { - PrimaryConstructorBaseTypeSyntax primaryConstructor = (PrimaryConstructorBaseTypeSyntax)classDeclaration.BaseList.Types.ElementAt(0); - RenderExtensionMethod(sourceBuilder, filterName, classTargetterMethodName, classDeclaration.ParameterList.Parameters, primaryConstructor.ArgumentList.Arguments); - } - else - { - RenderExtensionMethod(sourceBuilder, filterName, classTargetterMethodName, classDeclaration.ParameterList.Parameters, []); - } - - sourceBuilder.AppendLine(); - } - - foreach (ConstructorDeclarationSyntax constructor in classDeclaration.Members.OfType()) - { - if (constructor.Initializer == null) - continue; - - RenderExtensionMethod(sourceBuilder, filterName, classTargetterMethodName, constructor.ParameterList.Parameters, constructor.Initializer.ArgumentList.Arguments); - sourceBuilder.AppendLine(); - } - } - */ - - /* - private static void RenderExtensionMethod(StringBuilder sourceBuilder, string filterName, string classTargetterMethodName, SeparatedSyntaxList parameters, SeparatedSyntaxList arguments) - { - if (filterName == "ChatType") - filterName = "InChatType"; // Because it conflicting - - sourceBuilder - .Append("\t\t/// ").AppendLine() - .Append("\t\t/// Adds ").Append(filterName).Append(" filter to implicit handler").AppendLine() - .Append("\t\t/// ").AppendLine(); - - sourceBuilder.Append("\t\tpublic static TBuilder ").Append(filterName).Append("(this TBuilder builder"); - - if (parameters.Any()) - sourceBuilder.Append(", ").Append(parameters.ToFullString()); - - sourceBuilder - .Append(") where TBuilder : IHandlerBuilder").AppendLine() - .Append("\t\t{").AppendLine() - .Append("\t\t\tbuilder.AddTargetedFilter"); - - if (arguments.Count > 1) - sourceBuilder.Append("s"); - - sourceBuilder.Append("(").Append(classTargetterMethodName); - - if (arguments.Any()) - sourceBuilder.Append(", ").Append(arguments.ToFullString()); - - sourceBuilder - .Append(");").AppendLine() - .Append("\t\t\treturn builder;").AppendLine() - .Append("\t\t}").AppendLine(); - } - - private static void RenderTargeterMethod(StringBuilder sourceBuilder, string classTargetterMethodName, MethodDeclarationSyntax targeterMethod) - { - sourceBuilder.Append("\t\tprivate static ").Append(targeterMethod.ReturnType.ToString()).Append(" ").Append(classTargetterMethodName).Append(targeterMethod.ParameterList.ToFullString()); - - if (targeterMethod.ExpressionBody != null) - { - sourceBuilder.Append(targeterMethod.ExpressionBody.ToFullString()).Append(";").AppendLine(); - } - else if (targeterMethod.Body != null) - { - sourceBuilder.Append(targeterMethod.Body.ToFullString()); - } - } - */ } }