From 4485c56f8e6b973f089d74dc0ec90267000acc26 Mon Sep 17 00:00:00 2001 From: David Barbet Date: Thu, 14 Mar 2019 14:12:51 -0700 Subject: [PATCH] Move Go to definition to new fully loaded document API. --- .../GoToDefinitionCommandHandler.cs | 57 +++++++++---------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/src/EditorFeatures/Core/GoToDefinition/GoToDefinitionCommandHandler.cs b/src/EditorFeatures/Core/GoToDefinition/GoToDefinitionCommandHandler.cs index f97b5b35c8d..54433d6dbe9 100644 --- a/src/EditorFeatures/Core/GoToDefinition/GoToDefinitionCommandHandler.cs +++ b/src/EditorFeatures/Core/GoToDefinition/GoToDefinitionCommandHandler.cs @@ -10,6 +10,7 @@ using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Text.Editor.Commanding.Commands; using Microsoft.VisualStudio.Utilities; +using Roslyn.Utilities; using VSCommanding = Microsoft.VisualStudio.Commanding; namespace Microsoft.CodeAnalysis.Editor.GoToDefinition @@ -30,7 +31,8 @@ internal class GoToDefinitionCommandHandler : public VSCommanding.CommandState GetCommandState(GoToDefinitionCommandArgs args) { - var (document, service) = GetDocumentAndService(args.SubjectBuffer.CurrentSnapshot); + var document = args.SubjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges(); + var service = document?.GetLanguageService(); return service != null ? VSCommanding.CommandState.Available : VSCommanding.CommandState.Unavailable; @@ -38,18 +40,23 @@ public VSCommanding.CommandState GetCommandState(GoToDefinitionCommandArgs args) public bool ExecuteCommand(GoToDefinitionCommandArgs args, CommandExecutionContext context) { - var subjectBuffer = args.SubjectBuffer; - var (document, service) = GetDocumentAndService(subjectBuffer.CurrentSnapshot); - if (service != null) + using (context.OperationContext.AddScope(allowCancellation: true, EditorFeaturesResources.Navigating_to_definition)) { - var caretPos = args.TextView.GetCaretPoint(subjectBuffer); - if (caretPos.HasValue && TryExecuteCommand(document, caretPos.Value, service, context)) + var subjectBuffer = args.SubjectBuffer; + var document = subjectBuffer.CurrentSnapshot.GetFullyLoadedOpenDocumentInCurrentContextWithChangesAsync( + context.OperationContext).WaitAndGetResult(context.OperationContext.UserCancellationToken); + var service = document?.GetLanguageService(); + if (service != null) { - return true; + var caretPos = args.TextView.GetCaretPoint(subjectBuffer); + if (caretPos.HasValue && TryExecuteCommand(document, caretPos.Value, service, context)) + { + return true; + } } - } - return false; + return false; + } } // Internal for testing purposes only. @@ -61,30 +68,20 @@ internal bool TryExecuteCommand(Document document, int caretPosition, CommandExe internal bool TryExecuteCommand(Document document, int caretPosition, IGoToDefinitionService goToDefinitionService, CommandExecutionContext context) { - string errorMessage = null; - - using (context.OperationContext.AddScope(allowCancellation: true, EditorFeaturesResources.Navigating_to_definition)) - { - if (goToDefinitionService != null && - goToDefinitionService.TryGoToDefinition(document, caretPosition, context.OperationContext.UserCancellationToken)) - { - return true; - } - - errorMessage = EditorFeaturesResources.Cannot_navigate_to_the_symbol_under_the_caret; - } - - if (errorMessage != null) + if (goToDefinitionService != null && + goToDefinitionService.TryGoToDefinition(document, caretPosition, context.OperationContext.UserCancellationToken)) { - // We are about to show a modal UI dialog so we should take over the command execution - // wait context. That means the command system won't attempt to show its own wait dialog - // and also will take it into consideration when measuring command handling duration. - context.OperationContext.TakeOwnership(); - var workspace = document.Project.Solution.Workspace; - var notificationService = workspace.Services.GetService(); - notificationService.SendNotification(errorMessage, title: EditorFeaturesResources.Go_to_Definition, severity: NotificationSeverity.Information); + return true; } + var errorMessage = EditorFeaturesResources.Cannot_navigate_to_the_symbol_under_the_caret; + // We are about to show a modal UI dialog so we should take over the command execution + // wait context. That means the command system won't attempt to show its own wait dialog + // and also will take it into consideration when measuring command handling duration. + context.OperationContext.TakeOwnership(); + var workspace = document.Project.Solution.Workspace; + var notificationService = workspace.Services.GetService(); + notificationService.SendNotification(errorMessage, title: EditorFeaturesResources.Go_to_Definition, severity: NotificationSeverity.Information); return true; } } -- GitLab