From 6380927ebb96c3a5b0a1bbff2ed1e16d1140c45f Mon Sep 17 00:00:00 2001 From: Steve Cadwallader Date: Sun, 11 Dec 2016 08:23:59 -0500 Subject: [PATCH] Promote ExecuteCommand up to CommandHelper and update UsingStatementCleanupLogic to utilize it as well. --- CodeMaid/Helpers/CommandHelper.cs | 28 ++++++++++++++- CodeMaid/Logic/Cleaning/CodeCleanupManager.cs | 35 +++---------------- .../Cleaning/UsingStatementCleanupLogic.cs | 9 +++-- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/CodeMaid/Helpers/CommandHelper.cs b/CodeMaid/Helpers/CommandHelper.cs index 10ae1982..9127735b 100644 --- a/CodeMaid/Helpers/CommandHelper.cs +++ b/CodeMaid/Helpers/CommandHelper.cs @@ -1,4 +1,5 @@ -using EnvDTE; +using System; +using EnvDTE; using System.Linq; namespace SteveCadwallader.CodeMaid.Helpers @@ -67,6 +68,31 @@ public Command FindCommand(string guid, int id) return _package.IDE.Commands.OfType().FirstOrDefault(x => x.Guid == guid && x.ID == id); } + /// + /// Executes the specified command when available against the specified text document. + /// + /// The text document to cleanup. + /// The cleanup command name(s). + 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 } } \ No newline at end of file diff --git a/CodeMaid/Logic/Cleaning/CodeCleanupManager.cs b/CodeMaid/Logic/Cleaning/CodeCleanupManager.cs index 79d0fe6a..dd0f99c3 100644 --- a/CodeMaid/Logic/Cleaning/CodeCleanupManager.cs +++ b/CodeMaid/Logic/Cleaning/CodeCleanupManager.cs @@ -443,7 +443,7 @@ private void RunVisualStudioFormatDocument(TextDocument textDocument) { if (!Settings.Default.Cleaning_RunVisualStudioFormatDocumentCommand) return; - ExecuteCommand(textDocument, "Edit.FormatDocument"); + _commandHelper.ExecuteCommand(textDocument, "Edit.FormatDocument"); } /// @@ -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"); } /// @@ -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"); } /// @@ -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"); } /// @@ -492,31 +491,7 @@ private void RunOtherCleanupCommands(TextDocument textDocument) foreach (var commandName in _otherCleaningCommands.Value) { - ExecuteCommand(textDocument, commandName); - } - } - - /// - /// Executes the specified cleanup command when available against the specified text document. - /// - /// The text document to cleanup. - /// The cleanup command name(s). - 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); } } diff --git a/CodeMaid/Logic/Cleaning/UsingStatementCleanupLogic.cs b/CodeMaid/Logic/Cleaning/UsingStatementCleanupLogic.cs index 7f51880f..8e4448d7 100644 --- a/CodeMaid/Logic/Cleaning/UsingStatementCleanupLogic.cs +++ b/CodeMaid/Logic/Cleaning/UsingStatementCleanupLogic.cs @@ -14,6 +14,7 @@ internal class UsingStatementCleanupLogic #region Fields private readonly CodeMaidPackage _package; + private readonly CommandHelper _commandHelper; private readonly CachedSettingSet _usingStatementsToReinsertWhenRemoved = new CachedSettingSet(() => Settings.Default.Cleaning_UsingStatementsToReinsertWhenRemovedExpression, @@ -49,6 +50,8 @@ internal static UsingStatementCleanupLogic GetInstance(CodeMaidPackage package) private UsingStatementCleanupLogic(CodeMaidPackage package) { _package = package; + + _commandHelper = CommandHelper.GetInstance(_package); } #endregion Constructors @@ -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.