Skip to content

Commit

Permalink
Fix active file analysis in solution crawler
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Apr 21, 2021
1 parent 5d9af96 commit a1ad041
Showing 1 changed file with 8 additions and 29 deletions.
37 changes: 8 additions & 29 deletions src/Features/Core/Portable/SolutionCrawler/WorkCoordinator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ internal sealed partial class WorkCoordinator
private readonly IncrementalAnalyzerProcessor _documentAndProjectWorkerProcessor;
private readonly SemanticChangeProcessor _semanticChangeProcessor;

private Document? _lastActiveDocument;

public WorkCoordinator(
IAsynchronousOperationListener listener,
IEnumerable<Lazy<IIncrementalAnalyzerProvider, IncrementalAnalyzerProviderMetadata>> analyzerProviders,
Expand Down Expand Up @@ -85,7 +83,6 @@ public WorkCoordinator(
_optionService.OptionChanged += OnOptionChanged;

// subscribe to active document changed event for active file background analysis scope.
_lastActiveDocument = _documentTrackingService.GetActiveDocument(_registration.GetSolutionToAnalyze());
_documentTrackingService.ActiveDocumentChanged += OnActiveDocumentChanged;
}

Expand Down Expand Up @@ -214,33 +211,15 @@ public void Reanalyze(IIncrementalAnalyzer analyzer, ReanalyzeScope scope, bool
private void OnActiveDocumentChanged(object? sender, DocumentId activeDocumentId)
{
var solution = _registration.GetSolutionToAnalyze();
if (solution.GetProject(activeDocumentId.ProjectId) is not { } activeProject)
return;

// Check if we are only performing backgroung analysis for active file.
if (activeDocumentId != null)
{
// Change to active document needs to trigger following events in active file analysis scope:
// 1. Request analysis for newly active file, similar to a newly opened file.
// 2. Clear analysis data for prior active file, similar to a closed file.
// Note that if 'activeDocumentId' is null, i.e. user navigated to a non-source file,
// we are treating it as a no-op here.
// As soon as user switches to a source document, we will perform the appropriate analysis callbacks
// on the next active document changed event.
var activeDocument = solution.GetDocument(activeDocumentId);
if (activeDocument != null &&
SolutionCrawlerOptions.GetBackgroundAnalysisScope(activeDocument.Project) == BackgroundAnalysisScope.ActiveFile)
{
lock (_gate)
{
if (_lastActiveDocument != null)
{
EnqueueEvent(_lastActiveDocument.Project.Solution, _lastActiveDocument.Id, InvocationReasons.DocumentClosed, "OnDocumentClosed");
}

_lastActiveDocument = activeDocument;
}

EnqueueEvent(activeDocument.Project.Solution, activeDocument.Id, InvocationReasons.DocumentOpened, "OnDocumentOpened");
}
var analysisScope = SolutionCrawlerOptions.GetBackgroundAnalysisScope(activeProject);
if (analysisScope == BackgroundAnalysisScope.ActiveFile)
{
// When the active document changes and we are only analyzing the active file, trigger a document
// changed event to reanalyze the newly-active file.
EnqueueEvent(solution, activeDocumentId, InvocationReasons.DocumentChanged, nameof(OnActiveDocumentChanged));
}
}

Expand Down

0 comments on commit a1ad041

Please sign in to comment.