提交 4485c56f 编写于 作者: D David Barbet

Move Go to definition to new fully loaded document API.

上级 e2d6b7d4
......@@ -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<IGoToDefinitionService>();
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<IGoToDefinitionService>();
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<INotificationService>();
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<INotificationService>();
notificationService.SendNotification(errorMessage, title: EditorFeaturesResources.Go_to_Definition, severity: NotificationSeverity.Information);
return true;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册