/src/Workspaces/Core/Portable/CodeGeneration/CodeGenerationOptions.cs

https://gitlab.com/sharadag/TestProject2 · C# · 184 lines · 78 code · 20 blank · 86 comment · 5 complexity · 807c582306f022102bc99611a7cf486d MD5 · raw file

  1. // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  2. using System;
  3. using System.Collections.Generic;
  4. using Microsoft.CodeAnalysis;
  5. using Microsoft.CodeAnalysis.Text;
  6. using Roslyn.Utilities;
  7. namespace Microsoft.CodeAnalysis.CodeGeneration
  8. {
  9. /// <summary>
  10. /// Options for controlling the code produced by the <see cref="CodeGenerator"/>.
  11. /// </summary>
  12. internal class CodeGenerationOptions
  13. {
  14. public static readonly CodeGenerationOptions Default = new CodeGenerationOptions();
  15. /// <summary>
  16. /// A location used to determine the best place to generate a member. This is only used for
  17. /// determining which part of a partial type to generate in. If a type only has one part, or
  18. /// an API is used that specifies the type, then this is not used. A part is preferred if
  19. /// it surrounds this context location. If no part surrounds this location then a part is
  20. /// preferred if it comes from the same SyntaxTree as this location. If there is no
  21. /// such part, then any part may be used for generation.
  22. ///
  23. /// This option is not necessary if <see cref="AfterThisLocation"/> or <see cref="BeforeThisLocation"/> are
  24. /// provided.
  25. /// </summary>
  26. public Location ContextLocation { get; }
  27. /// <summary>
  28. /// A hint to the code generation service to specify where the generated code should be
  29. /// placed. Code will be generated after this location if the location is valid in the type
  30. /// or symbol being generated into, and it is possible to generate the code after it.
  31. ///
  32. /// If this option is provided, neither <see cref="ContextLocation"/> nor <see cref="BeforeThisLocation"/> are
  33. /// needed.
  34. /// </summary>
  35. public Location AfterThisLocation { get; }
  36. /// <summary>
  37. /// A hint to the code generation service to specify where the generated code should be
  38. /// placed. Code will be generated before this location if the location is valid in the type
  39. /// or symbol being generated into, and it is possible to generate the code after it.
  40. ///
  41. /// If this option is provided, neither <see cref="ContextLocation"/> nor <see cref="AfterThisLocation"/> are
  42. /// needed.
  43. /// </summary>
  44. public Location BeforeThisLocation { get; }
  45. /// <summary>
  46. /// True if the code generation service should try to automatically add imports to the file
  47. /// for any generated code. Defaults to true. Not used when generating directly into a
  48. /// declaration.
  49. /// </summary>
  50. public bool AddImports { get; }
  51. /// <summary>
  52. /// True if, when adding a System import, the import should be placed above non-System
  53. /// imports. Defaults to true. Only used if <see cref="AddImports"/> is true.
  54. /// </summary>
  55. public bool PlaceSystemNamespaceFirst { get; }
  56. /// <summary>
  57. /// Contains additional imports to be automatically added. This is useful for adding
  58. /// imports that are part of a list of statements.
  59. /// </summary>
  60. public IEnumerable<INamespaceSymbol> AdditionalImports { get; }
  61. /// <summary>
  62. /// True if members of a symbol should also be generated along with the declaration. If
  63. /// false, only the symbol's declaration will be generated.
  64. /// </summary>
  65. public bool GenerateMembers { get; }
  66. /// <summary>
  67. /// True if the code generator should merge namespaces which only contain other namespaces
  68. /// into a single declaration with a dotted name. False if the nesting should be preserved
  69. /// and each namespace declaration should be nested and should only have a single non-dotted
  70. /// name.
  71. ///
  72. /// Merging can only occur if the namespace only contains a single member that is also a
  73. /// namespace.
  74. /// </summary>
  75. public bool MergeNestedNamespaces { get; }
  76. /// <summary>
  77. /// True if the code generation should put multiple attributes in a single attribute
  78. /// declaration, or if should have a separate attribute declaration for each attribute. For
  79. /// example, in C# setting this to True this would produce "[Foo, Bar]" while setting it to
  80. /// False would produce "[Foo][Bar]"
  81. /// </summary>
  82. public bool MergeAttributes { get; }
  83. /// <summary>
  84. /// True if the code generator should always generate accessibility modifiers, even if they
  85. /// are the same as the defaults for that symbol. For example, a private field in C# does
  86. /// not need its accessibility specified as it will be private by default. However, if this
  87. /// option is set to true 'private' will still be generated.
  88. /// </summary>
  89. public bool GenerateDefaultAccessibility { get; }
  90. /// <summary>
  91. /// True if the code generator should generate empty bodies for methods along with the
  92. /// method declaration. If false, only method declarations will be generated.
  93. /// </summary>
  94. public bool GenerateMethodBodies { get; }
  95. /// <summary>
  96. /// True if the code generator should generate documentation comments where available
  97. /// </summary>
  98. public bool GenerateDocumentationComments { get; }
  99. /// <summary>
  100. /// True if the code generator should automatically attempt to choose the appropriate location
  101. /// to insert members. If false and a generation location is not specified by AfterThisLocation,
  102. /// or BeforeThisLocation, members will be inserted at the end of the destination definition.
  103. /// </summary>
  104. public bool AutoInsertionLocation { get; }
  105. /// <summary>
  106. /// True if the code generator should attempt to reuse the syntax of the constituent entities, such as members, access modifier tokens, etc. while attempting to generate code.
  107. /// If any of the member symbols have zero declaring syntax references (non-source symbols) OR two or more declaring syntax references (partial definitions), then syntax is not reused.
  108. /// If false, then the code generator will always synthesize a new syntax node and ignore the declaring syntax references.
  109. /// </summary>
  110. public bool ReuseSyntax { get; }
  111. public CodeGenerationOptions(
  112. Location contextLocation = null,
  113. Location afterThisLocation = null,
  114. Location beforeThisLocation = null,
  115. bool addImports = true,
  116. bool placeSystemNamespaceFirst = true,
  117. IEnumerable<INamespaceSymbol> additionalImports = null,
  118. bool generateMembers = true,
  119. bool mergeNestedNamespaces = true,
  120. bool mergeAttributes = true,
  121. bool generateDefaultAccessibility = true,
  122. bool generateMethodBodies = true,
  123. bool generateDocumentationComments = false,
  124. bool autoInsertionLocation = true,
  125. bool reuseSyntax = false)
  126. {
  127. CheckLocation(contextLocation, nameof(contextLocation));
  128. CheckLocation(afterThisLocation, nameof(afterThisLocation));
  129. CheckLocation(beforeThisLocation, nameof(beforeThisLocation));
  130. this.ContextLocation = contextLocation;
  131. this.AfterThisLocation = afterThisLocation;
  132. this.BeforeThisLocation = beforeThisLocation;
  133. this.AddImports = addImports;
  134. this.PlaceSystemNamespaceFirst = placeSystemNamespaceFirst;
  135. this.AdditionalImports = additionalImports ?? SpecializedCollections.EmptyEnumerable<INamespaceSymbol>();
  136. this.GenerateMembers = generateMembers;
  137. this.MergeNestedNamespaces = mergeNestedNamespaces;
  138. this.MergeAttributes = mergeAttributes;
  139. this.GenerateDefaultAccessibility = generateDefaultAccessibility;
  140. this.GenerateMethodBodies = generateMethodBodies;
  141. this.GenerateDocumentationComments = generateDocumentationComments;
  142. this.AutoInsertionLocation = autoInsertionLocation;
  143. this.ReuseSyntax = reuseSyntax;
  144. }
  145. private void CheckLocation(Location location, string name)
  146. {
  147. if (location != null && !location.IsInSource)
  148. {
  149. throw new ArgumentException(WorkspacesResources.LocationMustBeNullOrFromSource, name);
  150. }
  151. }
  152. internal Location BestLocation
  153. {
  154. get
  155. {
  156. return this.AfterThisLocation != null
  157. ? this.AfterThisLocation
  158. : this.BeforeThisLocation != null
  159. ? this.BeforeThisLocation
  160. : this.ContextLocation;
  161. }
  162. }
  163. }
  164. }