Skip to content

Commit 922f1d9

Browse files
committed
keyhook now handled same as mouse hook
1 parent f77b161 commit 922f1d9

File tree

5 files changed

+46
-16
lines changed

5 files changed

+46
-16
lines changed

RetailCoder.VBE/App.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public App(VBE vbe, IMessageBox messageBox,
8585
_hookActions = new Dictionary<Type, Action>
8686
{
8787
{ typeof(MouseHook), HandleMouseMessage },
88-
//{ typeof(KeyboardHook), HandleKeyboardMessage },
88+
{ typeof(KeyboardHook), HandleKeyboardMessage },
8989
};
9090

9191

@@ -104,18 +104,18 @@ private void _hooks_MessageReceived(object sender, HookEventArgs e)
104104

105105
private void HandleMouseMessage()
106106
{
107-
_stateBar.SetSelectionText(_parser.State.FindSelectedDeclaration(_vbe.ActiveCodePane));
108-
_appMenus.EvaluateCanExecute(_parser.State);
107+
RefreshSelection();
109108
}
110109

111110
private void HandleKeyboardMessage()
112111
{
113-
var pane = _vbe.ActiveCodePane;
114-
if (pane == null)
115-
{
116-
return;
117-
}
118-
_parser.State.OnParseRequested(this, pane.CodeModule.Parent);
112+
RefreshSelection();
113+
}
114+
115+
private void RefreshSelection()
116+
{
117+
_stateBar.SetSelectionText(_parser.State.FindSelectedDeclaration(_vbe.ActiveCodePane));
118+
_appMenus.EvaluateCanExecute(_parser.State);
119119
}
120120

121121
private void _configService_SettingsChanged(object sender, EventArgs e)

RetailCoder.VBE/Common/KeyboardHook.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel;
34
using System.Diagnostics;
5+
using System.Linq;
6+
using System.Runtime.InteropServices;
7+
using System.Windows.Forms;
48
using Microsoft.Vbe.Interop;
59
using Rubberduck.Common.WinAPI;
610

@@ -19,13 +23,29 @@ public KeyboardHook(VBE vbe)
1923
_callback = HookCallback;
2024
}
2125

26+
// keys that don't modify anything in the code pane but the current selection
27+
private static readonly IReadOnlyList<Keys> NavigationKeys = new[]
28+
{
29+
Keys.Down,
30+
Keys.Up,
31+
Keys.Left,
32+
Keys.Right,
33+
Keys.PageDown,
34+
Keys.PageUp,
35+
Keys.Home,
36+
Keys.End,
37+
};
38+
2239
private int _lastLineIndex;
2340
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
2441
{
2542
try
2643
{
2744
var pane = _vbe.ActiveCodePane;
28-
if (User32.IsVbeWindowActive((IntPtr)_vbe.MainWindow.HWnd) && pane != null && (WM)wParam == WM.KEYUP)
45+
if ((WM)wParam == WM.KEYDOWN
46+
&& pane != null
47+
&& NavigationKeys.Contains((Keys)Marshal.ReadInt32(lParam))
48+
&& User32.IsVbeWindowActive((IntPtr)_vbe.MainWindow.HWnd))
2949
{
3050
int startLine;
3151
int endLine;
@@ -36,11 +56,9 @@ private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
3656
pane.GetSelection(out startLine, out startColumn, out endLine, out endColumn);
3757
if (startLine != _lastLineIndex)
3858
{
39-
// if the current line has changed, let the KEYDOWN be written to the IDE, and notify on KEYUP:
4059
_lastLineIndex = startLine;
4160
if (nCode >= 0)
4261
{
43-
//var key = (Keys)Marshal.ReadInt32(lParam);
4462
OnMessageReceived();
4563
}
4664
}

RetailCoder.VBE/Common/RubberduckHooks.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void HookHotkeys()
4545
var config = _config.LoadConfiguration();
4646
var settings = config.UserSettings.GeneralSettings.HotkeySettings;
4747

48-
//AddHook(new KeyboardHook(_vbe));
48+
AddHook(new KeyboardHook(_vbe));
4949
AddHook(new MouseHook(_vbe));
5050
foreach (var hotkey in settings.Where(hotkey => hotkey.IsEnabled))
5151
{

Rubberduck.Parsing/VBA/RubberduckParser.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,18 @@ public void Resolve(CancellationToken token)
279279

280280
private void ResolveInternal(CancellationToken token)
281281
{
282-
if (!_state.HasAllParseTrees)
282+
var components = _state.Projects
283+
.Where(project => project.Protection == vbext_ProjectProtection.vbext_pp_none)
284+
.SelectMany(p => p.VBComponents.Cast<VBComponent>()).ToList();
285+
if (!_state.HasAllParseTrees(components))
283286
{
284287
return;
285288
}
286289

287290
foreach (var kvp in _state.ParseTrees)
288291
{
289292
var qualifiedName = kvp.Key;
290-
if (_force || _state.IsModified(qualifiedName))
293+
if (/*_force || _state.IsModified(qualifiedName)*/ true)
291294
{
292295
Debug.WriteLine(string.Format("Module '{0}' is new or was modified since last parse. Walking parse tree for declarations...", kvp.Key.ComponentName));
293296
// modified module; walk parse tree and re-acquire all declarations

Rubberduck.Parsing/VBA/RubberduckParserState.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,16 @@ public IParseTree GetParseTree(VBComponent component)
415415

416416
public IEnumerable<KeyValuePair<QualifiedModuleName, IParseTree>> ParseTrees { get { return _parseTrees; } }
417417

418-
public bool HasAllParseTrees { get { return _moduleStates.Count == _parseTrees.Count; } }
418+
public bool HasAllParseTrees(IReadOnlyList<VBComponent> expected)
419+
{
420+
var expectedModules = expected.Select(module => new QualifiedModuleName(module));
421+
foreach (var module in _moduleStates.Keys.Where(item => !expectedModules.Contains(item)))
422+
{
423+
ClearDeclarations(module.Component);
424+
}
425+
426+
return _parseTrees.Count == expected.Count;
427+
}
419428

420429
public TokenStreamRewriter GetRewriter(VBComponent component)
421430
{

0 commit comments

Comments
 (0)