Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,20 @@ internal sealed class PdbMatchingSourceTextProvider() : IEventListener, IPdbMatc
private bool _isActive;
private int _baselineSolutionVersion;
private readonly Dictionary<string, (DocumentState state, int solutionVersion)> _documentsWithChangedLoaderByPath = [];
private WorkspaceEventRegistration? _workspaceChangedDisposer;

public void StartListening(Workspace workspace)
=> workspace.WorkspaceChanged += WorkspaceChanged;
{
_workspaceChangedDisposer = workspace.RegisterWorkspaceChangedHandler(WorkspaceChanged);
}

public void StopListening(Workspace workspace)
=> workspace.WorkspaceChanged -= WorkspaceChanged;
{
_workspaceChangedDisposer?.Dispose();
_workspaceChangedDisposer = null;
}

private void WorkspaceChanged(object? sender, WorkspaceChangeEventArgs e)
private void WorkspaceChanged(WorkspaceChangeEventArgs e)
{
if (!_isActive)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public SettingsAggregator(
IAsynchronousOperationListener listener)
{
_workspace = workspace;
_workspace.WorkspaceChanged += UpdateProviders;
_ = workspace.RegisterWorkspaceChangedHandler(UpdateProviders);

var currentSolution = _workspace.CurrentSolution.SolutionState;
UpdateProviders(currentSolution);
Expand All @@ -48,7 +48,7 @@ public SettingsAggregator(
threadingContext.DisposalToken);
}

private void UpdateProviders(object? sender, WorkspaceChangeEventArgs e)
private void UpdateProviders(WorkspaceChangeEventArgs e)
{
switch (e.Kind)
{
Expand Down
11 changes: 8 additions & 3 deletions src/EditorFeatures/Core/InlineRename/InlineRenameSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ internal sealed partial class InlineRenameSession : IInlineRenameSession, IFeatu
private bool _isApplyingEdit;
private string _replacementText;
private readonly Dictionary<ITextBuffer, OpenTextBufferManager> _openTextBuffers = [];
private WorkspaceEventRegistration _workspaceChangedDisposer;

/// <summary>
/// The original <see cref="Document"/> where rename was triggered
Expand Down Expand Up @@ -161,7 +162,9 @@ public InlineRenameSession(
_inlineRenameSessionDurationLogBlock = Logger.LogBlock(FunctionId.Rename_InlineSession, CancellationToken.None);

Workspace = workspace;
Workspace.WorkspaceChanged += OnWorkspaceChanged;

// Requires the main thread due to OnWorkspaceChanged calling the Cancel method
_workspaceChangedDisposer = workspace.RegisterWorkspaceChangedHandler(OnWorkspaceChanged, WorkspaceEventOptions.RequiresMainThreadOptions);

_textBufferFactoryService = textBufferFactoryService;
_textBufferCloneService = textBufferCloneService;
Expand Down Expand Up @@ -383,7 +386,7 @@ private void VerifyNotDismissed()
}
}

private void OnWorkspaceChanged(object sender, WorkspaceChangeEventArgs args)
private void OnWorkspaceChanged(WorkspaceChangeEventArgs args)
{
if (args.Kind != WorkspaceChangeKind.DocumentChanged)
{
Expand Down Expand Up @@ -701,7 +704,9 @@ private void DismissUIAndRollbackEditsAndEndRenameSession_MustBeCalledOnUIThread

void DismissUIAndRollbackEdits()
{
Workspace.WorkspaceChanged -= OnWorkspaceChanged;
_workspaceChangedDisposer?.Dispose();
_workspaceChangedDisposer = null;

_textBufferAssociatedViewService.SubjectBuffersConnected -= OnSubjectBuffersConnected;

// Reenable completion now that the inline rename session is done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ internal partial class TaggerEventSources
{
private sealed class ParseOptionChangedEventSource(ITextBuffer subjectBuffer) : AbstractWorkspaceTrackingTaggerEventSource(subjectBuffer)
{
private WorkspaceEventRegistration? _workspaceChangedDisposer;

protected override void ConnectToWorkspace(Workspace workspace)
=> workspace.WorkspaceChanged += OnWorkspaceChanged;
{
_workspaceChangedDisposer = workspace.RegisterWorkspaceChangedHandler(OnWorkspaceChanged);
}

protected override void DisconnectFromWorkspace(Workspace workspace)
=> workspace.WorkspaceChanged -= OnWorkspaceChanged;
{
_workspaceChangedDisposer?.Dispose();
_workspaceChangedDisposer = null;
}

private void OnWorkspaceChanged(object? sender, WorkspaceChangeEventArgs e)
private void OnWorkspaceChanged(WorkspaceChangeEventArgs e)
{
if (e.Kind == WorkspaceChangeKind.ProjectChanged)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal partial class TaggerEventSources
private sealed class WorkspaceChangedEventSource : AbstractWorkspaceTrackingTaggerEventSource
{
private readonly AsyncBatchingWorkQueue _asyncDelay;
private WorkspaceEventRegistration? _workspaceChangedDisposer;

public WorkspaceChangedEventSource(
ITextBuffer subjectBuffer,
Expand All @@ -36,17 +37,19 @@ public WorkspaceChangedEventSource(

protected override void ConnectToWorkspace(Workspace workspace)
{
workspace.WorkspaceChanged += OnWorkspaceChanged;
_workspaceChangedDisposer = workspace.RegisterWorkspaceChangedHandler(OnWorkspaceChanged);
this.RaiseChanged();
}

protected override void DisconnectFromWorkspace(Workspace workspace)
{
workspace.WorkspaceChanged -= OnWorkspaceChanged;
_workspaceChangedDisposer?.Dispose();
_workspaceChangedDisposer = null;

this.RaiseChanged();
}

private void OnWorkspaceChanged(object? sender, WorkspaceChangeEventArgs eventArgs)
private void OnWorkspaceChanged(WorkspaceChangeEventArgs eventArgs)
=> _asyncDelay.AddWork();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ internal sealed partial class HostLegacySolutionEventsWorkspaceEventListener : I
private readonly IThreadingContext _threadingContext;
private readonly AsyncBatchingWorkQueue<WorkspaceChangeEventArgs> _eventQueue;

private WorkspaceEventRegistration? _workspaceChangedDisposer;

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public HostLegacySolutionEventsWorkspaceEventListener(
Expand All @@ -53,16 +55,19 @@ public void StartListening(Workspace workspace)
// We only support this option to disable crawling in internal speedometer and ddrit perf runs to lower noise.
// It is not exposed to the user.
if (_globalOptions.GetOption(SolutionCrawlerRegistrationService.EnableSolutionCrawler))
workspace.WorkspaceChanged += OnWorkspaceChanged;
_workspaceChangedDisposer = workspace.RegisterWorkspaceChangedHandler(OnWorkspaceChanged);
}

public void StopListening(Workspace workspace)
{
if (_globalOptions.GetOption(SolutionCrawlerRegistrationService.EnableSolutionCrawler))
workspace.WorkspaceChanged -= OnWorkspaceChanged;
{
_workspaceChangedDisposer?.Dispose();
_workspaceChangedDisposer = null;
}
}

private void OnWorkspaceChanged(object? sender, WorkspaceChangeEventArgs e)
private void OnWorkspaceChanged(WorkspaceChangeEventArgs e)
{
// Legacy workspace events exist solely to let unit testing continue to work using their own fork of solution
// crawler. As such, they only need events for the project types they care about. Specifically, that is only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.Threading;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Diagnostics;
Expand Down Expand Up @@ -56,10 +54,12 @@ public CodeAnalysisDiagnosticAnalyzerService(
_diagnosticAnalyzerService = diagnosticAnalyzerService;
_workspace = workspace;

_workspace.WorkspaceChanged += OnWorkspaceChanged;
// Main thread as OnWorkspaceChanged's call to IDiagnosticAnalyzerService.RequestDiagnosticRefresh isn't clear on
// threading requirements
_ = workspace.RegisterWorkspaceChangedHandler(OnWorkspaceChanged, WorkspaceEventOptions.RequiresMainThreadOptions);
}

private void OnWorkspaceChanged(object? sender, WorkspaceChangeEventArgs e)
private void OnWorkspaceChanged(WorkspaceChangeEventArgs e)
{
switch (e.Kind)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public IWorkspaceService CreateService(HostWorkspaceServices workspaceServices)

public CompileTimeSolutionProvider(Workspace workspace)
{
workspace.WorkspaceChanged += (s, e) =>
_ = workspace.RegisterWorkspaceChangedHandler((e) =>
{
if (e.Kind is WorkspaceChangeKind.SolutionCleared or WorkspaceChangeKind.SolutionRemoved)
{
Expand All @@ -79,7 +79,7 @@ public CompileTimeSolutionProvider(Workspace workspace)
_lastCompileTimeSolution = null;
}
}
};
});
}

private static bool IsRazorAnalyzerConfig(TextDocumentState documentState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ internal sealed class VisualStudioDesignerAttributeService :
// deliver them to VS in batches to prevent flooding the UI thread.
private readonly AsyncBatchingWorkQueue<DesignerAttributeData> _projectSystemNotificationQueue;

private WorkspaceEventRegistration? _workspaceChangedDisposer;

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public VisualStudioDesignerAttributeService(
Expand Down Expand Up @@ -92,7 +94,7 @@ void IEventListener.StartListening(Workspace workspace)
if (workspace != _workspace)
return;

_workspace.WorkspaceChanged += OnWorkspaceChanged;
_workspaceChangedDisposer = workspace.RegisterWorkspaceChangedHandler(OnWorkspaceChanged);
_workQueue.AddWork(cancelExistingWork: true);
}

Expand All @@ -101,10 +103,11 @@ void IEventListener.StopListening(Workspace workspace)
if (workspace != _workspace)
return;

_workspace.WorkspaceChanged -= OnWorkspaceChanged;
_workspaceChangedDisposer?.Dispose();
_workspaceChangedDisposer = null;
}

private void OnWorkspaceChanged(object sender, WorkspaceChangeEventArgs e)
private void OnWorkspaceChanged(WorkspaceChangeEventArgs e)
{
_workQueue.AddWork(cancelExistingWork: true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ internal abstract partial class AbstractObjectBrowserLibraryManager : AbstractLi
private ObjectListItem _activeListItem;
private AbstractListItemFactory _listItemFactory;
private readonly object _classMemberGate = new();
private WorkspaceEventRegistration _workspaceChangedDisposer;

protected AbstractObjectBrowserLibraryManager(
string languageName,
Expand All @@ -53,7 +54,7 @@ protected AbstractObjectBrowserLibraryManager(
_languageName = languageName;

Workspace = workspace;
Workspace.WorkspaceChanged += OnWorkspaceChanged;
_workspaceChangedDisposer = Workspace.RegisterWorkspaceChangedHandler(OnWorkspaceChanged);

_libraryService = new Lazy<ILibraryService>(() => Workspace.Services.GetLanguageServices(_languageName).GetService<ILibraryService>());
}
Expand All @@ -73,9 +74,12 @@ private AbstractListItemFactory GetListItemFactory()
}

public void Dispose()
=> this.Workspace.WorkspaceChanged -= OnWorkspaceChanged;
{
_workspaceChangedDisposer?.Dispose();
_workspaceChangedDisposer = null;
}

private void OnWorkspaceChanged(object sender, WorkspaceChangeEventArgs e)
private void OnWorkspaceChanged(WorkspaceChangeEventArgs e)
{
switch (e.Kind)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ protected override async Task EnableServiceAsync(CancellationToken cancellationT
var packageSourceProvider = await GetPackageSourceProviderAsync().ConfigureAwait(false);

// Start listening to additional events workspace changes.
Workspace.WorkspaceChanged += OnWorkspaceChanged;
_ = Workspace.RegisterWorkspaceChangedHandler(OnWorkspaceChanged);
packageSourceProvider.SourcesChanged += OnSourceProviderSourcesChanged;

// Kick off an initial set of work that will analyze the entire solution.
Expand Down Expand Up @@ -421,7 +421,7 @@ await UpdateStatusBarAsync(
}
}

private void OnWorkspaceChanged(object sender, WorkspaceChangeEventArgs e)
private void OnWorkspaceChanged(WorkspaceChangeEventArgs e)
{
// ThisCanBeCalledOnAnyThread();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal GraphQueryManager(
// indicating a change happened. And when UpdateExistingQueriesAsync fires, it will just see that there are
// no live queries and immediately return. So it's just simple to do things this way instead of trying to
// have state management where we try to decide if we should listen or not.
_workspace.WorkspaceChanged += (_, _) => _updateQueue.AddWork();
_ = _workspace.RegisterWorkspaceChangedHandler((_) => _updateQueue.AddWork());
}

public async Task AddQueriesAsync(IGraphContext context, ImmutableArray<IGraphQuery> graphQueries, CancellationToken disposalToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.StackTraceExplorer;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.StackTraceExplorer;
using Microsoft.VisualStudio.LanguageServices.Utilities;
using Microsoft.VisualStudio.Text.Classification;

Expand Down Expand Up @@ -53,7 +53,9 @@ public StackTraceExplorerViewModel(IThreadingContext threadingContext, Workspace
_threadingContext = threadingContext;
_workspace = workspace;

workspace.WorkspaceChanged += Workspace_WorkspaceChanged;
// Main thread dependency as Workspace_WorkspaceChanged modifies an ObservableCollection
_ = workspace.RegisterWorkspaceChangedHandler(Workspace_WorkspaceChanged, WorkspaceEventOptions.RequiresMainThreadOptions);

_classificationTypeMap = classificationTypeMap;
_formatMap = formatMap;

Expand Down Expand Up @@ -104,7 +106,7 @@ private void CallstackLines_CollectionChanged(object sender, System.Collections.
NotifyPropertyChanged(nameof(IsInstructionTextVisible));
}

private void Workspace_WorkspaceChanged(object sender, WorkspaceChangeEventArgs e)
private void Workspace_WorkspaceChanged(WorkspaceChangeEventArgs e)
{
if (e.Kind == WorkspaceChangeKind.SolutionChanged)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using System.Diagnostics.CodeAnalysis;
using Microsoft.VisualStudio.Shell;

namespace Microsoft.VisualStudio.LanguageServices.ValueTracking;

Expand Down Expand Up @@ -65,10 +65,10 @@ public void Initialize(ValueTrackingTreeViewModel viewModel, Workspace workspace
_workspace = workspace;
_threadingContext = threadingContext;

_workspace.WorkspaceChanged += OnWorkspaceChanged;
_ = _workspace.RegisterWorkspaceChangedHandler(OnWorkspaceChanged);
}

private void OnWorkspaceChanged(object sender, WorkspaceChangeEventArgs e)
private void OnWorkspaceChanged(WorkspaceChangeEventArgs e)
{
Contract.ThrowIfFalse(Initialized);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ private sealed class OpenSourceGeneratedFile : IDisposable
private VisualStudioInfoBar.InfoBarMessage? _currentInfoBarMessage;

private InfoBarInfo? _infoToShow = null;
private WorkspaceEventRegistration? _workspaceChangedDisposer;

public OpenSourceGeneratedFile(SourceGeneratedFileManager fileManager, ITextBuffer textBuffer, SourceGeneratedDocumentIdentity documentIdentity)
{
Expand All @@ -284,7 +285,7 @@ public OpenSourceGeneratedFile(SourceGeneratedFileManager fileManager, ITextBuff
readOnlyRegionEdit.Apply();
}

this.Workspace.WorkspaceChanged += OnWorkspaceChanged;
_workspaceChangedDisposer = this.Workspace.RegisterWorkspaceChangedHandler(OnWorkspaceChanged);

_batchingWorkQueue = new AsyncBatchingWorkQueue(
TimeSpan.FromSeconds(1),
Expand All @@ -311,7 +312,8 @@ public void Dispose()
{
_fileManager._threadingContext.ThrowIfNotOnUIThread();

this.Workspace.WorkspaceChanged -= OnWorkspaceChanged;
_workspaceChangedDisposer?.Dispose();
_workspaceChangedDisposer = null;

// Disconnect the buffer from the workspace before making it eligible for edits
DisconnectFromWorkspaceIfOpen();
Expand Down Expand Up @@ -422,7 +424,7 @@ public async ValueTask RefreshFileAsync(CancellationToken cancellationToken)
await EnsureWindowFrameInfoBarUpdatedAsync(cancellationToken).ConfigureAwait(true);
}

private void OnWorkspaceChanged(object sender, WorkspaceChangeEventArgs e)
private void OnWorkspaceChanged(WorkspaceChangeEventArgs e)
{
var projectId = _documentIdentity.DocumentId.ProjectId;

Expand Down
Loading
Loading