Skip to content

Commit

Permalink
#268: Add an option to prompt the user about whether or not reorganiz…
Browse files Browse the repository at this point in the history
…ation should run in the presence of preprocessor conditionals. Follows pattern of cleanup external files.
  • Loading branch information
codecadwallader committed Apr 9, 2016
1 parent 319cb94 commit 7a9db45
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 4 deletions.
85 changes: 83 additions & 2 deletions CodeMaid/Logic/Reorganizing/CodeReorganizationAvailabilityLogic.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using EnvDTE;
using SteveCadwallader.CodeMaid.Helpers;
using SteveCadwallader.CodeMaid.Properties;
using SteveCadwallader.CodeMaid.UI.Dialogs.Prompts;
using SteveCadwallader.CodeMaid.UI.Enumerations;
using System;

namespace SteveCadwallader.CodeMaid.Logic.Reorganizing
{
Expand Down Expand Up @@ -57,20 +61,54 @@ internal bool IsReorganizationEnvironmentAvailable()
/// Determines if the specified document can be reorganized.
/// </summary>
/// <param name="document">The document.</param>
/// <param name="allowUserPrompts">A flag indicating if user prompts should be allowed.</param>
/// <returns>True if item can be reorganized, otherwise false.</returns>
internal bool CanReorganize(Document document)
internal bool CanReorganize(Document document, bool allowUserPrompts = false)
{
return IsReorganizationEnvironmentAvailable() &&
document != null &&
document.GetCodeLanguage() == CodeLanguage.CSharp &&
!document.IsExternal() &&
!HasPreprocessorConditionalCompilationDirectives(document);
!IsDocumentExcludedBecausePreprocessorConditionals(document, allowUserPrompts);
}

#endregion Internal Methods

#region Private Methods

/// <summary>
/// Determines whether the specified document should be excluded because it contains
/// preprocessor conditionals. Conditionally includes prompting the user.
/// </summary>
/// <param name="document">The document.</param>
/// <param name="allowUserPrompts">A flag indicating if user prompts should be allowed.</param>
/// <returns>
/// True if document should be excluded because of preprocessor conditionals, otherwise false.
/// </returns>
private bool IsDocumentExcludedBecausePreprocessorConditionals(Document document, bool allowUserPrompts)
{
if (!HasPreprocessorConditionalCompilationDirectives(document)) return false;

switch ((AskYesNo)Settings.Default.Reorganizing_PerformWhenPreprocessorConditionals)
{
case AskYesNo.Ask:
if (allowUserPrompts)
{
return !PromptUserAboutReorganizingPreprocessorConditionals(document);
}
break;

case AskYesNo.Yes:
return false;

case AskYesNo.No:
return true;
}

// If unresolved, assume exclusion.
return true;
}

/// <summary>
/// Determines if the specified document contains preprocessor conditional compilation directives.
/// </summary>
Expand All @@ -93,6 +131,49 @@ private bool HasPreprocessorConditionalCompilationDirectives(Document document)
return false;
}

/// <summary>
/// Prompts the user about reorganizing files with preprocessor conditionals.
/// </summary>
/// <param name="document">The document.</param>
/// <returns>True if files with preprocessor conditionals should be reorganized, otherwise false.</returns>
private static bool PromptUserAboutReorganizingPreprocessorConditionals(Document document)
{
try
{
var viewModel = new YesNoPromptViewModel
{
Title = @"CodeMaid: Reorganize Preprocessor Conditionals",
Message = document.Name + " contains preprocessor conditionals (e.g. #if, #pragma) which reorganization does not currently support." +
Environment.NewLine + Environment.NewLine +
"Do you want to reorganize anyways (DANGEROUS)?",
CanRemember = true
};

var window = new YesNoPromptWindow { DataContext = viewModel };
var response = window.ShowModal();

if (!response.HasValue)
{
return false;
}

if (viewModel.Remember)
{
var preference = (int)(response.Value ? AskYesNo.Yes : AskYesNo.No);

Settings.Default.Reorganizing_PerformWhenPreprocessorConditionals = preference;
Settings.Default.Save();
}

return response.Value;
}
catch (Exception ex)
{
OutputWindowHelper.ExceptionWriteLine("Unable to prompt user about reorganizing preprocessor conditionals", ex);
return false;
}
}

#endregion Private Methods
}
}
2 changes: 1 addition & 1 deletion CodeMaid/Logic/Reorganizing/CodeReorganizationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ internal void MoveItemIntoBase(BaseCodeItem itemToMove, ICodeItemParent baseItem
/// <param name="document">The document for reorganizing.</param>
internal void Reorganize(Document document)
{
if (!_codeReorganizationAvailabilityLogic.CanReorganize(document)) return;
if (!_codeReorganizationAvailabilityLogic.CanReorganize(document, true)) return;

new UndoTransactionHelper(_package, $"CodeMaid Reorganize for '{document.Name}'").Run(
delegate
Expand Down
12 changes: 12 additions & 0 deletions CodeMaid/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions CodeMaid/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,9 @@
<Setting Name="Reorganizing_MemberTypeStructs" Type="System.String" Scope="User">
<Value Profile="(Default)">Structs||11||Structs</Value>
</Setting>
<Setting Name="Reorganizing_PerformWhenPreprocessorConditionals" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="Reorganizing_PrimaryOrderByAccessLevel" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SteveCadwallader.CodeMaid.UI.Dialogs.Options.Reorganizing"
xmlns:cnv="clr-namespace:SteveCadwallader.CodeMaid.UI.Converters">
xmlns:cnv="clr-namespace:SteveCadwallader.CodeMaid.UI.Converters"
xmlns:enums="clr-namespace:SteveCadwallader.CodeMaid.UI.Enumerations">
<DataTemplate DataType="{x:Type local:ReorganizingGeneralViewModel}">
<StackPanel>
<GroupBox Header="General">
Expand All @@ -19,6 +20,15 @@
<GroupBox Header="Cleanup">
<CheckBox Content="Run reorganize at start of cleanup" IsChecked="{Binding RunAtStartOfCleanup}" />
</GroupBox>

<GroupBox Header="Conditions">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Perform reorganization when preprocessor conditionals are present" VerticalAlignment="Center" />
<RadioButton Content="ask" IsChecked="{Binding PerformWhenPreprocessorConditionals, Converter={x:Static cnv:EnumToBooleanConverter.Default}, ConverterParameter={x:Static enums:AskYesNo.Ask}}" />
<RadioButton Content="yes" IsChecked="{Binding PerformWhenPreprocessorConditionals, Converter={x:Static cnv:EnumToBooleanConverter.Default}, ConverterParameter={x:Static enums:AskYesNo.Yes}}" />
<RadioButton Content="no" IsChecked="{Binding PerformWhenPreprocessorConditionals, Converter={x:Static cnv:EnumToBooleanConverter.Default}, ConverterParameter={x:Static enums:AskYesNo.No}}" />
</StackPanel>
</GroupBox>
</StackPanel>
</DataTemplate>
</ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SteveCadwallader.CodeMaid.Properties;
using SteveCadwallader.CodeMaid.UI.Enumerations;

namespace SteveCadwallader.CodeMaid.UI.Dialogs.Options.Reorganizing
{
Expand All @@ -21,6 +22,7 @@ public ReorganizingGeneralViewModel(CodeMaidPackage package, Settings activeSett
{
new SettingToOptionMapping<bool, bool>(x => ActiveSettings.Reorganizing_AlphabetizeMembersOfTheSameGroup, x => AlphabetizeMembersOfTheSameGroup),
new SettingToOptionMapping<bool, bool>(x => ActiveSettings.Reorganizing_KeepMembersWithinRegions, x => KeepMembersWithinRegions),
new SettingToOptionMapping<int, AskYesNo>(x => ActiveSettings.Reorganizing_PerformWhenPreprocessorConditionals, x => PerformWhenPreprocessorConditionals),
new SettingToOptionMapping<bool, bool>(x => ActiveSettings.Reorganizing_PrimaryOrderByAccessLevel, x => PrimaryOrderByAccessLevel),
new SettingToOptionMapping<bool, bool>(x => ActiveSettings.Reorganizing_RunAtStartOfCleanup, x => RunAtStartOfCleanup)
};
Expand Down Expand Up @@ -57,6 +59,15 @@ public bool KeepMembersWithinRegions
set { SetPropertyValue(value); }
}

/// <summary>
/// Gets or sets the options for performing reorganization when preprocessor conditionals are present.
/// </summary>
public AskYesNo PerformWhenPreprocessorConditionals
{
get { return GetPropertyValue<AskYesNo>(); }
set { SetPropertyValue(value); }
}

/// <summary>
/// Gets or sets the flag indicating if primary ordering should be by access level.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions CodeMaid/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,10 @@
<setting name="Reorganizing_MemberTypeStructs" serializeAs="String">
<value>Structs||11||Structs</value>
</setting>
<setting name="Reorganizing_PerformWhenPreprocessorConditionals"
serializeAs="String">
<value>0</value>
</setting>
<setting name="Reorganizing_PrimaryOrderByAccessLevel" serializeAs="String">
<value>False</value>
</setting>
Expand Down

0 comments on commit 7a9db45

Please sign in to comment.