@@ -16,6 +16,18 @@ public enum ResolutionState
16
16
Unresolved
17
17
}
18
18
19
+ public class ParserStateEventArgs : EventArgs
20
+ {
21
+ private readonly ParserState _state ;
22
+
23
+ public ParserStateEventArgs ( ParserState state )
24
+ {
25
+ _state = state ;
26
+ }
27
+
28
+ public ParserState State { get { return _state ; } }
29
+ }
30
+
19
31
public sealed class RubberduckParserState
20
32
{
21
33
public event EventHandler ParseRequest ;
@@ -30,14 +42,14 @@ public sealed class RubberduckParserState
30
42
private readonly ConcurrentDictionary < VBComponent , IParseTree > _parseTrees =
31
43
new ConcurrentDictionary < VBComponent , IParseTree > ( ) ;
32
44
33
- public event EventHandler StateChanged ;
45
+ public event EventHandler < ParserStateEventArgs > StateChanged ;
34
46
35
- private void OnStateChanged ( )
47
+ private void OnStateChanged ( ParserState state )
36
48
{
37
49
var handler = StateChanged ;
38
50
if ( handler != null )
39
51
{
40
- handler . Invoke ( this , EventArgs . Empty ) ;
52
+ handler . Invoke ( this , new ParserStateEventArgs ( state ) ) ;
41
53
}
42
54
}
43
55
@@ -56,16 +68,41 @@ public void SetModuleState(VBComponent component, ParserState state, SyntaxError
56
68
// prevent multiple threads from changing state simultaneously:
57
69
lock ( _lock )
58
70
{
59
- Status = _moduleStates . Values . Any ( value => value == ParserState . Error )
60
- ? ParserState . Error
61
- : _moduleStates . Values . Any ( value => value == ParserState . Parsing )
62
- ? ParserState . Parsing
63
- : _moduleStates . Values . Any ( value => value == ParserState . Resolving )
64
- ? ParserState . Resolving
65
- : ParserState . Ready ;
71
+ Status = EvaluateParserState ( ) ;
72
+
66
73
}
67
74
}
68
75
76
+ private ParserState EvaluateParserState ( )
77
+ {
78
+ var moduleStates = _moduleStates . Values . ToList ( ) ;
79
+ var state = Enum . GetValues ( typeof ( ParserState ) ) . Cast < ParserState > ( )
80
+ . SingleOrDefault ( value => moduleStates . All ( ps => ps == value ) ) ;
81
+
82
+ if ( state != default ( ParserState ) )
83
+ {
84
+ // if all modules are in the same state, we have our result.
85
+ return state ;
86
+ }
87
+
88
+ // intermediate states are toggled when *any* module has them.
89
+ if ( moduleStates . Any ( ms => ms == ParserState . Error ) )
90
+ {
91
+ // error state takes precedence over every other state
92
+ return ParserState . Error ;
93
+ }
94
+ if ( moduleStates . Any ( ms => ms == ParserState . Parsing || ms == ParserState . Parsed ) )
95
+ {
96
+ return ParserState . Parsing ;
97
+ }
98
+ if ( moduleStates . Any ( ms => ms == ParserState . Resolving ) )
99
+ {
100
+ return ParserState . Resolving ;
101
+ }
102
+
103
+ return ParserState . Pending ;
104
+ }
105
+
69
106
public ParserState GetModuleState ( VBComponent component )
70
107
{
71
108
return _moduleStates [ component ] ;
@@ -80,7 +117,7 @@ private set
80
117
if ( _status != value )
81
118
{
82
119
_status = value ;
83
- OnStateChanged ( ) ;
120
+ OnStateChanged ( value ) ;
84
121
}
85
122
}
86
123
}
@@ -125,7 +162,7 @@ public IEnumerable<CommentNode> Comments
125
162
{
126
163
get
127
164
{
128
- return _comments . Values . SelectMany ( comments => comments ) ;
165
+ return _comments . Values . SelectMany ( comments => comments . ToList ( ) ) ;
129
166
}
130
167
}
131
168
0 commit comments