VS 2022 Analyzer Project Help Needed

50 Views Asked by At

I'm interested in building a custom code analyzer for my organization. Following the steps outlined in this article, I created an analyzer solution in Visual Studio 2022. Without altering the code generated by the default template, when I attempt to run the analyzer on a simple console app I created in the VSIX instance of Visual Studio, nothing happens. According to the article, any token containing lowercase letters is supposed to be decorated with wavy underlines but that's not happening.

This is the code for the diagnostic analyzer:

namespace TestAnalyzer
{
    [DiagnosticAnalyzer(LanguageNames.CSharp)]
    public class TestAnalyzerAnalyzer : DiagnosticAnalyzer
    {
        public const string DiagnosticId = "TestAnalyzer";

        // You can change these strings in the Resources.resx file. If you do not want your analyzer to be localize-able, you can use regular strings for Title and MessageFormat.
        // See https://github.com/dotnet/roslyn/blob/main/docs/analyzers/Localizing%20Analyzers.md for more on localization
        private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.AnalyzerTitle), Resources.ResourceManager, typeof(Resources));
        private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.AnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
        private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.AnalyzerDescription), Resources.ResourceManager, typeof(Resources));
        private const string Category = "Naming";

        private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);

        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }

        public override void Initialize(AnalysisContext context)
        {
            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
            context.EnableConcurrentExecution();

            // TODO: Consider registering other actions that act on syntax instead of or in addition to symbols
            // See https://github.com/dotnet/roslyn/blob/main/docs/analyzers/Analyzer%20Actions%20Semantics.md for more information
            context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.NamedType);
        }

        private static void AnalyzeSymbol(SymbolAnalysisContext context)
        {
            // TODO: Replace the following code with your own analysis, generating Diagnostic objects for any issues you find
            var namedTypeSymbol = (INamedTypeSymbol)context.Symbol;

            // Find just those named type symbols with names containing lowercase letters.
            if (namedTypeSymbol.Name.ToCharArray().Any(char.IsLower))
            {
                // For all such symbols, produce a diagnostic.
                var diagnostic = Diagnostic.Create(Rule, namedTypeSymbol.Locations[0], namedTypeSymbol.Name);

                context.ReportDiagnostic(diagnostic);
            }
        }
    }
}

This is the code for the VSIX console app:

namespace ConsoleCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            const String wordtocheck1 = "racecar";
            const String wordtocheck2 = "racetrack";

            Console.WriteLine($"{wordtocheck1} {(ispalindrome(wordtocheck1) ? "is" : "is not")} a palindrome");
            Console.WriteLine($"{wordtocheck2} {(ispalindrome(wordtocheck2) ? "is" : "is not")} a palindrome");

            Console.ReadLine();
        }
        protected static Boolean ispalindrome(String word)
        {
            for(Int32 i = 0; i < (word.Length / 2); i++)
            {
                if (word[i] != word[(word.Length - 1) - i])
                    return false;
            }

            return true;
        }
    }
}

Is there some additional configuration needed that isn't mentioned in the article?

0

There are 0 best solutions below