@@ -21,33 +21,56 @@ internal Commands()
2121 /// <summary>Used to register and unregister a command target as a high priority command handler.</summary>
2222 public Task < IVsRegisterPriorityCommandTarget > GetPriorityCommandTargetAsync ( ) => VS . GetRequiredServiceAsync < SVsRegisterPriorityCommandTarget , IVsRegisterPriorityCommandTarget > ( ) ;
2323
24+ /// <summary>
25+ /// Finds a command by cannonical name.
26+ /// </summary>
27+ public async Task < CommandID ? > FindCommandAsync ( string name )
28+ {
29+ await ThreadHelper . JoinableTaskFactory . SwitchToMainThreadAsync ( ) ;
30+ IVsCommandWindow cw = await VS . Windows . GetCommandWindowAsync ( ) ;
31+
32+ var hr = cw . PrepareCommand ( name , out Guid commandGroup , out var commandId , out _ , new PREPARECOMMANDRESULT [ 0 ] ) ;
33+
34+ if ( hr == VSConstants . S_OK )
35+ {
36+ return new CommandID ( commandGroup , ( int ) commandId ) ;
37+ }
38+
39+ return null ;
40+ }
41+
2442 /// <summary>
2543 /// Executes a command by name
2644 /// </summary>
2745 /// <returns>Returns <see langword="true"/> if the command was succesfully executed; otherwise <see langword="false"/>.</returns>
2846 public async Task < bool > ExecuteAsync ( string name , string argument = "" )
2947 {
30- await ThreadHelper . JoinableTaskFactory . SwitchToMainThreadAsync ( ) ;
31- IVsCommandWindow cw = await VS . Windows . GetCommandWindowAsync ( ) ;
48+ CommandID ? cmd = await FindCommandAsync ( name ) ;
49+
50+ if ( cmd != null )
51+ {
52+ return await ExecuteAsync ( cmd . Guid , cmd . ID , argument ) ;
53+ }
3254
33- return cw . ExecuteCommand ( $ " { name } { argument } " ) == VSConstants . S_OK ;
55+ return false ;
3456 }
3557
3658 /// <summary>
3759 /// Executes a command by guid and ID
3860 /// </summary>
3961 /// <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 = "" )
62+ public Task < bool > ExecuteAsync ( Guid menuGroup , int commandId , string argument = "" )
4163 {
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 ) ;
64+ return ExecuteAsync ( new CommandID ( menuGroup , commandId ) , argument ) ;
65+ }
4966
50- return result == VSConstants . S_OK ;
67+ /// <summary>
68+ /// Executes a command by guid and ID
69+ /// </summary>
70+ /// <returns>Returns <see langword="true"/> if the command was succesfully executed; otherwise <see langword="false"/>.</returns>
71+ public Task < bool > ExecuteAsync ( CommandID cmd , string argument = "" )
72+ {
73+ return cmd . ExecuteAsync ( argument ) ;
5174 }
5275
5376 /// <summary>
@@ -56,7 +79,7 @@ public async Task<bool> ExecuteAsync(Guid menuGroup, uint commandId, string argu
5679 /// <returns>Returns <see langword="true"/> if the command was succesfully executed; otherwise <see langword="false"/>.</returns>
5780 public Task < bool > ExecuteAsync ( VSConstants . VSStd97CmdID command , string argument = "" )
5881 {
59- return ExecuteAsync ( typeof ( VSConstants . VSStd97CmdID ) . GUID , ( uint ) command , argument ) ;
82+ return ExecuteAsync ( typeof ( VSConstants . VSStd97CmdID ) . GUID , ( int ) command , argument ) ;
6083 }
6184
6285 /// <summary>
@@ -65,7 +88,7 @@ public Task<bool> ExecuteAsync(VSConstants.VSStd97CmdID command, string argument
6588 /// <returns>Returns <see langword="true"/> if the command was succesfully executed; otherwise <see langword="false"/>.</returns>
6689 public Task < bool > ExecuteAsync ( VSConstants . VSStd2KCmdID command , string argument = "" )
6790 {
68- return ExecuteAsync ( typeof ( VSConstants . VSStd2KCmdID ) . GUID , ( uint ) command , argument ) ;
91+ return ExecuteAsync ( typeof ( VSConstants . VSStd2KCmdID ) . GUID , ( int ) command , argument ) ;
6992 }
7093 }
7194}
0 commit comments