|
6 | 6 | using Microsoft.CodeAnalysis; |
7 | 7 | using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; |
8 | 8 | using Microsoft.Extensions.Options; |
| 9 | +using Microsoft.Build.Evaluation; |
9 | 10 |
|
10 | 11 | namespace Silk.NET.SilkTouch.Mods |
11 | 12 | { |
@@ -50,47 +51,12 @@ public override async Task ExecuteAsync(IModContext ctx, CancellationToken ct = |
50 | 51 | SeparatedList<ExpressionSyntax>( |
51 | 52 | cfg.WarningCodes.Select(warn => IdentifierName(warn))))); |
52 | 53 |
|
53 | | - var proj = ctx.SourceProject; |
54 | | - |
55 | | - foreach (var docId in proj?.DocumentIds ?? []) |
56 | | - { |
57 | | - var doc = |
58 | | - proj?.GetDocument(docId) ?? throw new InvalidOperationException("Document missing"); |
59 | | - if (await doc.GetSyntaxRootAsync(ct) is not { } root) |
60 | | - { |
61 | | - continue; |
62 | | - } |
63 | | - |
64 | | - // Find the using directive |
65 | | - SyntaxNode? namespaceNode = root.DescendantNodes().OfType<FileScopedNamespaceDeclarationSyntax>().FirstOrDefault(); |
66 | | - |
67 | | - if (namespaceNode == null) |
68 | | - { |
69 | | - // Skip the case where no using directive is found |
70 | | - continue; |
71 | | - } |
72 | | - |
73 | | - // Get the leading trivia (comments) of the first using directive |
74 | | - var leadingTrivia = namespaceNode.GetLeadingTrivia(); |
75 | | - |
76 | | - // Insert the pragma directive after the comments |
77 | | - var newLeadingTrivia = leadingTrivia.Insert(leadingTrivia.Count, pragmaDirective) |
78 | | - .Add(CarriageReturnLineFeed); |
79 | | - |
80 | | - // Update the using directive with the new leading trivia |
81 | | - var newUsingDirective = namespaceNode.WithLeadingTrivia(newLeadingTrivia); |
82 | | - |
83 | | - // Replace the old using directive with the new one in the root node |
84 | | - root = root.ReplaceNode(namespaceNode, newUsingDirective); |
85 | | - doc = doc.WithSyntaxRoot(root); |
86 | | - |
87 | | - proj = doc.Project; |
88 | | - } |
89 | | - |
90 | | - ctx.SourceProject = proj; |
91 | | - |
92 | | - proj = ctx.TestProject; |
| 54 | + ctx.SourceProject = await DisableWarningsAsync(ctx.SourceProject, pragmaDirective, ct); |
| 55 | + ctx.TestProject = await DisableWarningsAsync(ctx.TestProject, pragmaDirective, ct); |
| 56 | + } |
93 | 57 |
|
| 58 | + private async Task<Microsoft.CodeAnalysis.Project?> DisableWarningsAsync(Microsoft.CodeAnalysis.Project? proj, SyntaxTrivia pragmaDirective, CancellationToken ct) |
| 59 | + { |
94 | 60 | foreach (var docId in proj?.DocumentIds ?? []) |
95 | 61 | { |
96 | 62 | var doc = |
@@ -125,8 +91,7 @@ public override async Task ExecuteAsync(IModContext ctx, CancellationToken ct = |
125 | 91 |
|
126 | 92 | proj = doc.Project; |
127 | 93 | } |
128 | | - |
129 | | - ctx.TestProject = proj; |
| 94 | + return proj; |
130 | 95 | } |
131 | 96 | } |
132 | 97 | } |
0 commit comments