Skip to content

Commit

Permalink
Merge pull request #60526 from CyrusNajmabadi/removeFTAO
Browse files Browse the repository at this point in the history
Remove ForegroundThreadAffinitizedObject usage
  • Loading branch information
CyrusNajmabadi authored Apr 2, 2022
2 parents fc1e0ed + 27d9a8d commit ce61483
Show file tree
Hide file tree
Showing 76 changed files with 439 additions and 402 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
using System;
using System.Collections.Generic;
using System.Composition;
using Microsoft.CodeAnalysis.BraceCompletion;
using Microsoft.CodeAnalysis.AutomaticCompletion;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.BraceCompletion;
using Microsoft.CodeAnalysis.Host.Mef;

namespace Microsoft.CodeAnalysis.Editor.CSharp.AutomaticCompletion
Expand All @@ -18,9 +17,8 @@ internal class CSharpBraceCompletionServiceFactory : AbstractBraceCompletionServ
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpBraceCompletionServiceFactory(
[ImportMany(LanguageNames.CSharp)] IEnumerable<IBraceCompletionService> braceCompletionServices,
IThreadingContext threadingContext)
: base(braceCompletionServices, threadingContext)
[ImportMany(LanguageNames.CSharp)] IEnumerable<IBraceCompletionService> braceCompletionServices)
: base(braceCompletionServices)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.EventHookup
[ContentType(ContentTypeNames.CSharpContentType)]
[Name(PredefinedCommandHandlerNames.EventHookup)]
[Order(Before = PredefinedCommandHandlerNames.AutomaticCompletion)]
internal partial class EventHookupCommandHandler : ForegroundThreadAffinitizedObject
internal partial class EventHookupCommandHandler
{
private readonly IThreadingContext _threadingContext;
private readonly IInlineRenameService _inlineRenameService;
private readonly IAsynchronousOperationListener _asyncListener;
private readonly IGlobalOptionService _globalOptions;
Expand All @@ -57,8 +58,8 @@ public EventHookupCommandHandler(
EventHookupSessionManager eventHookupSessionManager,
IGlobalOptionService globalOptions,
IAsynchronousOperationListenerProvider listenerProvider)
: base(threadingContext)
{
_threadingContext = threadingContext;
_inlineRenameService = inlineRenameService;
_asyncListener = listenerProvider.GetListener(FeatureAttribute.EventHookup);
_globalOptions = globalOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

#nullable disable

using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.VisualStudio.Commanding;
using Microsoft.VisualStudio.Text.Editor.Commanding.Commands;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Editor.CSharp.EventHookup
{
Expand All @@ -16,14 +18,14 @@ internal partial class EventHookupCommandHandler :

public bool ExecuteCommand(EscapeKeyCommandArgs args, CommandExecutionContext context)
{
AssertIsForeground();
_threadingContext.ThrowIfNotOnUIThread();
EventHookupSessionManager.CancelAndDismissExistingSessions();
return false;
}

public CommandState GetCommandState(EscapeKeyCommandArgs args)
{
AssertIsForeground();
_threadingContext.ThrowIfNotOnUIThread();
return CommandState.Unspecified;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal partial class EventHookupCommandHandler : IChainedCommandHandler<TabKey
{
public void ExecuteCommand(TabKeyCommandArgs args, Action nextHandler, CommandExecutionContext context)
{
AssertIsForeground();
_threadingContext.ThrowIfNotOnUIThread();
if (!_globalOptions.GetOption(InternalFeatureOnOffOptions.EventHookup))
{
nextHandler();
Expand All @@ -55,7 +55,7 @@ public void ExecuteCommand(TabKeyCommandArgs args, Action nextHandler, CommandEx

public CommandState GetCommandState(TabKeyCommandArgs args, Func<CommandState> nextHandler)
{
AssertIsForeground();
_threadingContext.ThrowIfNotOnUIThread();
if (EventHookupSessionManager.CurrentSession != null)
{
return CommandState.Available;
Expand All @@ -68,7 +68,7 @@ public CommandState GetCommandState(TabKeyCommandArgs args, Func<CommandState> n

private void HandleTabWorker(ITextView textView, ITextBuffer subjectBuffer, Action nextHandler, CancellationToken cancellationToken)
{
AssertIsForeground();
_threadingContext.ThrowIfNotOnUIThread();

// For test purposes only!
if (EventHookupSessionManager.CurrentSession.TESTSessionHookupMutex != null)
Expand Down Expand Up @@ -107,7 +107,7 @@ private void HandleTabWorker(ITextView textView, ITextBuffer subjectBuffer, Acti

private void GenerateAndAddEventHandler(ITextView textView, ITextBuffer subjectBuffer, string eventHandlerMethodName, Action nextHandler, CancellationToken cancellationToken)
{
AssertIsForeground();
_threadingContext.ThrowIfNotOnUIThread();

using (Logger.LogBlock(FunctionId.EventHookup_Generate_Handler, cancellationToken))
{
Expand Down Expand Up @@ -151,7 +151,7 @@ private Solution CreateSolutionWithEventHandler(
out int plusEqualTokenEndPosition,
CancellationToken cancellationToken)
{
AssertIsForeground();
_threadingContext.ThrowIfNotOnUIThread();

// Mark the += token with an annotation so we can find it after formatting
var plusEqualsTokenAnnotation = new SyntaxAnnotation();
Expand Down Expand Up @@ -289,7 +289,7 @@ private static IMethodSymbol GetMethodSymbol(

private void BeginInlineRename(ITextView textView, int plusEqualTokenEndPosition, CancellationToken cancellationToken)
{
AssertIsForeground();
_threadingContext.ThrowIfNotOnUIThread();

if (_inlineRenameService.ActiveSession == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Editor.Commanding.Commands;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Editor.CSharp.EventHookup
{
internal partial class EventHookupCommandHandler : IChainedCommandHandler<TypeCharCommandArgs>
{
public void ExecuteCommand(TypeCharCommandArgs args, Action nextHandler, CommandExecutionContext context)
{
AssertIsForeground();
_threadingContext.ThrowIfNotOnUIThread();
nextHandler();

if (!_globalOptions.GetOption(InternalFeatureOnOffOptions.EventHookup))
Expand Down Expand Up @@ -58,7 +59,7 @@ public void ExecuteCommand(TypeCharCommandArgs args, Action nextHandler, Command

private bool IsTextualPlusEquals(ITextView textView, ITextBuffer subjectBuffer)
{
AssertIsForeground();
_threadingContext.ThrowIfNotOnUIThread();

var caretPoint = textView.GetCaretPoint(subjectBuffer);
if (!caretPoint.HasValue)
Expand All @@ -72,7 +73,7 @@ private bool IsTextualPlusEquals(ITextView textView, ITextBuffer subjectBuffer)

public CommandState GetCommandState(TypeCharCommandArgs args, Func<CommandState> nextHandler)
{
AssertIsForeground();
_threadingContext.ThrowIfNotOnUIThread();
return nextHandler();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Adornments;
using Microsoft.VisualStudio.Text.Editor;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Editor.CSharp.EventHookup
{
[Export]
internal sealed partial class EventHookupSessionManager : ForegroundThreadAffinitizedObject
internal sealed partial class EventHookupSessionManager
{
public readonly IThreadingContext ThreadingContext;

private readonly IToolTipService _toolTipService;
private IToolTipPresenter _toolTipPresenter;

Expand All @@ -33,14 +36,14 @@ internal sealed partial class EventHookupSessionManager : ForegroundThreadAffini
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public EventHookupSessionManager(IThreadingContext threadingContext, IToolTipService toolTipService)
: base(threadingContext)
{
ThreadingContext = threadingContext;
_toolTipService = toolTipService;
}

internal void EventHookupFoundInSession(EventHookupSession analyzedSession)
{
AssertIsForeground();
ThreadingContext.ThrowIfNotOnUIThread();

var caretPoint = analyzedSession.TextView.GetCaretPoint(analyzedSession.SubjectBuffer);

Expand Down Expand Up @@ -114,7 +117,7 @@ internal void BeginSession(

internal void CancelAndDismissExistingSessions()
{
AssertIsForeground();
ThreadingContext.ThrowIfNotOnUIThread();

if (CurrentSession != null)
{
Expand All @@ -137,7 +140,7 @@ internal void CancelAndDismissExistingSessions()
/// </summary>
private void TextBuffer_Changed(object sender, TextContentChangedEventArgs e)
{
AssertIsForeground();
ThreadingContext.ThrowIfNotOnUIThread();

foreach (var change in e.Changes)
{
Expand All @@ -154,7 +157,7 @@ private void TextBuffer_Changed(object sender, TextContentChangedEventArgs e)
/// </summary>
private void Caret_PositionChanged(object sender, EventArgs e)
{
AssertIsForeground();
ThreadingContext.ThrowIfNotOnUIThread();

if (CurrentSession == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ internal sealed partial class EventHookupSessionManager
/// += is being used to add an event handler to an event. If it is, then we also determine
/// a candidate name for the event handler.
/// </summary>
internal class EventHookupSession : ForegroundThreadAffinitizedObject
internal class EventHookupSession
{
public readonly Task<string> GetEventNameTask;
private readonly IThreadingContext _threadingContext;
private readonly CancellationTokenSource _cancellationTokenSource;
private readonly ITrackingPoint _trackingPoint;
private readonly ITrackingSpan _trackingSpan;
Expand All @@ -55,7 +56,7 @@ public ITrackingPoint TrackingPoint
{
get
{
AssertIsForeground();
_threadingContext.ThrowIfNotOnUIThread();
return _trackingPoint;
}
}
Expand All @@ -64,7 +65,7 @@ public ITrackingSpan TrackingSpan
{
get
{
AssertIsForeground();
_threadingContext.ThrowIfNotOnUIThread();
return _trackingSpan;
}
}
Expand All @@ -73,7 +74,7 @@ public ITextView TextView
{
get
{
AssertIsForeground();
_threadingContext.ThrowIfNotOnUIThread();
return _textView;
}
}
Expand All @@ -82,14 +83,14 @@ public ITextBuffer SubjectBuffer
{
get
{
AssertIsForeground();
_threadingContext.ThrowIfNotOnUIThread();
return _subjectBuffer;
}
}

public void Cancel()
{
AssertIsForeground();
_threadingContext.ThrowIfNotOnUIThread();
_cancellationTokenSource.Cancel();
}

Expand All @@ -100,9 +101,8 @@ public EventHookupSession(
ITextBuffer subjectBuffer,
IAsynchronousOperationListener asyncListener,
Mutex testSessionHookupMutex)
: base(eventHookupSessionManager.ThreadingContext)
{
AssertIsForeground();
_threadingContext = eventHookupSessionManager.ThreadingContext;
_cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = _cancellationTokenSource.Token;
_textView = textView;
Expand All @@ -129,7 +129,7 @@ public EventHookupSession(
var continuedTask = this.GetEventNameTask.SafeContinueWithFromAsync(
async t =>
{
await ThreadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(alwaysYield: true, cancellationToken);
await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(alwaysYield: true, cancellationToken);

if (t.Result != null)
{
Expand All @@ -153,7 +153,7 @@ public EventHookupSession(

private async Task<string> DetermineIfEventHookupAndGetHandlerNameAsync(Document document, int position, CancellationToken cancellationToken)
{
AssertIsBackground();
_threadingContext.ThrowIfNotOnBackgroundThread();

// For test purposes only!
if (TESTSessionHookupMutex != null)
Expand Down Expand Up @@ -191,7 +191,7 @@ private async Task<string> DetermineIfEventHookupAndGetHandlerNameAsync(Document

private async Task<SyntaxToken?> GetPlusEqualsTokenInsideAddAssignExpressionAsync(Document document, int position, CancellationToken cancellationToken)
{
AssertIsBackground();
_threadingContext.ThrowIfNotOnBackgroundThread();
var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var token = syntaxTree.FindTokenOnLeftOfPosition(position, cancellationToken);

Expand All @@ -210,7 +210,7 @@ private async Task<string> DetermineIfEventHookupAndGetHandlerNameAsync(Document

private IEventSymbol GetEventSymbol(SemanticModel semanticModel, SyntaxToken plusEqualsToken, CancellationToken cancellationToken)
{
AssertIsBackground();
_threadingContext.ThrowIfNotOnBackgroundThread();
if (plusEqualsToken.Parent is not AssignmentExpressionSyntax parentToken)
{
return null;
Expand All @@ -229,7 +229,7 @@ private string GetEventHandlerName(
IEventSymbol eventSymbol, SyntaxToken plusEqualsToken, SemanticModel semanticModel,
ISyntaxFactsService syntaxFactsService, NamingRule namingRule)
{
AssertIsBackground();
_threadingContext.ThrowIfNotOnBackgroundThread();
var objectPart = GetNameObjectPart(eventSymbol, plusEqualsToken, semanticModel, syntaxFactsService);
var basename = namingRule.NamingStyle.CreateName(ImmutableArray.Create(
string.Format("{0}_{1}", objectPart, eventSymbol.Name)));
Expand All @@ -249,7 +249,7 @@ private string GetEventHandlerName(
/// </summary>
private string GetNameObjectPart(IEventSymbol eventSymbol, SyntaxToken plusEqualsToken, SemanticModel semanticModel, ISyntaxFactsService syntaxFactsService)
{
AssertIsBackground();
_threadingContext.ThrowIfNotOnBackgroundThread();
var parentToken = plusEqualsToken.Parent as AssignmentExpressionSyntax;

if (parentToken.Left is MemberAccessExpressionSyntax memberAccessExpression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

using System;
using System.Composition;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.GoToDefinition;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.Editor.CSharp.GoToDefinition
{
Expand All @@ -16,8 +14,7 @@ internal sealed class CSharpGoToSymbolService : AbstractGoToSymbolService
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpGoToSymbolService(IThreadingContext threadingContext)
: base(threadingContext)
public CSharpGoToSymbolService()
{
}
}
Expand Down
Loading

0 comments on commit ce61483

Please sign in to comment.