提交 150b9bde 编写于 作者: D David Barbet

Reorder workspace/document/service checks in command handler to be more performant

上级 00b38c25
......@@ -52,16 +52,24 @@ public bool ExecuteCommand(GoToImplementationCommandArgs args, CommandExecutionC
using (context.OperationContext.AddScope(allowCancellation: true, EditorFeaturesResources.Locating_implementations))
{
var subjectBuffer = args.SubjectBuffer;
var document = subjectBuffer.CurrentSnapshot.GetFullyLoadedOpenDocumentInCurrentContextWithChangesAsync(
context.OperationContext).WaitAndGetResult(context.OperationContext.UserCancellationToken);
var findUsagesService = document?.GetLanguageService<IFindUsagesService>();
if (!Workspace.TryGetWorkspace(subjectBuffer.AsTextContainer(), out var workspace))
{
return false;
}
var findUsagesService = workspace.Services.GetLanguageServices(args.SubjectBuffer)?.GetService<IFindUsagesService>();
if (findUsagesService != null)
{
var caret = args.TextView.GetCaretPoint(args.SubjectBuffer);
if (caret.HasValue)
{
ExecuteCommand(document, caret.Value, findUsagesService, context);
return true;
var document = subjectBuffer.CurrentSnapshot.GetFullyLoadedOpenDocumentInCurrentContextWithChangesAsync(
context.OperationContext).WaitAndGetResult(context.OperationContext.UserCancellationToken);
if (document != null)
{
ExecuteCommand(document, caret.Value, findUsagesService, context);
return true;
}
}
}
......
......@@ -56,21 +56,17 @@ private bool ExecuteCommand(ITextView textView, ITextBuffer subjectBuffer, Comma
{
using (context.OperationContext.AddScope(allowCancellation: true, FeaturesResources.Change_signature))
{
var document = subjectBuffer.CurrentSnapshot.GetFullyLoadedOpenDocumentInCurrentContextWithChangesAsync(
context.OperationContext).WaitAndGetResult(context.OperationContext.UserCancellationToken);
if (document == null)
if (!Workspace.TryGetWorkspace(subjectBuffer.AsTextContainer(), out var workspace))
{
return false;
}
// TODO: reuse GetCommandState instead
var workspace = document.Project.Solution.Workspace;
if (!workspace.CanApplyChange(ApplyChangesKind.ChangeDocument))
{
return false;
}
var supportsFeatureService = document.Project.Solution.Workspace.Services.GetService<ITextBufferSupportsFeatureService>();
var supportsFeatureService = workspace.Services.GetService<ITextBufferSupportsFeatureService>();
if (!supportsFeatureService.SupportsRefactorings(subjectBuffer))
{
return false;
......@@ -82,6 +78,13 @@ private bool ExecuteCommand(ITextView textView, ITextBuffer subjectBuffer, Comma
return false;
}
var document = subjectBuffer.CurrentSnapshot.GetFullyLoadedOpenDocumentInCurrentContextWithChangesAsync(
context.OperationContext).WaitAndGetResult(context.OperationContext.UserCancellationToken);
if (document == null)
{
return false;
}
var reorderParametersService = document.GetLanguageService<AbstractChangeSignatureService>();
var result = reorderParametersService.ChangeSignature(
document,
......
......@@ -50,33 +50,37 @@ public bool ExecuteCommand(ExtractInterfaceCommandArgs args, CommandExecutionCon
{
using (context.OperationContext.AddScope(allowCancellation: true, EditorFeaturesResources.Extract_Interface))
{
var document = args.SubjectBuffer.CurrentSnapshot.GetFullyLoadedOpenDocumentInCurrentContextWithChangesAsync(
context.OperationContext).WaitAndGetResult(context.OperationContext.UserCancellationToken);
if (document == null)
var subjectBuffer = args.SubjectBuffer;
if (!Workspace.TryGetWorkspace(subjectBuffer.AsTextContainer(), out var workspace))
{
return false;
}
var workspace = document.Project.Solution.Workspace;
if (!workspace.CanApplyChange(ApplyChangesKind.AddDocument) ||
!workspace.CanApplyChange(ApplyChangesKind.ChangeDocument))
{
return false;
}
var supportsFeatureService = document.Project.Solution.Workspace.Services.GetService<ITextBufferSupportsFeatureService>();
if (!supportsFeatureService.SupportsRefactorings(args.SubjectBuffer))
var supportsFeatureService = workspace.Services.GetService<ITextBufferSupportsFeatureService>();
if (!supportsFeatureService.SupportsRefactorings(subjectBuffer))
{
return false;
}
var caretPoint = args.TextView.GetCaretPoint(args.SubjectBuffer);
var caretPoint = args.TextView.GetCaretPoint(subjectBuffer);
if (!caretPoint.HasValue)
{
return false;
}
var document = subjectBuffer.CurrentSnapshot.GetFullyLoadedOpenDocumentInCurrentContextWithChangesAsync(
context.OperationContext).WaitAndGetResult(context.OperationContext.UserCancellationToken);
if (document == null)
{
return false;
}
// 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.
......
......@@ -72,23 +72,29 @@ public VSCommanding.CommandState GetCommandState(ExtractMethodCommandArgs args)
public bool ExecuteCommand(ExtractMethodCommandArgs args, CommandExecutionContext context)
{
var document = args.SubjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();
if (document == null)
// Finish any rename that had been started. We'll do this here before we enter the
// wait indicator for Extract Method
if (_renameService.ActiveSession != null)
{
_renameService.ActiveSession.Commit();
}
var subjectBuffer = args.SubjectBuffer;
if (!Workspace.TryGetWorkspace(subjectBuffer.AsTextContainer(), out var workspace))
{
return false;
}
var supportsFeatureService = document.Project.Solution.Workspace.Services.GetService<ITextBufferSupportsFeatureService>();
if (!supportsFeatureService.SupportsRefactorings(args.SubjectBuffer))
var supportsFeatureService = workspace.Services.GetService<ITextBufferSupportsFeatureService>();
if (!supportsFeatureService.SupportsRefactorings(subjectBuffer))
{
return false;
}
// Finish any rename that had been started. We'll do this here before we enter the
// wait indicator for Extract Method
if (_renameService.ActiveSession != null)
var document = subjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();
if (document == null)
{
_renameService.ActiveSession.Commit();
return false;
}
using (context.OperationContext.AddScope(allowCancellation: true, EditorFeaturesResources.Applying_Extract_Method_refactoring))
......
......@@ -40,7 +40,17 @@ public bool ExecuteCommand(OrganizeDocumentCommandArgs args, CommandExecutionCon
{
using (context.OperationContext.AddScope(allowCancellation: true, EditorFeaturesResources.Organizing_document))
{
this.Organize(args.SubjectBuffer, context.OperationContext);
var cancellationToken = context.OperationContext.UserCancellationToken;
var document = args.SubjectBuffer.CurrentSnapshot.GetFullyLoadedOpenDocumentInCurrentContextWithChangesAsync(context.OperationContext)
.WaitAndGetResult(cancellationToken);
if (document != null)
{
var newDocument = OrganizingService.OrganizeAsync(document, cancellationToken: cancellationToken).WaitAndGetResult(cancellationToken);
if (document != newDocument)
{
ApplyTextChange(document, newDocument);
}
}
}
return true;
......@@ -99,20 +109,6 @@ public bool ExecuteCommand(SortAndRemoveUnnecessaryImportsCommandArgs args, Comm
return true;
}
private void Organize(ITextBuffer subjectBuffer, IUIThreadOperationContext operationContext)
{
var cancellationToken = operationContext.UserCancellationToken;
var document = subjectBuffer.CurrentSnapshot.GetFullyLoadedOpenDocumentInCurrentContextWithChangesAsync(operationContext).WaitAndGetResult(cancellationToken);
if (document != null)
{
var newDocument = OrganizingService.OrganizeAsync(document, cancellationToken: cancellationToken).WaitAndGetResult(cancellationToken);
if (document != newDocument)
{
ApplyTextChange(document, newDocument);
}
}
}
private void SortAndRemoveUnusedImports(ITextBuffer subjectBuffer, IUIThreadOperationContext operationContext)
{
var cancellationToken = operationContext.UserCancellationToken;
......
......@@ -36,12 +36,6 @@ public CallHierarchyCommandHandler([ImportMany] IEnumerable<ICallHierarchyPresen
}
public bool ExecuteCommand(ViewCallHierarchyCommandArgs args, CommandExecutionContext context)
{
AddRootNode(args, context);
return true;
}
private void AddRootNode(ViewCallHierarchyCommandArgs args, CommandExecutionContext context)
{
using (var waitScope = context.OperationContext.AddScope(allowCancellation: true, EditorFeaturesResources.Computing_Call_Hierarchy_Information))
{
......@@ -50,7 +44,7 @@ private void AddRootNode(ViewCallHierarchyCommandArgs args, CommandExecutionCont
context.OperationContext).WaitAndGetResult(cancellationToken);
if (document == null)
{
return;
return true;
}
var workspace = document.Project.Solution.Workspace;
......@@ -85,6 +79,8 @@ private void AddRootNode(ViewCallHierarchyCommandArgs args, CommandExecutionCont
notificationService.SendNotification(EditorFeaturesResources.Cursor_must_be_on_a_member_name, severity: NotificationSeverity.Information);
}
}
return true;
}
public VSCommanding.CommandState GetCommandState(ViewCallHierarchyCommandArgs args)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册