0

I've written an analyzer, and verified it was working.

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class ConstDiagnosticAnalyzer : DiagnosticAnalyzer
{
    public const string DiagnosticId = "LocalizationTool";

    private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.ConstAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
    private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.ConstAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
    private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.ConstAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
    private const string Category = "Naming";

    private static ResourceLocalizationRule localizationRule = new ResourceLocalizationRule();

    private static 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.RegisterSyntaxNodeAction(AnalyzeConstDeclaration, SyntaxKind.FieldDeclaration);
    }

    public static void AnalyzeConstDeclaration(SyntaxNodeAnalysisContext context)
    {
        var fieldDeclaration = (FieldDeclarationSyntax)context.Node;

        if (false == IsValidConstDeclaration(context, fieldDeclaration))
        {
            return;
        }

        var firstVariable = fieldDeclaration.Declaration.Variables.FirstOrDefault();
        var firstSymbol = context.SemanticModel.GetDeclaredSymbol(firstVariable);

        context.ReportDiagnostic(Diagnostic.Create(Rule, context.Node.GetLocation(), firstSymbol.Name));
    }

    private static bool IsValidConstDeclaration(SyntaxNodeAnalysisContext context, FieldDeclarationSyntax fieldDeclaration)
    {
        if (false == fieldDeclaration.Modifiers.Any(SyntaxKind.ConstKeyword))
        {
            return false;
        }

        return true;
    }
}

I've written a simple class which reports for analyzing const. This was underlining appropriately at one point. It's analyzer seems to be triggering because the resolution appears under Quick Actions, However the underlining is not working as seem in the image below

enter image description here

Adding Error Listenter image description here

As you can see there are warnings that have no messages. Why are there no messages when I'm clearly appending a message

johnny 5
  • 19,893
  • 50
  • 121
  • 195
  • Could you show your error window too? You might have overridden the severity to message (from warning) in a ruleset file, and therefore there's no underlining. – Tamas May 30 '17 at 21:18
  • What @Tamas said, and also, in your Visual Studio settings you can set the severity level that you want to be warned about. – Daniel Mendonça Jun 02 '17 at 12:36
  • You're right there is a warning appearing in the errors list it just doesn't have a message associated with it for some reason – johnny 5 Jun 02 '17 at 14:11

1 Answers1

0

It turns out this was cause by an overlooked warning. My resources were having a missing manifest exception.

So when they were trying to grab out the resource file descriptions etc, they were in-accessible. So the analyzer would trigger but it would have no messages.

I ended up using this solution to fix the missing manifest

johnny 5
  • 19,893
  • 50
  • 121
  • 195