@@ -140,7 +140,6 @@ private void Sinks_ProjectAdded(object sender, ProjectEventArgs e)
140140 if ( ! e . Project . VBE . IsInDesignMode ) { return ; }
141141
142142 Logger . Debug ( "Project '{0}' was added." , e . ProjectId ) ;
143- RefreshProjects ( _vbe ) ; // note side-effect: assigns ProjectId/HelpFile
144143 OnParseRequested ( sender ) ;
145144 }
146145
@@ -149,7 +148,6 @@ private void Sinks_ProjectRemoved(object sender, ProjectEventArgs e)
149148 if ( ! e . Project . VBE . IsInDesignMode ) { return ; }
150149
151150 Debug . Assert ( e . ProjectId != null ) ;
152- RemoveProject ( e . ProjectId , true ) ;
153151 OnParseRequested ( sender ) ;
154152 }
155153
@@ -164,9 +162,6 @@ private void Sinks_ProjectRenamed(object sender, ProjectRenamedEventArgs e)
164162
165163 Logger . Debug ( "Project {0} was renamed." , e . ProjectId ) ;
166164
167- RemoveProject ( e . ProjectId ) ;
168- RefreshProjects ( e . Project . VBE ) ;
169-
170165 OnParseRequested ( sender ) ;
171166 }
172167
@@ -207,14 +202,26 @@ private void Sinks_ComponentRenamed(object sender, ComponentRenamedEventArgs e)
207202
208203 Logger . Debug ( "Component '{0}' was renamed to '{1}'." , e . OldName , e . Component . Name ) ;
209204
205+ //todo: Find out for which situation this drastic (and problematic) cache invalidation has been introduced.
206+ if ( ComponentIsWorksheet ( e ) )
207+ {
208+ RemoveProject ( e . ProjectId ) ;
209+ Logger . Debug ( "Project '{0}' was removed." , e . Component . Name ) ;
210+ }
211+
212+ OnParseRequested ( sender ) ;
213+ }
214+
215+ private bool ComponentIsWorksheet ( ComponentRenamedEventArgs e )
216+ {
210217 var componentIsWorksheet = false ;
211218 foreach ( var declaration in AllUserDeclarations )
212219 {
213220 if ( declaration . ProjectId == e . ProjectId &&
214221 declaration . DeclarationType == DeclarationType . ClassModule &&
215222 declaration . IdentifierName == e . OldName )
216223 {
217- foreach ( var superType in ( ( ClassModuleDeclaration ) declaration ) . Supertypes )
224+ foreach ( var superType in ( ( ClassModuleDeclaration ) declaration ) . Supertypes )
218225 {
219226 if ( superType . IdentifierName == "Worksheet" )
220227 {
@@ -227,19 +234,7 @@ private void Sinks_ComponentRenamed(object sender, ComponentRenamedEventArgs e)
227234 }
228235 }
229236
230- if ( componentIsWorksheet )
231- {
232- RemoveProject ( e . ProjectId ) ;
233- Logger . Debug ( "Project '{0}' was removed." , e . Component . Name ) ;
234-
235- RefreshProjects ( e . Project . VBE ) ;
236- }
237- else
238- {
239- RemoveRenamedComponent ( e . ProjectId , e . OldName ) ;
240- }
241-
242- OnParseRequested ( sender ) ;
237+ return componentIsWorksheet ;
243238 }
244239
245240 public void OnStatusMessageUpdate ( string message )
@@ -765,20 +760,20 @@ public void AddUnresolvedMemberDeclaration(UnboundMemberDeclaration declaration)
765760 }
766761 }
767762
768- private void ClearStateCache ( string projectId , bool notifyStateChanged = false )
763+ public void ClearStateCache ( string projectId , bool notifyStateChanged = false )
769764 {
770765 try
771766 {
772- foreach ( var moduleState in _moduleStates )
767+ foreach ( var moduleState in _moduleStates . Where ( moduleState => moduleState . Key . ProjectId == projectId ) )
773768 {
774- if ( moduleState . Key . ProjectId == projectId && moduleState . Key . Component != null )
769+ if ( moduleState . Key . Component != null )
775770 {
776771 while ( ! ClearStateCache ( moduleState . Key . Component ) )
777772 {
778773 // until Hell freezes over?
779774 }
780775 }
781- else if ( moduleState . Key . ProjectId == projectId && moduleState . Key . Component == null )
776+ else if ( moduleState . Key . Component == null )
782777 {
783778 // store project module name
784779 var qualifiedModuleName = moduleState . Key ;
@@ -790,8 +785,9 @@ private void ClearStateCache(string projectId, bool notifyStateChanged = false)
790785 }
791786 }
792787 }
793- catch ( COMException )
788+ catch ( COMException exception )
794789 {
790+ Logger . Error ( exception , $ "Unexpected COMException while clearing the project with projectId { projectId } . Clearing all modules.") ;
795791 _moduleStates . Clear ( ) ;
796792 }
797793
@@ -849,28 +845,6 @@ public bool ClearStateCache(QualifiedModuleName module, bool notifyStateChanged
849845 return success ;
850846 }
851847
852- private bool RemoveRenamedComponent ( string projectId , string oldComponentName )
853- {
854- var keys = new List < QualifiedModuleName > ( ) ;
855- foreach ( var key in _moduleStates . Keys )
856- {
857- if ( key . ComponentName == oldComponentName && key . ProjectId == projectId )
858- {
859- keys . Add ( key ) ;
860- }
861- }
862-
863- var success = keys . Count != 0 && RemoveKeysFromCollections ( keys ) ;
864-
865- if ( success )
866- {
867- OnStateChanged ( this , ParserState . ResolvedDeclarations ) ; // trigger test explorer and code explorer updates
868- OnStateChanged ( this , ParserState . Ready ) ; // trigger find all references &c. updates
869- }
870-
871- return success ;
872- }
873-
874848 private bool RemoveKeysFromCollections ( IEnumerable < QualifiedModuleName > keys )
875849 {
876850 var success = true ;
0 commit comments