Skip to content

Commit d7b2504

Browse files
committed
Clean up the parser state some
1 parent 37d09e1 commit d7b2504

File tree

1 file changed

+16
-41
lines changed

1 file changed

+16
-41
lines changed

Rubberduck.Parsing/VBA/RubberduckParserState.cs

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ public void AddDeclaration(Declaration declaration)
430430
}
431431
}
432432

433-
public void ClearStateCache(VBProject project, bool setStateChanged = false)
433+
public void ClearStateCache(VBProject project, bool notifyStateChanged = false)
434434
{
435435
try
436436
{
@@ -447,7 +447,7 @@ public void ClearStateCache(VBProject project, bool setStateChanged = false)
447447
_declarations.Clear();
448448
}
449449

450-
if (setStateChanged)
450+
if (notifyStateChanged)
451451
{
452452
OnStateChanged();
453453
}
@@ -461,45 +461,18 @@ public void ClearBuiltInReferences()
461461
}
462462
}
463463

464-
public bool ClearStateCache(VBComponent component, bool setStateChanged = false)
464+
public bool ClearStateCache(VBComponent component, bool notifyStateChanged = false)
465465
{
466466
var match = new QualifiedModuleName(component);
467467
var keys = _declarations.Keys.Where(kvp => kvp.Equals(match))
468468
.Union(new[] { match }).Distinct(); // make sure the key is present, even if there are no declarations left
469469

470-
var success = true;
471-
var declarationsRemoved = 0;
472-
foreach (var key in keys)
473-
{
474-
ConcurrentDictionary<Declaration, byte> declarations = null;
475-
success = success && (!_declarations.ContainsKey(key) || _declarations.TryRemove(key, out declarations));
476-
declarationsRemoved = declarations == null ? 0 : declarations.Count;
477-
478-
IParseTree tree;
479-
success = success && (!_parseTrees.ContainsKey(key) || _parseTrees.TryRemove(key, out tree));
480-
481-
int contentHash;
482-
success = success && (!_moduleContentHashCodes.ContainsKey(key) || _moduleContentHashCodes.TryRemove(key, out contentHash));
483-
484-
IList<IAnnotation> annotations;
485-
success = success && (!_annotations.ContainsKey(key) || _annotations.TryRemove(key, out annotations));
486-
487-
ITokenStream stream;
488-
success = success && (!_tokenStreams.ContainsKey(key) || _tokenStreams.TryRemove(key, out stream));
489-
490-
ParserState state;
491-
success = success && (!_moduleStates.ContainsKey(key) || _moduleStates.TryRemove(key, out state));
492-
493-
SyntaxErrorException exception;
494-
success = success && (!_moduleExceptions.ContainsKey(key) || _moduleExceptions.TryRemove(key, out exception));
495-
496-
IList<CommentNode> nodes;
497-
success = success && (!_comments.ContainsKey(key) || _comments.TryRemove(key, out nodes));
498-
}
470+
var success = RemoveKeysFromCollections(keys);
499471

500472
var projectId = component.Collection.Parent.HelpFile;
501473
var sameProjectDeclarations = _declarations.Where(item => item.Key.ProjectId == projectId).ToList();
502-
if (sameProjectDeclarations.Any() && sameProjectDeclarations.Count(item => item.Value.Any(key => key.Key.DeclarationType == DeclarationType.Project)) == sameProjectDeclarations.Count)
474+
if (sameProjectDeclarations.Any() &&
475+
sameProjectDeclarations.Count(item => item.Value.Any(key => key.Key.DeclarationType == DeclarationType.Project)) == sameProjectDeclarations.Count)
503476
{
504477
// only the project declaration is left; remove it.
505478
ConcurrentDictionary<Declaration, byte> declarations;
@@ -508,12 +481,11 @@ public bool ClearStateCache(VBComponent component, bool setStateChanged = false)
508481
Debug.WriteLine(string.Format("Removed Project declaration for project Id {0}", projectId));
509482
}
510483

511-
if (setStateChanged)
484+
if (notifyStateChanged)
512485
{
513486
OnStateChanged();
514487
}
515488

516-
Debug.WriteLine("ClearDeclarations({0}): {1} - {2} declarations removed", component.Name, success ? "succeeded" : "failed", declarationsRemoved);
517489
return success;
518490
}
519491

@@ -522,13 +494,19 @@ public bool RemoveRenamedComponent(VBComponent component, string oldComponentNam
522494
var match = new QualifiedModuleName(component, oldComponentName);
523495
var keys = _declarations.Keys.Where(kvp => kvp.ComponentName == oldComponentName && kvp.ProjectId == match.ProjectId);
524496

497+
var success = RemoveKeysFromCollections(keys);
498+
499+
OnStateChanged();
500+
return success;
501+
}
502+
503+
private bool RemoveKeysFromCollections(IEnumerable<QualifiedModuleName> keys)
504+
{
525505
var success = true;
526-
var declarationsRemoved = 0;
527506
foreach (var key in keys)
528507
{
529-
ConcurrentDictionary<Declaration, byte> declarations = null;
508+
ConcurrentDictionary<Declaration, byte> declarations;
530509
success = success && (!_declarations.ContainsKey(key) || _declarations.TryRemove(key, out declarations));
531-
declarationsRemoved = declarations == null ? 0 : declarations.Count;
532510

533511
IParseTree tree;
534512
success = success && (!_parseTrees.ContainsKey(key) || _parseTrees.TryRemove(key, out tree));
@@ -552,9 +530,6 @@ public bool RemoveRenamedComponent(VBComponent component, string oldComponentNam
552530
success = success && (!_comments.ContainsKey(key) || _comments.TryRemove(key, out nodes));
553531
}
554532

555-
OnStateChanged();
556-
557-
Debug.WriteLine("RemoveRenamedComponent({0}): {1} - {2} declarations removed", oldComponentName, success ? "succeeded" : "failed", declarationsRemoved);
558533
return success;
559534
}
560535

0 commit comments

Comments
 (0)