ToB企服应用市场:ToB评测及商务社交产业平台
标题:
使用 `Roslyn` 分析器和修复器对.cs源代码添加头部解释
[打印本页]
作者:
丝
时间:
2024-9-7 14:48
标题:
使用 `Roslyn` 分析器和修复器对.cs源代码添加头部解释
之前写过
两篇关于Roslyn源天生器天生源代码的用例
,本日使用Roslyn的代码修复器CodeFixProvider实现一个cs文件头部解释的功能,
代码修复器会同时涉及到CodeFixProvider和DiagnosticAnalyzer,
实现FileHeaderAnalyzer
起首我们知道修复器的先决条件是分析器,好比这里,如果要对代码添加头部解释,那么分析器必须要给出对应的分析提醒:
我们起首实现实现名为FileHeaderAnalyzer的分析器:
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class FileHeaderAnalyzer : DiagnosticAnalyzer
{
public const string DiagnosticId = "GEN050";
private static readonly LocalizableString Title = "文件缺少头部信息";
private static readonly LocalizableString MessageFormat = "文件缺少头部信息";
private static readonly LocalizableString Description = "每个文件应包含头部信息.";
private const string Category = "Document";
private static readonly DiagnosticDescriptor Rule = new(
DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [Rule];
public override void Initialize(AnalysisContext context)
{
if (context is null)
return;
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterSyntaxTreeAction(AnalyzeSyntaxTree);
}
private static void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context)
{
var root = context.Tree.GetRoot(context.CancellationToken);
var firstToken = root.GetFirstToken();
// 检查文件是否以注释开头
var hasHeaderComment = firstToken.LeadingTrivia.Any(trivia => trivia.IsKind(SyntaxKind.SingleLineCommentTrivia) || trivia.IsKind(SyntaxKind.MultiLineCommentTrivia));
if (!hasHeaderComment)
{
var diagnostic = Diagnostic.Create(Rule, Location.Create(context.Tree, TextSpan.FromBounds(0, 0)));
context.ReportDiagnostic(diagnostic);
}
}
}
复制代码
FileHeaderAnalyzer分析器的原理很简单,需要重载几个方法,重点是Initialize方法,这里的RegisterSyntaxTreeAction即核心代码,SyntaxTreeAnalysisContext对象取到当前源代码的SyntaxNode根节点,然后判断TA的第一个SyntaxToken是否为解释行(SyntaxKind.SingleLineCommentTrivia|SyntaxKind.MultiLineCommentTrivia)
如果不为解释行,那么就通知分析器!
实现了上面的代码我们看一下效果:
并且编译的时候分析器将会在错误面板中显示告诫清单:
实现CodeFixProvider
分析器完成了,现在我们就来实现名为AddFileHeaderCodeFixProvider的修复器,
[code]/// /// 主动给文件添加头部解释/// [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AddFileHeaderCodeFixProvider))][Shared]public class AddFileHeaderCodeFixProvider : CodeFixProvider{ private const string Title = "添加文件头部信息"; //约定模板文件的名称 private const string ConfigFileName = "Biwen.AutoClassGen.Comment"; private const string VarPrefix = "$";//变量前缀 //如果模板不存在的时候的默认解释文本 private const string DefaultComment = """ // Licensed to the {Product} under one or more agreements. // The {Product} licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. """; #region regex private const RegexOptions ROptions = RegexOptions.Compiled | RegexOptions.Singleline; private static readonly Regex VersionRegex = new(@"(.*?)", ROptions); private static readonly Regex CopyrightRegex = new(@"(.*?)", ROptions); private static readonly Regex CompanyRegex = new(@"(.*?)", ROptions); private static readonly Regex DescriptionRegex = new(@"(.*?)", ROptions); private static readonly Regex AuthorsRegex = new(@"(.*?)", ROptions); private static readonly Regex ProductRegex = new(@"(.*?)", ROptions); private static readonly Regex TargetFrameworkRegex = new(@"(.*?)", ROptions); private static readonly Regex TargetFrameworksRegex = new(@"(.*?)", ROptions); private static readonly Regex ImportRegex = new(@"
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4