Roslyn Analyzer 未清除上次诊断
Roslyn Analyzer not clearing last diagnostic
我创建了一个 Roslyn 分析器,它根据一组规则验证文本文件,并注意到当我修复最后一个问题时,诊断并没有消失。我创建了一个简单示例来复制以下问题。
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class Analyzer1Analyzer : DiagnosticAnalyzer
{
public const string DiagnosticId = "TestCompilationAnalyzer";
private static readonly LocalizableString Title = "TestAnalyzer";
private static readonly LocalizableString MessageFormat = "This message should always appear {0}";
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, "Test", DiagnosticSeverity.Warning, isEnabledByDefault: true);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterAdditionalFileAction(AdditionalFileAction);
}
private void AdditionalFileAction(AdditionalFileAnalysisContext cxt)
{
var text = File.ReadAllText(cxt.AdditionalFile.Path);
if (text.Contains("help"))
{
cxt.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, $"help"));
}
if (text.Contains("test"))
{
cxt.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, $"test"));
}
}
}
如果我将 help 和 test 这两个词添加到我的附加文件中,两个诊断都会出现,但如果我删除两个,最后显示的任何内容都不会消失。
我找到了使用代码生成器的解决方法
[Generator]
public class SchemaTestAnalyzer : ISourceGenerator
{
public const string DiagnosticId = "TestCompilationAnalyzer";
private static readonly LocalizableString Title = "TestAnalyzer";
private static readonly LocalizableString MessageFormat = "This message should always appear {0}";
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, "Test", DiagnosticSeverity.Warning, isEnabledByDefault: true);
public void Execute(GeneratorExecutionContext context)
{
var additionalFile = context.AdditionalFiles.FirstOrDefault();
if (additionalFile != null)
{
var text = File.ReadAllText(additionalFile.Path);
if (text.Contains("help"))
{
context.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, "help"));
}
if (text.Contains("test"))
{
context.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, "test"));
}
}
}
public void Initialize(GeneratorInitializationContext context) { }
}
这些按预期工作,最后一个问题在解决和重新分析后消失
我创建了一个 Roslyn 分析器,它根据一组规则验证文本文件,并注意到当我修复最后一个问题时,诊断并没有消失。我创建了一个简单示例来复制以下问题。
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class Analyzer1Analyzer : DiagnosticAnalyzer
{
public const string DiagnosticId = "TestCompilationAnalyzer";
private static readonly LocalizableString Title = "TestAnalyzer";
private static readonly LocalizableString MessageFormat = "This message should always appear {0}";
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, "Test", DiagnosticSeverity.Warning, isEnabledByDefault: true);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterAdditionalFileAction(AdditionalFileAction);
}
private void AdditionalFileAction(AdditionalFileAnalysisContext cxt)
{
var text = File.ReadAllText(cxt.AdditionalFile.Path);
if (text.Contains("help"))
{
cxt.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, $"help"));
}
if (text.Contains("test"))
{
cxt.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, $"test"));
}
}
}
如果我将 help 和 test 这两个词添加到我的附加文件中,两个诊断都会出现,但如果我删除两个,最后显示的任何内容都不会消失。
我找到了使用代码生成器的解决方法
[Generator]
public class SchemaTestAnalyzer : ISourceGenerator
{
public const string DiagnosticId = "TestCompilationAnalyzer";
private static readonly LocalizableString Title = "TestAnalyzer";
private static readonly LocalizableString MessageFormat = "This message should always appear {0}";
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, "Test", DiagnosticSeverity.Warning, isEnabledByDefault: true);
public void Execute(GeneratorExecutionContext context)
{
var additionalFile = context.AdditionalFiles.FirstOrDefault();
if (additionalFile != null)
{
var text = File.ReadAllText(additionalFile.Path);
if (text.Contains("help"))
{
context.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, "help"));
}
if (text.Contains("test"))
{
context.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, "test"));
}
}
}
public void Initialize(GeneratorInitializationContext context) { }
}
这些按预期工作,最后一个问题在解决和重新分析后消失