Repository navigation
Expand file tree
/
Copy pathCodeFixProvider.cs
More file actions
103 lines (96 loc) · 3.95 KB
/
Copy pathCodeFixProvider.cs
File metadata and controls
103 lines (96 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
// see https://github.com/dotnet/roslyn/blob/main/docs/roslyn-analyzers/rules/RS1038.md
namespace ModInteropImportGenerator.CodeFix
{
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(CodeGenerator))]
public class CodeGenerator : CodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds => [ModInteropImportDiagnosticAnalyzer.PreparedForCodeFixID];
public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
var act =
CodeAction.Create("Strip Import Generator",
cancel => StripAsync(context, cancel),
nameof(CodeGenerator));
context.RegisterCodeFix(act, context.Diagnostics);
return Task.CompletedTask;
}
private static async Task<Document> StripAsync(CodeFixContext context, System.Threading.CancellationToken cancel)
{
var model = await context.Document.GetSemanticModelAsync(cancel);
var root = await context.Document.GetSyntaxRootAsync(cancel);
if (model == null || root == null)
{
return context.Document;
}
var _attrNode = root.FindNode(context.Span);
if (_attrNode is not AttributeSyntax attrNode)
{
return context.Document;
}
// if it's a nested class, we should mark all containing classes as partial
var list = attrNode.AncestorsAndSelf().OfType<ClassDeclarationSyntax>().ToList();
// perform the rest actions in the walker rewriter
var newRoot = new StripWalker(list).Visit(root);
return context.Document.WithSyntaxRoot(newRoot);
}
public sealed override FixAllProvider? GetFixAllProvider()
{
return null;
}
}
class StripWalker(List<ClassDeclarationSyntax> clas)
: CSharpSyntaxRewriter()
{
public override SyntaxNode? VisitClassDeclaration(ClassDeclarationSyntax node)
{
// the classes we are interested in are all in the `clas`
if (clas.Count == 0 || node != clas.Last())
{
return node;
}
clas.RemoveAt(clas.Count - 1);
ClassDeclarationSyntax newNode;
if (clas.Count == 0)
{
// walk through all the methods
// add partial and remove body
var newMembers = SyntaxFactory.List(node.Members.Select(mem =>
{
if (mem is MethodDeclarationSyntax method)
{
if (!method.Modifiers.Any(m => m.IsKind(SyntaxKind.PartialKeyword)))
{
method = method.AddModifiers(SyntaxFactory.Token(SyntaxKind.PartialKeyword));
}
return method
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
.WithBody(null)
.WithExpressionBody(null);
}
return mem;
}));
newNode = node.WithMembers(newMembers);
}
else
{
newNode = (ClassDeclarationSyntax)base.VisitClassDeclaration(node)!;
}
// mark all classes as partial
if (!newNode.Modifiers.Any(mod => mod.IsKind(SyntaxKind.PartialKeyword)))
{
newNode = newNode.AddModifiers(SyntaxFactory.Token(SyntaxKind.PartialKeyword));
}
return newNode;
}
}
}