|
5 | 5 | using Microsoft.VisualStudio.OLE.Interop; |
6 | 6 | using Microsoft.VisualStudio.Shell; |
7 | 7 | using Microsoft.VisualStudio.Shell.Interop; |
8 | | -using Task = System.Threading.Tasks.Task; |
9 | 8 |
|
10 | 9 | namespace Community.VisualStudio.Toolkit |
11 | 10 | { |
@@ -93,32 +92,38 @@ public Task<bool> ExecuteAsync(CommandID cmd, string argument = "") |
93 | 92 | /// <summary> |
94 | 93 | /// Intercept any command before it is being handled by other command handlers. |
95 | 94 | /// </summary> |
96 | | - public Task InterceptAsync(VSConstants.VSStd97CmdID command, Func<CommandProgression> func) |
| 95 | + /// <returns>Returns an <see cref="IDisposable"/> that will remove the command interceptor when disposed.</returns> |
| 96 | + public Task<IDisposable> InterceptAsync(VSConstants.VSStd97CmdID command, Func<CommandProgression> func) |
97 | 97 | => InterceptAsync(typeof(VSConstants.VSStd97CmdID).GUID, (int)command, func); |
98 | 98 |
|
99 | 99 | /// <summary> |
100 | 100 | /// Intercept any command before it is being handled by other command handlers. |
101 | 101 | /// </summary> |
102 | | - public Task InterceptAsync(VSConstants.VSStd2KCmdID command, Func<CommandProgression> func) |
| 102 | + /// <returns>Returns an <see cref="IDisposable"/> that will remove the command interceptor when disposed.</returns> |
| 103 | + public Task<IDisposable> InterceptAsync(VSConstants.VSStd2KCmdID command, Func<CommandProgression> func) |
103 | 104 | => InterceptAsync(typeof(VSConstants.VSStd2KCmdID).GUID, (int)command, func); |
104 | 105 |
|
105 | 106 | /// <summary> |
106 | 107 | /// Intercept any command before it is being handled by other command handlers. |
107 | 108 | /// </summary> |
108 | | - public Task InterceptAsync(Guid menuGroup, int commandId, Func<CommandProgression> func) |
| 109 | + /// <returns>Returns an <see cref="IDisposable"/> that will remove the command interceptor when disposed.</returns> |
| 110 | + public Task<IDisposable> InterceptAsync(Guid menuGroup, int commandId, Func<CommandProgression> func) |
109 | 111 | => InterceptAsync(new CommandID(menuGroup, commandId), func); |
110 | 112 |
|
111 | 113 | /// <summary> |
112 | 114 | /// Intercept any command before it is being handled by other command handlers. |
113 | 115 | /// </summary> |
114 | | - public async Task InterceptAsync(CommandID cmd, Func<CommandProgression> func) |
| 116 | + /// <returns>Returns an <see cref="IDisposable"/> that will remove the command interceptor when disposed.</returns> |
| 117 | + public async Task<IDisposable> InterceptAsync(CommandID cmd, Func<CommandProgression> func) |
115 | 118 | { |
116 | 119 | await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); |
117 | 120 |
|
118 | 121 | IVsRegisterPriorityCommandTarget? priority = await VS.Services.GetPriorityCommandTargetAsync(); |
119 | 122 | CommandInterceptor interceptor = new(cmd, func); |
120 | 123 |
|
121 | | - ErrorHandler.ThrowOnFailure(priority.RegisterPriorityCommandTarget(0, interceptor, out _)); |
| 124 | + ErrorHandler.ThrowOnFailure(priority.RegisterPriorityCommandTarget(0, interceptor, out uint cookie)); |
| 125 | + |
| 126 | + return new Disposable(() => priority.UnregisterPriorityCommandTarget(cookie)); |
122 | 127 | } |
123 | 128 | } |
124 | 129 |
|
|
0 commit comments