|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License"). |
| 2 | +// See the LICENSE file in the project root for more information. |
| 3 | + |
| 4 | +using System.Globalization; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | +using System.Text; |
| 7 | +using Microsoft.CodeAnalysis; |
| 8 | + |
| 9 | +namespace HIDDevices.Generator; |
| 10 | + |
| 11 | +public static class Diagnostics |
| 12 | +{ |
| 13 | + // ReSharper disable ArrangeObjectCreationWhenTypeEvident - See https://github.com/dotnet/roslyn-analyzers/issues/5957 |
| 14 | +#pragma warning disable IDE0090 // Use 'new(...)' |
| 15 | + public static readonly DiagnosticDescriptor Completed = new DiagnosticDescriptor( |
| 16 | + "HUT0001", |
| 17 | + "Generation succeeded", |
| 18 | + "Generation succeeded for version {0}. {1} usages in {2} usage pages in {3:g}.", |
| 19 | + "Generation", |
| 20 | + DiagnosticSeverity.Info, |
| 21 | + true); |
| 22 | + |
| 23 | + public static readonly DiagnosticDescriptor Cancelled = new DiagnosticDescriptor( |
| 24 | + "HUT00FF", |
| 25 | + "Cancelled", |
| 26 | + "Generation was cancelled", |
| 27 | + "Generation", |
| 28 | + DiagnosticSeverity.Error, |
| 29 | + true); |
| 30 | + |
| 31 | + public static readonly DiagnosticDescriptor CacheFolderCreationFailed = new DiagnosticDescriptor( |
| 32 | + "HUT1001", |
| 33 | + "Could not find/create HID Usage Table Caching folder", |
| 34 | + "Folder {0} could not be created: {1}", |
| 35 | + "Caching", |
| 36 | + DiagnosticSeverity.Warning, |
| 37 | + true); |
| 38 | + |
| 39 | + public static readonly DiagnosticDescriptor CachingDisabled = new DiagnosticDescriptor( |
| 40 | + "HUT1002", |
| 41 | + "Caching disabled", |
| 42 | + "Caching disabled as cache file locations could not be determined", |
| 43 | + "Caching", |
| 44 | + DiagnosticSeverity.Warning, |
| 45 | + true); |
| 46 | + |
| 47 | + public static readonly DiagnosticDescriptor PdfNotFound = new DiagnosticDescriptor( |
| 48 | + "HUT1003", |
| 49 | + "PDF Not found", |
| 50 | + "Failed to find the PDF file {0}: {1}", |
| 51 | + "Caching", |
| 52 | + DiagnosticSeverity.Error, |
| 53 | + true); |
| 54 | + |
| 55 | + public static readonly DiagnosticDescriptor JsonDeserializationFailed = new DiagnosticDescriptor( |
| 56 | + "HUT2001", |
| 57 | + "JSON Deserialization Failure", |
| 58 | + "Deserialization of the JSON HID USage Tables failed: {0}", |
| 59 | + "Deserialization", |
| 60 | + DiagnosticSeverity.Error, |
| 61 | + true); |
| 62 | + |
| 63 | + public static readonly DiagnosticDescriptor JsonAttachmentNotFound = new DiagnosticDescriptor( |
| 64 | + "HUT2002", |
| 65 | + "JSON attachment not found in PDF file", |
| 66 | + "Could not find the JSON attachment in '{0}'", |
| 67 | + "Deserialization", |
| 68 | + DiagnosticSeverity.Error, |
| 69 | + true); |
| 70 | + |
| 71 | + public static readonly DiagnosticDescriptor JsonExtractionFailed = new DiagnosticDescriptor( |
| 72 | + "HUT2003", |
| 73 | + "Error extracting JSON attachment from PDF file", |
| 74 | + "Extracting the JSON attachment from '{0}' failed: {1}", |
| 75 | + "Deserialization", |
| 76 | + DiagnosticSeverity.Error, |
| 77 | + true); |
| 78 | + // ReSharper restore ArrangeObjectCreationWhenTypeEvident |
| 79 | +#pragma warning restore IDE0090 // Use 'new(...)' |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Adds a <see cref="Diagnostic" /> to the users compilation based on a <see cref="DiagnosticDescriptor" />. |
| 83 | + /// </summary> |
| 84 | + /// <param name="context">The execution context.</param> |
| 85 | + /// <param name="descriptor">The diagnostic descriptor.</param> |
| 86 | + /// <param name="location"></param> |
| 87 | + /// <param name="messageArgs"></param> |
| 88 | + /// <remarks> |
| 89 | + /// The severity of the diagnostic may cause the compilation to fail, depending on the <see cref="Compilation" /> |
| 90 | + /// settings. |
| 91 | + /// </remarks> |
| 92 | + /// <seealso cref="GeneratorExecutionContext.ReportDiagnostic(Diagnostic)" /> |
| 93 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 94 | + public static void Report(this GeneratorExecutionContext context, DiagnosticDescriptor descriptor, |
| 95 | + Location location, params object[] messageArgs) |
| 96 | + => context.ReportDiagnostic(Diagnostic.Create(descriptor, location, messageArgs)); |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Get's a C#-safe version of a name. |
| 100 | + /// </summary> |
| 101 | + /// <param name="unsafe"></param> |
| 102 | + /// <returns></returns> |
| 103 | + public static string GetSafe(this string @unsafe) |
| 104 | + { |
| 105 | + // Create safe name |
| 106 | + var builder = new StringBuilder(@unsafe.Length); |
| 107 | + var afterSpace = true; |
| 108 | + foreach (var ch in @unsafe) |
| 109 | + { |
| 110 | + switch (char.GetUnicodeCategory(ch)) |
| 111 | + { |
| 112 | + case UnicodeCategory.UppercaseLetter: |
| 113 | + case UnicodeCategory.LowercaseLetter: |
| 114 | + case UnicodeCategory.TitlecaseLetter: |
| 115 | + case UnicodeCategory.ModifierLetter: |
| 116 | + case UnicodeCategory.OtherLetter: |
| 117 | + // Always allowed in C# class names |
| 118 | + break; |
| 119 | + |
| 120 | + case UnicodeCategory.ConnectorPunctuation: |
| 121 | + // Language specification allows '_' as first character. |
| 122 | + if (builder.Length < 1 && ch != '_') |
| 123 | + { |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + break; |
| 128 | + |
| 129 | + case UnicodeCategory.LetterNumber: |
| 130 | + case UnicodeCategory.NonSpacingMark: |
| 131 | + case UnicodeCategory.SpacingCombiningMark: |
| 132 | + case UnicodeCategory.DecimalDigitNumber: |
| 133 | + case UnicodeCategory.Format: |
| 134 | + // Only valid after first character, so add a '_' prefix. |
| 135 | + if (builder.Length < 1) |
| 136 | + { |
| 137 | + builder.Append('_'); |
| 138 | + } |
| 139 | + |
| 140 | + break; |
| 141 | + |
| 142 | + case UnicodeCategory.SpaceSeparator: |
| 143 | + afterSpace = true; |
| 144 | + continue; |
| 145 | + default: |
| 146 | + // Skip characters |
| 147 | + continue; |
| 148 | + } |
| 149 | + |
| 150 | + char c; |
| 151 | + if (afterSpace) |
| 152 | + { |
| 153 | + afterSpace = false; |
| 154 | + c = char.ToUpperInvariant(ch); |
| 155 | + } |
| 156 | + else |
| 157 | + { |
| 158 | + c = ch; |
| 159 | + } |
| 160 | + |
| 161 | + builder.Append(c); |
| 162 | + } |
| 163 | + |
| 164 | + return builder.ToString(); |
| 165 | + } |
| 166 | + |
| 167 | + public static IndentStringBuilder AppendComment(this IndentStringBuilder builder, string comment, |
| 168 | + bool cStyle = false) |
| 169 | + { |
| 170 | + if (cStyle) |
| 171 | + { |
| 172 | + builder.AppendLine("/*").Indent(" *").AppendLine(comment).Outdent().AppendLine(" */"); |
| 173 | + } |
| 174 | + else |
| 175 | + { |
| 176 | + builder.Indent("// ").AppendLine(comment).Outdent(); |
| 177 | + } |
| 178 | + |
| 179 | + return builder; |
| 180 | + } |
| 181 | + |
| 182 | + public static IndentStringBuilder AppendSummary(this IndentStringBuilder builder, string comment) |
| 183 | + => builder.Indent("// ").AppendLine("<summary>").Indent().AppendLine(comment).Outdent().AppendLine("</summary>") |
| 184 | + .Outdent(); |
| 185 | + |
| 186 | + public static IndentStringBuilder AppendDescription(this IndentStringBuilder builder, string description) |
| 187 | + => builder.Append("[Description(").AppendQuoted(description).AppendLine(")]"); |
| 188 | + |
| 189 | + public static IndentStringBuilder OpenBrace(this IndentStringBuilder builder) |
| 190 | + => builder.AppendLine("{").Indent(); |
| 191 | + |
| 192 | + public static IndentStringBuilder CloseBrace(this IndentStringBuilder builder) |
| 193 | + => builder.Outdent().AppendLine("}"); |
| 194 | +} |
0 commit comments