@@ -25,7 +25,7 @@ public abstract class COMReferenceSynchronizerBase : ICOMReferenceSynchronizer,
25
25
private readonly IReferencedDeclarationsCollector _referencedDeclarationsCollector ;
26
26
27
27
private readonly List < QualifiedModuleName > _unloadedCOMReferences = new List < QualifiedModuleName > ( ) ;
28
- private readonly List < ( string projectId , string referencedProjectId , int newPriority , int oldPriority ) > _referencesWithChangedPriority = new List < ( string projectId , string referencedProjectId , int newPriority , int oldPriority ) > ( ) ;
28
+ private readonly List < ( string projectId , string referencedProjectId ) > _referencesAffectedByPriorityChanges = new List < ( string projectId , string referencedProjectId ) > ( ) ;
29
29
30
30
private readonly Dictionary < ( string identifierName , string fullPath ) , string > _projectIdsByFilePathAndProjectName = new Dictionary < ( string identifierName , string fullPath ) , string > ( ) ;
31
31
@@ -59,7 +59,8 @@ protected COMReferenceSynchronizerBase(RubberduckParserState state, IParserState
59
59
60
60
61
61
public bool LastSyncOfCOMReferencesLoadedReferences { get ; private set ; }
62
- public IEnumerable < QualifiedModuleName > COMReferencesUnloadedUnloadedInLastSync => _unloadedCOMReferences ;
62
+ public IEnumerable < QualifiedModuleName > COMReferencesUnloadedInLastSync => _unloadedCOMReferences ;
63
+ public IEnumerable < ( string projectId , string referencedProjectId ) > COMReferencesAffectedByPriorityChangesInLastSync => _referencesAffectedByPriorityChanges ;
63
64
64
65
private readonly IDictionary < string , ReferencePriorityMap > _projectReferences = new Dictionary < string , ReferencePriorityMap > ( ) ;
65
66
public IReadOnlyCollection < ReferencePriorityMap > ProjectReferences => _projectReferences . Values . AsReadOnly ( ) ;
@@ -77,7 +78,7 @@ public void SyncComReferences(CancellationToken token)
77
78
78
79
LastSyncOfCOMReferencesLoadedReferences = false ;
79
80
_unloadedCOMReferences . Clear ( ) ;
80
- _referencesWithChangedPriority . Clear ( ) ;
81
+ _referencesAffectedByPriorityChanges . Clear ( ) ;
81
82
RefreshReferencedByProjectId ( ) ;
82
83
83
84
var unmapped = new ConcurrentBag < ReferenceInfo > ( ) ;
@@ -91,31 +92,55 @@ public void SyncComReferences(CancellationToken token)
91
92
LoadReferences ( referencesToLoad , unmapped , token ) ;
92
93
}
93
94
94
- DetermineReferencesWithChangedPriorities ( _projectReferences , oldProjectReferences ) ;
95
+ DetermineReferencesAffectedByPriorityChanges ( _projectReferences , oldProjectReferences ) ;
96
+ UnloadNoLongerExistingReferences ( _projectReferences , oldProjectReferences ) ;
95
97
98
+ parsingStageTimer . Stop ( ) ;
99
+ parsingStageTimer . Log ( "Loaded and unloaded referenced libraries in {0}ms." ) ;
100
+ }
101
+
102
+ private void UnloadNoLongerExistingReferences ( IDictionary < string , ReferencePriorityMap > newProjectReferences , IDictionary < string , ReferencePriorityMap > oldProjectReferences )
103
+ {
96
104
var noLongerReferencedProjectIds = oldProjectReferences . Keys
97
- . Where ( projectId => ! _projectReferences . ContainsKey ( projectId ) )
105
+ . Where ( projectId => ! newProjectReferences . ContainsKey ( projectId ) )
98
106
. ToList ( ) ;
99
107
100
108
foreach ( var referencedProjectId in noLongerReferencedProjectIds )
101
109
{
102
110
UnloadComReference ( referencedProjectId ) ;
103
111
}
112
+ }
104
113
105
- parsingStageTimer . Stop ( ) ;
106
- parsingStageTimer . Log ( "Loaded and unloaded referenced libraries in {0}ms." ) ;
114
+ private void DetermineReferencesAffectedByPriorityChanges ( IDictionary < string , ReferencePriorityMap > newProjectReferences , IDictionary < string , ReferencePriorityMap > oldProjectReferences )
115
+ {
116
+ var referencePriorityChanges = ReferencePriorityChanges ( newProjectReferences , oldProjectReferences ) ;
117
+
118
+ foreach ( var oldMap in oldProjectReferences . Values )
119
+ {
120
+ foreach ( var projectId in referencePriorityChanges . Keys )
121
+ {
122
+ if ( oldMap . TryGetValue ( projectId , out var priority )
123
+ && referencePriorityChanges [ projectId ]
124
+ . Any ( tpl => ( tpl . oldPriority <= priority && tpl . newPriority >= priority )
125
+ || ( tpl . oldPriority >= priority && tpl . newPriority <= priority ) ) )
126
+ {
127
+ _referencesAffectedByPriorityChanges . Add ( ( projectId , oldMap . ReferencedProjectId ) ) ;
128
+ }
129
+ }
130
+ }
107
131
}
108
132
109
- private void DetermineReferencesWithChangedPriorities ( IDictionary < string , ReferencePriorityMap > newProjectReferences , IDictionary < string , ReferencePriorityMap > oldProjectReferences )
133
+ private static IDictionary < string , List < ( int newPriority , int oldPriority ) > > ReferencePriorityChanges ( IDictionary < string , ReferencePriorityMap > newProjectReferences , IDictionary < string , ReferencePriorityMap > oldProjectReferences )
110
134
{
135
+ var referencePriorityChanges = new Dictionary < string , List < ( int newPriority , int oldPriority ) > > ( ) ;
111
136
foreach ( var referencedProjectId in oldProjectReferences . Keys )
112
137
{
113
138
var oldMap = oldProjectReferences [ referencedProjectId ] ;
114
139
if ( ! newProjectReferences . TryGetValue ( referencedProjectId , out var newMap ) )
115
140
{
116
141
foreach ( var projectId in oldMap . Keys )
117
142
{
118
- _referencesWithChangedPriority . Add ( ( projectId , referencedProjectId , int . MaxValue , oldMap [ projectId ] ) ) ;
143
+ AddPriorityChangeToDictionary ( projectId , int . MaxValue , oldMap [ projectId ] , referencePriorityChanges ) ;
119
144
}
120
145
}
121
146
else
@@ -130,17 +155,15 @@ private void DetermineReferencesWithChangedPriorities(IDictionary<string, Refere
130
155
131
156
if ( newPriority != oldPriority )
132
157
{
133
- _referencesWithChangedPriority . Add ( ( projectId , referencedProjectId , newPriority ,
134
- oldMap [ projectId ] ) ) ;
158
+ AddPriorityChangeToDictionary ( projectId , newPriority , oldMap [ projectId ] , referencePriorityChanges ) ;
135
159
}
136
160
}
137
161
138
162
foreach ( var projectId in newMap . Keys )
139
163
{
140
164
if ( ! oldMap . ContainsKey ( projectId ) )
141
165
{
142
- _referencesWithChangedPriority . Add (
143
- ( projectId , referencedProjectId , newMap [ projectId ] , int . MaxValue ) ) ;
166
+ AddPriorityChangeToDictionary ( projectId , newMap [ projectId ] , int . MaxValue , referencePriorityChanges ) ;
144
167
}
145
168
}
146
169
}
@@ -156,9 +179,27 @@ private void DetermineReferencesWithChangedPriorities(IDictionary<string, Refere
156
179
var newMap = newProjectReferences [ referencedProjectId ] ;
157
180
foreach ( var projectId in newMap . Keys )
158
181
{
159
- _referencesWithChangedPriority . Add ( ( projectId , referencedProjectId , newMap [ projectId ] , int . MaxValue ) ) ;
182
+ AddPriorityChangeToDictionary ( projectId , newMap [ projectId ] , int . MaxValue , referencePriorityChanges ) ;
160
183
}
161
184
}
185
+
186
+ return referencePriorityChanges ;
187
+ }
188
+
189
+ private static void AddPriorityChangeToDictionary (
190
+ string projectId ,
191
+ int newPriority ,
192
+ int oldPriority ,
193
+ IDictionary < string , List < ( int newPriority , int oldPriority ) > > dict )
194
+ {
195
+ if ( dict . TryGetValue ( projectId , out var changeList ) )
196
+ {
197
+ changeList . Add ( ( newPriority , oldPriority ) ) ;
198
+ }
199
+ else
200
+ {
201
+ dict . Add ( projectId , new List < ( int newPriority , int oldPriority ) > { ( newPriority , oldPriority ) } ) ;
202
+ }
162
203
}
163
204
164
205
private Dictionary < string , List < ReferenceInfo > > ReferencedByProjectId ( )
0 commit comments