@@ -96,7 +96,7 @@ private void Sinks_ProjectAdded(object sender, IProjectEventArgs e)
96
96
97
97
Logger . Debug ( "Project '{0}' was added." , e . ProjectId ) ;
98
98
99
- AddProject ( e . ProjectId ) ; // note side-effect: assigns ProjectId/HelpFile
99
+ RefreshProjects ( ) ; // note side-effect: assigns ProjectId/HelpFile
100
100
OnParseRequested ( sender ) ;
101
101
}
102
102
@@ -122,7 +122,7 @@ private void Sinks_ProjectRenamed(object sender, IProjectRenamedEventArgs e)
122
122
Logger . Debug ( "Project {0} was renamed." , e . ProjectId ) ;
123
123
124
124
RemoveProject ( e . ProjectId ) ;
125
- AddProject ( e . ProjectId ) ;
125
+ RefreshProjects ( ) ;
126
126
127
127
OnParseRequested ( sender ) ;
128
128
}
@@ -189,7 +189,7 @@ private void Sinks_ComponentRenamed(object sender, IComponentRenamedEventArgs e)
189
189
RemoveProject ( e . ProjectId ) ;
190
190
Logger . Debug ( "Project '{0}' was removed." , e . ComponentName ) ;
191
191
192
- AddProject ( e . ProjectId ) ;
192
+ RefreshProjects ( ) ;
193
193
}
194
194
else
195
195
{
@@ -209,49 +209,23 @@ public void OnStatusMessageUpdate(string message)
209
209
}
210
210
}
211
211
212
- public void AddProject ( string projectId )
212
+ /// <summary>
213
+ /// Refreshes our list of cached projects.
214
+ /// Be sure to reparse after calling this in case there
215
+ /// were projects with duplicate ID's to clear the old
216
+ /// declarations referencing the project by the old ID.
217
+ /// </summary>
218
+ public void RefreshProjects ( )
213
219
{
214
- var projects = new List < VBProject > ( ) ;
215
- foreach ( VBProject p in _vbe . VBProjects )
220
+ _projects . Clear ( ) ;
221
+ foreach ( VBProject project in _vbe . VBProjects )
216
222
{
217
- if ( p . HelpFile != null && _projects . Keys . Contains ( p . HelpFile ) )
223
+ if ( string . IsNullOrEmpty ( project . HelpFile ) || _projects . Keys . Contains ( project . HelpFile ) )
218
224
{
219
- continue ;
225
+ project . AssignProjectId ( ) ;
220
226
}
221
227
222
- projects . Add ( p ) ;
223
- }
224
-
225
- if ( projects . Count == 0 )
226
- {
227
- Logger . Debug ( "Project was not found and will not be added to parser state." ) ;
228
- return ;
229
- }
230
-
231
- foreach ( var project in projects )
232
- {
233
- if ( project . Protection == vbext_ProjectProtection . vbext_pp_locked )
234
- {
235
- // adding protected project to parser state is asking for COMExceptions..
236
- Logger . Debug ( "Project is protected and will not be added to parser state." ) ;
237
- return ;
238
- }
239
-
240
- if ( string . IsNullOrEmpty ( project . HelpFile ) )
241
- {
242
- // assigns the help file and returns the value to reduce COM calls
243
- projectId = project . AssignProjectId ( ) ;
244
- }
245
-
246
- if ( ! _projects . ContainsKey ( projectId ) )
247
- {
248
- _projects . Add ( projectId , project ) ;
249
- }
250
-
251
- foreach ( VBComponent component in project . VBComponents )
252
- {
253
- _moduleStates . TryAdd ( new QualifiedModuleName ( component ) , new ModuleState ( ParserState . Pending ) ) ;
254
- }
228
+ _projects . Add ( project . HelpFile , project ) ;
255
229
}
256
230
}
257
231
@@ -723,70 +697,18 @@ public void ClearBuiltInReferences()
723
697
{
724
698
continue ;
725
699
}
726
-
727
- var hasReference = false ;
700
+
728
701
foreach ( var reference in declaration . References )
729
- {
730
- hasReference = true ;
731
- break ;
732
- }
733
-
734
- if ( hasReference )
735
702
{
736
703
declaration . ClearReferences ( ) ;
704
+ break ;
737
705
}
738
706
}
739
707
}
740
708
741
709
public bool ClearStateCache ( VBComponent component , bool notifyStateChanged = false )
742
710
{
743
- if ( component == null ) { return false ; }
744
-
745
- var match = new QualifiedModuleName ( component ) ;
746
-
747
- var keys = new List < QualifiedModuleName > { match } ;
748
- foreach ( var key in _moduleStates . Keys )
749
- {
750
- if ( key . Equals ( match ) && ! keys . Contains ( key ) )
751
- {
752
- keys . Add ( key ) ;
753
- }
754
- }
755
-
756
- var success = RemoveKeysFromCollections ( keys ) ;
757
-
758
- var projectId = component . Collection . Parent . HelpFile ;
759
- var sameProjectDeclarations = new List < KeyValuePair < QualifiedModuleName , ModuleState > > ( ) ;
760
- foreach ( var item in _moduleStates )
761
- {
762
- if ( item . Key . ProjectId == projectId )
763
- {
764
- sameProjectDeclarations . Add ( new KeyValuePair < QualifiedModuleName , ModuleState > ( item . Key , item . Value ) ) ;
765
- }
766
- }
767
-
768
- var projectCount = 0 ;
769
- foreach ( var item in sameProjectDeclarations )
770
- {
771
- if ( item . Value . Declarations == null ) { continue ; }
772
-
773
- foreach ( var declaration in item . Value . Declarations )
774
- {
775
- if ( declaration . Key . DeclarationType == DeclarationType . Project )
776
- {
777
- projectCount ++ ;
778
- break ;
779
- }
780
- }
781
- }
782
-
783
- if ( notifyStateChanged )
784
- {
785
- OnStateChanged ( ParserState . ResolvedDeclarations ) ; // trigger test explorer and code explorer updates
786
- OnStateChanged ( ParserState . Ready ) ; // trigger find all references &c. updates
787
- }
788
-
789
- return success ;
711
+ return component != null && ClearStateCache ( new QualifiedModuleName ( component ) , notifyStateChanged ) ;
790
712
}
791
713
792
714
public bool ClearStateCache ( QualifiedModuleName component , bool notifyStateChanged = false )
@@ -802,31 +724,6 @@ public bool ClearStateCache(QualifiedModuleName component, bool notifyStateChang
802
724
803
725
var success = RemoveKeysFromCollections ( keys ) ;
804
726
805
- var projectId = component . ProjectId ;
806
- var sameProjectDeclarations = new List < KeyValuePair < QualifiedModuleName , ModuleState > > ( ) ;
807
- foreach ( var item in _moduleStates )
808
- {
809
- if ( item . Key . ProjectId == projectId )
810
- {
811
- sameProjectDeclarations . Add ( new KeyValuePair < QualifiedModuleName , ModuleState > ( item . Key , item . Value ) ) ;
812
- }
813
- }
814
-
815
- var projectCount = 0 ;
816
- foreach ( var item in sameProjectDeclarations )
817
- {
818
- if ( item . Value . Declarations == null ) { continue ; }
819
-
820
- foreach ( var declaration in item . Value . Declarations )
821
- {
822
- if ( declaration . Key . DeclarationType == DeclarationType . Project )
823
- {
824
- projectCount ++ ;
825
- break ;
826
- }
827
- }
828
- }
829
-
830
727
if ( notifyStateChanged )
831
728
{
832
729
OnStateChanged ( ParserState . ResolvedDeclarations ) ; // trigger test explorer and code explorer updates
@@ -914,13 +811,19 @@ public bool IsDirty()
914
811
{
915
812
foreach ( var project in Projects )
916
813
{
917
- foreach ( VBComponent component in project . VBComponents )
814
+ try
918
815
{
919
- if ( IsNewOrModified ( component ) )
816
+ foreach ( VBComponent component in project . VBComponents )
920
817
{
921
- return true ;
818
+ if ( IsNewOrModified ( component ) )
819
+ {
820
+ return true ;
821
+ }
922
822
}
923
823
}
824
+ catch ( COMException )
825
+ {
826
+ }
924
827
}
925
828
926
829
return false ;
0 commit comments