Added support for ReplyKeyboardMarkup generation

This commit is contained in:
2025-08-19 01:05:45 +04:00
parent 065430a52f
commit 707f4222ff
8 changed files with 85 additions and 20 deletions
@@ -9,9 +9,11 @@ namespace Telegrator.Analyzers
[Generator(LanguageNames.CSharp)] [Generator(LanguageNames.CSharp)]
public class GeneratedKeyboardMarkupGenerator : IIncrementalGenerator public class GeneratedKeyboardMarkupGenerator : IIncrementalGenerator
{ {
// Return types
private const string InlineReturnType = "InlineKeyboardMarkup"; private const string InlineReturnType = "InlineKeyboardMarkup";
private const string ReplyReturnType = "ReplyKeyboardMarkup"; private const string ReplyReturnType = "ReplyKeyboardMarkup";
// Attribute names
private const string CallbackDataAttribute = "CallbackButton"; private const string CallbackDataAttribute = "CallbackButton";
private const string CallbackGameAttribute = "GameButton"; private const string CallbackGameAttribute = "GameButton";
private const string CopyTextAttribute = "CopyTextButton"; private const string CopyTextAttribute = "CopyTextButton";
@@ -21,12 +23,21 @@ namespace Telegrator.Analyzers
private const string QueryChosenAttribute = "QueryChosenButton"; private const string QueryChosenAttribute = "QueryChosenButton";
private const string QueryCurrentAttribute = "QueryCurrentButton"; private const string QueryCurrentAttribute = "QueryCurrentButton";
private const string UrlRedirectAttribute = "UrlRedirectButton"; private const string UrlRedirectAttribute = "UrlRedirectButton";
private const string RequestChatAttribute = "RequestChatButton";
private const string RequestContactAttribute = "RequestContactButton";
private const string RequestLocationAttribute = "RequestLocationButton";
private const string RequestPoolAttribute = "RequestPoolButton";
private const string RequestUsersAttribute = "RequestUsersButton";
private const string WebAppAttribute = "WebApp"; private const string WebAppAttribute = "WebApp";
// Markup lists
private static readonly string[] InlineAttributes = [CallbackDataAttribute, CallbackGameAttribute, CopyTextAttribute, LoginRequestAttribute, PayRequestAttribute, UrlRedirectAttribute, WebAppAttribute, SwitchQueryAttribute, QueryChosenAttribute, QueryCurrentAttribute]; private static readonly string[] InlineAttributes = [CallbackDataAttribute, CallbackGameAttribute, CopyTextAttribute, LoginRequestAttribute, PayRequestAttribute, UrlRedirectAttribute, WebAppAttribute, SwitchQueryAttribute, QueryChosenAttribute, QueryCurrentAttribute];
private static readonly string[] ReplyAttributes = []; private static readonly string[] ReplyAttributes = [RequestChatAttribute, RequestContactAttribute, RequestLocationAttribute, RequestPoolAttribute, RequestUsersAttribute, WebAppAttribute];
// Usings
private static readonly string[] DefaultUsings = ["Telegram.Bot.Types.ReplyMarkups"]; private static readonly string[] DefaultUsings = ["Telegram.Bot.Types.ReplyMarkups"];
// Markup layouts
private static readonly Dictionary<string, MemberAccessExpressionSyntax> InlineKeyboardLayout = new Dictionary<string, MemberAccessExpressionSyntax>() private static readonly Dictionary<string, MemberAccessExpressionSyntax> InlineKeyboardLayout = new Dictionary<string, MemberAccessExpressionSyntax>()
{ {
{ CallbackDataAttribute, AccessExpression("InlineKeyboardButton", "WithCallbackData") }, { CallbackDataAttribute, AccessExpression("InlineKeyboardButton", "WithCallbackData") },
@@ -43,21 +54,29 @@ namespace Telegrator.Analyzers
private static readonly Dictionary<string, MemberAccessExpressionSyntax> ReplyKeyboardLayout = new Dictionary<string, MemberAccessExpressionSyntax>() private static readonly Dictionary<string, MemberAccessExpressionSyntax> ReplyKeyboardLayout = new Dictionary<string, MemberAccessExpressionSyntax>()
{ {
{ RequestChatAttribute, AccessExpression("KeyboardButton", "WithRequestChat") },
{ RequestContactAttribute, AccessExpression("KeyboardButton", "WithRequestContact") },
{ RequestLocationAttribute, AccessExpression("KeyboardButton", "WithRequestLocation") },
{ RequestPoolAttribute, AccessExpression("KeyboardButton", "WithRequestPoll") },
{ RequestUsersAttribute, AccessExpression("KeyboardButton", "WithRequestUsers") },
{ WebAppAttribute, AccessExpression("KeyboardButton", "WithWebApp") }
}; };
// Markup map
private static readonly Dictionary<string, Dictionary<string, MemberAccessExpressionSyntax>> LayoutNames = new Dictionary<string, Dictionary<string, MemberAccessExpressionSyntax>>() private static readonly Dictionary<string, Dictionary<string, MemberAccessExpressionSyntax>> LayoutNames = new Dictionary<string, Dictionary<string, MemberAccessExpressionSyntax>>()
{ {
{ InlineReturnType, InlineKeyboardLayout }, { InlineReturnType, InlineKeyboardLayout },
{ ReplyReturnType, ReplyKeyboardLayout } { ReplyReturnType, ReplyKeyboardLayout }
}; };
// Diagnostic descriptors
private static readonly DiagnosticDescriptor WrongReturnType = new DiagnosticDescriptor("TG_1001", "Wrong return type", string.Empty, "Modelling", DiagnosticSeverity.Error, true); private static readonly DiagnosticDescriptor WrongReturnType = new DiagnosticDescriptor("TG_1001", "Wrong return type", string.Empty, "Modelling", DiagnosticSeverity.Error, true);
private static readonly DiagnosticDescriptor UnsupportedAttribute = new DiagnosticDescriptor("TG_1002", "Unsupported attribute", string.Empty, "Modelling", DiagnosticSeverity.Error, true); private static readonly DiagnosticDescriptor UnsupportedAttribute = new DiagnosticDescriptor("TG_1002", "Unsupported or invalid attribute", string.Empty, "Modelling", DiagnosticSeverity.Error, true);
private static readonly DiagnosticDescriptor NotPartialMethod = new DiagnosticDescriptor("TG_1003", "Not a partial method", string.Empty, "Modelling", DiagnosticSeverity.Error, true); private static readonly DiagnosticDescriptor NotPartialMethod = new DiagnosticDescriptor("TG_1003", "Not a partial method", string.Empty, "Modelling", DiagnosticSeverity.Error, true);
private static readonly DiagnosticDescriptor UseBodylessMethod = new DiagnosticDescriptor("TG_1004", "Use bodyless method", string.Empty, "Modelling", DiagnosticSeverity.Error, true); private static readonly DiagnosticDescriptor UseBodylessMethod = new DiagnosticDescriptor("TG_1004", "Use bodyless method", string.Empty, "Modelling", DiagnosticSeverity.Error, true);
private static readonly DiagnosticDescriptor UseParametrlessMethod = new DiagnosticDescriptor("TG_1005", "Use parametrless method", string.Empty, "Modelling", DiagnosticSeverity.Error, true); private static readonly DiagnosticDescriptor UseParametrlessMethod = new DiagnosticDescriptor("TG_1005", "Use parametrless method", string.Empty, "Modelling", DiagnosticSeverity.Error, true);
// Trivias
private static SyntaxTrivia TabulationTrivia => SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, "\t"); private static SyntaxTrivia TabulationTrivia => SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, "\t");
private static SyntaxTrivia WhitespaceTrivia => SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, " "); private static SyntaxTrivia WhitespaceTrivia => SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, " ");
private static SyntaxTrivia NewLineTrivia => SyntaxFactory.SyntaxTrivia(SyntaxKind.EndOfLineTrivia, "\n"); private static SyntaxTrivia NewLineTrivia => SyntaxFactory.SyntaxTrivia(SyntaxKind.EndOfLineTrivia, "\n");
@@ -15,7 +15,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<PackageLicenseFile>LICENSE</PackageLicenseFile> <PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile> <PackageReadmeFile>README.md</PackageReadmeFile>
<Version>1.15.0</Version> <Version>1.15.1</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
+1 -1
View File
@@ -16,7 +16,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<PackageLicenseFile>LICENSE</PackageLicenseFile> <PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile> <PackageReadmeFile>README.md</PackageReadmeFile>
<Version>1.15.0</Version> <Version>1.15.1</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
-8
View File
@@ -20,8 +20,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Telegrator.Hosting.Web", "T
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Telegrator.RoslynGenerators", "dev\Telegrator.RoslynGenerators\Telegrator.RoslynGenerators.csproj", "{93658B7F-C651-4C78-2CB1-2C0AE00C45B5}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Telegrator.RoslynGenerators", "dev\Telegrator.RoslynGenerators\Telegrator.RoslynGenerators.csproj", "{93658B7F-C651-4C78-2CB1-2C0AE00C45B5}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SosalBot", "..\SosalBot\SosalBot\SosalBot.csproj", "{D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
AnalyzersDebug|Any CPU = AnalyzersDebug|Any CPU AnalyzersDebug|Any CPU = AnalyzersDebug|Any CPU
@@ -65,12 +63,6 @@ Global
{93658B7F-C651-4C78-2CB1-2C0AE00C45B5}.Debug|Any CPU.Build.0 = Debug|Any CPU {93658B7F-C651-4C78-2CB1-2C0AE00C45B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{93658B7F-C651-4C78-2CB1-2C0AE00C45B5}.Release|Any CPU.ActiveCfg = Release|Any CPU {93658B7F-C651-4C78-2CB1-2C0AE00C45B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{93658B7F-C651-4C78-2CB1-2C0AE00C45B5}.Release|Any CPU.Build.0 = Release|Any CPU {93658B7F-C651-4C78-2CB1-2C0AE00C45B5}.Release|Any CPU.Build.0 = Release|Any CPU
{D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}.AnalyzersDebug|Any CPU.ActiveCfg = Release|Any CPU
{D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}.AnalyzersDebug|Any CPU.Build.0 = Release|Any CPU
{D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@@ -1,6 +0,0 @@
namespace Telegrator.Markups
{
internal class GeneratedReplyKeybooardMarkupAttributes
{
}
}
+20
View File
@@ -0,0 +1,20 @@
using System.Reflection;
using Telegram.Bot.Types.ReplyMarkups;
namespace Telegrator.Markups
{
/*
internal static class KeyboardMapper
{
public static InlineKeyboardMarkup MapInline(MemberInfo member)
{
}
public static ReplyKeyboardMarkup MapReply()
{
}
}
*/
}
@@ -132,4 +132,44 @@ namespace Telegrator.Markups
/// </summary> /// </summary>
public string Query { get; } = switchInlineQueryCurrentChat; public string Query { get; } = switchInlineQueryCurrentChat;
} }
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class RequestChatButtonAttribute(string name, int requestId, bool chatIsChannel) : Attribute
{
public string Name { get; } = name;
public int RequestId { get; } = requestId;
public bool ChatIsChannel { get; } = chatIsChannel;
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class RequestContactButtonAttribute(string name) : Attribute
{
public string Name { get; } = name;
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class RequestLocationButtonAttribute(string name) : Attribute
{
public string Name { get; } = name;
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class RequestPoolButtonAttribute(string name, KeyboardButtonPollType requestPoll) : Attribute
{
public string Name { get; } = name;
public KeyboardButtonPollType PollType { get; } = requestPoll;
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class RequestUsersButtonAttribute(string name, int requestId, int? maxQuantity = null) : Attribute
{
public string Name { get; } = name;
public int RequestId { get; } = requestId;
public int? MaxQuantity { get; } = maxQuantity;
}
} }
+1 -1
View File
@@ -17,7 +17,7 @@
<PackageTags>telegram;bot;mediator;attributes;aspect;hosting;host;framework;easy;simple;handlers</PackageTags> <PackageTags>telegram;bot;mediator;attributes;aspect;hosting;host;framework;easy;simple;handlers</PackageTags>
<PackageReadmeFile>README.md</PackageReadmeFile> <PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE</PackageLicenseFile> <PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>1.15.0</Version> <Version>1.15.1</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>