Skip to content

Commit 5af7fed

Browse files
committed
Provide a method to request a parser cancellation
1 parent 559991e commit 5af7fed

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

Rubberduck.Parsing/VBA/IParseManager.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,28 @@ public interface IParseManager
99
event EventHandler<ParserStateEventArgs> StateChanged;
1010
event EventHandler<ParseProgressEventArgs> ModuleStateChanged;
1111

12+
/// <summary>
13+
/// Requests reparse.
14+
/// </summary>
15+
/// <param name="requestor">The object requesting a reparse.</param>
1216
void OnParseRequested(object requestor);
17+
18+
/// <summary>
19+
/// Requests cancellation of the current parse.
20+
/// Always request a parse after requesting a cancellation.
21+
/// Failing to do so leaves RD in an undefined state.
22+
/// </summary>
23+
/// <param name="requestor">The object requesting a reparse.</param>
24+
void OnParseCancellationRequested(object requestor);
25+
26+
/// <summary>
27+
/// Suspends the parser for the action provided after the current parse has finished.
28+
/// Any incoming parse requests will be executed afterwards.
29+
/// </summary>
30+
/// <param name="requestor">The object requesting a reparse.</param>
31+
/// <param name="allowedRunStates">The states in which the action may be performed.</param>
32+
/// <param name="busyAction">The action to perform.</param>
33+
/// <param name="millisecondsTimeout">The timeout for waiting on the current parse to finish.</param>
1334
SuspensionResult OnSuspendParser(object requestor, IEnumerable<ParserState> allowedRunStates, Action busyAction, int millisecondsTimeout = -1);
1435
void MarkAsModified(QualifiedModuleName module);
1536
}

Rubberduck.Parsing/VBA/ParseCoordinator.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public ParseCoordinator(
6969
_rewritingManager = rewritingManager;
7070

7171
state.ParseRequest += ReparseRequested;
72+
state.ParseCancellationRequested += ParseCancellationRequested;
7273
state.SuspendRequest += SuspendRequested;
7374

7475
_requestorStack = new ConcurrentStack<object>();
@@ -99,6 +100,14 @@ private void ReparseRequested(object sender, EventArgs e)
99100
BeginParse(sender);
100101
}
101102

103+
private void ParseCancellationRequested(object requestor, EventArgs e)
104+
{
105+
lock (CancellationSyncObject)
106+
{
107+
Cancel();
108+
}
109+
}
110+
102111
public void SuspendRequested(object sender, RubberduckStatusSuspendParserEventArgs e)
103112
{
104113
if (ParsingSuspendLock.IsReadLockHeld)

Rubberduck.Parsing/VBA/RubberduckParserState.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public sealed class RubberduckParserState : IDisposable, IDeclarationFinderProvi
127127
new ConcurrentDictionary<QualifiedModuleName, ModuleState>();
128128

129129
public event EventHandler<EventArgs> ParseRequest;
130+
public event EventHandler<EventArgs> ParseCancellationRequested;
130131
public event EventHandler<RubberduckStatusSuspendParserEventArgs> SuspendRequest;
131132
public event EventHandler<RubberduckStatusMessageEventArgs> StatusMessageUpdate;
132133

@@ -963,14 +964,7 @@ public bool RemoveDeclaration(Declaration declaration)
963964
return _moduleStates[key].Declarations.TryRemove(declaration, out _);
964965
}
965966

966-
/// <summary>
967-
/// Ensures parser state accounts for built-in declarations.
968-
/// </summary>
969-
/// <summary>
970-
/// Requests reparse for specified component.
971-
/// Omit parameter to request a full reparse.
972-
/// </summary>
973-
/// <param name="requestor">The object requesting a reparse.</param>
967+
/// <inheritdoc />
974968
public void OnParseRequested(object requestor)
975969
{
976970
var handler = ParseRequest;
@@ -981,6 +975,18 @@ public void OnParseRequested(object requestor)
981975
}
982976
}
983977

978+
/// <inheritdoc />
979+
public void OnParseCancellationRequested(object requestor)
980+
{
981+
var handler = ParseCancellationRequested;
982+
if (handler != null && IsEnabled)
983+
{
984+
var args = EventArgs.Empty;
985+
handler.Invoke(requestor, args);
986+
}
987+
}
988+
989+
/// <inheritdoc />
984990
public SuspensionResult OnSuspendParser(object requestor, IEnumerable<ParserState> allowedRunStates, Action busyAction, int millisecondsTimeout = NoTimeout)
985991
{
986992
if (millisecondsTimeout < NoTimeout)

0 commit comments

Comments
 (0)