Skip to content

Commit

Permalink
Promote ExecuteCommand up to CommandHelper and update UsingStatementC…
Browse files Browse the repository at this point in the history
…leanupLogic to utilize it as well.
  • Loading branch information
codecadwallader committed Dec 11, 2016
1 parent 802e158 commit 6380927
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 34 deletions.
28 changes: 27 additions & 1 deletion CodeMaid/Helpers/CommandHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EnvDTE;
using System;
using EnvDTE;
using System.Linq;

namespace SteveCadwallader.CodeMaid.Helpers
Expand Down Expand Up @@ -67,6 +68,31 @@ public Command FindCommand(string guid, int id)
return _package.IDE.Commands.OfType<Command>().FirstOrDefault(x => x.Guid == guid && x.ID == id);
}

/// <summary>
/// Executes the specified command when available against the specified text document.
/// </summary>
/// <param name="textDocument">The text document to cleanup.</param>
/// <param name="commandNames">The cleanup command name(s).</param>
public void ExecuteCommand(TextDocument textDocument, params string[] commandNames)
{
try
{
var command = FindCommand(commandNames);
if (command != null && command.IsAvailable)
{
using (new CursorPositionRestorer(textDocument))
{
_package.IDE.ExecuteCommand(command.Name, string.Empty);
}
}
}
catch (Exception ex)
{
// OK if fails, not available for some file types.
OutputWindowHelper.DiagnosticWriteLine($"Unable to execute command(s) {string.Join(",", commandNames)} on {textDocument.Parent.FullName}", ex);
}
}

#endregion Methods
}
}
35 changes: 5 additions & 30 deletions CodeMaid/Logic/Cleaning/CodeCleanupManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ private void RunVisualStudioFormatDocument(TextDocument textDocument)
{
if (!Settings.Default.Cleaning_RunVisualStudioFormatDocumentCommand) return;

ExecuteCommand(textDocument, "Edit.FormatDocument");
_commandHelper.ExecuteCommand(textDocument, "Edit.FormatDocument");
}

/// <summary>
Expand All @@ -456,8 +456,7 @@ private void RunJetBrainsReSharperCleanup(TextDocument textDocument)

// This command changed to include the leading 'ReSharper.' in version 2016.1.
// Execute both commands for backwards compatibility.
ExecuteCommand(textDocument, "ReSharper_SilentCleanupCode");
ExecuteCommand(textDocument, "ReSharper.ReSharper_SilentCleanupCode");
_commandHelper.ExecuteCommand(textDocument, "ReSharper_SilentCleanupCode", "ReSharper.ReSharper_SilentCleanupCode");
}

/// <summary>
Expand All @@ -468,7 +467,7 @@ private void RunTelerikJustCodeCleanup(TextDocument textDocument)
{
if (!Settings.Default.ThirdParty_UseTelerikJustCodeCleanup) return;

ExecuteCommand(textDocument, "JustCode.JustCode_CleanCodeWithDefaultProfile");
_commandHelper.ExecuteCommand(textDocument, "JustCode.JustCode_CleanCodeWithDefaultProfile");
}

/// <summary>
Expand All @@ -479,7 +478,7 @@ private void RunXAMLStylerCleanup(TextDocument textDocument)
{
if (!Settings.Default.ThirdParty_UseXAMLStylerCleanup) return;

ExecuteCommand(textDocument, "EditorContextMenus.XAMLEditor.BeautifyXaml", "EditorContextMenus.XAMLEditor.FormatXAML");
_commandHelper.ExecuteCommand(textDocument, "EditorContextMenus.XAMLEditor.BeautifyXaml", "EditorContextMenus.XAMLEditor.FormatXAML");
}

/// <summary>
Expand All @@ -492,31 +491,7 @@ private void RunOtherCleanupCommands(TextDocument textDocument)

foreach (var commandName in _otherCleaningCommands.Value)
{
ExecuteCommand(textDocument, commandName);
}
}

/// <summary>
/// Executes the specified cleanup command when available against the specified text document.
/// </summary>
/// <param name="textDocument">The text document to cleanup.</param>
/// <param name="commandNames">The cleanup command name(s).</param>
private void ExecuteCommand(TextDocument textDocument, params string[] commandNames)
{
try
{
var command = _commandHelper.FindCommand(commandNames);
if (command != null && command.IsAvailable)
{
using (new CursorPositionRestorer(textDocument))
{
_package.IDE.ExecuteCommand(command.Name, string.Empty);
}
}
}
catch
{
// OK if fails, not available for some file types.
_commandHelper.ExecuteCommand(textDocument, commandName);
}
}

Expand Down
9 changes: 6 additions & 3 deletions CodeMaid/Logic/Cleaning/UsingStatementCleanupLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal class UsingStatementCleanupLogic
#region Fields

private readonly CodeMaidPackage _package;
private readonly CommandHelper _commandHelper;

private readonly CachedSettingSet<string> _usingStatementsToReinsertWhenRemoved =
new CachedSettingSet<string>(() => Settings.Default.Cleaning_UsingStatementsToReinsertWhenRemovedExpression,
Expand Down Expand Up @@ -49,6 +50,8 @@ internal static UsingStatementCleanupLogic GetInstance(CodeMaidPackage package)
private UsingStatementCleanupLogic(CodeMaidPackage package)
{
_package = package;

_commandHelper = CommandHelper.GetInstance(_package);
}

#endregion Constructors
Expand Down Expand Up @@ -83,12 +86,12 @@ from editPoint in TextDocumentHelper.FindMatches(textDocument, string.Format(pat

if (_package.IDEVersion >= 15)
{
_package.IDE.ExecuteCommand("Edit.RemoveAndSort", string.Empty);
_commandHelper.ExecuteCommand(textDocument, "Edit.RemoveAndSort");
}
else
{
_package.IDE.ExecuteCommand("Edit.RemoveUnusedUsings", string.Empty);
_package.IDE.ExecuteCommand("Edit.SortUsings", string.Empty);
_commandHelper.ExecuteCommand(textDocument, "Edit.RemoveUnusedUsings");
_commandHelper.ExecuteCommand(textDocument, "Edit.SortUsings");
}

// Check each using statement point and re-insert it if removed.
Expand Down

0 comments on commit 6380927

Please sign in to comment.