Skip to content
This repository
Browse code

Fixed the bug where VsVim interferred with user automation functions

  • Loading branch information...
commit df0b6e6c1c95ff53acc14cbd5ad3cf5ccca05cd0 1 parent 444d735
Jared Parsons authored April 08, 2010
2  VsVim/HostFactory.cs
@@ -100,7 +100,7 @@ void IVsTextViewCreationListener.VsTextViewCreated(IVsTextView vsView)
100 100
             }
101 101
 
102 102
             var buffer = opt.Value;
103  
-            var filter = new VsCommandFilter(buffer, vsView);
  103
+            var filter = new VsCommandFilter(buffer, vsView, _serviceProvider);
104 104
             _filterMap.Add(buffer, filter);
105 105
         }
106 106
     }
16  VsVim/VsCommandFilter.cs
... ...
@@ -1,7 +1,6 @@
1 1
 using System;
2 2
 using System.Collections.Generic;
3 3
 using System.Text;
4  
-using Microsoft.VisualStudio.OLE.Interop;
5 4
 using Microsoft.VisualStudio;
6 5
 using Microsoft.VisualStudio.TextManager.Interop;
7 6
 using Microsoft.VisualStudio.Text.Editor;
@@ -9,6 +8,9 @@
9 8
 using System.Windows.Input;
10 9
 using Microsoft.FSharp.Core;
11 10
 using Vim;
  11
+using Microsoft.VisualStudio.OLE.Interop;
  12
+using IServiceProvider = System.IServiceProvider;
  13
+using Microsoft.VisualStudio.Shell;
12 14
 
13 15
 namespace VsVim
14 16
 {
@@ -22,17 +24,27 @@ internal sealed class VsCommandFilter : IOleCommandTarget
22 24
         private readonly IVimBuffer _buffer;
23 25
         private readonly IVsTextView _textView;
24 26
         private readonly IOleCommandTarget _nextTarget;
  27
+        private readonly IServiceProvider _serviceProvider;
25 28
 
26  
-        internal VsCommandFilter(IVimBuffer buffer, IVsTextView view)
  29
+        internal VsCommandFilter(IVimBuffer buffer, IVsTextView view, IServiceProvider provider)
27 30
         {
28 31
             _buffer = buffer;
29 32
             _textView = view;
  33
+            _serviceProvider = provider;
30 34
             var hr = view.AddCommandFilter(this, out _nextTarget);
31 35
         }
32 36
 
33 37
         internal bool TryConvert(Guid commandGroup, uint commandId, IntPtr pvaIn, out KeyInput kiOutput)
34 38
         {
35 39
             kiOutput = null;
  40
+
  41
+            // Don't ever process a command when we are in an automation function.  Doing so will cause VsVim to 
  42
+            // intercept items like running Macros and certain wizard functionality
  43
+            if (VsShellUtilities.IsInAutomationFunction(_serviceProvider))
  44
+            {
  45
+                return false;
  46
+            }
  47
+
36 48
             EditCommand command;
37 49
             if (!CommandUtil.TryConvert(commandGroup, commandId, pvaIn, out command))
38 50
             {
72  VsVimTest/VsCommandFilterTest.cs
... ...
@@ -0,0 +1,72 @@
  1
+using System;
  2
+using System.Collections.Generic;
  3
+using System.Linq;
  4
+using System.Text;
  5
+using NUnit.Framework;
  6
+using Vim;
  7
+using Moq;
  8
+using Microsoft.VisualStudio.TextManager.Interop;
  9
+using Microsoft.VisualStudio;
  10
+using VsVim;
  11
+using EnvDTE;
  12
+
  13
+namespace VsVimTest
  14
+{
  15
+    [TestFixture]
  16
+    public class VsCommandFilterTest
  17
+    {
  18
+        private Mock<IVimBuffer> _buffer;
  19
+        private Mock<IVsTextView> _textView;
  20
+        private Mock<IServiceProvider> _serviceProvider;
  21
+        private Mock<IVsExtensibility> _vsExt;
  22
+        private VsCommandFilter _filter;
  23
+
  24
+        [SetUp]
  25
+        public void SetUp()
  26
+        {
  27
+            _buffer = new Mock<IVimBuffer>();
  28
+            _textView = new Mock<IVsTextView>();
  29
+            _vsExt = new Mock<IVsExtensibility>(MockBehavior.Strict);
  30
+            _vsExt.Setup(x => x.IsInAutomationFunction()).Returns(0);
  31
+            _serviceProvider = MockObjectFactory.CreateServiceProvider(Tuple.Create(typeof(IVsExtensibility), (object)_vsExt.Object));
  32
+            _filter = new VsCommandFilter(_buffer.Object, _textView.Object, _serviceProvider.Object);
  33
+        }
  34
+
  35
+        private void AssertCannotConvert2K(VSConstants.VSStd2KCmdID id)
  36
+        {
  37
+            KeyInput ki;
  38
+            Assert.IsFalse(_filter.TryConvert(VSConstants.VSStd2K, (uint)id, IntPtr.Zero, out ki));
  39
+        }
  40
+
  41
+        private void AssertCanConvert2K(VSConstants.VSStd2KCmdID id, KeyInput expected)
  42
+        {
  43
+            KeyInput ki;
  44
+            Assert.IsTrue(_filter.TryConvert(VSConstants.VSStd2K, (uint)id, IntPtr.Zero, out ki));
  45
+            Assert.AreEqual(expected,ki);
  46
+        }
  47
+
  48
+        [Test]
  49
+        public void TryConvert1()
  50
+        {
  51
+            _buffer.Setup(x => x.CanProcessInput(It.IsAny<KeyInput>())).Returns(true);
  52
+            AssertCanConvert2K(VSConstants.VSStd2KCmdID.TAB, InputUtil.VimKeyToKeyInput(VimKey.TabKey));
  53
+        }
  54
+
  55
+        [Test]
  56
+        public void TryConvert2()
  57
+        {
  58
+            _buffer.Setup(x => x.CanProcessInput(It.IsAny<KeyInput>())).Returns(false);
  59
+            AssertCannotConvert2K(VSConstants.VSStd2KCmdID.TAB);
  60
+        }
  61
+
  62
+        [Test, Description("Don't convert keys when in automation")]
  63
+        public void TryConvert3()
  64
+        {
  65
+            _vsExt.Setup(x => x.IsInAutomationFunction()).Returns(1);
  66
+            _buffer.Setup(x => x.CanProcessInput(It.IsAny<KeyInput>())).Returns(true);
  67
+            AssertCannotConvert2K(VSConstants.VSStd2KCmdID.TAB);
  68
+        }
  69
+
  70
+
  71
+    }
  72
+}
1  VsVimTest/VsVimTest.csproj
@@ -143,6 +143,7 @@
143 143
       <DependentUpon>TestResources.resx</DependentUpon>
144 144
     </Compile>
145 145
     <Compile Include="Utils\CharPointer.cs" />
  146
+    <Compile Include="VsCommandFilterTest.cs" />
146 147
     <Compile Include="VsVimHostTest.cs" />
147 148
   </ItemGroup>
148 149
   <ItemGroup>

0 notes on commit df0b6e6

Please sign in to comment.
Something went wrong with that request. Please try again.