Skip to content

Commit 1dcc114

Browse files
committed
Generate task to capture component parser result to allow to push the state changes after the component parser returned.
1 parent 776904b commit 1dcc114

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

Rubberduck.Parsing/VBA/ParseCoordinator.cs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -551,30 +551,50 @@ private void Cancel(bool createNewTokenSource = true)
551551

552552
private void ParseAsyncInternal(IVBComponent component, CancellationToken token, TokenStreamRewriter rewriter = null)
553553
{
554+
State.SetModuleState(component, ParserState.Parsing);
555+
554556
var preprocessor = _preprocessorFactory();
555557
var parser = new ComponentParseTask(component, preprocessor, _attributeParser, rewriter);
556-
parser.ParseFailure += (sender, e) =>
558+
559+
var finishedParseTask = FinishedComponentParseTask(parser, token); //This runs synchronously.
560+
561+
if (finishedParseTask.IsFaulted)
557562
{
558-
State.SetModuleState(component, ParserState.Error, e.Cause as SyntaxErrorException);
559-
};
560-
parser.ParseCompleted += (sender, e) =>
563+
State.SetModuleState(component, ParserState.Error, finishedParseTask.Exception.InnerException as SyntaxErrorException);
564+
}
565+
else if (finishedParseTask.IsCompleted)
561566
{
567+
var result = finishedParseTask.Result;
562568
lock (State)
569+
{
563570
lock (component)
564571
{
565-
State.SetModuleAttributes(component, e.Attributes);
566-
State.AddParseTree(component, e.ParseTree);
567-
State.AddTokenStream(component, e.Tokens);
568-
State.SetModuleComments(component, e.Comments);
569-
State.SetModuleAnnotations(component, e.Annotations);
572+
State.SetModuleAttributes(component, result.Attributes);
573+
State.AddParseTree(component, result.ParseTree);
574+
State.AddTokenStream(component, result.Tokens);
575+
State.SetModuleComments(component, result.Comments);
576+
State.SetModuleAnnotations(component, result.Annotations);
570577

571578
// This really needs to go last
572579
State.SetModuleState(component, ParserState.Parsed);
573580
}
581+
}
582+
}
583+
}
584+
585+
private Task<ComponentParseTask.ParseCompletionArgs> FinishedComponentParseTask(ComponentParseTask parser, CancellationToken token)
586+
{
587+
var tcs = new TaskCompletionSource<ComponentParseTask.ParseCompletionArgs>();
588+
parser.ParseFailure += (sender, e) =>
589+
{
590+
tcs.SetException(e.Cause);
591+
};
592+
parser.ParseCompleted += (sender, e) =>
593+
{
594+
tcs.SetResult(e);
574595
};
575-
State.SetModuleState(component, ParserState.Parsing);
576-
577596
parser.Start(token);
597+
return tcs.Task;
578598
}
579599

580600
private readonly ConcurrentDictionary<string, Declaration> _projectDeclarations = new ConcurrentDictionary<string, Declaration>();

0 commit comments

Comments
 (0)