Skip to content

Commit 7a566b5

Browse files
committed
Merge pull request #1324 from retailcoder/next
stabilized selection status, handled hook exceptions
2 parents eb79c9c + 37da2d0 commit 7a566b5

File tree

10 files changed

+45
-33
lines changed

10 files changed

+45
-33
lines changed

RetailCoder.VBE/App.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Infralution.Localization.Wpf;
1919
using Rubberduck.Common.Dispatch;
2020
using Rubberduck.Common.Hotkeys;
21+
using Rubberduck.VBEditor.VBEInterfaces.RubberduckCodePane;
2122

2223
namespace Rubberduck
2324
{
@@ -325,7 +326,7 @@ private void LoadConfig()
325326
}
326327
catch (CultureNotFoundException exception)
327328
{
328-
_logger.Error(exception, "Error Setting Culture for RubberDuck");
329+
_logger.Error(exception, "Error Setting Culture for Rubberduck");
329330
_messageBox.Show(exception.Message, "Rubberduck", MessageBoxButtons.OK, MessageBoxIcon.Error);
330331
_config.UserSettings.GeneralSettings.Language.Code = currentCulture.Name;
331332
_configService.SaveConfiguration(_config);

RetailCoder.VBE/Common/Hotkeys/Hotkey.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void Detach()
6363
{
6464
if (!IsAttached)
6565
{
66-
throw new InvalidOperationException(Rubberduck.UI.RubberduckUI.CommonHotkey_HookDetached);
66+
return;
6767
}
6868

6969
User32.UnregisterHotKey(_hWndVbe, HotkeyInfo.HookId);
@@ -76,7 +76,7 @@ private void HookKey(Keys key, uint shift)
7676
{
7777
if (IsAttached)
7878
{
79-
throw new InvalidOperationException(Rubberduck.UI.RubberduckUI.CommonHotkey_HookAttached);
79+
return;
8080
}
8181

8282
var hookId = (IntPtr)Kernel32.GlobalAddAtom(Guid.NewGuid().ToString());

RetailCoder.VBE/Common/KeyboardHook.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ public KeyboardHook(VBE vbe)
3636
Keys.End,
3737
};
3838

39-
private int _lastLineIndex;
4039
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
4140
{
4241
try
4342
{
4443
var pane = _vbe.ActiveCodePane;
45-
if ((WM)wParam == WM.KEYDOWN
44+
if ((WM)wParam == WM.KEYUP
4645
&& pane != null
4746
&& NavigationKeys.Contains((Keys)Marshal.ReadInt32(lParam))
4847
&& User32.IsVbeWindowActive((IntPtr)_vbe.MainWindow.HWnd))
@@ -54,13 +53,9 @@ private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
5453

5554
// not using extension method because a QualifiedSelection would be overkill:
5655
pane.GetSelection(out startLine, out startColumn, out endLine, out endColumn);
57-
if (startLine != _lastLineIndex)
56+
if (nCode >= 0)
5857
{
59-
_lastLineIndex = startLine;
60-
if (nCode >= 0)
61-
{
62-
OnMessageReceived();
63-
}
58+
OnMessageReceived();
6459
}
6560
}
6661

@@ -117,6 +112,8 @@ public void Detach()
117112

118113
if (!User32.UnhookWindowsHookEx(_hookId))
119114
{
115+
IsAttached = false;
116+
_hookId = IntPtr.Zero;
120117
throw new Win32Exception();
121118
}
122119

RetailCoder.VBE/Common/MouseHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
2626
if (User32.IsVbeWindowActive((IntPtr)_vbe.MainWindow.HWnd) && nCode >= 0 && pane != null)
2727
{
2828
var button = (WM)wParam;
29-
if (button == WM.RBUTTONDOWN || button == WM.LBUTTONDOWN)
29+
if (button == WM.RBUTTONUP || button == WM.LBUTTONUP)
3030
{
3131
// handle right-click to evaluate commands' CanExecute before the context menu is shown;
3232
// handle left-click to do the same before the Rubberduck menu is drawn, too.

RetailCoder.VBE/Common/RubberduckHooks.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ public void Detach()
109109
{
110110
foreach (var hook in Hooks)
111111
{
112-
hook.Detach();
113112
hook.MessageReceived -= hook_MessageReceived;
113+
hook.Detach();
114114
}
115115
}
116116
catch (Win32Exception exception)

RetailCoder.VBE/UI/Command/MenuItems/RubberduckCommandBar.cs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,28 @@ public void SetStatusText(string value = null)
4646

4747
public void SetSelectionText(Declaration declaration)
4848
{
49-
UiDispatcher.Invoke(() =>
49+
if (declaration == null && _vbe.ActiveCodePane != null)
5050
{
51-
if (declaration == null && _vbe.ActiveCodePane != null)
52-
{
53-
var selection = _vbe.ActiveCodePane.GetSelection();
54-
SetSelectionText(selection);
55-
}
56-
else if (declaration != null)
57-
{
58-
_selectionButton.Caption = string.Format("{0} ({1}): {2} ({3})",
59-
declaration.QualifiedName.QualifiedModuleName,
60-
declaration.QualifiedSelection.Selection,
61-
declaration.IdentifierName,
62-
RubberduckUI.ResourceManager.GetString("DeclarationType_" + declaration.DeclarationType));
63-
}
64-
});
51+
var selection = _vbe.ActiveCodePane.GetSelection();
52+
SetSelectionText(selection);
53+
}
54+
else if (declaration != null && !declaration.IsBuiltIn && declaration.DeclarationType != DeclarationType.Class && declaration.DeclarationType != DeclarationType.Module)
55+
{
56+
_selectionButton.Caption = string.Format("{0} ({1}): {2} ({3})",
57+
declaration.QualifiedName.QualifiedModuleName,
58+
declaration.QualifiedSelection.Selection,
59+
declaration.IdentifierName,
60+
RubberduckUI.ResourceManager.GetString("DeclarationType_" + declaration.DeclarationType));
61+
}
62+
else if (declaration != null)
63+
{
64+
var selection = _vbe.ActiveCodePane.GetSelection();
65+
_selectionButton.Caption = string.Format("{0}: {1} ({2}) {3}",
66+
declaration.QualifiedName.QualifiedModuleName,
67+
declaration.IdentifierName,
68+
RubberduckUI.ResourceManager.GetString("DeclarationType_" + declaration.DeclarationType),
69+
selection.Selection);
70+
}
6571
}
6672

6773
private void SetSelectionText(QualifiedSelection selection)

RetailCoder.VBE/UI/Command/Refactorings/RefactorIntroduceParameterCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public override bool CanExecute(object parameter)
3131
var selection = Vbe.ActiveCodePane.GetSelection();
3232
var target = _state.AllUserDeclarations.FindTarget(selection, new []{DeclarationType.Variable, DeclarationType.Constant});
3333

34-
var canExecute = target != null && target.ParentScopeDeclaration.DeclarationType.HasFlag(DeclarationType.Member);
34+
var canExecute = target != null && target.ParentScopeDeclaration != null && target.ParentScopeDeclaration.DeclarationType.HasFlag(DeclarationType.Member);
3535

3636
Debug.WriteLine("{0}.CanExecute evaluates to {1}", GetType().Name, canExecute);
3737
return canExecute;

Rubberduck.Parsing/VBA/RubberduckParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ private void ResolveInternal(CancellationToken token)
292292
var qualifiedName = kvp.Key;
293293
if (/*_force || _state.IsModified(qualifiedName)*/ true)
294294
{
295-
Debug.WriteLine(string.Format("Module '{0}' is new or was modified since last parse. Walking parse tree for declarations...", kvp.Key.ComponentName));
295+
Debug.WriteLine("Module '{0}' {1}", qualifiedName.ComponentName, _state.IsModified(qualifiedName) ? "was modified" : "was NOT modified");
296296
// modified module; walk parse tree and re-acquire all declarations
297297
if (token.IsCancellationRequested) return;
298298
ResolveDeclarations(qualifiedName.Component, kvp.Value);

Rubberduck.Parsing/VBA/RubberduckParserState.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public void SetModuleState(VBComponent component, ParserState state, SyntaxError
135135
{
136136
// ghost component shouldn't even exist
137137
ClearDeclarations(component);
138+
Status = EvaluateParserState();
138139
return;
139140
}
140141
}
@@ -499,11 +500,16 @@ public Declaration FindSelectedDeclaration(CodePane activeCodePane)
499500
var matches = AllDeclarations
500501
.Where(item => item.DeclarationType != DeclarationType.Project &&
501502
item.DeclarationType != DeclarationType.ModuleOption &&
503+
item.DeclarationType != DeclarationType.Class &&
504+
item.DeclarationType != DeclarationType.Module &&
502505
(IsSelectedDeclaration(selection, item) ||
503506
item.References.Any(reference => IsSelectedReference(selection, reference))));
504507
try
505508
{
506-
_selectedDeclaration = matches.SingleOrDefault();
509+
var match = matches.SingleOrDefault() ?? AllUserDeclarations
510+
.SingleOrDefault(item => (item.DeclarationType == DeclarationType.Class || item.DeclarationType == DeclarationType.Module)
511+
&& item.QualifiedName.QualifiedModuleName.Equals(selection.QualifiedName));
512+
_selectedDeclaration = match;
507513
}
508514
catch (InvalidOperationException exception)
509515
{
@@ -522,7 +528,7 @@ public Declaration FindSelectedDeclaration(CodePane activeCodePane)
522528
private static bool IsSelectedDeclaration(QualifiedSelection selection, Declaration declaration)
523529
{
524530
return declaration.QualifiedSelection.QualifiedName.Equals(selection.QualifiedName)
525-
&& declaration.QualifiedSelection.Selection.ContainsFirstCharacter(selection.Selection);
531+
&& (declaration.QualifiedSelection.Selection.ContainsFirstCharacter(selection.Selection));
526532
}
527533

528534
private static bool IsSelectedReference(QualifiedSelection selection, IdentifierReference reference)

Rubberduck.VBEEditor/Selection.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ public bool Equals(Selection other)
7777

7878
public override string ToString()
7979
{
80-
return string.Format("L{0}C{1} - L{2}C{3}", _startLine, _startColumn, _endLine, _endColumn);
80+
return (_startLine == _endLine && _startColumn == _endColumn)
81+
? string.Format("L{0}C{1}", _startLine, _startColumn)
82+
: string.Format("L{0}C{1} - L{2}C{3}", _startLine, _startColumn, _endLine, _endColumn);
8183
}
8284

8385
public override bool Equals(object obj)

0 commit comments

Comments
 (0)