Skip to content

Commit

Permalink
Fixed the bug where VsVim interferred with user automation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpar committed Apr 9, 2010
1 parent 444d735 commit df0b6e6
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
2 changes: 1 addition & 1 deletion VsVim/HostFactory.cs
Expand Up @@ -100,7 +100,7 @@ void IVsTextViewCreationListener.VsTextViewCreated(IVsTextView vsView)
}

var buffer = opt.Value;
var filter = new VsCommandFilter(buffer, vsView);
var filter = new VsCommandFilter(buffer, vsView, _serviceProvider);
_filterMap.Add(buffer, filter);
}
}
Expand Down
16 changes: 14 additions & 2 deletions VsVim/VsCommandFilter.cs
@@ -1,14 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudio.Text.Editor;
using System.Runtime.InteropServices;
using System.Windows.Input;
using Microsoft.FSharp.Core;
using Vim;
using Microsoft.VisualStudio.OLE.Interop;
using IServiceProvider = System.IServiceProvider;
using Microsoft.VisualStudio.Shell;

namespace VsVim
{
Expand All @@ -22,17 +24,27 @@ internal sealed class VsCommandFilter : IOleCommandTarget
private readonly IVimBuffer _buffer;
private readonly IVsTextView _textView;
private readonly IOleCommandTarget _nextTarget;
private readonly IServiceProvider _serviceProvider;

internal VsCommandFilter(IVimBuffer buffer, IVsTextView view)
internal VsCommandFilter(IVimBuffer buffer, IVsTextView view, IServiceProvider provider)
{
_buffer = buffer;
_textView = view;
_serviceProvider = provider;
var hr = view.AddCommandFilter(this, out _nextTarget);
}

internal bool TryConvert(Guid commandGroup, uint commandId, IntPtr pvaIn, out KeyInput kiOutput)
{
kiOutput = null;

// Don't ever process a command when we are in an automation function. Doing so will cause VsVim to
// intercept items like running Macros and certain wizard functionality
if (VsShellUtilities.IsInAutomationFunction(_serviceProvider))
{
return false;
}

EditCommand command;
if (!CommandUtil.TryConvert(commandGroup, commandId, pvaIn, out command))
{
Expand Down
72 changes: 72 additions & 0 deletions VsVimTest/VsCommandFilterTest.cs
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Vim;
using Moq;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudio;
using VsVim;
using EnvDTE;

namespace VsVimTest
{
[TestFixture]
public class VsCommandFilterTest
{
private Mock<IVimBuffer> _buffer;
private Mock<IVsTextView> _textView;
private Mock<IServiceProvider> _serviceProvider;
private Mock<IVsExtensibility> _vsExt;
private VsCommandFilter _filter;

[SetUp]
public void SetUp()
{
_buffer = new Mock<IVimBuffer>();
_textView = new Mock<IVsTextView>();
_vsExt = new Mock<IVsExtensibility>(MockBehavior.Strict);
_vsExt.Setup(x => x.IsInAutomationFunction()).Returns(0);
_serviceProvider = MockObjectFactory.CreateServiceProvider(Tuple.Create(typeof(IVsExtensibility), (object)_vsExt.Object));
_filter = new VsCommandFilter(_buffer.Object, _textView.Object, _serviceProvider.Object);
}

private void AssertCannotConvert2K(VSConstants.VSStd2KCmdID id)
{
KeyInput ki;
Assert.IsFalse(_filter.TryConvert(VSConstants.VSStd2K, (uint)id, IntPtr.Zero, out ki));
}

private void AssertCanConvert2K(VSConstants.VSStd2KCmdID id, KeyInput expected)
{
KeyInput ki;
Assert.IsTrue(_filter.TryConvert(VSConstants.VSStd2K, (uint)id, IntPtr.Zero, out ki));
Assert.AreEqual(expected,ki);
}

[Test]
public void TryConvert1()
{
_buffer.Setup(x => x.CanProcessInput(It.IsAny<KeyInput>())).Returns(true);
AssertCanConvert2K(VSConstants.VSStd2KCmdID.TAB, InputUtil.VimKeyToKeyInput(VimKey.TabKey));
}

[Test]
public void TryConvert2()
{
_buffer.Setup(x => x.CanProcessInput(It.IsAny<KeyInput>())).Returns(false);
AssertCannotConvert2K(VSConstants.VSStd2KCmdID.TAB);
}

[Test, Description("Don't convert keys when in automation")]
public void TryConvert3()
{
_vsExt.Setup(x => x.IsInAutomationFunction()).Returns(1);
_buffer.Setup(x => x.CanProcessInput(It.IsAny<KeyInput>())).Returns(true);
AssertCannotConvert2K(VSConstants.VSStd2KCmdID.TAB);
}


}
}
1 change: 1 addition & 0 deletions VsVimTest/VsVimTest.csproj
Expand Up @@ -143,6 +143,7 @@
<DependentUpon>TestResources.resx</DependentUpon>
</Compile>
<Compile Include="Utils\CharPointer.cs" />
<Compile Include="VsCommandFilterTest.cs" />
<Compile Include="VsVimHostTest.cs" />
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit df0b6e6

Please sign in to comment.