Skip to content

Commit 37d09e1

Browse files
committed
Remove renamed component correctly
1 parent af2458a commit 37d09e1

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

RetailCoder.VBE/App.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ async void sink_ComponentRenamed(object sender, DispatcherRenamedEventArgs<VBCom
274274

275275
Debug.WriteLine(string.Format("Component '{0}' was renamed.", e.Item.Name));
276276

277-
_parser.State.OnParseRequested(sender, e.Item);
277+
_parser.State.RemoveRenamedComponent(e.Item, e.OldName);
278278
}
279279

280280
async void sink_ComponentRemoved(object sender, DispatcherEventArgs<VBComponent> e)

Rubberduck.Parsing/VBA/RubberduckParserState.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,47 @@ public bool ClearStateCache(VBComponent component, bool setStateChanged = false)
517517
return success;
518518
}
519519

520+
public bool RemoveRenamedComponent(VBComponent component, string oldComponentName)
521+
{
522+
var match = new QualifiedModuleName(component, oldComponentName);
523+
var keys = _declarations.Keys.Where(kvp => kvp.ComponentName == oldComponentName && kvp.ProjectId == match.ProjectId);
524+
525+
var success = true;
526+
var declarationsRemoved = 0;
527+
foreach (var key in keys)
528+
{
529+
ConcurrentDictionary<Declaration, byte> declarations = null;
530+
success = success && (!_declarations.ContainsKey(key) || _declarations.TryRemove(key, out declarations));
531+
declarationsRemoved = declarations == null ? 0 : declarations.Count;
532+
533+
IParseTree tree;
534+
success = success && (!_parseTrees.ContainsKey(key) || _parseTrees.TryRemove(key, out tree));
535+
536+
int contentHash;
537+
success = success && (!_moduleContentHashCodes.ContainsKey(key) || _moduleContentHashCodes.TryRemove(key, out contentHash));
538+
539+
IList<IAnnotation> annotations;
540+
success = success && (!_annotations.ContainsKey(key) || _annotations.TryRemove(key, out annotations));
541+
542+
ITokenStream stream;
543+
success = success && (!_tokenStreams.ContainsKey(key) || _tokenStreams.TryRemove(key, out stream));
544+
545+
ParserState state;
546+
success = success && (!_moduleStates.ContainsKey(key) || _moduleStates.TryRemove(key, out state));
547+
548+
SyntaxErrorException exception;
549+
success = success && (!_moduleExceptions.ContainsKey(key) || _moduleExceptions.TryRemove(key, out exception));
550+
551+
IList<CommentNode> nodes;
552+
success = success && (!_comments.ContainsKey(key) || _comments.TryRemove(key, out nodes));
553+
}
554+
555+
OnStateChanged();
556+
557+
Debug.WriteLine("RemoveRenamedComponent({0}): {1} - {2} declarations removed", oldComponentName, success ? "succeeded" : "failed", declarationsRemoved);
558+
return success;
559+
}
560+
520561
public void AddTokenStream(VBComponent component, ITokenStream stream)
521562
{
522563
_tokenStreams[new QualifiedModuleName(component)] = stream;

Rubberduck.VBEEditor/QualifiedModuleName.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,34 @@ public QualifiedModuleName(VBComponent component)
5959
: 0;
6060
}
6161

62+
/// <summary>
63+
/// Creates a QualifiedModuleName for removing renamed declarations.
64+
/// Do not use this overload.
65+
/// </summary>
66+
public QualifiedModuleName(VBComponent component, string oldComponentName)
67+
{
68+
_project = null; // field is only assigned when the instance refers to a VBProject.
69+
70+
_component = component;
71+
_componentName = oldComponentName;
72+
_project = component == null ? null : component.Collection.Parent;
73+
_projectName = _project == null ? string.Empty : _project.Name;
74+
_projectPath = string.Empty;
75+
_projectId = GetProjectId(_project);
76+
77+
_contentHashCode = 0;
78+
if (component == null)
79+
{
80+
return;
81+
}
82+
83+
var module = component.CodeModule;
84+
_contentHashCode = module.CountOfLines > 0
85+
// ReSharper disable once UseIndexedProperty
86+
? module.get_Lines(1, module.CountOfLines).GetHashCode()
87+
: 0;
88+
}
89+
6290
/// <summary>
6391
/// Creates a QualifiedModuleName for a built-in declaration.
6492
/// Do not use this overload for user declarations.

0 commit comments

Comments
 (0)