11
11
12
12
namespace Rubberduck . Parsing . VBA
13
13
{
14
+ public enum ResolutionState
15
+ {
16
+ Unresolved
17
+ }
18
+
14
19
public class RubberduckParserState
15
20
{
16
21
// keys are the declarations; values indicate whether a declaration is resolved.
17
- private readonly ConcurrentHashSet < Declaration > _declarations =
18
- new ConcurrentHashSet < Declaration > ( ) ;
22
+ private readonly ConcurrentDictionary < Declaration , ResolutionState > _declarations =
23
+ new ConcurrentDictionary < Declaration , ResolutionState > ( ) ;
19
24
20
25
private readonly ConcurrentDictionary < VBComponent , ITokenStream > _tokenStreams =
21
26
new ConcurrentDictionary < VBComponent , ITokenStream > ( ) ;
@@ -61,7 +66,7 @@ public ParserState GetModuleState(VBComponent component)
61
66
}
62
67
63
68
private ParserState _status ;
64
- public ParserState Status { get { return _status ; } private set { if ( _status != value ) { _status = value ; OnStateChanged ( ) ; } } }
69
+ public ParserState Status { get { return _status ; } private set { if ( _status != value ) { _status = value ; OnStateChanged ( ) ; } } }
65
70
66
71
private IEnumerable < QualifiedContext > _obsoleteCallContexts = new List < QualifiedContext > ( ) ;
67
72
@@ -90,7 +95,7 @@ public IEnumerable<QualifiedContext> ObsoleteLetContexts
90
95
91
96
public IEnumerable < CommentNode > Comments
92
97
{
93
- get
98
+ get
94
99
{
95
100
return _comments . Values . SelectMany ( comments => comments ) ;
96
101
}
@@ -104,45 +109,34 @@ public void SetModuleComments(VBComponent component, IEnumerable<CommentNode> co
104
109
/// <summary>
105
110
/// Gets a copy of the collected declarations.
106
111
/// </summary>
107
- public IEnumerable < Declaration > AllDeclarations { get { return _declarations ; } }
112
+ public IEnumerable < Declaration > AllDeclarations { get { return _declarations . Keys . ToList ( ) ; } }
108
113
109
114
/// <summary>
110
115
/// Adds the specified <see cref="Declaration"/> to the collection (replaces existing).
111
116
/// </summary>
112
117
public void AddDeclaration ( Declaration declaration )
113
118
{
114
- if ( _declarations . Add ( declaration ) )
119
+ if ( _declarations . TryAdd ( declaration , ResolutionState . Unresolved ) )
115
120
{
116
121
return ;
117
122
}
118
123
119
124
if ( RemoveDeclaration ( declaration ) )
120
125
{
121
- _declarations . Add ( declaration ) ;
126
+ _declarations . TryAdd ( declaration , ResolutionState . Unresolved ) ;
122
127
}
123
128
}
124
129
125
130
public void ClearDeclarations ( VBComponent component )
126
131
{
127
- var declarations = _declarations . Where ( k =>
132
+ var declarations = _declarations . Keys . Where ( k =>
128
133
k . QualifiedName . QualifiedModuleName . Project == component . Collection . Parent
129
134
&& k . ComponentName == component . Name ) ;
130
135
131
- while ( true )
136
+ foreach ( var declaration in declarations )
132
137
{
133
- try
134
- {
135
- foreach ( var declaration in declarations )
136
- {
137
- _declarations . Remove ( declaration ) ;
138
- }
139
-
140
- return ;
141
- }
142
- catch ( InvalidOperationException )
143
- {
144
-
145
- }
138
+ ResolutionState state ;
139
+ _declarations . TryRemove ( declaration , out state ) ;
146
140
}
147
141
}
148
142
@@ -170,7 +164,8 @@ public TokenStreamRewriter GetRewriter(VBComponent component)
170
164
/// <returns>Returns true when successful.</returns>
171
165
private bool RemoveDeclaration ( Declaration declaration )
172
166
{
173
- return _declarations . Remove ( declaration ) ;
167
+ ResolutionState state ;
168
+ return _declarations . TryRemove ( declaration , out state ) ;
174
169
}
175
170
176
171
/// <summary>
@@ -179,15 +174,15 @@ private bool RemoveDeclaration(Declaration declaration)
179
174
/// </summary>
180
175
public void AddBuiltInDeclarations ( IHostApplication hostApplication )
181
176
{
182
- if ( _declarations . Any ( declaration => declaration . IsBuiltIn ) )
177
+ if ( _declarations . Any ( declaration => declaration . Key . IsBuiltIn ) )
183
178
{
184
179
return ;
185
180
}
186
181
187
182
var builtInDeclarations = VbaStandardLib . Declarations ;
188
183
189
184
// cannot be strongly-typed here because of constraints on COM interop and generics in the inheritance hierarchy. </rant>
190
- if ( hostApplication /*is ExcelApp*/ . ApplicationName == "Excel" )
185
+ if ( hostApplication /*is ExcelApp*/ . ApplicationName == "Excel" )
191
186
{
192
187
builtInDeclarations = builtInDeclarations . Concat ( ExcelObjectModel . Declarations ) ;
193
188
}
0 commit comments