Skip to content

Commit d50f1ae

Browse files
committed
Convert TestParseCoordinator into SynchronousParseCoordinator and refactor out Parse method
1 parent 5d30fc3 commit d50f1ae

File tree

5 files changed

+86
-90
lines changed

5 files changed

+86
-90
lines changed

Rubberduck.API/VBA/Parser.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Globalization;
55
using System.Linq;
66
using System.Runtime.InteropServices;
7+
using System.Threading;
78
using Rubberduck.Common;
89
using Rubberduck.Parsing.PreProcessing;
910
using Rubberduck.Parsing.Symbols.DeclarationLoaders;
@@ -60,15 +61,17 @@ public sealed class Parser : IParser, IDisposable
6061
{
6162
private RubberduckParserState _state;
6263
private AttributeParser _attributeParser;
63-
private ParseCoordinator _parser;
64+
private SynchronousParseCoordinator _parser;
6465
private IVBE _vbe;
6566
private IVBEEvents _vbeEvents;
6667
private readonly IUiDispatcher _dispatcher;
68+
private readonly CancellationTokenSource _tokenSource;
6769

6870
internal Parser()
6971
{
7072
UiContextProvider.Initialize();
7173
_dispatcher = new UiDispatcher(UiContextProvider.Instance());
74+
_tokenSource = new CancellationTokenSource();
7275
}
7376

7477
// vbe is the com coclass interface from the interop assembly.
@@ -137,7 +140,7 @@ internal Parser(object vbe) : this()
137140
supertypeClearer
138141
);
139142

140-
_parser = new ParseCoordinator(
143+
_parser = new SynchronousParseCoordinator(
141144
_state,
142145
parsingStageService,
143146
parsingCacheService,
@@ -151,8 +154,7 @@ internal Parser(object vbe) : this()
151154
/// </summary>
152155
public void Parse()
153156
{
154-
// blocking call
155-
_parser.Parse(this);
157+
_parser.Parse(_tokenSource);
156158
}
157159

158160
/// <summary>

Rubberduck.Parsing/Rubberduck.Parsing.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@
352352
<Compile Include="VBA\BuiltInDeclarationLoader.cs" />
353353
<Compile Include="PreProcessing\TokensValue.cs" />
354354
<Compile Include="VBA\SupertypeClearer.cs" />
355+
<Compile Include="VBA\SynchronousParseCoordinator.cs" />
355356
<Compile Include="VBA\SynchronousSupertypeClearer.cs" />
356357
<Compile Include="VBA\SupertypeClearerBase.cs" />
357358
<Compile Include="VBA\ISupertypeClearer.cs" />

Rubberduck.Parsing/VBA/ParseCoordinator.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public void SuspendRequested(object sender, RubberduckStatusSuspendParserEventAr
182182
/// Overriden in the unit test project to facilicate synchronous unit tests
183183
/// Refer to TestParserCoordinator class in the unit test project.
184184
/// </remarks>
185-
protected virtual void BeginParse(object sender)
185+
public virtual void BeginParse(object sender)
186186
{
187187
Task.Run(() => ParseAll(sender));
188188
}
@@ -205,17 +205,6 @@ private void Cancel(bool createNewTokenSource = true)
205205
}
206206
}
207207

208-
/// <summary>
209-
/// Support synchronous parsing.
210-
/// </summary>
211-
/// <remarks>Primarily used for the reflection API.
212-
/// Note this is also overriden in the TestParseCoordinator class.
213-
/// </remarks>
214-
public virtual void Parse(object sender)
215-
{
216-
ParseAll(sender);
217-
}
218-
219208
private void ExecuteCommonParseActivities(IReadOnlyCollection<QualifiedModuleName> toParse, IReadOnlyCollection<QualifiedModuleName> toReresolveReferencesInput, CancellationToken token)
220209
{
221210
token.ThrowIfCancellationRequested();
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Threading;
3+
4+
namespace Rubberduck.Parsing.VBA
5+
{
6+
public class SynchronousParseCoordinator : ParseCoordinator
7+
{
8+
public SynchronousParseCoordinator(
9+
RubberduckParserState state,
10+
IParsingStageService parsingStageService,
11+
IParsingCacheService parsingCacheService,
12+
IProjectManager projectManager,
13+
IParserStateManager parserStateManager) : base(
14+
state,
15+
parsingStageService,
16+
parsingCacheService,
17+
projectManager,
18+
parserStateManager)
19+
{ }
20+
21+
public override void BeginParse(object sender)
22+
{
23+
ParseInternal(CurrentCancellationTokenSource.Token);
24+
}
25+
26+
public void Parse(CancellationTokenSource token)
27+
{
28+
SetSavedCancellationTokenSource(token);
29+
ParseInternal(token.Token);
30+
}
31+
32+
/// <summary>
33+
/// For the use of tests & reflection API only
34+
/// </summary>
35+
private void SetSavedCancellationTokenSource(CancellationTokenSource tokenSource)
36+
{
37+
var oldTokenSource = CurrentCancellationTokenSource;
38+
CurrentCancellationTokenSource = tokenSource;
39+
40+
oldTokenSource?.Cancel();
41+
oldTokenSource?.Dispose();
42+
}
43+
44+
protected void ParseInternal(CancellationToken token)
45+
{
46+
var lockTaken = false;
47+
try
48+
{
49+
if (!ParsingSuspendLock.IsWriteLockHeld)
50+
{
51+
ParsingSuspendLock.EnterReadLock();
52+
}
53+
Monitor.Enter(ParsingRunSyncObject, ref lockTaken);
54+
ParseAllInternal(this, token);
55+
}
56+
catch (OperationCanceledException)
57+
{
58+
//This is the point to which the cancellation should break.
59+
}
60+
finally
61+
{
62+
if (lockTaken)
63+
{
64+
Monitor.Exit(ParsingRunSyncObject);
65+
}
66+
if (ParsingSuspendLock.IsReadLockHeld)
67+
{
68+
ParsingSuspendLock.ExitReadLock();
69+
}
70+
}
71+
}
72+
}
73+
}

RubberduckTests/Mocks/MockParser.cs

Lines changed: 5 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -19,75 +19,6 @@
1919

2020
namespace RubberduckTests.Mocks
2121
{
22-
internal class TestParseCoordinator : ParseCoordinator
23-
{
24-
public TestParseCoordinator(
25-
RubberduckParserState state,
26-
IParsingStageService parsingStageService,
27-
IParsingCacheService parsingCacheService,
28-
IProjectManager projectManager,
29-
IParserStateManager parserStateManager) : base(
30-
state,
31-
parsingStageService,
32-
parsingCacheService,
33-
projectManager,
34-
parserStateManager)
35-
{ }
36-
37-
protected override void BeginParse(object sender)
38-
{
39-
ParseInternal(CurrentCancellationTokenSource.Token);
40-
}
41-
42-
public void Parse(object sender, CancellationTokenSource token)
43-
{
44-
SetSavedCancellationTokenSource(token);
45-
ParseInternal(token.Token);
46-
}
47-
48-
/// <summary>
49-
/// For the use of tests & reflection API only
50-
/// </summary>
51-
private void SetSavedCancellationTokenSource(CancellationTokenSource tokenSource)
52-
{
53-
var oldTokenSource = CurrentCancellationTokenSource;
54-
CurrentCancellationTokenSource = tokenSource;
55-
56-
oldTokenSource?.Cancel();
57-
oldTokenSource?.Dispose();
58-
}
59-
60-
protected void ParseInternal(CancellationToken token)
61-
{
62-
var lockTaken = false;
63-
try
64-
{
65-
if (!ParsingSuspendLock.IsWriteLockHeld)
66-
{
67-
ParsingSuspendLock.EnterReadLock();
68-
}
69-
Monitor.Enter(ParsingRunSyncObject, ref lockTaken);
70-
ParseAllInternal(this, token);
71-
}
72-
catch (OperationCanceledException)
73-
{
74-
//This is the point to which the cancellation should break.
75-
}
76-
finally
77-
{
78-
if (lockTaken)
79-
{
80-
Monitor.Exit(ParsingRunSyncObject);
81-
}
82-
if (ParsingSuspendLock.IsReadLockHeld)
83-
{
84-
ParsingSuspendLock.ExitReadLock();
85-
}
86-
}
87-
}
88-
89-
}
90-
9122
public static class MockParser
9223
{
9324
public static RubberduckParserState ParseString(string inputCode, out QualifiedModuleName qualifiedModuleName)
@@ -105,7 +36,7 @@ public static RubberduckParserState ParseString(string inputCode, out QualifiedM
10536
return parser.State;
10637
}
10738

108-
public static ParseCoordinator Create(IVBE vbe, string serializedDeclarationsPath = null)
39+
public static SynchronousParseCoordinator Create(IVBE vbe, string serializedDeclarationsPath = null)
10940
{
11041
var vbeEvents = MockVbeEvents.CreateMockVbeEvents(new Moq.Mock<IVBE>());
11142
var declarationFinderFactory = new DeclarationFinderFactory();
@@ -114,14 +45,14 @@ public static ParseCoordinator Create(IVBE vbe, string serializedDeclarationsPat
11445
return Create(vbe, state, projectRepository, serializedDeclarationsPath);
11546
}
11647

117-
public static ParseCoordinator Create(IVBE vbe, RubberduckParserState state, IProjectsRepository projectRepository, string serializedDeclarationsPath = null)
48+
public static SynchronousParseCoordinator Create(IVBE vbe, RubberduckParserState state, IProjectsRepository projectRepository, string serializedDeclarationsPath = null)
11849
{
11950
var attributeParser = new TestAttributeParser(() => new VBAPreprocessor(double.Parse(vbe.Version, CultureInfo.InvariantCulture)), state.ProjectsProvider);
12051
var sourceCodeHandler = new Mock<ISourceCodeHandler>().Object;
12152
return Create(vbe, state, attributeParser, sourceCodeHandler, projectRepository, serializedDeclarationsPath);
12253
}
12354

124-
public static ParseCoordinator Create(IVBE vbe, RubberduckParserState state, IAttributeParser attributeParser, ISourceCodeHandler sourceCodeHandler, IProjectsRepository projectRepository, string serializedDeclarationsPath = null)
55+
public static SynchronousParseCoordinator Create(IVBE vbe, RubberduckParserState state, IAttributeParser attributeParser, ISourceCodeHandler sourceCodeHandler, IProjectsRepository projectRepository, string serializedDeclarationsPath = null)
12556
{
12657
var path = serializedDeclarationsPath ??
12758
Path.Combine(Path.GetDirectoryName(Assembly.GetAssembly(typeof(MockParser)).Location), "TestFiles", "Resolver");
@@ -173,7 +104,7 @@ public static ParseCoordinator Create(IVBE vbe, RubberduckParserState state, IAt
173104
supertypeClearer
174105
);
175106

176-
return new TestParseCoordinator(
107+
return new SynchronousParseCoordinator(
177108
state,
178109
parsingStageService,
179110
parsingCacheService,
@@ -200,7 +131,7 @@ public static RubberduckParserState CreateAndParse(IVBE vbe, string serializedDe
200131
return parser.State;
201132
}
202133

203-
private static ParseCoordinator CreateWithLibraries(IVBE vbe, string serializedDeclarationsPath = null, IEnumerable<string> testLibraries = null)
134+
private static SynchronousParseCoordinator CreateWithLibraries(IVBE vbe, string serializedDeclarationsPath = null, IEnumerable<string> testLibraries = null)
204135
{
205136
var parser = Create(vbe, serializedDeclarationsPath);
206137
if (testLibraries != null)

0 commit comments

Comments
 (0)