1
1
using System ;
2
2
using System . Linq ;
3
3
using System . Threading . Tasks ;
4
- using Rubberduck . Parsing ;
5
4
using Rubberduck . Parsing . Symbols ;
5
+ using Rubberduck . Parsing . VBA ;
6
6
using Rubberduck . VBEditor . Events ;
7
7
using Rubberduck . VBEditor . SafeComWrappers ;
8
8
using Rubberduck . VBEditor . SafeComWrappers . Abstract ;
@@ -24,11 +24,13 @@ public class SelectionChangeService : ISelectionChangeService, IDisposable
24
24
25
25
private Declaration _lastSelectedDeclaration ;
26
26
private readonly IVBE _vbe ;
27
- private readonly IParseCoordinator _parser ;
27
+ private readonly IDeclarationFinderProvider _declarationFinderProvider ;
28
+ private readonly ISelectedDeclarationService _selectedDeclarationService ;
28
29
29
- public SelectionChangeService ( IVBE vbe , IParseCoordinator parser )
30
+ public SelectionChangeService ( IVBE vbe , IDeclarationFinderProvider declarationFinderProvider , ISelectedDeclarationService selectedDeclarationService )
30
31
{
31
- _parser = parser ;
32
+ _declarationFinderProvider = declarationFinderProvider ;
33
+ _selectedDeclarationService = selectedDeclarationService ;
32
34
_vbe = vbe ;
33
35
VbeNativeServices . SelectionChanged += OnVbeSelectionChanged ;
34
36
VbeNativeServices . WindowFocusChange += OnVbeFocusChanged ;
@@ -38,15 +40,9 @@ private void OnVbeSelectionChanged(object sender, EventArgs e)
38
40
{
39
41
Task . Run ( ( ) =>
40
42
{
41
- using ( var active = _vbe . ActiveCodePane )
42
- {
43
- if ( active == null )
44
- {
45
- return ;
46
- }
47
- var eventArgs = new DeclarationChangedEventArgs ( _vbe , _parser . State . FindSelectedDeclaration ( active ) ) ;
48
- DispatchSelectedDeclaration ( eventArgs ) ;
49
- }
43
+ var selectedDeclaration = _selectedDeclarationService . SelectedDeclaration ( ) ;
44
+ var eventArgs = new DeclarationChangedEventArgs ( _vbe , selectedDeclaration ) ;
45
+ DispatchSelectedDeclaration ( eventArgs ) ;
50
46
} ) ;
51
47
}
52
48
@@ -71,8 +67,15 @@ private void OnVbeFocusChanged(object sender, WindowChangedEventArgs e)
71
67
{
72
68
using ( var pane = VbeNativeServices . GetCodePaneFromHwnd ( e . Hwnd ) )
73
69
{
74
- DispatchSelectedDeclaration (
75
- new DeclarationChangedEventArgs ( _vbe , _parser . State . FindSelectedDeclaration ( pane ) ) ) ;
70
+ var selection = pane . GetQualifiedSelection ( ) ;
71
+ if ( ! selection . HasValue )
72
+ {
73
+ return ;
74
+ }
75
+
76
+ var selectedDeclaration = _selectedDeclarationService . SelectedDeclaration ( selection . Value ) ;
77
+ var eventArgs = new DeclarationChangedEventArgs ( _vbe , selectedDeclaration ) ;
78
+ DispatchSelectedDeclaration ( eventArgs ) ;
76
79
}
77
80
} ) ;
78
81
break ;
@@ -125,7 +128,7 @@ private void DispatchSelectedDesignerDeclaration(IVBComponent component)
125
128
{
126
129
var name = selected . Single ( ) . Name ;
127
130
var control =
128
- _parser . State . DeclarationFinder . MatchName ( name )
131
+ _declarationFinderProvider . DeclarationFinder ? . MatchName ( name )
129
132
. SingleOrDefault ( d => d . DeclarationType == DeclarationType . Control
130
133
&& d . ProjectId == parent . ProjectId
131
134
&& d . ParentDeclaration . IdentifierName == component . Name ) ;
@@ -134,7 +137,7 @@ private void DispatchSelectedDesignerDeclaration(IVBComponent component)
134
137
return ;
135
138
}
136
139
var form =
137
- _parser . State . DeclarationFinder . MatchName ( component . Name )
140
+ _declarationFinderProvider . DeclarationFinder ? . MatchName ( component . Name )
138
141
. SingleOrDefault ( d => d . DeclarationType . HasFlag ( DeclarationType . ClassModule )
139
142
&& d . ProjectId == parent . ProjectId ) ;
140
143
@@ -144,7 +147,7 @@ private void DispatchSelectedDesignerDeclaration(IVBComponent component)
144
147
145
148
private void DispatchSelectedProjectNodeDeclaration ( IVBComponent component )
146
149
{
147
- if ( _parser . State . DeclarationFinder == null )
150
+ if ( _declarationFinderProvider . DeclarationFinder == null )
148
151
{
149
152
return ;
150
153
}
@@ -155,7 +158,7 @@ private void DispatchSelectedProjectNodeDeclaration(IVBComponent component)
155
158
{
156
159
//The user might have selected the project node in Project Explorer. If they've chosen a folder, we'll return the project anyway.
157
160
var project =
158
- _parser . State . DeclarationFinder . UserDeclarations ( DeclarationType . Project )
161
+ _declarationFinderProvider . DeclarationFinder . UserDeclarations ( DeclarationType . Project )
159
162
. SingleOrDefault ( decl => decl . ProjectId . Equals ( active . ProjectId ) ) ;
160
163
161
164
DispatchSelectedDeclaration ( new DeclarationChangedEventArgs ( _vbe , project ) ) ;
@@ -168,10 +171,10 @@ private void DispatchSelectedProjectNodeDeclaration(IVBComponent component)
168
171
{
169
172
170
173
var module =
171
- _parser . State . AllUserDeclarations . SingleOrDefault (
172
- decl => decl . DeclarationType . HasFlag ( DeclarationType . Module ) &&
173
- decl . IdentifierName . Equals ( component . Name ) &&
174
- decl . ProjectId . Equals ( active . ProjectId ) ) ;
174
+ _declarationFinderProvider . DeclarationFinder
175
+ ? . UserDeclarations ( DeclarationType . Module )
176
+ . SingleOrDefault ( decl => decl . IdentifierName . Equals ( component . Name )
177
+ && decl . ProjectId . Equals ( active . ProjectId ) ) ;
175
178
176
179
DispatchSelectedDeclaration ( new DeclarationChangedEventArgs ( _vbe , module ) ) ;
177
180
}
0 commit comments