private void ExecuteCommand(ITextView textView, ITextBuffer subjectBuffer, Action nextHandler) { var document = subjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges(); if (document == null) { nextHandler(); return; } // TODO: reuse GetCommandState instead var workspace = document.Project.Solution.Workspace; if (!workspace.CanApplyChange(ApplyChangesKind.ChangeDocument)) { nextHandler(); return; } var supportsFeatureService = document.Project.Solution.Workspace.Services.GetService <IDocumentSupportsFeatureService>(); if (!supportsFeatureService.SupportsRefactorings(document)) { nextHandler(); return; } var caretPoint = textView.GetCaretPoint(subjectBuffer); if (!caretPoint.HasValue) { nextHandler(); return; } ChangeSignatureResult result = null; var waitResult = _waitIndicator.Wait( FeaturesResources.Change_signature, allowCancel: true, action: w => { var reorderParametersService = document.GetLanguageService <AbstractChangeSignatureService>(); result = reorderParametersService.ChangeSignature( document, caretPoint.Value.Position, (errorMessage, severity) => workspace.Services.GetService <INotificationService>().SendNotification(errorMessage, severity: severity), w.CancellationToken); }); if (waitResult == WaitIndicatorResult.Canceled) { return; } if (result == null || !result.Succeeded) { return; } var finalSolution = result.UpdatedSolution; var previewService = workspace.Services.GetService <IPreviewDialogService>(); if (previewService != null && result.PreviewChanges) { finalSolution = previewService.PreviewChanges( string.Format(EditorFeaturesResources.Preview_Changes_0, EditorFeaturesResources.Change_Signature), "vs.csharp.refactoring.preview", EditorFeaturesResources.Change_Signature_colon, result.Name, result.Glyph.GetValueOrDefault(), result.UpdatedSolution, document.Project.Solution); } if (finalSolution == null) { // User clicked cancel. return; } using (var workspaceUndoTransaction = workspace.OpenGlobalUndoTransaction(FeaturesResources.Change_signature)) { if (!workspace.TryApplyChanges(finalSolution)) { // TODO: handle failure return; } workspaceUndoTransaction.Commit(); } }
private bool ExecuteCommand(ITextView textView, ITextBuffer subjectBuffer, CommandExecutionContext context) { var document = subjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges(); if (document == null) { 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 <IDocumentSupportsFeatureService>(); if (!supportsFeatureService.SupportsRefactorings(document)) { return(false); } var caretPoint = textView.GetCaretPoint(subjectBuffer); if (!caretPoint.HasValue) { return(false); } ChangeSignatureResult result = null; using (context.WaitContext.AddScope(allowCancellation: true, FeaturesResources.Change_signature)) { var reorderParametersService = document.GetLanguageService <AbstractChangeSignatureService>(); result = reorderParametersService.ChangeSignature( document, caretPoint.Value.Position, (errorMessage, severity) => { // 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.WaitContext.TakeOwnership(); workspace.Services.GetService <INotificationService>().SendNotification(errorMessage, severity: severity); }, context.WaitContext.UserCancellationToken); } if (result == null || !result.Succeeded) { return(true); } var finalSolution = result.UpdatedSolution; var previewService = workspace.Services.GetService <IPreviewDialogService>(); if (previewService != null && result.PreviewChanges) { // 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.WaitContext.TakeOwnership(); finalSolution = previewService.PreviewChanges( string.Format(EditorFeaturesResources.Preview_Changes_0, EditorFeaturesResources.Change_Signature), "vs.csharp.refactoring.preview", EditorFeaturesResources.Change_Signature_colon, result.Name, result.Glyph.GetValueOrDefault(), result.UpdatedSolution, document.Project.Solution); } if (finalSolution == null) { // User clicked cancel. return(true); } using (var workspaceUndoTransaction = workspace.OpenGlobalUndoTransaction(FeaturesResources.Change_signature)) { if (!workspace.TryApplyChanges(finalSolution)) { // TODO: handle failure return(true); } workspaceUndoTransaction.Commit(); } return(true); }