PageRenderTime 291ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/Project/Src/AddIns/ReSharper610/Options/StyleCopOptionsSettingsKey.cs

#
C# | 299 lines | 138 code | 47 blank | 114 comment | 8 complexity | 11797ff51cc5071e838ec2f008cdd19f MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="StyleCopOptionsSettingsKey.cs" company="http://stylecop.codeplex.com">
  3. // MS-PL
  4. // </copyright>
  5. // <license>
  6. // This source code is subject to terms and conditions of the Microsoft
  7. // Public License. A copy of the license can be found in the License.html
  8. // file at the root of this distribution. If you cannot locate the
  9. // Microsoft Public License, please send an email to dlr@microsoft.com.
  10. // By using this source code in any fashion, you are agreeing to be bound
  11. // by the terms of the Microsoft Public License. You must not remove this
  12. // notice, or any other, from this software.
  13. // </license>
  14. // <summary>
  15. // Class to hold all of the Configurable options for this addin.
  16. // </summary>
  17. // --------------------------------------------------------------------------------------------------------------------
  18. extern alias JB;
  19. namespace StyleCop.ReSharper610.Options
  20. {
  21. #region Using Directives
  22. using System.IO;
  23. using System.Reflection;
  24. using System.Windows.Forms;
  25. using JetBrains.Application.Settings;
  26. using Microsoft.Win32;
  27. using StyleCop.ReSharper610.Core;
  28. #endregion
  29. /// <summary>
  30. /// Class to hold all of the Configurable options for this addin.
  31. /// </summary>
  32. [SettingsKey(typeof(Missing), "StyleCop Options")]
  33. public class StyleCopOptionsSettingsKey
  34. {
  35. #region Fields
  36. /// <summary>
  37. /// Set to true to always check for updates when Visual Studio starts.
  38. /// </summary>
  39. private bool alwaysCheckForUpdatesWhenVisualStudioStarts;
  40. /// <summary>
  41. /// Set to true when we've attempted to get the StyleCop path.
  42. /// </summary>
  43. private bool attemptedToGetStyleCopPath;
  44. /// <summary>
  45. /// Tracks whether we should check for updates.
  46. /// </summary>
  47. private bool automaticallyCheckForUpdates;
  48. /// <summary>
  49. /// The number of days between update checks.
  50. /// </summary>
  51. private int daysBetweenUpdateChecks;
  52. /// <summary>
  53. /// The value of the detected path for StyleCop.
  54. /// </summary>
  55. private string styleCopDetectedPath;
  56. #endregion
  57. #region Public Properties
  58. /// <summary>
  59. /// Gets or sets a value indicating whether AlwaysCheckForUpdatesWhenVisualStudioStarts.
  60. /// </summary>
  61. [SettingsEntry(false, "Always Check For Updates When Visual Studio Starts")]
  62. public bool AlwaysCheckForUpdatesWhenVisualStudioStarts
  63. {
  64. get
  65. {
  66. return this.alwaysCheckForUpdatesWhenVisualStudioStarts;
  67. }
  68. set
  69. {
  70. this.alwaysCheckForUpdatesWhenVisualStudioStarts = value;
  71. SetRegistry("AlwaysCheckForUpdatesWhenVisualStudioStarts", value, RegistryValueKind.DWord);
  72. }
  73. }
  74. /// <summary>
  75. /// Gets or sets a value indicating whether the analysis executes as you type.
  76. /// </summary>
  77. [SettingsEntry(true, "Analysis Enabled")]
  78. public bool AnalysisEnabled { get; set; }
  79. /// <summary>
  80. /// Gets or sets a value indicating whether we check for updates when plugin starts.
  81. /// </summary>
  82. [SettingsEntry(true, "Automatically Check For Updates")]
  83. public bool AutomaticallyCheckForUpdates
  84. {
  85. get
  86. {
  87. return this.automaticallyCheckForUpdates;
  88. }
  89. set
  90. {
  91. this.automaticallyCheckForUpdates = value;
  92. SetRegistry("AutomaticallyCheckForUpdates", value, RegistryValueKind.DWord);
  93. }
  94. }
  95. /// <summary>
  96. /// Gets or sets DashesCountInFileHeader.
  97. /// </summary>
  98. [SettingsEntry(116, "Dashes Count In File Header")]
  99. public int DashesCountInFileHeader { get; set; }
  100. /// <summary>
  101. /// Gets or sets DaysBetweenUpdateChecks.
  102. /// </summary>
  103. [SettingsEntry(7, "Days Between Update Checks")]
  104. public int DaysBetweenUpdateChecks
  105. {
  106. get
  107. {
  108. return this.daysBetweenUpdateChecks;
  109. }
  110. set
  111. {
  112. this.daysBetweenUpdateChecks = value;
  113. SetRegistry("DaysBetweenUpdateChecks", value, RegistryValueKind.DWord);
  114. }
  115. }
  116. /// <summary>
  117. /// Gets or sets a value indicating whether descriptive text should be inserted into missing documentation headers.
  118. /// </summary>
  119. [SettingsEntry(true, "Insert Text Into Documentation")]
  120. public bool InsertTextIntoDocumentation { get; set; }
  121. /// <summary>
  122. /// Gets or sets the last update check date.
  123. /// </summary>
  124. [SettingsEntry("1900-01-01", "Last Update Check Date")]
  125. public string LastUpdateCheckDate { get; set; }
  126. /// <summary>
  127. /// Gets or sets the ParsingPerformance value. 9 means every time R# calls us, 8 means after 1 second, 7 means after 2 seconds, etc.
  128. /// </summary>
  129. /// <value>
  130. /// The performance value.
  131. /// </value>
  132. [SettingsEntry(7, "Parsing Performance")]
  133. public int ParsingPerformance { get; set; }
  134. /// <summary>
  135. /// Gets or sets the Specified Assembly Path.
  136. /// </summary>
  137. /// <value>
  138. /// The allow null attribute.
  139. /// </value>
  140. [SettingsEntry("", "Specified Assembly Path")]
  141. public string SpecifiedAssemblyPath { get; set; }
  142. /// <summary>
  143. /// Gets or sets the text for inserting suppress message attributes.
  144. /// </summary>
  145. [SettingsEntry("Reviewed. Suppression is OK here.", "Suppress StyleCop Attribute Justification Text")]
  146. public string SuppressStyleCopAttributeJustificationText { get; set; }
  147. /// <summary>
  148. /// Gets or sets a value indicating whether to use exclude from style cop setting.
  149. /// </summary>
  150. [SettingsEntry(true, "Use Exclude From StyleCop Setting")]
  151. public bool UseExcludeFromStyleCopSetting { get; set; }
  152. /// <summary>
  153. /// Gets or sets a value indicating whether declaration comments should be multi line or single line.
  154. /// </summary>
  155. [SettingsEntry(false, "Use Single Line Declaration Comments")]
  156. public bool UseSingleLineDeclarationComments { get; set; }
  157. #endregion
  158. #region Public Methods and Operators
  159. /// <summary>
  160. /// Detects the style cop path.
  161. /// </summary>
  162. /// <returns>
  163. /// The path to the detected StyleCop assembly.
  164. /// </returns>
  165. public static string DetectStyleCopPath()
  166. {
  167. string assemblyPath = GetStyleCopPath();
  168. return StyleCopReferenceHelper.LocationValid(assemblyPath) ? assemblyPath : null;
  169. }
  170. /// <summary>
  171. /// Gets the assembly location.
  172. /// </summary>
  173. /// <returns>
  174. /// The path to the StyleCop assembly.
  175. /// </returns>
  176. public string GetAssemblyPath()
  177. {
  178. if (!this.attemptedToGetStyleCopPath)
  179. {
  180. this.attemptedToGetStyleCopPath = true;
  181. if (!string.IsNullOrEmpty(this.SpecifiedAssemblyPath))
  182. {
  183. if (StyleCopReferenceHelper.LocationValid(this.SpecifiedAssemblyPath))
  184. {
  185. this.styleCopDetectedPath = this.SpecifiedAssemblyPath;
  186. return this.styleCopDetectedPath;
  187. }
  188. // Location not valid. Blank it and automatically get location
  189. this.SpecifiedAssemblyPath = null;
  190. }
  191. this.styleCopDetectedPath = DetectStyleCopPath();
  192. if (string.IsNullOrEmpty(this.styleCopDetectedPath))
  193. {
  194. MessageBox.Show(
  195. string.Format("Failed to find the StyleCop Assembly. Please check your StyleCop installation."),
  196. "Error Finding StyleCop Assembly",
  197. MessageBoxButtons.OK,
  198. MessageBoxIcon.Error);
  199. }
  200. }
  201. return this.styleCopDetectedPath;
  202. }
  203. #endregion
  204. #region Methods
  205. /// <summary>
  206. /// Gets the StyleCop assembly path.
  207. /// </summary>
  208. /// <returns>
  209. /// The path to the StyleCop assembly or null if not found.
  210. /// </returns>
  211. private static string GetStyleCopPath()
  212. {
  213. string directory = RetrieveFromRegistry() ?? Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  214. return directory == null ? directory : Path.Combine(directory, Constants.StyleCopAssemblyName);
  215. }
  216. /// <summary>
  217. /// Gets the StyleCop install location from the registry. This registry key is created by StyleCop during install.
  218. /// </summary>
  219. /// <returns>
  220. /// Returns the registry key value or null if not found.
  221. /// </returns>
  222. private static string RetrieveFromRegistry()
  223. {
  224. const string SubKey = @"SOFTWARE\CodePlex\StyleCop";
  225. const string Key = "InstallDir";
  226. RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(SubKey);
  227. return registryKey == null ? null : registryKey.GetValue(Key) as string;
  228. }
  229. /// <summary>
  230. /// Sets a registry key value in the registry.
  231. /// </summary>
  232. /// <param name="key">
  233. /// The sub key to create.
  234. /// </param>
  235. /// <param name="value">
  236. /// The value to use.
  237. /// </param>
  238. /// <param name="valueKind">
  239. /// The type of registry key value to set.
  240. /// </param>
  241. private static void SetRegistry(string key, object value, RegistryValueKind valueKind)
  242. {
  243. const string SubKey = @"SOFTWARE\CodePlex\StyleCop";
  244. RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(SubKey);
  245. if (registryKey != null)
  246. {
  247. registryKey.SetValue(key, value, valueKind);
  248. }
  249. }
  250. #endregion
  251. }
  252. }