Skip to content

Commit

Permalink
Merge pull request #4 from matkoch/master
Browse files Browse the repository at this point in the history
Fix CodeCleanup descriptors
  • Loading branch information
citizenmatt authored Apr 7, 2017
2 parents b6d5149 + 9487c39 commit 6a0c766
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 110 deletions.
13 changes: 7 additions & 6 deletions src/StyleCop.ReSharper/CodeCleanup/StyleCopCodeCleanupModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public class StyleCopCodeCleanupModule : ICodeCleanupModule
/// <summary>
/// StyleCop descriptor.
/// </summary>
public static readonly StyleCopDescriptor Descriptor = new StyleCopDescriptor();
public static readonly FixViolationsDescriptor FIX_VIOLATIONS = new FixViolationsDescriptor();
public static readonly CreateXmlDocStubsDescriptor CREATE_XML_DOC_STUB = new CreateXmlDocStubsDescriptor ();

/// <summary>
/// Gets the collection of option descriptors.
Expand All @@ -59,7 +60,7 @@ public ICollection<CodeCleanupOptionDescriptor> Descriptors
{
get
{
return new CodeCleanupOptionDescriptor[] { Descriptor };
return new CodeCleanupOptionDescriptor[] { FIX_VIOLATIONS, CREATE_XML_DOC_STUB };
}
}

Expand Down Expand Up @@ -138,15 +139,14 @@ public void Process(
return;
}

StyleCopCodeCleanupOptions options = profile.GetSetting(Descriptor);

if (!options.FixViolations)
if (!profile.GetSetting (FIX_VIOLATIONS))
{
return;
}

var services = solution.GetPsiServices();
services.Transactions.Execute("Code cleanup", () => this.InternalProcess(projectFile.ToProjectFile(), file, options.CreateXmlDocStubs));
services.Transactions.Execute("Code cleanup", () => this.InternalProcess(projectFile.ToProjectFile(), file, profile.GetSetting(CREATE_XML_DOC_STUB)));

StyleCopTrace.Out();
}
Expand All @@ -162,7 +162,8 @@ public void Process(
/// </param>
public void SetDefaultSetting(CodeCleanupProfile profile, CodeCleanup.DefaultProfileType profileType)
{
profile.SetSetting(Descriptor, new StyleCopCodeCleanupOptions());
profile.SetSetting(FIX_VIOLATIONS, value: true);
profile.SetSetting(CREATE_XML_DOC_STUB, value: false);
}

/// <summary>
Expand Down
113 changes: 12 additions & 101 deletions src/StyleCop.ReSharper/CodeCleanup/StyleCopDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,117 +11,28 @@ namespace StyleCop.ReSharper.CodeCleanup
{
using System;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Xml;

using JetBrains.ReSharper.Feature.Services.CodeCleanup;
using JetBrains.Util;

/// <summary>
/// The style cop descriptor.
/// </summary>
[DisplayName("Fix StyleCop violations")]
[Category(CSharpCategory)]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class StyleCopDescriptor : CodeCleanupOptionDescriptor<StyleCopCodeCleanupOptions>
[Category (CSharpCategory)]
[DisplayName ("Fix StyleCop violations")]
[DefaultValue (true)]
public class FixViolationsDescriptor : CodeCleanupBoolOptionDescriptor
{
/// <summary>
/// Initializes a new instance of the <see cref="StyleCopDescriptor"/> class.
/// </summary>
public StyleCopDescriptor()
: base("StyleCop")
public FixViolationsDescriptor ()
: base ("FixViolations")
{
}

/// <summary>
/// Summarize the options for the Code Cleanup dialog
/// </summary>
/// <param name="profile">
/// The selected profile
/// </param>
/// <returns>
/// A string that summarizes the options
/// </returns>
public override string Present(CodeCleanupProfile profile)
{
return profile.GetSetting(this).ToString();
}

/// <summary>
/// Save the options
/// </summary>
/// <param name="element">
/// The parent XML element to add the options to
/// </param>
/// <param name="value">
/// The options to save
/// </param>
public override void Save(XmlElement element, StyleCopCodeCleanupOptions value)
{
element.CreateLeafElementWithValue(
"FixViolations",
value.FixViolations.ToString(CultureInfo.InvariantCulture));
element.CreateLeafElementWithValue(
"CreateXmlDocStubs",
value.CreateXmlDocStubs.ToString(CultureInfo.InvariantCulture));
}

/// <summary>
/// Load the options
/// </summary>
/// <param name="element">
/// The parent XML element
/// </param>
/// <returns>
/// A loaded instance of the options
/// </returns>
public override StyleCopCodeCleanupOptions Load(XmlElement element)
{
return new StyleCopCodeCleanupOptions
{
FixViolations = bool.Parse(XmlUtil.ReadLeafElementValue(element, "FixViolations")),
CreateXmlDocStubs = bool.Parse(XmlUtil.ReadLeafElementValue(element, "CreateXmlDocStubs"))
};
}
}

/// <summary>
/// Options for code cleanup
/// </summary>
public class StyleCopCodeCleanupOptions
[Category (CSharpCategory)]
[DisplayName ("Create XML doc stubs")]
[DefaultValue (false)]
public class CreateXmlDocStubsDescriptor : CodeCleanupBoolOptionDescriptor
{
/// <summary>
/// Option to fix StyleCop violations
/// </summary>
[DisplayName("Fix StyleCop violations")]
public bool FixViolations { get; set; }

/// <summary>
/// Options to generate XML doc stubs
/// </summary>
[DisplayName("Create XML doc stubs")]
public bool CreateXmlDocStubs { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="StyleCopCodeCleanupOptions"/> class.
/// </summary>
public StyleCopCodeCleanupOptions()
{
this.FixViolations = true;

// TODO: What's the best default?
// I like the argument that we shouldn't blindly create XML docs, but set
// them properly, but it's nice to be able to fix up everything we can...
this.CreateXmlDocStubs = false;
}

public override string ToString()
public CreateXmlDocStubsDescriptor ()
: base ("CreateXmlDocStubs")
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(this.FixViolations ? "Yes" : "No");
stringBuilder.Append(this.CreateXmlDocStubs ? " (create XML doc stubs)" : " (do not create XML doc stubs)");
return stringBuilder.ToString();
}
}
}
8 changes: 5 additions & 3 deletions src/StyleCop.ReSharper/Options/CodeStyleOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ public static void CodeStyleOptionsReset(IContextBoundSettingsStore settingsStor

SetCodeCleanupProfileSetting(styleCopProfile, "CSReorderTypeMembers", null, true);

styleCopProfile.SetSetting(StyleCopCodeCleanupModule.Descriptor, new StyleCopCodeCleanupOptions());
styleCopProfile.SetSetting(StyleCopCodeCleanupModule.FIX_VIOLATIONS, value: true);
styleCopProfile.SetSetting(StyleCopCodeCleanupModule.CREATE_XML_DOC_STUB, value: false);

codeCleanupSettings.SetProfiles(profiles, settingsStore);
codeCleanupSettings.SetSilentCleanupProfileName(settingsStore, styleCopProfile.Name);
Expand Down Expand Up @@ -1406,8 +1407,9 @@ public static bool CodeStyleOptionsValid(IContextBoundSettingsStore settingsStor
return false;
}

StyleCopCodeCleanupOptions options = styleCopProfile.GetSetting(StyleCopCodeCleanupModule.Descriptor);
if (!options.FixViolations || options.CreateXmlDocStubs)
var fixViolations = styleCopProfile.GetSetting(StyleCopCodeCleanupModule.FIX_VIOLATIONS);
var createXmlDocStubs = styleCopProfile.GetSetting (StyleCopCodeCleanupModule.CREATE_XML_DOC_STUB);
if (!fixViolations || createXmlDocStubs)
{
return false;
}
Expand Down

0 comments on commit 6a0c766

Please sign in to comment.