|
| 1 | +#define DiagnosticTemplateBuilderExperimentTestOutput |
| 2 | + |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Diagnostics.CodeAnalysis; |
| 5 | +using JetBrains.Annotations; |
| 6 | +using NexusMods.Abstractions.Diagnostics.References; |
| 7 | + |
| 8 | +namespace NexusMods.Abstractions.Diagnostics; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// A builder used at compile time by a source generator to create a diagnostic template. |
| 12 | +/// </summary> |
| 13 | +/// <remarks> |
| 14 | +/// This should never be used at runtime! |
| 15 | +/// </remarks> |
| 16 | +/// <seealso cref="IDiagnosticTemplate"/> |
| 17 | +[PublicAPI] |
| 18 | +public static class DiagnosticTemplateBuilder |
| 19 | +{ |
| 20 | + /// <summary> |
| 21 | + /// Start of the builder chain. |
| 22 | + /// </summary> |
| 23 | + public static IWithIdStep Start() => ThrowException(); |
| 24 | + |
| 25 | + [DoesNotReturn] |
| 26 | + [ContractAnnotation("=> halt")] |
| 27 | + private static IWithIdStep ThrowException() |
| 28 | + { |
| 29 | + throw new UnreachableException($"The {nameof(DiagnosticTemplateBuilder)} is not supposed to be used at runtime!"); |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// ID step. |
| 34 | + /// </summary> |
| 35 | + public interface IWithIdStep |
| 36 | + { |
| 37 | + /// <summary> |
| 38 | + /// Sets the <see cref="DiagnosticId"/>. |
| 39 | + /// </summary> |
| 40 | + IWithSeverityStep WithId(DiagnosticId id); |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Severity Step. |
| 45 | + /// </summary> |
| 46 | + public interface IWithSeverityStep |
| 47 | + { |
| 48 | + /// <summary> |
| 49 | + /// Sets the <see cref="DiagnosticSeverity"/>. |
| 50 | + /// </summary> |
| 51 | + IWithMessageStep WithSeverity(DiagnosticSeverity severity); |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Message Step. |
| 56 | + /// </summary> |
| 57 | + public interface IWithMessageStep |
| 58 | + { |
| 59 | + /// <summary> |
| 60 | + /// Sets the message template. |
| 61 | + /// </summary> |
| 62 | + IFinishStep WithMessage(string message, Func<IMessageBuilder, IMessageBuilder> messageBuilder); |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Message Builder. |
| 67 | + /// </summary> |
| 68 | + public interface IMessageBuilder |
| 69 | + { |
| 70 | + /// <summary> |
| 71 | + /// Adds data references to the message. |
| 72 | + /// </summary> |
| 73 | + IMessageBuilder AddDataReference<T>(string name) where T : IDataReference; |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Finish step. |
| 78 | + /// </summary> |
| 79 | + public interface IFinishStep |
| 80 | + { |
| 81 | + /// <summary> |
| 82 | + /// Ends the builder. |
| 83 | + /// </summary> |
| 84 | + IDiagnosticTemplate Finish(); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/// <summary> |
| 89 | +/// Output of <see cref="DiagnosticTemplateBuilder"/>. |
| 90 | +/// </summary> |
| 91 | +/// <seealso cref="DiagnosticTemplateBuilder"/> |
| 92 | +public interface IDiagnosticTemplate; |
| 93 | + |
| 94 | +#if DiagnosticTemplateBuilderExperimentTestOutput |
| 95 | +#if RELEASE |
| 96 | +#else |
| 97 | + |
| 98 | +[SuppressMessage("ReSharper", "UnusedType.Global")] |
| 99 | +[SuppressMessage("ReSharper", "UnusedMember.Local")] |
| 100 | +internal partial class Test |
| 101 | +{ |
| 102 | + private static readonly IDiagnosticTemplate Diagnostic1Template = DiagnosticTemplateBuilder |
| 103 | + .Start() |
| 104 | + .WithId(new DiagnosticId(source: "MyCoolSource", number: 13)) |
| 105 | + .WithSeverity(DiagnosticSeverity.Warning) |
| 106 | + .WithMessage("Mod '{Mod}' is not working!", messageBuilder => messageBuilder |
| 107 | + .AddDataReference<ModReference>("Mod") |
| 108 | + ) |
| 109 | + .Finish(); |
| 110 | +} |
| 111 | + |
| 112 | +[SuppressMessage("ReSharper", "UnusedType.Global")] |
| 113 | +[SuppressMessage("ReSharper", "UnusedMember.Global")] |
| 114 | +[SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Global")] |
| 115 | +internal partial class Test |
| 116 | +{ |
| 117 | + public static Diagnostic<Diagnostic1MessageData> CreateDiagnostic1(ModReference mod) |
| 118 | + { |
| 119 | + var messageData = new Diagnostic1MessageData(mod); |
| 120 | + |
| 121 | + return new Diagnostic<Diagnostic1MessageData> |
| 122 | + { |
| 123 | + Id = new DiagnosticId(source: "MyCoolSource", number: 13), |
| 124 | + Severity = DiagnosticSeverity.Warning, |
| 125 | + Message = DiagnosticMessage.From("Mod '{Mod}' is not working!"), |
| 126 | + MessageData = messageData, |
| 127 | + DataReferences = new Dictionary<DataReferenceDescription, IDataReference> |
| 128 | + { |
| 129 | + { DataReferenceDescription.From("Mod"), messageData.Mod }, |
| 130 | + }, |
| 131 | + }; |
| 132 | + } |
| 133 | + |
| 134 | + public readonly struct Diagnostic1MessageData |
| 135 | + { |
| 136 | + public readonly ModReference Mod; |
| 137 | + |
| 138 | + public Diagnostic1MessageData(ModReference mod) |
| 139 | + { |
| 140 | + Mod = mod; |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | +#endif |
| 145 | +#endif |
0 commit comments