11using System ;
22using Microsoft . Office . Core ;
33using Microsoft . Vbe . Interop ;
4+ using Rubberduck . Parsing ;
45using Rubberduck . Parsing . Symbols ;
56using Rubberduck . Parsing . VBA ;
67using Rubberduck . Properties ;
@@ -13,23 +14,53 @@ public class RubberduckCommandBar : IDisposable
1314 {
1415 private readonly RubberduckParserState _state ;
1516 private readonly VBE _vbe ;
17+ private readonly ISinks _sinks ;
1618 private readonly IShowParserErrorsCommand _command ;
1719
1820 private CommandBarButton _refreshButton ;
1921 private CommandBarButton _statusButton ;
2022 private CommandBarButton _selectionButton ;
2123 private CommandBar _commandbar ;
2224
23- public RubberduckCommandBar ( RubberduckParserState state , VBE vbe , IShowParserErrorsCommand command )
25+ public RubberduckCommandBar ( RubberduckParserState state , VBE vbe , ISinks sinks , IShowParserErrorsCommand command )
2426 {
2527 _state = state ;
2628 _vbe = vbe ;
29+ _sinks = sinks ;
2730 _command = command ;
2831 _state . StateChanged += State_StateChanged ;
2932 Initialize ( ) ;
33+
34+ _sinks . ProjectRemoved += ProjectRemoved ;
35+ _sinks . ComponentActivated += ComponentActivated ;
36+ _sinks . ComponentSelected += ComponentSelected ;
3037 }
3138
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 )
3364 {
3465 if ( _state . Status == ParserState . Error )
3566 {
@@ -39,7 +70,7 @@ private void _statusButton_Click(CommandBarButton Ctrl, ref bool CancelDefault)
3970
4071 public void SetStatusText ( string value = null )
4172 {
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 ) ;
4374 UiDispatcher . Invoke ( ( ) => _statusButton . Caption = text ) ;
4475 }
4576
@@ -51,6 +82,10 @@ public void SetSelectionText(Declaration declaration)
5182 if ( selection . HasValue ) { SetSelectionText ( selection . Value ) ; }
5283 _selectionButton . TooltipText = _selectionButton . Caption ;
5384 }
85+ else if ( declaration == null && _vbe . ActiveCodePane == null )
86+ {
87+ UiDispatcher . Invoke ( ( ) => _selectionButton . Caption = string . Empty ) ;
88+ }
5489 else if ( declaration != null && ! declaration . IsBuiltIn && declaration . DeclarationType != DeclarationType . ClassModule && declaration . DeclarationType != DeclarationType . ProceduralModule )
5590 {
5691 var typeName = declaration . HasTypeHint
@@ -61,7 +96,7 @@ public void SetSelectionText(Declaration declaration)
6196 declaration . QualifiedSelection . Selection ,
6297 declaration . QualifiedName . QualifiedModuleName ,
6398 declaration . IdentifierName ,
64- RubberduckUI . ResourceManager . GetString ( "DeclarationType_" + declaration . DeclarationType , UI . Settings . Settings . Culture ) ,
99+ RubberduckUI . ResourceManager . GetString ( "DeclarationType_" + declaration . DeclarationType , Settings . Settings . Culture ) ,
65100 string . IsNullOrEmpty ( declaration . AsTypeName ) ? string . Empty : ": " + typeName ) ;
66101
67102 _selectionButton . TooltipText = string . IsNullOrEmpty ( declaration . DescriptionString )
@@ -82,7 +117,7 @@ public void SetSelectionText(Declaration declaration)
82117 selection . Value . Selection ,
83118 declaration . QualifiedName . QualifiedModuleName ,
84119 declaration . IdentifierName ,
85- RubberduckUI . ResourceManager . GetString ( "DeclarationType_" + declaration . DeclarationType , UI . Settings . Settings . Culture ) ,
120+ RubberduckUI . ResourceManager . GetString ( "DeclarationType_" + declaration . DeclarationType , Settings . Settings . Culture ) ,
86121 string . IsNullOrEmpty ( declaration . AsTypeName ) ? string . Empty : ": " + typeName ) ;
87122 }
88123 _selectionButton . TooltipText = string . IsNullOrEmpty ( declaration . DescriptionString )
@@ -100,7 +135,7 @@ private void State_StateChanged(object sender, EventArgs e)
100135 {
101136 if ( _state . Status != ParserState . ResolvedDeclarations )
102137 {
103- SetStatusText ( RubberduckUI . ResourceManager . GetString ( "ParserState_" + _state . Status , UI . Settings . Settings . Culture ) ) ;
138+ SetStatusText ( RubberduckUI . ResourceManager . GetString ( "ParserState_" + _state . Status , Settings . Settings . Culture ) ) ;
104139 }
105140 }
106141
@@ -115,7 +150,7 @@ private void OnRefresh()
115150 }
116151 }
117152
118- public void Initialize ( )
153+ private void Initialize ( )
119154 {
120155 _commandbar = _vbe . CommandBars . Add ( "Rubberduck" , MsoBarPosition . msoBarTop , false , true ) ;
121156
@@ -139,7 +174,7 @@ public void Initialize()
139174 _commandbar . Visible = true ;
140175 }
141176
142- private void refreshButton_Click ( CommandBarButton Ctrl , ref bool CancelDefault )
177+ private void refreshButton_Click ( CommandBarButton ctrl , ref bool cancelDefault )
143178 {
144179 OnRefresh ( ) ;
145180 }
@@ -154,6 +189,10 @@ public void Dispose()
154189
155190 _state . StateChanged -= State_StateChanged ;
156191
192+ _sinks . ProjectRemoved -= ProjectRemoved ;
193+ _sinks . ComponentActivated -= ComponentActivated ;
194+ _sinks . ComponentSelected -= ComponentSelected ;
195+
157196 _refreshButton . Delete ( ) ;
158197 _selectionButton . Delete ( ) ;
159198 _statusButton . Delete ( ) ;
0 commit comments