XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension.NetCore.TodoHighLight

【DLL】自动检测TODO注释并高亮

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension.NetCore.TodoHighLight

添加项目文件。

73673a0
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

4 个文件 +143 -0
Added XFEExtension.NetCore.TodoHighLight.Analyzer/Diagnostics/TodoCommentAnalyzerDiagnostics.cs +81 -0
@@ -0,0 +1,81 @@
1 using Microsoft.CodeAnalysis;
2 using Microsoft.CodeAnalysis.CSharp;
3 using Microsoft.CodeAnalysis.Diagnostics;
4 using System.Collections.Immutable;
5 using System.Linq;
6 using System.Text.RegularExpressions;
7
8 namespace XFEExtension.NetCore.TodoHighLight.Analyzer.Diagnostics
9 {
10 [DiagnosticAnalyzer(LanguageNames.CSharp)]
11 public class TodoCommentAnalyzerDiagnostics : DiagnosticAnalyzer
12 {
13 public const string TodoCommentId = "TODO";
14
15 public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
16 {
17 get
18 {
19 var descriptor = new DiagnosticDescriptor(TodoCommentId,
20 "TODO��������",
21 "��������{0}",
22 "XFEExtension.NetCore.Analyzer.Diagnostics",
23 DiagnosticSeverity.Warning,
24 true,
25 "TODO�Ĵ�������.",
26 "https://www.xfegzs.com/codespace/diagnostics/TODO.html");
27 return ImmutableArray.Create(descriptor);
28 }
29 }
30
31 public override void Initialize(AnalysisContext context)
32 {
33 context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
34 context.EnableConcurrentExecution();
35 context.RegisterSyntaxTreeAction(AnalyzeSyntaxTree);
36 }
37
38 private void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context)
39 {
40 var root = context.Tree.GetRoot(context.CancellationToken);
41 var todoComments = root.DescendantTrivia().Where(trivia => trivia.IsKind(SyntaxKind.SingleLineCommentTrivia) && trivia.ToString().Contains("TODO:"));
42 foreach (var todoComment in todoComments)
43 {
44 var match = Regex.Match(todoComment.ToString(), @"//TODO:\s*(\d)?\s*(.*)");
45 if (match.Success)
46 {
47 var level = match.Groups[1].Value != "" ? int.Parse(match.Groups[1].Value) : 2;
48 var task = match.Groups[2].Value;
49
50 var descriptor = new DiagnosticDescriptor(TodoCommentId,
51 "TODO��������",
52 "��������{0}",
53 "XFEExtension.NetCore.Analyzer.Diagnostics",
54 GetSeverityFromInt(level),
55 true,
56 "TODO�Ĵ�������.",
57 "https://www.xfegzs.com/codespace/diagnostics/TODO.html");
58 var diagnostic = Diagnostic.Create(descriptor, todoComment.GetLocation(), task);
59 context.ReportDiagnostic(diagnostic);
60 }
61 }
62 }
63
64 private static DiagnosticSeverity GetSeverityFromInt(int level)
65 {
66 switch (level)
67 {
68 case 0:
69 return DiagnosticSeverity.Hidden;
70 case 1:
71 return DiagnosticSeverity.Info;
72 case 2:
73 return DiagnosticSeverity.Warning;
74 case 3:
75 return DiagnosticSeverity.Error;
76 default:
77 return DiagnosticSeverity.Warning;
78 }
79 }
80 }
81 }
Added XFEExtension.NetCore.TodoHighLight.Analyzer/XFEExtension.NetCore.TodoHighLight.Analyzer.csproj +20 -0
@@ -0,0 +1,20 @@
1 <Project Sdk="Microsoft.NET.Sdk">
2
3 <PropertyGroup>
4 <TargetFramework>netstandard2.0</TargetFramework>
5 </PropertyGroup>
6
7 <PropertyGroup>
8 <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
9 <!-- Generates a package at build -->
10 <IncludeBuildOutput>false</IncludeBuildOutput>
11 <!-- Do not include the generator as a lib dependency -->
12 </PropertyGroup>
13
14 <ItemGroup>
15 <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
16 <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" PrivateAssets="all" />
17 <PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.9.2" />
18 </ItemGroup>
19
20 </Project>
Added XFEExtension.NetCore.TodoHighLight.sln +30 -0
@@ -0,0 +1,30 @@
1 Microsoft Visual Studio Solution File, Format Version 12.00
2 # Visual Studio Version 17
3 VisualStudioVersion = 17.11.34929.205
4 MinimumVisualStudioVersion = 10.0.40219.1
5 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XFEExtension.NetCore.TodoHighLight", "XFEExtension.NetCore.TodoHighLight\XFEExtension.NetCore.TodoHighLight.csproj", "{8321B4B8-8DCE-4FBB-AB30-C607AF237FE8}"
6 EndProject
7 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XFEExtension.NetCore.TodoHighLight.Analyzer", "XFEExtension.NetCore.TodoHighLight.Analyzer\XFEExtension.NetCore.TodoHighLight.Analyzer.csproj", "{13C9A94D-3294-4792-BB30-0B400E342432}"
8 EndProject
9 Global
10 GlobalSection(ExtensibilityGlobals) = postSolution
11 SolutionGuid = {397CFC0A-2F50-4922-B902-F170C1801A37}
12 EndGlobalSection
13 GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 {13C9A94D-3294-4792-BB30-0B400E342432}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 {13C9A94D-3294-4792-BB30-0B400E342432}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 {13C9A94D-3294-4792-BB30-0B400E342432}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 {13C9A94D-3294-4792-BB30-0B400E342432}.Release|Any CPU.Build.0 = Release|Any CPU
18 {8321B4B8-8DCE-4FBB-AB30-C607AF237FE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19 {8321B4B8-8DCE-4FBB-AB30-C607AF237FE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
20 {8321B4B8-8DCE-4FBB-AB30-C607AF237FE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
21 {8321B4B8-8DCE-4FBB-AB30-C607AF237FE8}.Release|Any CPU.Build.0 = Release|Any CPU
22 EndGlobalSection
23 GlobalSection(SolutionConfigurationPlatforms) = preSolution
24 Debug|Any CPU = Debug|Any CPU
25 Release|Any CPU = Release|Any CPU
26 EndGlobalSection
27 GlobalSection(SolutionProperties) = preSolution
28 HideSolutionNode = FALSE
29 EndGlobalSection
30 EndGlobal
Added XFEExtension.NetCore.TodoHighLight/XFEExtension.NetCore.TodoHighLight.csproj +12 -0
@@ -0,0 +1,12 @@
1 <Project Sdk="Microsoft.NET.Sdk">
2
3 <PropertyGroup>
4 <TargetFramework>net8.0</TargetFramework>
5 <ImplicitUsings>enable</ImplicitUsings>
6 <Nullable>enable</Nullable>
7 </PropertyGroup>
8
9 <ItemGroup>
10 <None Include="..\XFEExtension.NetCore.TodoHighLight.Analyzer\bin\Release\netstandard2.0\XFEExtension.NetCore.TodoHighLight.Analyzer.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
11 </ItemGroup>
12 </Project>