예제 #1
0
        private bool Execute(EncapsulateFieldCommandArgs args, IUIThreadOperationScope waitScope)
        {
            using var token = _listener.BeginAsyncOperation("EncapsulateField");

            var cancellationToken = waitScope.Context.UserCancellationToken;
            var document          = args.SubjectBuffer.CurrentSnapshot.GetFullyLoadedOpenDocumentInCurrentContextWithChanges(
                waitScope.Context, _threadingContext);

            if (document == null)
            {
                return(false);
            }

            var spans = args.TextView.Selection.GetSnapshotSpansOnBuffer(args.SubjectBuffer);

            var service = document.GetLanguageService <AbstractEncapsulateFieldService>();

            var result = service.EncapsulateFieldAsync(document, spans.First().Span.ToTextSpan(), true, cancellationToken).WaitAndGetResult(cancellationToken);

            // 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.
            waitScope.Context.TakeOwnership();

            var workspace = document.Project.Solution.Workspace;

            if (result == null)
            {
                var notificationService = workspace.Services.GetService <INotificationService>();
                notificationService.SendNotification(EditorFeaturesResources.Please_select_the_definition_of_the_field_to_encapsulate, severity: NotificationSeverity.Error);
                return(false);
            }

            waitScope.AllowCancellation = false;
            cancellationToken           = waitScope.Context.UserCancellationToken;

            var finalSolution = result.GetSolutionAsync(cancellationToken).WaitAndGetResult(cancellationToken);

            var previewService = workspace.Services.GetService <IPreviewDialogService>();

            if (previewService != null)
            {
                finalSolution = previewService.PreviewChanges(
                    string.Format(EditorFeaturesResources.Preview_Changes_0, EditorFeaturesResources.Encapsulate_Field),
                    "vs.csharp.refactoring.preview",
                    EditorFeaturesResources.Encapsulate_Field_colon,
                    result.GetNameAsync(cancellationToken).WaitAndGetResult(cancellationToken),
                    result.GetGlyphAsync(cancellationToken).WaitAndGetResult(cancellationToken),
                    finalSolution,
                    document.Project.Solution);
            }

            if (finalSolution == null)
            {
                // User clicked cancel.
                return(true);
            }

            using (var undoTransaction = _undoManager.GetTextBufferUndoManager(args.SubjectBuffer).TextBufferUndoHistory.CreateTransaction(EditorFeaturesResources.Encapsulate_Field))
            {
                if (!workspace.TryApplyChanges(finalSolution))
                {
                    undoTransaction.Cancel();
                    return(false);
                }

                undoTransaction.Complete();
            }

            return(true);
        }
 public ProgressTrackerAdapter(IUIThreadOperationScope uiThreadOperationScope)
 {
     Requires.NotNull(uiThreadOperationScope, nameof(uiThreadOperationScope));
     _uiThreadOperationScope = uiThreadOperationScope;
 }
예제 #3
0
 protected override void OnScopeProgressChanged(IUIThreadOperationScope changedScope)
 => UpdateProgress();