Skip to content

Commit

Permalink
chore: docs for tools and minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Dec 19, 2023
1 parent 8c20bbc commit 6162f8d
Showing 1 changed file with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ namespace DisCatSharp.Analyzer
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class AttributeAnalyzer : DiagnosticAnalyzer
{
/// <summary>
/// The diagnostic ID prefix.
/// </summary>
public const string DIAGNOSTIC_ID_PREFIX = "DCS";

/// <summary>
/// The diagnostic category.
/// </summary>
public const string CATEGORY = "Usage";

private static readonly LocalizableString s_titleExperimental = new LocalizableResourceString(nameof(Resources.AnalyzerTitleExperimental), Resources.ResourceManager, typeof(Resources));
Expand Down Expand Up @@ -89,9 +96,16 @@ public class AttributeAnalyzer : DiagnosticAnalyzer
private static readonly DiagnosticDescriptor s_requiresFeatureRule = new DiagnosticDescriptor(DIAGNOSTIC_ID_PREFIX + "0200", s_titleRequiresFeature, s_messageFormatRequiresFeature,
CATEGORY, DiagnosticSeverity.Warning, true, s_descriptionRequiresFeature, "https://docs.dcs.aitsys.dev/vs/analyzer/dcs/0200");

/// <summary>
/// Returns a set of descriptors for the diagnostics that this analyzer is capable of producing.
/// </summary>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
=> ImmutableArray.Create(s_experimentalRule, s_deprecatedRule, s_discordInExperimentRule, s_discordDeprecatedRule, s_discordUnreleasedRule, s_requiresFeatureRule);

/// <summary>
/// Called once at session start to register actions in the analysis context.
/// </summary>
/// <param name="context">The analysis context.</param>
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
Expand All @@ -109,6 +123,10 @@ public override void Initialize(AnalysisContext context)
context.RegisterSyntaxNodeAction(StatusAnalyzer, SyntaxKind.SimpleMemberAccessExpression);
}

/// <summary>
/// Analyzes the status of various components.
/// </summary>
/// <param name="context">The syntac node analysis context.</param>
private static void StatusAnalyzer(SyntaxNodeAnalysisContext context)
{
var invocation = context.Node;
Expand Down Expand Up @@ -175,18 +193,26 @@ private static void StatusAnalyzer(SyntaxNodeAnalysisContext context)
return;
}

/// <summary>
/// Analyzes the experimental state of various components.
/// </summary>
/// <param name="context">The symbol analysis context.</param>
private static void ExperimentalAnalyzer(SymbolAnalysisContext context)
{
Console.WriteLine(new LocalizableResourceString(nameof(Resources.Handling), Resources.ResourceManager, typeof(Resources)) + context.Symbol.Kind.ToString());
var syntaxTrees = from x in context.Symbol.Locations
where x.IsInSource
select x.SourceTree;
var declaration = context.Symbol;

// ReSharper disable HeuristicUnreachableCode
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
if (null == declaration)
{
Console.WriteLine(new LocalizableResourceString(nameof(Resources.Faulty), Resources.ResourceManager, typeof(Resources)));
return;
}
// ReSharper enable HeuristicUnreachableCode

var attributes = declaration.GetAttributes();

Expand Down Expand Up @@ -248,6 +274,13 @@ where x.IsInSource
return;
}

/// <summary>
/// Checks if the attribute is the desired attribute.
/// </summary>
/// <param name="semanticModel">The current semantic model.</param>
/// <param name="attribute">>The current attribute data.</param>
/// <param name="desiredAttributeType">The target attribute type to check for.</param>
/// <returns>Whether the <paramref name="attribute"/> contains the <paramref name="desiredAttributeType"/>.</returns>
private static bool IsRequiredAttribute(SemanticModel semanticModel, AttributeData attribute, Type desiredAttributeType)
{
if (desiredAttributeType.FullName is null)
Expand All @@ -259,25 +292,47 @@ private static bool IsRequiredAttribute(SemanticModel semanticModel, AttributeDa
return result;
}

/// <summary>
/// Gets the message from the attribute.
/// </summary>
/// <param name="attribute">The current attribute data.</param>
/// <returns>The message.</returns>
private static string GetMessage(AttributeData attribute)
=> attribute.ConstructorArguments.Length < 1
? "Do not use in production."
: attribute.ConstructorArguments[0].Value as string;

/// <summary>
/// Gets the feature message from the attribute.
/// </summary>
/// <param name="attribute">The current attribute data.</param>
/// <returns>The message.</returns>
private static string GetFeatureMessage(AttributeData attribute)
{
var featureReqEnum = (Features)attribute.ConstructorArguments[0].Value;
if (attribute is null)
return string.Empty;

var featureReqEnum = (Features)attribute.ConstructorArguments[0].Value!;
var description = attribute.ConstructorArguments.Length > 1
? attribute.ConstructorArguments[1].Value as string
: "No additional information.";
return $"{featureReqEnum.ToFeaturesString()} | {description}";
}
}

/// <summary>
/// Represents a various helper methods.
/// </summary>
internal static class Helpers
{
/// <summary>
/// Gets the feature strings.
/// </summary>
internal static Dictionary<Features, string> FeaturesStrings { get; set; }

/// <summary>
/// Initializes the <see cref="Helpers"/> class.
/// </summary>
static Helpers()
{
FeaturesStrings = new Dictionary<Features, string>();
Expand All @@ -289,12 +344,17 @@ static Helpers()
{
var xsv = xv.ToString();
var xmv = ti.DeclaredMembers.FirstOrDefault(xm => xm.Name == xsv);
var xav = xmv.GetCustomAttribute<FeatureDescriptionAttribute>();
var xav = xmv?.GetCustomAttribute<FeatureDescriptionAttribute>();

FeaturesStrings[xv] = xav.Description;
FeaturesStrings[xv] = xav?.Description ?? "No description given.";
}
}

/// <summary>
/// Converts a feature enum to a string.
/// </summary>
/// <param name="features">The feature enum to convert.</param>
/// <returns>The string representation of the feature enum.</returns>
public static string ToFeaturesString(this Features features)
{
var strs = FeaturesStrings
Expand Down

0 comments on commit 6162f8d

Please sign in to comment.