Skip to content

Commit 246a3c7

Browse files
committed
added current selection label to RubberduckCommandBar
1 parent 3b4d686 commit 246a3c7

File tree

6 files changed

+66
-19
lines changed

6 files changed

+66
-19
lines changed

RetailCoder.VBE/App.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ private void _hooks_MessageReceived(object sender, HookEventArgs e)
104104

105105
private void HandleMouseMessage()
106106
{
107+
_stateBar.SetSelectionText(_parser.State.FindSelectedDeclaration(_vbe.ActiveCodePane));
107108
_appMenus.EvaluateCanExecute(_parser.State);
108109
}
109110

RetailCoder.VBE/Common/MouseHook.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
4141
Debug.WriteLine(exception);
4242
}
4343

44-
return IntPtr.Zero;
44+
return User32.CallNextHookEx(_hookId, nCode, wParam, lParam);
4545
}
4646

4747
private void OnMessageReceived()
@@ -85,12 +85,14 @@ public void Detach()
8585
return;
8686
}
8787

88+
IsAttached = false;
8889
if (!User32.UnhookWindowsHookEx(_hookId))
8990
{
91+
_hookId = IntPtr.Zero;
9092
throw new Win32Exception();
9193
}
9294

93-
IsAttached = false;
95+
_hookId = IntPtr.Zero;
9496
Debug.WriteLine("{0}: {1}", GetType().Name, IsAttached ? "Attached" : "Detached");
9597
}
9698
}

RetailCoder.VBE/Common/RubberduckHooks.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel;
34
using System.Diagnostics;
45
using System.Linq;
56
using System.Runtime.InteropServices;
@@ -91,7 +92,7 @@ public void Attach()
9192

9293
IsAttached = true;
9394
}
94-
catch (Exception exception)
95+
catch (Win32Exception exception)
9596
{
9697
Debug.WriteLine(exception);
9798
}
@@ -111,13 +112,12 @@ public void Detach()
111112
hook.Detach();
112113
hook.MessageReceived -= hook_MessageReceived;
113114
}
114-
115-
IsAttached = false;
116115
}
117-
catch (Exception exception)
116+
catch (Win32Exception exception)
118117
{
119118
Debug.WriteLine(exception);
120119
}
120+
IsAttached = false;
121121
}
122122

123123
private void hook_MessageReceived(object sender, HookEventArgs e)
@@ -166,9 +166,9 @@ private IntPtr WindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam)
166166
suppress = HandleHotkeyMessage(wParam);
167167
break;
168168

169-
//case WM.ACTIVATEAPP:
170-
// HandleActivateAppMessage(wParam);
171-
// break;
169+
case WM.ACTIVATEAPP:
170+
HandleActivateAppMessage(wParam);
171+
break;
172172
}
173173
}
174174

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
using System.Diagnostics;
33
using Microsoft.Office.Core;
44
using Microsoft.Vbe.Interop;
5+
using Rubberduck.Parsing.Symbols;
56
using Rubberduck.Parsing.VBA;
67
using Rubberduck.Properties;
78
using Rubberduck.UI.Command.MenuItems.ParentMenus;
9+
using Rubberduck.VBEditor;
10+
using Rubberduck.VBEditor.VBEInterfaces.RubberduckCodePane;
11+
using ParserState = Rubberduck.Parsing.VBA.ParserState;
812

913
namespace Rubberduck.UI.Command.MenuItems
1014
{
@@ -16,6 +20,7 @@ public class RubberduckCommandBar
1620

1721
private CommandBarButton _refreshButton;
1822
private CommandBarButton _statusButton;
23+
private CommandBarButton _selectionButton;
1924

2025
public RubberduckCommandBar(RubberduckParserState state, VBE vbe, IShowParserErrorsCommand command)
2126
{
@@ -36,13 +41,38 @@ private void _statusButton_Click(CommandBarButton Ctrl, ref bool CancelDefault)
3641

3742
public void SetStatusText(string value = null)
3843
{
39-
_statusButton.Caption = value ?? RubberduckUI.ResourceManager.GetString("ParserState_" + _state.Status);
44+
UiDispatcher.Invoke(() => _statusButton.Caption = value ?? RubberduckUI.ResourceManager.GetString("ParserState_" + _state.Status));
45+
}
46+
47+
public void SetSelectionText(Declaration declaration)
48+
{
49+
UiDispatcher.Invoke(() =>
50+
{
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+
});
65+
}
66+
67+
private void SetSelectionText(QualifiedSelection selection)
68+
{
69+
UiDispatcher.Invoke(() => _selectionButton.Caption = selection.ToString());
4070
}
4171

4272
private void State_StateChanged(object sender, EventArgs e)
4373
{
4474
Debug.WriteLine("RubberduckCommandBar handles StateChanged...");
45-
UiDispatcher.Invoke(() => SetStatusText(RubberduckUI.ResourceManager.GetString("ParserState_" + _state.Status)));
75+
SetStatusText(RubberduckUI.ResourceManager.GetString("ParserState_" + _state.Status));
4676
}
4777

4878
public event EventHandler Refresh;
@@ -72,6 +102,11 @@ public void Initialize()
72102
_statusButton.Tag = "Status";
73103
_statusButton.Click += _statusButton_Click;
74104

105+
_selectionButton = (CommandBarButton)commandbar.Controls.Add(MsoControlType.msoControlButton);
106+
_selectionButton.Style = MsoButtonStyle.msoButtonCaption;
107+
_selectionButton.BeginGroup = true;
108+
_selectionButton.Enabled = false;
109+
75110
commandbar.Visible = true;
76111
}
77112

Rubberduck.Parsing/VBA/RubberduckParserState.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,19 @@ public Declaration FindSelectedDeclaration(CodePane activeCodePane)
487487

488488
if (!selection.Equals(default(QualifiedSelection)))
489489
{
490-
_selectedDeclaration = AllDeclarations
491-
.SingleOrDefault(item => item.DeclarationType != DeclarationType.Project &&
492-
item.DeclarationType != DeclarationType.ModuleOption &&
493-
(IsSelectedDeclaration(selection, item) ||
494-
item.References.Any(reference => IsSelectedReference(selection, reference))));
490+
var matches = AllDeclarations
491+
.Where(item => item.DeclarationType != DeclarationType.Project &&
492+
item.DeclarationType != DeclarationType.ModuleOption &&
493+
(IsSelectedDeclaration(selection, item) ||
494+
item.References.Any(reference => IsSelectedReference(selection, reference))));
495+
try
496+
{
497+
_selectedDeclaration = matches.SingleOrDefault();
498+
}
499+
catch (InvalidOperationException exception)
500+
{
501+
Debug.WriteLine(exception);
502+
}
495503
}
496504

497505
if (_selectedDeclaration != null)

RubberduckTests/Inspections/ImplicitPublicMemberInspectionTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ public class ImplicitPublicMemberInspectionTests
1616
[TestMethod]
1717
public void ImplicitPublicMember_ReturnsResult_Sub()
1818
{
19-
const string inputCode =
20-
@"Sub Foo()
21-
End Sub";
19+
const string inputCode = @"
20+
Sub Foo()
21+
End Sub
22+
";
2223

2324
//Arrange
2425
var builder = new MockVbeBuilder();

0 commit comments

Comments
 (0)