Strings.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // ReSharper disable MemberCanBePrivate.Global
  2. using System;
  3. using System.CodeDom.Compiler;
  4. using System.Diagnostics;
  5. using System.Diagnostics.CodeAnalysis;
  6. using Terminal.Gui.Analyzers.Internal.Attributes;
  7. namespace Terminal.Gui.Analyzers.Internal.Constants;
  8. /// <summary>String constants for frequently-used boilerplate.</summary>
  9. /// <remarks>These are for performance, instead of using Roslyn to build it all during execution of analyzers.</remarks>
  10. internal static class Strings
  11. {
  12. internal const string AnalyzersAttributesNamespace = $"{InternalAnalyzersNamespace}.Attributes";
  13. internal const string AssemblyExtendedEnumTypeAttributeFullName = $"{AnalyzersAttributesNamespace}.{nameof (AssemblyExtendedEnumTypeAttribute)}";
  14. internal const string DefaultTypeNameSuffix = "Extensions";
  15. internal const string FallbackClassNamespace = $"{TerminalGuiRootNamespace}";
  16. internal const string InternalAnalyzersNamespace = $"{AnalyzersRootNamespace}.Internal";
  17. internal const string TerminalGuiRootNamespace = "Terminal.Gui";
  18. private const string AnalyzersRootNamespace = $"{TerminalGuiRootNamespace}.Analyzers";
  19. private const string NetStandard20CompatibilityNamespace = $"{InternalAnalyzersNamespace}.Compatibility";
  20. /// <summary>
  21. /// Names of dotnet namespaces and types. Included as compile-time constants to avoid unnecessary work for the Roslyn
  22. /// source generators.
  23. /// </summary>
  24. /// <remarks>Implemented as nested static types because XmlDoc doesn't work on namespaces.</remarks>
  25. internal static class DotnetNames
  26. {
  27. /// <summary>Fully-qualified attribute type names. Specific applications (uses) are in <see cref="Applications"/>.</summary>
  28. internal static class Attributes
  29. {
  30. /// <inheritdoc cref="CompilerGeneratedAttribute"/>
  31. internal const string CompilerGenerated = $"{Namespaces.System_Runtime_CompilerServices}.{nameof (CompilerGeneratedAttribute)}";
  32. /// <inheritdoc cref="DebuggerNonUserCodeAttribute"/>
  33. internal const string DebuggerNonUserCode = $"{Namespaces.System_Diagnostics}.{nameof (DebuggerNonUserCodeAttribute)}";
  34. /// <inheritdoc cref="ExcludeFromCodeCoverageAttribute"/>
  35. internal const string ExcludeFromCodeCoverage = $"{Namespaces.System_Diagnostics_CodeAnalysis}.{nameof (ExcludeFromCodeCoverageAttribute)}";
  36. internal const string Flags = $"{Namespaces.SystemNs}.{nameof (FlagsAttribute)}";
  37. internal const string GeneratedCode = $"{Namespaces.System_CodeDom_Compiler}.{nameof (GeneratedCodeAttribute)}";
  38. /// <inheritdoc cref="MethodImplOptions.AggressiveInlining"/>
  39. /// <remarks>Use of this attribute should be carefully evaluated.</remarks>
  40. internal const string MethodImpl = $"{Namespaces.System_Runtime_CompilerServices}.{nameof (MethodImplAttribute)}";
  41. /// <summary>Attributes formatted for use in code, including square brackets.</summary>
  42. internal static class Applications
  43. {
  44. // ReSharper disable MemberHidesStaticFromOuterClass
  45. internal const string Flags = $"[{Attributes.Flags}]";
  46. /// <inheritdoc cref="System.CodeDom.Compiler.GeneratedCodeAttribute"/>
  47. internal const string GeneratedCode = $"""[{Attributes.GeneratedCode}("{InternalAnalyzersNamespace}","1.0")]""";
  48. /// <inheritdoc cref="MethodImplOptions.AggressiveInlining"/>
  49. /// <remarks>Use of this attribute should be carefully evaluated.</remarks>
  50. internal const string AggressiveInlining = $"[{MethodImpl}({Types.MethodImplOptions}.{nameof (MethodImplOptions.AggressiveInlining)})]";
  51. /// <inheritdoc cref="DebuggerNonUserCodeAttribute"/>
  52. internal const string DebuggerNonUserCode = $"[{Attributes.DebuggerNonUserCode}]";
  53. /// <inheritdoc cref="CompilerGeneratedAttribute"/>
  54. internal const string CompilerGenerated = $"[{Attributes.CompilerGenerated}]";
  55. /// <inheritdoc cref="ExcludeFromCodeCoverageAttribute"/>
  56. internal const string ExcludeFromCodeCoverage = $"[{Attributes.ExcludeFromCodeCoverage}]";
  57. // ReSharper restore MemberHidesStaticFromOuterClass
  58. }
  59. }
  60. /// <summary>Names of dotnet namespaces.</summary>
  61. internal static class Namespaces
  62. {
  63. internal const string SystemNs = nameof (System);
  64. // ReSharper disable InconsistentNaming
  65. internal const string System_CodeDom = $"{SystemNs}.{nameof (System.CodeDom)}";
  66. internal const string System_CodeDom_Compiler = $"{System_CodeDom}.{nameof (System.CodeDom.Compiler)}";
  67. internal const string System_ComponentModel = $"{SystemNs}.{nameof (System.ComponentModel)}";
  68. internal const string System_Diagnostics = $"{SystemNs}.{nameof (System.Diagnostics)}";
  69. internal const string System_Diagnostics_CodeAnalysis = $"{System_Diagnostics}.{nameof (System.Diagnostics.CodeAnalysis)}";
  70. internal const string System_Numerics = $"{SystemNs}.{nameof (System.Numerics)}";
  71. internal const string System_Runtime = $"{SystemNs}.{nameof (System.Runtime)}";
  72. internal const string System_Runtime_CompilerServices = $"{System_Runtime}.{nameof (System.Runtime.CompilerServices)}";
  73. // ReSharper restore InconsistentNaming
  74. }
  75. internal static class Types
  76. {
  77. internal const string Attribute = $"{Namespaces.SystemNs}.{nameof (System.Attribute)}";
  78. internal const string AttributeTargets = $"{Namespaces.SystemNs}.{nameof (System.AttributeTargets)}";
  79. internal const string AttributeUsageAttribute = $"{Namespaces.SystemNs}.{nameof (System.AttributeUsageAttribute)}";
  80. internal const string MethodImplOptions =
  81. $"{Namespaces.System_Runtime_CompilerServices}.{nameof (System.Runtime.CompilerServices.MethodImplOptions)}";
  82. }
  83. }
  84. internal static class Templates
  85. {
  86. internal const string AutoGeneratedCommentBlock = $"""
  87. //------------------------------------------------------------------------------
  88. // <auto-generated>
  89. // This file and the code it contains was generated by a source generator in
  90. // the {InternalAnalyzersNamespace} library.
  91. //
  92. // Modifications to this file are not supported and will be lost when
  93. // source generation is triggered, either implicitly or explicitly.
  94. // </auto-generated>
  95. //------------------------------------------------------------------------------
  96. """;
  97. /// <summary>
  98. /// A set of explicit type aliases to work around Terminal.Gui having name collisions with types like
  99. /// <see cref="System.Attribute"/>.
  100. /// </summary>
  101. internal const string DotnetExplicitTypeAliasUsingDirectives = $"""
  102. using Attribute = {DotnetNames.Types.Attribute};
  103. using AttributeUsageAttribute = {DotnetNames.Types.AttributeUsageAttribute};
  104. using GeneratedCode = {DotnetNames.Attributes.GeneratedCode};
  105. """;
  106. /// <summary>Using directives for common namespaces in generated code.</summary>
  107. internal const string DotnetNamespaceUsingDirectives = $"""
  108. using {DotnetNames.Namespaces.SystemNs};
  109. using {DotnetNames.Namespaces.System_CodeDom};
  110. using {DotnetNames.Namespaces.System_CodeDom_Compiler};
  111. using {DotnetNames.Namespaces.System_ComponentModel};
  112. using {DotnetNames.Namespaces.System_Numerics};
  113. using {DotnetNames.Namespaces.System_Runtime};
  114. using {DotnetNames.Namespaces.System_Runtime_CompilerServices};
  115. """;
  116. /// <summary>
  117. /// A set of empty namespaces that MAY be referenced in generated code, especially in using statements,
  118. /// which are always included to avoid additional complexity due to conditional compilation.
  119. /// </summary>
  120. internal const string DummyNamespaceDeclarations = $$"""
  121. // These are dummy declarations to avoid complexity with conditional compilation.
  122. #pragma warning disable IDE0079 // Remove unnecessary suppression
  123. #pragma warning disable RCS1259 // Remove empty syntax
  124. namespace {{TerminalGuiRootNamespace}} { }
  125. namespace {{AnalyzersRootNamespace}} { }
  126. namespace {{InternalAnalyzersNamespace}} { }
  127. namespace {{NetStandard20CompatibilityNamespace}} { }
  128. namespace {{AnalyzersAttributesNamespace}} { }
  129. #pragma warning restore RCS1259 // Remove empty syntax
  130. #pragma warning restore IDE0079 // Remove unnecessary suppression
  131. """;
  132. internal const string StandardHeader = $"""
  133. {AutoGeneratedCommentBlock}
  134. // ReSharper disable RedundantUsingDirective
  135. // ReSharper disable once RedundantNullableDirective
  136. {NullableContextDirective}
  137. {StandardUsingDirectivesText}
  138. """;
  139. /// <summary>
  140. /// Standard set of using directives for generated extension method class files.
  141. /// Not all are always needed, but all are included so we don't have to worry about it.
  142. /// </summary>
  143. internal const string StandardUsingDirectivesText = $"""
  144. {DotnetNamespaceUsingDirectives}
  145. {DotnetExplicitTypeAliasUsingDirectives}
  146. using {TerminalGuiRootNamespace};
  147. using {AnalyzersRootNamespace};
  148. using {InternalAnalyzersNamespace};
  149. using {AnalyzersAttributesNamespace};
  150. using {NetStandard20CompatibilityNamespace};
  151. """;
  152. internal const string AttributesForGeneratedInterfaces = $"""
  153. {DotnetNames.Attributes.Applications.GeneratedCode}
  154. {DotnetNames.Attributes.Applications.CompilerGenerated}
  155. """;
  156. internal const string AttributesForGeneratedTypes = $"""
  157. {DotnetNames.Attributes.Applications.GeneratedCode}
  158. {DotnetNames.Attributes.Applications.CompilerGenerated}
  159. {DotnetNames.Attributes.Applications.DebuggerNonUserCode}
  160. {DotnetNames.Attributes.Applications.ExcludeFromCodeCoverage}
  161. """;
  162. /// <summary>
  163. /// Preprocessor directive to enable nullability context for generated code.<br/>
  164. /// This should always be emitted, as it applies only to generated code.<br/>
  165. /// As such, generated code MUST be properly annotated.
  166. /// </summary>
  167. internal const string NullableContextDirective = "#nullable enable";
  168. }
  169. }