返回提交历史
Added
XFE各类拓展.NetCore.XUnit.Analyzer/CodeFix/XUnitCodeFixProvider.cs
+59
-0
Renamed
XFE各类拓展.NetCore.XUnit.Analyzer/Diagnostics/XUnitCodeAnalyzer.cs
+16
-11
Renamed
XFE各类拓展.NetCore.XUnit.Analyzer/Generator/XUnitCodeGenerator.cs
+2
-2
Modified
XFE各类拓展.NetCore.XUnit.Analyzer/XFE各类拓展.NetCore.XUnit.Analyzer.csproj
+1
-0
XFEstudio/XFEExtension.NetCore.XUnit
现在可以完美诊断错误信息并提供代码修补操作
2193df6
代码差异
4 个文件
+78
-13
@@ -0,0 +1,59 @@
1
using Microsoft.CodeAnalysis;
2
using Microsoft.CodeAnalysis.CodeActions;
3
using Microsoft.CodeAnalysis.CodeFixes;
4
using Microsoft.CodeAnalysis.CSharp;
5
using Microsoft.CodeAnalysis.CSharp.Syntax;
6
using System.Collections.Immutable;
7
using System.Linq;
8
using System.Threading;
9
using System.Threading.Tasks;
10
using XFE各类拓展.NetCore.XUnit.Analyzer.Diagnostics;
11
12
namespace XFE各类拓展.NetCore.XUnit.Analyzer.CodeFix
13
{
14
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(XUnitCodeFixProvider))]
15
public class XUnitCodeFixProvider : CodeFixProvider
16
{
17
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(XUnitCodeAnalyzer.SMTestID);
18
19
public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
20
21
public override Task RegisterCodeFixesAsync(CodeFixContext context)
22
{
23
foreach (var diagnostic in context.Diagnostics)
24
{
25
if (diagnostic.Id == XUnitCodeAnalyzer.SMTestID)
26
{
27
context.RegisterCodeFix(CodeAction.Create(title: "改用MTest特性",
28
createChangedDocument: c => ChangeSMTestToMTestAsync(context.Document, diagnostic, c),
29
equivalenceKey: "改用MTest特性"),
30
diagnostic: diagnostic);
31
context.RegisterCodeFix(CodeAction.Create(title: "将方法改为静态方法",
32
createChangedDocument: c => MakeMethodStaticAsync(context.Document, diagnostic, c),
33
equivalenceKey: "将方法改为静态方法"),
34
diagnostic: diagnostic);
35
}
36
}
37
return Task.CompletedTask;
38
}
39
40
public static async Task<Document> MakeMethodStaticAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
41
{
42
var root = await document.GetSyntaxRootAsync(cancellationToken);
43
var attribute = root.FindToken(diagnostic.Location.SourceSpan.Start).Parent.AncestorsAndSelf().OfType<AttributeSyntax>().First();
44
var method = attribute.Parent.Parent as MethodDeclarationSyntax;
45
var newMethod = method.AddModifiers(SyntaxFactory.Token(SyntaxKind.StaticKeyword));
46
var newRoot = root.ReplaceNode(method, newMethod);
47
return document.WithSyntaxRoot(newRoot);
48
}
49
50
public static async Task<Document> ChangeSMTestToMTestAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
51
{
52
var root = await document.GetSyntaxRootAsync(cancellationToken);
53
var attribute = root.FindToken(diagnostic.Location.SourceSpan.Start).Parent.AncestorsAndSelf().OfType<AttributeSyntax>().First();
54
var newAttribute = SyntaxFactory.Attribute(SyntaxFactory.ParseName("MTest"));
55
var newRoot = root.ReplaceNode(attribute, newAttribute);
56
return document.WithSyntaxRoot(newRoot);
57
}
58
}
59
}
@@ -1,30 +1,35 @@
1
1
using Microsoft.CodeAnalysis;
2
2
using Microsoft.CodeAnalysis.CSharp;
3
3
using Microsoft.CodeAnalysis.CSharp.Syntax;
4
using Microsoft.CodeAnalysis.Diagnostics;
5
using System.Collections.Immutable;
4
6
using System.Linq;
5
7
6
namespace XFE各类拓展.NetCore.XUnit.Analyzer
8
namespace XFE各类拓展.NetCore.XUnit.Analyzer.Diagnostics
7
9
{
8
[Generator]
9
public class XUnitCodeAnalyzer : ISourceGenerator
10
[DiagnosticAnalyzer(LanguageNames.CSharp)]
11
public class XUnitCodeAnalyzer : DiagnosticAnalyzer
10
12
{
11
public static readonly DiagnosticDescriptor SMTestError = new DiagnosticDescriptor("XFE0001",
13
public const string SMTestID = "XFE0001";
14
public static readonly DiagnosticDescriptor SMTestError = new DiagnosticDescriptor(SMTestID,
12
15
"不能标记非静态方法",
13
16
"SMTest特性不能用在静态方法之外的地方:'{0}'",
14
"XFE各类拓展.NetCore.XUnit.Analyzer",
17
"XFE各类拓展.NetCore.XUnit.Analyzer.Diagnostics",
15
18
DiagnosticSeverity.Error,
16
19
true,
17
20
"SMTest特性不能用在静态方法之外的地方.",
18
21
"https://www.xfegzs.com/codespace/diagnostics/XFE0001.html");
19
public void Initialize(GeneratorInitializationContext context)
20
{
21
}
22
22
23
public void Execute(GeneratorExecutionContext context)
23
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(SMTestError);
24
25
public override void Initialize(AnalysisContext context)
24
26
{
25
AttributeAnalyzer(context);
27
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
28
context.EnableConcurrentExecution();
29
context.RegisterSyntaxNodeAction(AttributeAnalyzer, SyntaxKind.Attribute);
26
30
}
27
public static void AttributeAnalyzer(GeneratorExecutionContext context)
31
32
public static void AttributeAnalyzer(SyntaxNodeAnalysisContext context)
28
33
{
29
34
var syntaxTrees = context.Compilation.SyntaxTrees;
30
35
foreach (var syntaxTree in syntaxTrees)
@@ -1,6 +1,6 @@
1
1
using Microsoft.CodeAnalysis;
2
2
3
namespace XFE各类拓展.NetCore.XUnit
3
namespace XFE各类拓展.NetCore.XUnit.Analyzer.Generator
4
4
{
5
5
[Generator]
6
6
public class XUnitCodeGenerator : ISourceGenerator
@@ -9,7 +9,7 @@ namespace XFE各类拓展.NetCore.XUnit
9
9
{
10
10
context.RegisterForPostInitialization(init =>
11
11
{
12
string globalUsingCode = @"global using XFE各类拓展.NetCore.XUnit;";
12
string globalUsingCode = @"global using global::XFE各类拓展.NetCore.XUnit;";
13
13
init.AddSource("GlobalUsing.g.cs", globalUsingCode);
14
14
string xUnitCode = $@"namespace XFE各类拓展.NetCore.XUnit
15
15
{{
@@ -14,6 +14,7 @@
14
14
<ItemGroup>
15
15
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
16
16
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" PrivateAssets="all" />
17
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.9.2" />
17
18
</ItemGroup>
18
19
19
20
</Project>