Skip to content

Commit 6380927

Browse files
Promote ExecuteCommand up to CommandHelper and update UsingStatementCleanupLogic to utilize it as well.
1 parent 802e158 commit 6380927

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

CodeMaid/Helpers/CommandHelper.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using EnvDTE;
1+
using System;
2+
using EnvDTE;
23
using System.Linq;
34

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

71+
/// <summary>
72+
/// Executes the specified command when available against the specified text document.
73+
/// </summary>
74+
/// <param name="textDocument">The text document to cleanup.</param>
75+
/// <param name="commandNames">The cleanup command name(s).</param>
76+
public void ExecuteCommand(TextDocument textDocument, params string[] commandNames)
77+
{
78+
try
79+
{
80+
var command = FindCommand(commandNames);
81+
if (command != null && command.IsAvailable)
82+
{
83+
using (new CursorPositionRestorer(textDocument))
84+
{
85+
_package.IDE.ExecuteCommand(command.Name, string.Empty);
86+
}
87+
}
88+
}
89+
catch (Exception ex)
90+
{
91+
// OK if fails, not available for some file types.
92+
OutputWindowHelper.DiagnosticWriteLine($"Unable to execute command(s) {string.Join(",", commandNames)} on {textDocument.Parent.FullName}", ex);
93+
}
94+
}
95+
7096
#endregion Methods
7197
}
7298
}

CodeMaid/Logic/Cleaning/CodeCleanupManager.cs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ private void RunVisualStudioFormatDocument(TextDocument textDocument)
443443
{
444444
if (!Settings.Default.Cleaning_RunVisualStudioFormatDocumentCommand) return;
445445

446-
ExecuteCommand(textDocument, "Edit.FormatDocument");
446+
_commandHelper.ExecuteCommand(textDocument, "Edit.FormatDocument");
447447
}
448448

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

457457
// This command changed to include the leading 'ReSharper.' in version 2016.1.
458458
// Execute both commands for backwards compatibility.
459-
ExecuteCommand(textDocument, "ReSharper_SilentCleanupCode");
460-
ExecuteCommand(textDocument, "ReSharper.ReSharper_SilentCleanupCode");
459+
_commandHelper.ExecuteCommand(textDocument, "ReSharper_SilentCleanupCode", "ReSharper.ReSharper_SilentCleanupCode");
461460
}
462461

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

471-
ExecuteCommand(textDocument, "JustCode.JustCode_CleanCodeWithDefaultProfile");
470+
_commandHelper.ExecuteCommand(textDocument, "JustCode.JustCode_CleanCodeWithDefaultProfile");
472471
}
473472

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

482-
ExecuteCommand(textDocument, "EditorContextMenus.XAMLEditor.BeautifyXaml", "EditorContextMenus.XAMLEditor.FormatXAML");
481+
_commandHelper.ExecuteCommand(textDocument, "EditorContextMenus.XAMLEditor.BeautifyXaml", "EditorContextMenus.XAMLEditor.FormatXAML");
483482
}
484483

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

493492
foreach (var commandName in _otherCleaningCommands.Value)
494493
{
495-
ExecuteCommand(textDocument, commandName);
496-
}
497-
}
498-
499-
/// <summary>
500-
/// Executes the specified cleanup command when available against the specified text document.
501-
/// </summary>
502-
/// <param name="textDocument">The text document to cleanup.</param>
503-
/// <param name="commandNames">The cleanup command name(s).</param>
504-
private void ExecuteCommand(TextDocument textDocument, params string[] commandNames)
505-
{
506-
try
507-
{
508-
var command = _commandHelper.FindCommand(commandNames);
509-
if (command != null && command.IsAvailable)
510-
{
511-
using (new CursorPositionRestorer(textDocument))
512-
{
513-
_package.IDE.ExecuteCommand(command.Name, string.Empty);
514-
}
515-
}
516-
}
517-
catch
518-
{
519-
// OK if fails, not available for some file types.
494+
_commandHelper.ExecuteCommand(textDocument, commandName);
520495
}
521496
}
522497

CodeMaid/Logic/Cleaning/UsingStatementCleanupLogic.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ internal class UsingStatementCleanupLogic
1414
#region Fields
1515

1616
private readonly CodeMaidPackage _package;
17+
private readonly CommandHelper _commandHelper;
1718

1819
private readonly CachedSettingSet<string> _usingStatementsToReinsertWhenRemoved =
1920
new CachedSettingSet<string>(() => Settings.Default.Cleaning_UsingStatementsToReinsertWhenRemovedExpression,
@@ -49,6 +50,8 @@ internal static UsingStatementCleanupLogic GetInstance(CodeMaidPackage package)
4950
private UsingStatementCleanupLogic(CodeMaidPackage package)
5051
{
5152
_package = package;
53+
54+
_commandHelper = CommandHelper.GetInstance(_package);
5255
}
5356

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

8487
if (_package.IDEVersion >= 15)
8588
{
86-
_package.IDE.ExecuteCommand("Edit.RemoveAndSort", string.Empty);
89+
_commandHelper.ExecuteCommand(textDocument, "Edit.RemoveAndSort");
8790
}
8891
else
8992
{
90-
_package.IDE.ExecuteCommand("Edit.RemoveUnusedUsings", string.Empty);
91-
_package.IDE.ExecuteCommand("Edit.SortUsings", string.Empty);
93+
_commandHelper.ExecuteCommand(textDocument, "Edit.RemoveUnusedUsings");
94+
_commandHelper.ExecuteCommand(textDocument, "Edit.SortUsings");
9295
}
9396

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

0 commit comments

Comments
 (0)