Skip to content

Commit

Permalink
Remove use of WaitAndGetResult
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed Dec 17, 2024
1 parent 1cc4e1f commit 9cc6fa4
Showing 1 changed file with 17 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#nullable disable

using System;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
Expand All @@ -14,67 +15,53 @@
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Text.Editor.Commanding.Commands;
using Roslyn.Utilities;

namespace Microsoft.VisualStudio.LanguageServices.Implementation.Library.ClassView;

internal abstract class AbstractSyncClassViewCommandHandler : ICommandHandler<SyncClassViewCommandArgs>
internal abstract class AbstractSyncClassViewCommandHandler(
IThreadingContext threadingContext,
SVsServiceProvider serviceProvider) : ICommandHandler<SyncClassViewCommandArgs>
{
private const string ClassView = "Class View";
private readonly IThreadingContext _threadingContext;
private readonly IServiceProvider _serviceProvider;
private readonly IThreadingContext _threadingContext = threadingContext;
private readonly IServiceProvider _serviceProvider = serviceProvider;

public string DisplayName => ServicesVSResources.Sync_Class_View;

protected AbstractSyncClassViewCommandHandler(
IThreadingContext threadingContext,
SVsServiceProvider serviceProvider)
{
Contract.ThrowIfNull(serviceProvider);
_threadingContext = threadingContext;
_serviceProvider = serviceProvider;
}

public bool ExecuteCommand(SyncClassViewCommandArgs args, CommandExecutionContext context)
{
_threadingContext.ThrowIfNotOnUIThread();

var caretPosition = args.TextView.GetCaretPoint(args.SubjectBuffer) ?? -1;
if (caretPosition < 0)
{
return false;
}

using var waitScope = context.OperationContext.AddScope(allowCancellation: true, string.Format(ServicesVSResources.Synchronizing_with_0, ClassView));
return _threadingContext.JoinableTaskFactory.Run(() => ExecuteCommandAsync(args, context, caretPosition));
}

private async Task<bool> ExecuteCommandAsync(
SyncClassViewCommandArgs args, CommandExecutionContext context, int caretPosition)
{
var snapshot = args.SubjectBuffer.CurrentSnapshot;

using var waitScope = context.OperationContext.AddScope(allowCancellation: true, string.Format(ServicesVSResources.Synchronizing_with_0, ClassView));
var document = snapshot.GetFullyLoadedOpenDocumentInCurrentContextWithChangesAsync(
context.OperationContext).WaitAndGetResult(context.OperationContext.UserCancellationToken);
var document = await snapshot.GetFullyLoadedOpenDocumentInCurrentContextWithChangesAsync(
context.OperationContext).ConfigureAwait(true);
if (document == null)
{
return true;
}

var syntaxFactsService = document.GetLanguageService<ISyntaxFactsService>();
if (syntaxFactsService == null)
{
return true;
}

var libraryService = document.GetLanguageService<ILibraryService>();
if (libraryService == null)
{
return true;
}

var userCancellationToken = context.OperationContext.UserCancellationToken;
var semanticModel = document
.GetSemanticModelAsync(userCancellationToken)
.WaitAndGetResult(userCancellationToken);
var semanticModel = await document.GetSemanticModelAsync(userCancellationToken).ConfigureAwait(true);

var root = semanticModel.SyntaxTree
.GetRootAsync(userCancellationToken)
.WaitAndGetResult(userCancellationToken);
var root = await semanticModel.SyntaxTree.GetRootAsync(userCancellationToken).ConfigureAwait(true);

var memberDeclaration = syntaxFactsService.GetContainingMemberDeclaration(root, caretPosition);

Expand All @@ -83,22 +70,15 @@ public bool ExecuteCommand(SyncClassViewCommandArgs args, CommandExecutionContex
: null;

while (symbol != null && !IsValidSymbolToSynchronize(symbol))
{
symbol = symbol.ContainingSymbol;
}

IVsNavInfo navInfo = null;
if (symbol != null)
{
navInfo = libraryService.NavInfoFactory.CreateForSymbol(symbol, document.Project, semanticModel.Compilation, useExpandedHierarchy: true);
}

navInfo ??= libraryService.NavInfoFactory.CreateForProject(document.Project);

if (navInfo == null)
{
return true;
}

var navigationTool = _serviceProvider.GetServiceOnMainThread<SVsClassView, IVsNavigationTool>();
navigationTool.NavigateToNavInfo(navInfo);
Expand Down

0 comments on commit 9cc6fa4

Please sign in to comment.