|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 5 | +using Microsoft.CodeAnalysis.CSharp; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; |
| 8 | +using Microsoft.CodeAnalysis.Editing; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using Microsoft.Extensions.Options; |
| 11 | +using System.Reflection; |
| 12 | +using Silk.NET.SilkTouch.Utility; |
| 13 | +using Silk.NET.SilkTouch.Sources; |
| 14 | + |
| 15 | +namespace Silk.NET.SilkTouch.Mods |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// A mod to Add warning disabling pragmas |
| 19 | + /// </summary> |
| 20 | + /// <param name="config">The configuration to use.</param> |
| 21 | + [ModConfiguration<Configuration>] |
| 22 | + public class DisableWarnings( |
| 23 | + IOptionsSnapshot<DisableWarnings.Configuration> config) : Mod |
| 24 | + { |
| 25 | + /// <summary> |
| 26 | + /// The configuration for the <see cref="DisableWarnings"/> mod. |
| 27 | + /// </summary> |
| 28 | + public class Configuration |
| 29 | + { |
| 30 | + /// <summary> |
| 31 | + /// Warning Codes to Disable |
| 32 | + /// </summary> |
| 33 | + public string[]? WarningCodes { get; init; } |
| 34 | + } |
| 35 | + |
| 36 | + /// <inheritdoc /> |
| 37 | + public override async Task ExecuteAsync(IModContext ctx, CancellationToken ct = default) |
| 38 | + { |
| 39 | + await base.ExecuteAsync(ctx, ct); |
| 40 | + |
| 41 | + // Read the configuration. |
| 42 | + var cfg = config.Get(ctx.JobKey); |
| 43 | + |
| 44 | + if (cfg.WarningCodes is null) |
| 45 | + { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + var proj = ctx.SourceProject; |
| 50 | + |
| 51 | + // Create the pragma directive trivia |
| 52 | + var pragmaDirective = Trivia( |
| 53 | + PragmaWarningDirectiveTrivia( |
| 54 | + Token(SyntaxKind.DisableKeyword), |
| 55 | + true) |
| 56 | + .WithErrorCodes( |
| 57 | + SeparatedList<ExpressionSyntax>( |
| 58 | + cfg.WarningCodes.Select(warn => IdentifierName(warn))))); |
| 59 | + |
| 60 | + foreach (var docId in proj?.DocumentIds ?? []) |
| 61 | + { |
| 62 | + var doc = |
| 63 | + proj?.GetDocument(docId) ?? throw new InvalidOperationException("Document missing"); |
| 64 | + if (await doc.GetSyntaxRootAsync(ct) is not { } root) |
| 65 | + { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + // Find the using directive |
| 70 | + SyntaxNode? namespaceNode = root.DescendantNodes().OfType<FileScopedNamespaceDeclarationSyntax>().FirstOrDefault(); |
| 71 | + |
| 72 | + if (namespaceNode == null) |
| 73 | + { |
| 74 | + // Skip the case where no using directive is found |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + // Get the leading trivia (comments) of the first using directive |
| 79 | + var leadingTrivia = namespaceNode.GetLeadingTrivia(); |
| 80 | + |
| 81 | + // Insert the pragma directive after the comments |
| 82 | + var newLeadingTrivia = leadingTrivia.Insert(leadingTrivia.Count, pragmaDirective) |
| 83 | + .Add(CarriageReturnLineFeed); |
| 84 | + |
| 85 | + // Update the using directive with the new leading trivia |
| 86 | + var newUsingDirective = namespaceNode.WithLeadingTrivia(newLeadingTrivia); |
| 87 | + |
| 88 | + // Replace the old using directive with the new one in the root node |
| 89 | + root = root.ReplaceNode(namespaceNode, newUsingDirective); |
| 90 | + doc = doc.WithSyntaxRoot(root); |
| 91 | + |
| 92 | + proj = doc.Project; |
| 93 | + } |
| 94 | + |
| 95 | + ctx.SourceProject = proj; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments