Skip to content

Commit 7d6c72a

Browse files
Added utility method for firing command
Fixes #64
1 parent cafc1a2 commit 7d6c72a

File tree

5 files changed

+74
-22
lines changed

5 files changed

+74
-22
lines changed

src/Community.VisualStudio.Toolkit.Shared/Commands/Commanding.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.ComponentModel.Design;
3+
using System.Runtime.InteropServices;
4+
using System.Threading.Tasks;
5+
using Microsoft.VisualStudio;
6+
using Microsoft.VisualStudio.OLE.Interop;
7+
using Microsoft.VisualStudio.Shell;
8+
using Microsoft.VisualStudio.Shell.Interop;
9+
10+
namespace Community.VisualStudio.Toolkit
11+
{
12+
/// <summary>A collection of services related to the command system.</summary>
13+
public class Commands
14+
{
15+
internal Commands()
16+
{ }
17+
18+
/// <summary>Provides methods to manage the global designer verbs and menu commands available in design mode, and to show some types of shortcut menus.</summary>
19+
public Task<IMenuCommandService> GetCommandServiceAsync() => VS.GetRequiredServiceAsync<IMenuCommandService, IMenuCommandService>();
20+
21+
/// <summary>Used to register and unregister a command target as a high priority command handler.</summary>
22+
public Task<IVsRegisterPriorityCommandTarget> GetPriorityCommandTargetAsync() => VS.GetRequiredServiceAsync<SVsRegisterPriorityCommandTarget, IVsRegisterPriorityCommandTarget>();
23+
24+
/// <summary>
25+
/// Executes a command by name
26+
/// </summary>
27+
/// <returns>Returns <see langword="true"/> if the command was succesfully executed; otherwise <see langword="false"/>.</returns>
28+
public async Task<bool> ExecuteAsync(string name, string argument = "")
29+
{
30+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
31+
IVsCommandWindow cw = await VS.Windows.GetCommandWindowAsync();
32+
33+
return cw.ExecuteCommand($"{name} {argument}") == VSConstants.S_OK;
34+
}
35+
36+
/// <summary>
37+
/// Executes a command by guid and ID
38+
/// </summary>
39+
/// <returns>Returns <see langword="true"/> if the command was succesfully executed; otherwise <see langword="false"/>.</returns>
40+
public async Task<bool> ExecuteAsync(Guid menuGroup, uint commandId, string argument = "")
41+
{
42+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
43+
IOleCommandTarget cs = await VS.GetRequiredServiceAsync<SUIHostCommandDispatcher, IOleCommandTarget>();
44+
45+
IntPtr inArgPtr = Marshal.AllocCoTaskMem(200);
46+
Marshal.GetNativeVariantForObject(argument, inArgPtr);
47+
48+
var result = cs.Exec(menuGroup, commandId, (uint)OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, inArgPtr, IntPtr.Zero);
49+
50+
return result == VSConstants.S_OK;
51+
}
52+
53+
/// <summary>
54+
/// Executes a command by guid and ID
55+
/// </summary>
56+
/// <returns>Returns <see langword="true"/> if the command was succesfully executed; otherwise <see langword="false"/>.</returns>
57+
public Task<bool> ExecuteAsync(VSConstants.VSStd97CmdID command, string argument = "")
58+
{
59+
return ExecuteAsync(typeof(VSConstants.VSStd97CmdID).GUID, (uint)command, argument);
60+
}
61+
62+
/// <summary>
63+
/// Executes a command by guid and ID
64+
/// </summary>
65+
/// <returns>Returns <see langword="true"/> if the command was succesfully executed; otherwise <see langword="false"/>.</returns>
66+
public Task<bool> ExecuteAsync(VSConstants.VSStd2KCmdID command, string argument = "")
67+
{
68+
return ExecuteAsync(typeof(VSConstants.VSStd2KCmdID).GUID, (uint)command, argument);
69+
}
70+
}
71+
}

src/Community.VisualStudio.Toolkit.Shared/Events/BuildEvents.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal BuildEvents()
1515
{
1616
ThreadHelper.ThrowIfNotOnUIThread();
1717
var svc = ServiceProvider.GlobalProvider.GetService(typeof(SVsSolutionBuildManager)) as IVsSolutionBuildManager;
18-
Assumes.Present(svc);
18+
Assumes.Present(svc);
1919
svc!.AdviseUpdateSolutionEvents(this, out _);
2020
}
2121

src/Community.VisualStudio.Toolkit.Shared/VS.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Community.VisualStudio.Toolkit
1313
public static class VS
1414
{
1515
/// <summary>A collection of services related to the command system.</summary>
16-
public static Commanding Commanding => new();
16+
public static Commands Commands => new();
1717

1818
/// <summary>A collection of services related to the debugger.</summary>
1919
public static Debugger Debugger => new();

src/Community.VisualStudio.Toolkit.Shared/VSSDK.Helpers.Shared.projitems

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<Compile Include="$(MSBuildThisFileDirectory)Themes\ToolkitResourceKeys.cs" />
4343
<Compile Include="$(MSBuildThisFileDirectory)Themes\Themes.cs" />
4444
<Compile Include="$(MSBuildThisFileDirectory)Notifications\MessageBox.cs" />
45-
<Compile Include="$(MSBuildThisFileDirectory)Commands\Commanding.cs" />
45+
<Compile Include="$(MSBuildThisFileDirectory)Commands\Commands.cs" />
4646
<Compile Include="$(MSBuildThisFileDirectory)Events\Events.cs" />
4747
<Compile Include="$(MSBuildThisFileDirectory)ToolkitPackage.cs" />
4848
<Compile Include="$(MSBuildThisFileDirectory)ToolWindows\BaseToolWindow.cs" />

0 commit comments

Comments
 (0)