Skip to content

Commit 623ecb0

Browse files
committed
Deduplication of code in DisableWarnings
1 parent bb8b897 commit 623ecb0

File tree

1 file changed

+7
-42
lines changed

1 file changed

+7
-42
lines changed

sources/SilkTouch/SilkTouch/Mods/DisableWarnings.cs

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.CodeAnalysis;
77
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
88
using Microsoft.Extensions.Options;
9+
using Microsoft.Build.Evaluation;
910

1011
namespace Silk.NET.SilkTouch.Mods
1112
{
@@ -50,47 +51,12 @@ public override async Task ExecuteAsync(IModContext ctx, CancellationToken ct =
5051
SeparatedList<ExpressionSyntax>(
5152
cfg.WarningCodes.Select(warn => IdentifierName(warn)))));
5253

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+
}
9357

58+
private async Task<Microsoft.CodeAnalysis.Project?> DisableWarningsAsync(Microsoft.CodeAnalysis.Project? proj, SyntaxTrivia pragmaDirective, CancellationToken ct)
59+
{
9460
foreach (var docId in proj?.DocumentIds ?? [])
9561
{
9662
var doc =
@@ -125,8 +91,7 @@ public override async Task ExecuteAsync(IModContext ctx, CancellationToken ct =
12591

12692
proj = doc.Project;
12793
}
128-
129-
ctx.TestProject = proj;
94+
return proj;
13095
}
13196
}
13297
}

0 commit comments

Comments
 (0)