Skip to content

Commit

Permalink
Merge pull request #52812 from CyrusNajmabadi/foregroundNotification
Browse files Browse the repository at this point in the history
Remove usages of ForegroundNotificationService in favor of JTF.
  • Loading branch information
CyrusNajmabadi authored Apr 22, 2021
2 parents 636c019 + eff66ef commit 6b664eb
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public async Task TestGetTagsOnBufferTagger()

var provider = new CopyPasteAndPrintingClassificationBufferTaggerProvider(
workspace.ExportProvider.GetExportedValue<IThreadingContext>(),
workspace.ExportProvider.GetExportedValue<IForegroundNotificationService>(),
workspace.ExportProvider.GetExportedValue<ClassificationTypeMap>(),
listenerProvider);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ private SnapshotSpan? CachedTaggedSpan

private void OnEventSourceChanged(object sender, TaggerEventArgs _)
{
_owner._notificationService.RegisterNotification(
OnEventSourceChanged_OnForeground,
_owner._asyncListener.BeginAsyncOperation("SemanticClassificationBufferTaggerProvider"),
_cancellationTokenSource.Token);
_owner.ThreadingContext.JoinableTaskFactory.RunAsync(async () =>
{
using var _ = _owner._asyncListener.BeginAsyncOperation("SemanticClassificationBufferTaggerProvider");
await _owner.ThreadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(_cancellationTokenSource.Token);
OnEventSourceChanged_OnForeground();
});
}

private void OnEventSourceChanged_OnForeground()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,16 @@ namespace Microsoft.CodeAnalysis.Editor.Implementation.Classification
internal partial class CopyPasteAndPrintingClassificationBufferTaggerProvider : ForegroundThreadAffinitizedObject, ITaggerProvider
{
private readonly IAsynchronousOperationListener _asyncListener;
private readonly IForegroundNotificationService _notificationService;
private readonly ClassificationTypeMap _typeMap;

[ImportingConstructor]
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
public CopyPasteAndPrintingClassificationBufferTaggerProvider(
IThreadingContext threadingContext,
IForegroundNotificationService notificationService,
ClassificationTypeMap typeMap,
IAsynchronousOperationListenerProvider listenerProvider)
: base(threadingContext)
{
_notificationService = notificationService;
_typeMap = typeMap;
_asyncListener = listenerProvider.GetListener(FeatureAttribute.Classification);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,17 @@ namespace Microsoft.VisualStudio.LanguageServices.Implementation
internal class VisualStudioInfoBarService : ForegroundThreadAffinitizedObject, IInfoBarService
{
private readonly SVsServiceProvider _serviceProvider;
private readonly IForegroundNotificationService _foregroundNotificationService;
private readonly IAsynchronousOperationListener _listener;

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public VisualStudioInfoBarService(
IThreadingContext threadingContext,
SVsServiceProvider serviceProvider,
IForegroundNotificationService foregroundNotificationService,
IAsynchronousOperationListenerProvider listenerProvider)
: base(threadingContext)
{
_serviceProvider = serviceProvider;
_foregroundNotificationService = foregroundNotificationService;
_listener = listenerProvider.GetListener(FeatureAttribute.InfoBar);
}

Expand All @@ -46,13 +43,13 @@ public void ShowInfoBar(string message, params InfoBarUI[] items)
ThisCanBeCalledOnAnyThread();

// We can be called from any thread since errors can occur anywhere, however we can only construct and InfoBar from the UI thread.
_foregroundNotificationService.RegisterNotification(() =>
this.ThreadingContext.JoinableTaskFactory.RunAsync(async () =>
{
using var _ = _listener.BeginAsyncOperation(nameof(ShowInfoBar));
await this.ThreadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(ThreadingContext.DisposalToken);
if (TryGetInfoBarData(out var infoBarHost))
{
CreateInfoBar(infoBarHost, message, items);
}
}, _listener.BeginAsyncOperation(nameof(ShowInfoBar)), ThreadingContext.DisposalToken);
});
}

private bool TryGetInfoBarData(out IVsInfoBarHost infoBarHost)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Shared.TestHooks;

namespace Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem
{
Expand Down Expand Up @@ -177,8 +179,12 @@ private void IncludeUpdated(object sender, string fileChanged)
// waiting for the foreground thread to release its lock on the file change service.
// To avoid this, just queue up a Task to do the work on the foreground thread later, after
// the lock on the file change service has been released.
_ruleSetManager._foregroundNotificationService.RegisterNotification(
() => IncludeUpdateCore(), _ruleSetManager._listener.BeginAsyncOperation("IncludeUpdated"), _disposalToken);
_ruleSetManager._threadingContext.JoinableTaskFactory.RunAsync(async () =>
{
using var _ = _ruleSetManager._listener.BeginAsyncOperation("IncludeUpdated");
await _ruleSetManager._threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(alwaysYield: true, _disposalToken);
IncludeUpdateCore();
});
}

private void IncludeUpdateCore()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#nullable disable

using Microsoft.CodeAnalysis.Editor;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Roslyn.Utilities;
Expand All @@ -13,17 +13,19 @@ namespace Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem
{
internal sealed partial class VisualStudioRuleSetManager : IWorkspaceService
{
private readonly IThreadingContext _threadingContext;
private readonly FileChangeWatcher _fileChangeWatcher;
private readonly IForegroundNotificationService _foregroundNotificationService;
private readonly IAsynchronousOperationListener _listener;

private readonly ReferenceCountedDisposableCache<string, RuleSetFile> _ruleSetFileMap = new();

public VisualStudioRuleSetManager(
FileChangeWatcher fileChangeWatcher, IForegroundNotificationService foregroundNotificationService, IAsynchronousOperationListener listener)
IThreadingContext threadingContext,
FileChangeWatcher fileChangeWatcher,
IAsynchronousOperationListener listener)
{
_threadingContext = threadingContext;
_fileChangeWatcher = fileChangeWatcher;
_foregroundNotificationService = foregroundNotificationService;
_listener = listener;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,35 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
using System.Collections.Generic;
using System.Composition;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Editor;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

namespace Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem
{
[ExportWorkspaceServiceFactory(typeof(VisualStudioRuleSetManager), ServiceLayer.Host), Shared]
internal sealed class VisualStudioRuleSetManagerFactory : IWorkspaceServiceFactory
{
private readonly IThreadingContext _threadingContext;
private readonly FileChangeWatcherProvider _fileChangeWatcherProvider;
private readonly IForegroundNotificationService _foregroundNotificationService;
private readonly IAsynchronousOperationListener _listener;

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public VisualStudioRuleSetManagerFactory(
IThreadingContext threadingContext,
FileChangeWatcherProvider fileChangeWatcherProvider,
IForegroundNotificationService foregroundNotificationService,
IAsynchronousOperationListenerProvider listenerProvider)
{
_threadingContext = threadingContext;
_fileChangeWatcherProvider = fileChangeWatcherProvider;
_foregroundNotificationService = foregroundNotificationService;
_listener = listenerProvider.GetListener(FeatureAttribute.RuleSetEditor);
}

public IWorkspaceService CreateService(HostWorkspaceServices workspaceServices)
=> new VisualStudioRuleSetManager(_fileChangeWatcherProvider.Watcher, _foregroundNotificationService, _listener);
=> new VisualStudioRuleSetManager(_threadingContext, _fileChangeWatcherProvider.Watcher, _listener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Imports System.IO
Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.Editor
Imports Microsoft.CodeAnalysis.Editor.Shared.Utilities
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.Shared.TestHooks
Imports Microsoft.CodeAnalysis.Test.Utilities
Expand Down Expand Up @@ -48,7 +49,7 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.ProjectSystemShim

Dim fileChangeService = New MockVsFileChangeEx
Dim fileChangeWatcher = New FileChangeWatcher(Task.FromResult(Of IVsAsyncFileChangeEx)(fileChangeService))
Dim ruleSetManager = New VisualStudioRuleSetManager(fileChangeWatcher, workspace.ExportProvider.GetExportedValue(Of IForegroundNotificationService)(), AsynchronousOperationListenerProvider.NullListener)
Dim ruleSetManager = New VisualStudioRuleSetManager(workspace.ExportProvider.GetExportedValue(Of IThreadingContext), fileChangeWatcher, AsynchronousOperationListenerProvider.NullListener)
Using visualStudioRuleSet = ruleSetManager.GetOrCreateRuleSet(ruleSetPath)

' Signing up for file change notifications is lazy, so read the rule set to force it.
Expand Down Expand Up @@ -92,7 +93,7 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.ProjectSystemShim
Using workspace = New TestWorkspace()
Dim fileChangeService = New MockVsFileChangeEx
Dim fileChangeWatcher = New FileChangeWatcher(Task.FromResult(Of IVsAsyncFileChangeEx)(fileChangeService))
Dim ruleSetManager = New VisualStudioRuleSetManager(fileChangeWatcher, workspace.ExportProvider.GetExportedValue(Of IForegroundNotificationService)(), AsynchronousOperationListenerProvider.NullListener)
Dim ruleSetManager = New VisualStudioRuleSetManager(workspace.ExportProvider.GetExportedValue(Of IThreadingContext), fileChangeWatcher, AsynchronousOperationListenerProvider.NullListener)
Using visualStudioRuleSet = ruleSetManager.GetOrCreateRuleSet(ruleSetPath)

' Signing up for file change notifications is lazy, so read the rule set to force it.
Expand Down Expand Up @@ -140,7 +141,7 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.ProjectSystemShim
Dim listenerProvider = workspace.ExportProvider.GetExportedValue(Of IAsynchronousOperationListenerProvider)
Dim listener = listenerProvider.GetListener("test")

Dim ruleSetManager = New VisualStudioRuleSetManager(fileChangeWatcher, workspace.ExportProvider.GetExportedValue(Of IForegroundNotificationService)(), listener)
Dim ruleSetManager = New VisualStudioRuleSetManager(workspace.ExportProvider.GetExportedValue(Of IThreadingContext), fileChangeWatcher, listener)
Using ruleSet1 = ruleSetManager.GetOrCreateRuleSet(ruleSetPath)
Dim handlerCalled As Boolean = False
AddHandler ruleSet1.Target.Value.UpdatedOnDisk, Sub() handlerCalled = True
Expand Down Expand Up @@ -181,7 +182,7 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.ProjectSystemShim
Dim listenerProvider = workspace.ExportProvider.GetExportedValue(Of IAsynchronousOperationListenerProvider)
Dim listener = listenerProvider.GetListener("test")

Dim ruleSetManager = New VisualStudioRuleSetManager(fileChangeWatcher, workspace.ExportProvider.GetExportedValue(Of IForegroundNotificationService)(), listener)
Dim ruleSetManager = New VisualStudioRuleSetManager(workspace.ExportProvider.GetExportedValue(Of IThreadingContext), fileChangeWatcher, listener)
Using ruleSet1 = ruleSetManager.GetOrCreateRuleSet(ruleSetPath)

' Signing up for file change notifications is lazy, so read the rule set to force it.
Expand Down Expand Up @@ -226,7 +227,7 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.ProjectSystemShim
Using workspace = New TestWorkspace()
Dim fileChangeService = New MockVsFileChangeEx
Dim fileChangeWatcher = New FileChangeWatcher(Task.FromResult(Of IVsAsyncFileChangeEx)(fileChangeService))
Dim ruleSetManager = New VisualStudioRuleSetManager(fileChangeWatcher, workspace.ExportProvider.GetExportedValue(Of IForegroundNotificationService)(), AsynchronousOperationListenerProvider.NullListener)
Dim ruleSetManager = New VisualStudioRuleSetManager(workspace.ExportProvider.GetExportedValue(Of IThreadingContext), fileChangeWatcher, AsynchronousOperationListenerProvider.NullListener)
Using ruleSet1 = ruleSetManager.GetOrCreateRuleSet(ruleSetPath)

' Signing up for file change notifications is lazy, so read the rule set to force it.
Expand Down Expand Up @@ -264,7 +265,7 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.ProjectSystemShim
Using workspace = New TestWorkspace()
Dim fileChangeService = New MockVsFileChangeEx
Dim fileChangeWatcher = New FileChangeWatcher(Task.FromResult(Of IVsAsyncFileChangeEx)(fileChangeService))
Dim ruleSetManager = New VisualStudioRuleSetManager(fileChangeWatcher, workspace.ExportProvider.GetExportedValue(Of IForegroundNotificationService)(), AsynchronousOperationListenerProvider.NullListener)
Dim ruleSetManager = New VisualStudioRuleSetManager(workspace.ExportProvider.GetExportedValue(Of IThreadingContext), fileChangeWatcher, AsynchronousOperationListenerProvider.NullListener)
Using ruleSet = ruleSetManager.GetOrCreateRuleSet(ruleSetPath)

Dim generalDiagnosticOption = ruleSet.Target.Value.GetGeneralDiagnosticOption()
Expand Down

0 comments on commit 6b664eb

Please sign in to comment.