1
1
using System ;
2
2
using Microsoft . Office . Core ;
3
3
using Microsoft . Vbe . Interop ;
4
+ using Rubberduck . Parsing ;
4
5
using Rubberduck . Parsing . Symbols ;
5
6
using Rubberduck . Parsing . VBA ;
6
7
using Rubberduck . Properties ;
@@ -13,23 +14,53 @@ public class RubberduckCommandBar : IDisposable
13
14
{
14
15
private readonly RubberduckParserState _state ;
15
16
private readonly VBE _vbe ;
17
+ private readonly ISinks _sinks ;
16
18
private readonly IShowParserErrorsCommand _command ;
17
19
18
20
private CommandBarButton _refreshButton ;
19
21
private CommandBarButton _statusButton ;
20
22
private CommandBarButton _selectionButton ;
21
23
private CommandBar _commandbar ;
22
24
23
- public RubberduckCommandBar ( RubberduckParserState state , VBE vbe , IShowParserErrorsCommand command )
25
+ public RubberduckCommandBar ( RubberduckParserState state , VBE vbe , ISinks sinks , IShowParserErrorsCommand command )
24
26
{
25
27
_state = state ;
26
28
_vbe = vbe ;
29
+ _sinks = sinks ;
27
30
_command = command ;
28
31
_state . StateChanged += State_StateChanged ;
29
32
Initialize ( ) ;
33
+
34
+ _sinks . ProjectRemoved += ProjectRemoved ;
35
+ _sinks . ComponentActivated += ComponentActivated ;
36
+ _sinks . ComponentSelected += ComponentSelected ;
30
37
}
31
38
32
- private void _statusButton_Click ( CommandBarButton Ctrl , ref bool CancelDefault )
39
+ private void ProjectRemoved ( object sender , IProjectEventArgs e )
40
+ {
41
+ SetSelectionText ( ) ;
42
+ }
43
+
44
+ private void ComponentActivated ( object sender , IComponentEventArgs e )
45
+ {
46
+ SetSelectionText ( ) ;
47
+ }
48
+
49
+ private void ComponentSelected ( object sender , IComponentEventArgs e )
50
+ {
51
+ SetSelectionText ( ) ;
52
+ }
53
+
54
+ private void SetSelectionText ( )
55
+ {
56
+ var selectedDeclaration = _vbe . ActiveCodePane != null
57
+ ? _state . FindSelectedDeclaration ( _vbe . ActiveCodePane )
58
+ : null ;
59
+
60
+ SetSelectionText ( selectedDeclaration ) ;
61
+ }
62
+
63
+ private void _statusButton_Click ( CommandBarButton ctrl , ref bool cancelDefault )
33
64
{
34
65
if ( _state . Status == ParserState . Error )
35
66
{
@@ -39,7 +70,7 @@ private void _statusButton_Click(CommandBarButton Ctrl, ref bool CancelDefault)
39
70
40
71
public void SetStatusText ( string value = null )
41
72
{
42
- var text = value ?? RubberduckUI . ResourceManager . GetString ( "ParserState_" + _state . Status , UI . Settings . Settings . Culture ) ;
73
+ var text = value ?? RubberduckUI . ResourceManager . GetString ( "ParserState_" + _state . Status , Settings . Settings . Culture ) ;
43
74
UiDispatcher . Invoke ( ( ) => _statusButton . Caption = text ) ;
44
75
}
45
76
@@ -51,6 +82,10 @@ public void SetSelectionText(Declaration declaration)
51
82
if ( selection . HasValue ) { SetSelectionText ( selection . Value ) ; }
52
83
_selectionButton . TooltipText = _selectionButton . Caption ;
53
84
}
85
+ else if ( declaration == null && _vbe . ActiveCodePane == null )
86
+ {
87
+ UiDispatcher . Invoke ( ( ) => _selectionButton . Caption = string . Empty ) ;
88
+ }
54
89
else if ( declaration != null && ! declaration . IsBuiltIn && declaration . DeclarationType != DeclarationType . ClassModule && declaration . DeclarationType != DeclarationType . ProceduralModule )
55
90
{
56
91
var typeName = declaration . HasTypeHint
@@ -61,7 +96,7 @@ public void SetSelectionText(Declaration declaration)
61
96
declaration . QualifiedSelection . Selection ,
62
97
declaration . QualifiedName . QualifiedModuleName ,
63
98
declaration . IdentifierName ,
64
- RubberduckUI . ResourceManager . GetString ( "DeclarationType_" + declaration . DeclarationType , UI . Settings . Settings . Culture ) ,
99
+ RubberduckUI . ResourceManager . GetString ( "DeclarationType_" + declaration . DeclarationType , Settings . Settings . Culture ) ,
65
100
string . IsNullOrEmpty ( declaration . AsTypeName ) ? string . Empty : ": " + typeName ) ;
66
101
67
102
_selectionButton . TooltipText = string . IsNullOrEmpty ( declaration . DescriptionString )
@@ -82,7 +117,7 @@ public void SetSelectionText(Declaration declaration)
82
117
selection . Value . Selection ,
83
118
declaration . QualifiedName . QualifiedModuleName ,
84
119
declaration . IdentifierName ,
85
- RubberduckUI . ResourceManager . GetString ( "DeclarationType_" + declaration . DeclarationType , UI . Settings . Settings . Culture ) ,
120
+ RubberduckUI . ResourceManager . GetString ( "DeclarationType_" + declaration . DeclarationType , Settings . Settings . Culture ) ,
86
121
string . IsNullOrEmpty ( declaration . AsTypeName ) ? string . Empty : ": " + typeName ) ;
87
122
}
88
123
_selectionButton . TooltipText = string . IsNullOrEmpty ( declaration . DescriptionString )
@@ -100,7 +135,7 @@ private void State_StateChanged(object sender, EventArgs e)
100
135
{
101
136
if ( _state . Status != ParserState . ResolvedDeclarations )
102
137
{
103
- SetStatusText ( RubberduckUI . ResourceManager . GetString ( "ParserState_" + _state . Status , UI . Settings . Settings . Culture ) ) ;
138
+ SetStatusText ( RubberduckUI . ResourceManager . GetString ( "ParserState_" + _state . Status , Settings . Settings . Culture ) ) ;
104
139
}
105
140
}
106
141
@@ -115,7 +150,7 @@ private void OnRefresh()
115
150
}
116
151
}
117
152
118
- public void Initialize ( )
153
+ private void Initialize ( )
119
154
{
120
155
_commandbar = _vbe . CommandBars . Add ( "Rubberduck" , MsoBarPosition . msoBarTop , false , true ) ;
121
156
@@ -139,7 +174,7 @@ public void Initialize()
139
174
_commandbar . Visible = true ;
140
175
}
141
176
142
- private void refreshButton_Click ( CommandBarButton Ctrl , ref bool CancelDefault )
177
+ private void refreshButton_Click ( CommandBarButton ctrl , ref bool cancelDefault )
143
178
{
144
179
OnRefresh ( ) ;
145
180
}
@@ -154,6 +189,10 @@ public void Dispose()
154
189
155
190
_state . StateChanged -= State_StateChanged ;
156
191
192
+ _sinks . ProjectRemoved -= ProjectRemoved ;
193
+ _sinks . ComponentActivated -= ComponentActivated ;
194
+ _sinks . ComponentSelected -= ComponentSelected ;
195
+
157
196
_refreshButton . Delete ( ) ;
158
197
_selectionButton . Delete ( ) ;
159
198
_statusButton . Delete ( ) ;
0 commit comments