18
18
using System . Runtime . InteropServices ;
19
19
using Rubberduck . VBEditor . Application ;
20
20
using Rubberduck . VBEditor . Extensions ;
21
- using Rubberduck . VBEditor . SafeComWrappers ;
22
21
23
22
// ReSharper disable LoopCanBeConvertedToQuery
24
23
@@ -62,7 +61,7 @@ public ParseCoordinator(
62
61
_preprocessorFactory = preprocessorFactory ;
63
62
_customDeclarationLoaders = customDeclarationLoaders ;
64
63
_isTestScope = isTestScope ;
65
- _serializedDeclarationsPath = serializedDeclarationsPath
64
+ _serializedDeclarationsPath = serializedDeclarationsPath
66
65
?? Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) , "Rubberduck" , "declarations" ) ;
67
66
_hostApp = _vbe . HostApplication ( ) ;
68
67
@@ -72,7 +71,7 @@ public ParseCoordinator(
72
71
// Do not access this from anywhere but ReparseRequested.
73
72
// ReparseRequested needs to have a reference to all the cancellation tokens,
74
73
// but the cancelees need to use their own token.
75
- private readonly List < CancellationTokenSource > _cancellationTokens = new List < CancellationTokenSource > { new CancellationTokenSource ( ) } ;
74
+ private readonly List < CancellationTokenSource > _cancellationTokens = new List < CancellationTokenSource > { new CancellationTokenSource ( ) } ;
76
75
77
76
private void ReparseRequested ( object sender , EventArgs e )
78
77
{
@@ -183,7 +182,6 @@ private void ExecuteCommonParseActivities(List<IVBComponent> toParse, Cancellati
183
182
184
183
if ( token . IsCancellationRequested || State . Status >= ParserState . Error )
185
184
{
186
- _state . IsEnabled = true ;
187
185
return ;
188
186
}
189
187
@@ -192,7 +190,6 @@ private void ExecuteCommonParseActivities(List<IVBComponent> toParse, Cancellati
192
190
193
191
if ( token . IsCancellationRequested || State . Status >= ParserState . Error )
194
192
{
195
- _state . IsEnabled = true ;
196
193
return ;
197
194
}
198
195
@@ -224,7 +221,7 @@ private void SetModuleStates(List<IVBComponent> components, ParserState parserSt
224
221
options . MaxDegreeOfParallelism = _maxDegreeOfModuleStateChangeParallelism ;
225
222
226
223
Parallel . ForEach ( components , options , component => State . SetModuleState ( component , parserState , null , false ) ) ;
227
-
224
+
228
225
State . EvaluateParserState ( ) ;
229
226
}
230
227
@@ -413,7 +410,7 @@ private void ResolveAllReferences(CancellationToken token)
413
410
{
414
411
var components = State . ParseTrees . Select ( kvp => kvp . Key . Component ) . ToList ( ) ;
415
412
SetModuleStates ( components , ParserState . ResolvingReferences ) ;
416
-
413
+
417
414
ExecuteCompilationPasses ( ) ;
418
415
419
416
var options = new ParallelOptions ( ) ;
@@ -474,7 +471,7 @@ private void ResolveReferences(DeclarationFinder finder, IVBComponent component,
474
471
watch . ElapsedMilliseconds , Thread . CurrentThread . ManagedThreadId ) ;
475
472
476
473
//Evaluation of the overall status has to be defered to allow processing of undeclared variables before setting the ready state.
477
- State . SetModuleState ( component , ParserState . Ready , null , false ) ;
474
+ State . SetModuleState ( component , ParserState . Ready , null , false ) ;
478
475
}
479
476
catch ( Exception exception )
480
477
{
@@ -500,16 +497,16 @@ private void AddUndeclaredVariablesToDeclarations()
500
497
private void ParseAll ( object requestor , CancellationTokenSource token )
501
498
{
502
499
State . RefreshProjects ( _vbe ) ;
503
-
500
+
504
501
var components = State . Projects . SelectMany ( project => project . VBComponents ) . ToList ( ) ;
505
502
506
- var componentsRemoved = ClearStateCacheForRemovedComponents ( components ) ;
503
+ var componentsRemoved = ClearStateCashForRemovedComponents ( components ) ;
507
504
508
505
// invalidation cleanup should go into ParseAsync?
509
506
CleanUpComponentAttributes ( components ) ;
510
507
511
- var toParse = components . Where ( component => State . IsNewOrModified ( component ) ) . ToList ( ) ;
512
-
508
+ var toParse = components . Where ( component => State . IsNewOrModified ( component ) ) . ToList ( ) ;
509
+
513
510
if ( toParse . Count == 0 )
514
511
{
515
512
if ( componentsRemoved ) // trigger UI updates
@@ -525,10 +522,10 @@ private void ParseAll(object requestor, CancellationTokenSource token)
525
522
}
526
523
527
524
/// <summary>
528
- /// Clears state cache for removed components.
525
+ /// Clears state cach for removed components.
529
526
/// Returns whether components have been removed.
530
527
/// </summary>
531
- private bool ClearStateCacheForRemovedComponents ( List < IVBComponent > components )
528
+ private bool ClearStateCashForRemovedComponents ( List < IVBComponent > components )
532
529
{
533
530
var removedModuledecalrations = RemovedModuleDeclarations ( components ) ;
534
531
var componentRemoved = removedModuledecalrations . Any ( ) ;
@@ -591,7 +588,7 @@ private string GetReferenceProjectId(IReference reference, IReadOnlyList<IVBProj
591
588
Logger . Warn ( e ) ;
592
589
}
593
590
}
594
-
591
+
595
592
if ( project != null )
596
593
{
597
594
if ( string . IsNullOrEmpty ( project . ProjectId ) )
@@ -620,7 +617,7 @@ private void SyncComReferences(IReadOnlyList<IVBProject> projects)
620
617
var reference = references [ priority ] ;
621
618
if ( reference . IsBroken )
622
619
{
623
- continue ;
620
+ continue ;
624
621
}
625
622
626
623
// skip loading Rubberduck.tlb (GUID is defined in AssemblyInfo.cs)
@@ -642,7 +639,7 @@ private void SyncComReferences(IReadOnlyList<IVBProject> projects)
642
639
643
640
if ( map == null )
644
641
{
645
- map = new ReferencePriorityMap ( referencedProjectId ) { { projectId , priority } } ;
642
+ map = new ReferencePriorityMap ( referencedProjectId ) { { projectId , priority } } ;
646
643
_projectReferences . Add ( map ) ;
647
644
}
648
645
else
@@ -734,7 +731,7 @@ private void UnloadComReference(IReference reference, IReadOnlyList<IVBProject>
734
731
map = map != null ? null : item ;
735
732
}
736
733
}
737
-
734
+
738
735
if ( map == null || ! map . IsLoaded )
739
736
{
740
737
// we're removing a reference we weren't tracking? ...this shouldn't happen.
@@ -750,108 +747,10 @@ private void UnloadComReference(IReference reference, IReadOnlyList<IVBProject>
750
747
}
751
748
752
749
753
- private readonly ConcurrentDictionary < string , Declaration > _projectDeclarations = new ConcurrentDictionary < string , Declaration > ( ) ;
754
- private void ResolveDeclarations ( IVBComponent component , IParseTree tree )
755
- {
756
- if ( component == null ) { return ; }
757
-
758
- var qualifiedModuleName = new QualifiedModuleName ( component ) ;
759
-
760
- var stopwatch = Stopwatch . StartNew ( ) ;
761
- try
762
- {
763
- var project = component . Collection . Parent ;
764
- var projectQualifiedName = new QualifiedModuleName ( project ) ;
765
- Declaration projectDeclaration ;
766
- if ( ! _projectDeclarations . TryGetValue ( projectQualifiedName . ProjectId , out projectDeclaration ) )
767
- {
768
- projectDeclaration = CreateProjectDeclaration ( projectQualifiedName , project ) ;
769
- _projectDeclarations . AddOrUpdate ( projectQualifiedName . ProjectId , projectDeclaration , ( s , c ) => projectDeclaration ) ;
770
- State . AddDeclaration ( projectDeclaration ) ;
771
- }
772
- Logger . Debug ( "Creating declarations for module {0}." , qualifiedModuleName . Name ) ;
773
-
774
- var emitter = new Emitter ( _state ) ;
775
- string typeName = null ;
776
- if ( component . Type == ComponentType . Document )
777
- {
778
- //typeName = emitter.ExecuteWithResult<string>(project, emitter.GetTypeNameFunctionBody(component.Name), "GetTypeName");
779
- }
780
-
781
- var declarationsListener = new DeclarationSymbolsListener ( State , qualifiedModuleName , component . Type , State . GetModuleAnnotations ( component ) , State . GetModuleAttributes ( component ) , projectDeclaration , typeName ) ;
782
- ParseTreeWalker . Default . Walk ( declarationsListener , tree ) ;
783
- foreach ( var createdDeclaration in declarationsListener . CreatedDeclarations )
784
- {
785
- State . AddDeclaration ( createdDeclaration ) ;
786
- }
787
- }
788
- catch ( Exception exception )
789
- {
790
- Logger . Error ( exception , "Exception thrown acquiring declarations for '{0}' (thread {1})." , component . Name , Thread . CurrentThread . ManagedThreadId ) ;
791
- State . SetModuleState ( component , ParserState . ResolverError ) ;
792
- }
793
- stopwatch . Stop ( ) ;
794
- Logger . Debug ( "{0}ms to resolve declarations for component {1}" , stopwatch . ElapsedMilliseconds , component . Name ) ;
795
- }
796
-
797
- private Declaration CreateProjectDeclaration ( QualifiedModuleName projectQualifiedName , IVBProject project )
798
- {
799
- var qualifiedName = projectQualifiedName . QualifyMemberName ( project . Name ) ;
800
- var projectId = qualifiedName . QualifiedModuleName . ProjectId ;
801
- var projectDeclaration = new ProjectDeclaration ( qualifiedName , project . Name , false , project ) ;
802
-
803
- var references = new List < ReferencePriorityMap > ( ) ;
804
- foreach ( var item in _projectReferences )
805
- {
806
- if ( item . ContainsKey ( projectId ) )
807
- {
808
- references . Add ( item ) ;
809
- }
810
- }
811
-
812
- foreach ( var reference in references )
813
- {
814
- int priority = reference [ projectId ] ;
815
- projectDeclaration . AddProjectReference ( reference . ReferencedProjectId , priority ) ;
816
- }
817
- return projectDeclaration ;
818
- }
819
-
820
- private void ResolveReferences ( DeclarationFinder finder , IVBComponent component , IParseTree tree )
821
- {
822
- Debug . Assert ( State . GetModuleState ( component ) == ParserState . ResolvingReferences ) ;
823
-
824
- var qualifiedName = new QualifiedModuleName ( component ) ;
825
- Logger . Debug ( "Resolving identifier references in '{0}'... (thread {1})" , qualifiedName . Name , Thread . CurrentThread . ManagedThreadId ) ;
826
-
827
- var resolver = new IdentifierReferenceResolver ( qualifiedName , finder ) ;
828
- var listener = new IdentifierReferenceListener ( resolver ) ;
829
-
830
- if ( ! string . IsNullOrWhiteSpace ( tree . GetText ( ) . Trim ( ) ) )
831
- {
832
- var walker = new ParseTreeWalker ( ) ;
833
- try
834
- {
835
- var watch = Stopwatch . StartNew ( ) ;
836
- walker . Walk ( listener , tree ) ;
837
- watch . Stop ( ) ;
838
- Logger . Debug ( "Binding resolution done for component '{0}' in {1}ms (thread {2})" , component . Name ,
839
- watch . ElapsedMilliseconds , Thread . CurrentThread . ManagedThreadId ) ;
840
-
841
- State . SetModuleState ( component , ParserState . Ready ) ;
842
- }
843
- catch ( Exception exception )
844
- {
845
- Logger . Error ( exception , "Exception thrown resolving '{0}' (thread {1})." , component . Name , Thread . CurrentThread . ManagedThreadId ) ;
846
- State . SetModuleState ( component , ParserState . ResolverError ) ;
847
- }
848
- }
849
- }
850
-
851
750
public void Dispose ( )
852
751
{
853
752
State . ParseRequest -= ReparseRequested ;
854
753
Cancel ( false ) ;
855
754
}
856
755
}
857
- }
756
+ }
0 commit comments