Skip to content

Commit

Permalink
Merge pull request #410 from reduckted/feature/unregister-command-int…
Browse files Browse the repository at this point in the history
…erceptor

Allow command interceptors to be unregistered
[release]
  • Loading branch information
madskristensen authored Jan 26, 2023
2 parents a37ebbb + c864ff4 commit 4230c40
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Task = System.Threading.Tasks.Task;

namespace Community.VisualStudio.Toolkit
{
Expand Down Expand Up @@ -93,32 +92,38 @@ public Task<bool> ExecuteAsync(CommandID cmd, string argument = "")
/// <summary>
/// Intercept any command before it is being handled by other command handlers.
/// </summary>
public Task InterceptAsync(VSConstants.VSStd97CmdID command, Func<CommandProgression> func)
/// <returns>Returns an <see cref="IDisposable"/> that will remove the command interceptor when disposed.</returns>
public Task<IDisposable> InterceptAsync(VSConstants.VSStd97CmdID command, Func<CommandProgression> func)
=> InterceptAsync(typeof(VSConstants.VSStd97CmdID).GUID, (int)command, func);

/// <summary>
/// Intercept any command before it is being handled by other command handlers.
/// </summary>
public Task InterceptAsync(VSConstants.VSStd2KCmdID command, Func<CommandProgression> func)
/// <returns>Returns an <see cref="IDisposable"/> that will remove the command interceptor when disposed.</returns>
public Task<IDisposable> InterceptAsync(VSConstants.VSStd2KCmdID command, Func<CommandProgression> func)
=> InterceptAsync(typeof(VSConstants.VSStd2KCmdID).GUID, (int)command, func);

/// <summary>
/// Intercept any command before it is being handled by other command handlers.
/// </summary>
public Task InterceptAsync(Guid menuGroup, int commandId, Func<CommandProgression> func)
/// <returns>Returns an <see cref="IDisposable"/> that will remove the command interceptor when disposed.</returns>
public Task<IDisposable> InterceptAsync(Guid menuGroup, int commandId, Func<CommandProgression> func)
=> InterceptAsync(new CommandID(menuGroup, commandId), func);

/// <summary>
/// Intercept any command before it is being handled by other command handlers.
/// </summary>
public async Task InterceptAsync(CommandID cmd, Func<CommandProgression> func)
/// <returns>Returns an <see cref="IDisposable"/> that will remove the command interceptor when disposed.</returns>
public async Task<IDisposable> InterceptAsync(CommandID cmd, Func<CommandProgression> func)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

IVsRegisterPriorityCommandTarget? priority = await VS.Services.GetPriorityCommandTargetAsync();
CommandInterceptor interceptor = new(cmd, func);

ErrorHandler.ThrowOnFailure(priority.RegisterPriorityCommandTarget(0, interceptor, out _));
ErrorHandler.ThrowOnFailure(priority.RegisterPriorityCommandTarget(0, interceptor, out uint cookie));

return new Disposable(() => priority.UnregisterPriorityCommandTarget(cookie));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace Community.VisualStudio.Toolkit
{
internal sealed class Disposable : IDisposable
{
private readonly Action _action;
private bool _disposed;

public Disposable(Action action)
{
_action = action;
}

public void Dispose()
{
if (!_disposed)
{
_disposed = true;
_action();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Debugger\Debugger.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\Disposable.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\IToolWindowPaneAware.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\ToolkitToolWindowPane.cs" />
<Compile Include="$(MSBuildThisFileDirectory)\Attributes\ProvideBraceCompletionAttribute.cs" />
Expand Down

0 comments on commit 4230c40

Please sign in to comment.