Skip to content

Commit 524a07b

Browse files
committed
Reload the project when the main workbook/document/whatever is renamed. This prevents COM exceptions when accessing other components in the project.
1 parent f3a6b6d commit 524a07b

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

RetailCoder.VBE/App.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,47 @@ async void sink_ComponentRenamed(object sender, DispatcherRenamedEventArgs<VBCom
336336
_sourceControlPanelVM.HandleRenamedComponent(e.Item, e.OldName);
337337

338338
_logger.Debug("Component '{0}' was renamed to '{1}'.", e.OldName, e.Item.Name);
339+
340+
var projectId = e.Item.Collection.Parent.HelpFile;
341+
var componentDeclaration = _parser.State.AllDeclarations.FirstOrDefault(f =>
342+
f.ProjectId == projectId &&
343+
f.DeclarationType == DeclarationType.ClassModule &&
344+
f.IdentifierName == e.OldName);
345+
346+
if (e.Item.Type == vbext_ComponentType.vbext_ct_Document &&
347+
componentDeclaration != null &&
348+
349+
// according to ThunderFrame, Excel is the only one we explicitly support
350+
// with two Document-component types just skip the Worksheet component
351+
((ClassModuleDeclaration) componentDeclaration).Supertypes.All(a => a.IdentifierName != "Worksheet"))
352+
{
353+
_componentsEventsSinks.Remove(projectId);
354+
_referencesEventsSinks.Remove(projectId);
355+
_parser.State.RemoveProject(projectId);
356+
357+
_logger.Debug("Project '{0}' was removed.", e.Item.Name);
358+
Tuple<IConnectionPoint, int> componentsTuple;
359+
if (_componentsEventsConnectionPoints.TryGetValue(projectId, out componentsTuple))
360+
{
361+
componentsTuple.Item1.Unadvise(componentsTuple.Item2);
362+
_componentsEventsConnectionPoints.Remove(projectId);
363+
}
364+
365+
Tuple<IConnectionPoint, int> referencesTuple;
366+
if (_referencesEventsConnectionPoints.TryGetValue(projectId, out referencesTuple))
367+
{
368+
referencesTuple.Item1.Unadvise(referencesTuple.Item2);
369+
_referencesEventsConnectionPoints.Remove(projectId);
370+
}
339371

340-
_parser.State.RemoveRenamedComponent(e.Item, e.OldName);
372+
_parser.State.AddProject(e.Item.Collection.Parent);
373+
}
374+
else
375+
{
376+
_parser.State.RemoveRenamedComponent(e.Item, e.OldName);
377+
}
378+
379+
_parser.State.OnParseRequested(this);
341380
}
342381

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

RetailCoder.VBE/Refactorings/Rename/RenameRefactoring.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ private void Rename()
199199
if (ModuleDeclarationTypes.Contains(_model.Target.DeclarationType))
200200
{
201201
RenameModule();
202+
return; // renaming a component automatically triggers a reparse
202203
}
203204
else if (_model.Target.DeclarationType == DeclarationType.Project)
204205
{

0 commit comments

Comments
 (0)