public void ExecuteCommand(FormatDocumentCommandArgs args, Action nextHandler)
 {
     if (!TryExecuteCommand(args))
     {
         nextHandler();
     }
 }
        public bool ExecuteCommand(FormatDocumentCommandArgs args, CommandExecutionContext context)
        {
            if (!CanExecuteCommand(args.SubjectBuffer))
            {
                return(false);
            }

            var document = args.SubjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();

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

            var formattingService = document.GetLanguageService <IFormattingInteractionService>();

            if (formattingService == null || !formattingService.SupportsFormatDocument)
            {
                return(false);
            }

            using (context.OperationContext.AddScope(allowCancellation: true, EditorFeaturesResources.Formatting_document))
            {
                Format(args.TextView, args.SubjectBuffer, document, selectionOpt: null, context.OperationContext.UserCancellationToken);
            }

            return(true);
        }
Ejemplo n.º 3
0
        protected static async Task AssertFormatWithViewAsync(string expectedWithMarker, string codeWithMarker, bool debugMode = false)
        {
            var editorOperations = new Mock <IEditorOperations>(MockBehavior.Strict);
            var editorOperationsFactoryService = new Mock <IEditorOperationsFactoryService>(MockBehavior.Strict);

            editorOperations.Setup(o => o.AddAfterTextBufferChangePrimitive());
            editorOperations.Setup(o => o.AddBeforeTextBufferChangePrimitive());

            editorOperationsFactoryService.Setup(s => s.GetEditorOperations(It.IsAny <ITextView>())).Returns(editorOperations.Object);

            using (var workspace = await TestWorkspace.CreateCSharpAsync(codeWithMarker))
            {
                // set up caret position
                var testDocument = workspace.Documents.Single();
                var view         = testDocument.GetTextView();
                view.Caret.MoveTo(new SnapshotPoint(view.TextSnapshot, testDocument.CursorPosition.Value));

                // get original buffer
                var buffer = workspace.Documents.First().GetTextBuffer();

                var commandHandler = new FormatCommandHandler(TestWaitIndicator.Default, workspace.GetService <ITextUndoHistoryRegistry>(), editorOperationsFactoryService.Object);

                var commandArgs = new FormatDocumentCommandArgs(view, view.TextBuffer);
                commandHandler.ExecuteCommand(commandArgs, () => { });
                MarkupTestFile.GetPosition(expectedWithMarker, out var expected, out int expectedPosition);

                Assert.Equal(expected, view.TextSnapshot.GetText());

                var caretPosition = view.Caret.Position.BufferPosition.Position;
                Assert.True(expectedPosition == caretPosition,
                            string.Format("Caret positioned incorrectly. Should have been {0}, but was {1}.", expectedPosition, caretPosition));
            }
        }
        public bool ExecuteCommand(FormatDocumentCommandArgs args, CommandExecutionContext context)
        {
            //if (!args.SubjectBuffer.CanApplyChangeDocumentToWorkspace())
            //{
            //    return false;
            //}

            var document = args.SubjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();

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

            var formattingService = document.GetLanguageService <IEditorFormattingService>();

            if (formattingService == null || !formattingService.SupportsFormatDocument)
            {
                return(false);
            }

            using (context.OperationContext.AddScope(true, EditorFeaturesResources.Formatting_document))
            {
                Format(args.TextView, document, null, context.OperationContext.UserCancellationToken);
            }

            return(true);
        }
Ejemplo n.º 5
0
        private void CodeCleanupOrFormat(
            FormatDocumentCommandArgs args, Document document,
            IProgressTracker progressTracker, CancellationToken cancellationToken)
        {
            var codeCleanupService = document.GetLanguageService <ICodeCleanupService>();

            var docOptions = document.GetOptionsAsync(cancellationToken).WaitAndGetResult(cancellationToken);

            if (docOptions.GetOption(CodeCleanupOptions.PerformAdditionalCodeCleanupDuringFormatting))
            {
                // Start with a single progress item, which is the one to actually apply
                // the changes.
                progressTracker.AddItems(1);

                // Code cleanup
                var oldDoc = document;

                var codeCleanupChanges = GetCodeCleanupAndFormatChangesAsync(
                    document, codeCleanupService, progressTracker, cancellationToken).WaitAndGetResult(cancellationToken);

                if (codeCleanupChanges != null && codeCleanupChanges.Length > 0)
                {
                    progressTracker.Description = EditorFeaturesResources.Applying_changes;
                    ApplyChanges(oldDoc, codeCleanupChanges.ToList(), selectionOpt: null, cancellationToken);
                }

                progressTracker.ItemCompleted();
                return;
            }

            ShowGoldBarForCodeCleanupConfigurationIfNeeded(document);
            Format(args.TextView, document, selectionOpt: null, cancellationToken);
        }
        public bool ExecuteCommand(FormatDocumentCommandArgs args, CommandExecutionContext context)
        {
            if (!CanExecuteCommand(args.SubjectBuffer))
            {
                return(false);
            }

            var document = args.SubjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();

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

            using (context.OperationContext.AddScope(allowCancellation: true, EditorFeaturesResources.Formatting_document))
            {
                var cancellationToken = context.OperationContext.UserCancellationToken;

                var docOptions = document.GetOptionsAsync(cancellationToken).WaitAndGetResult(cancellationToken);

                using (var transaction = new CaretPreservingEditTransaction(
                           EditorFeaturesResources.Formatting, args.TextView, _undoHistoryRegistry, _editorOperationsFactoryService))
                {
                    var codeCleanupService = document.GetLanguageService <ICodeCleanupService>();

                    if (codeCleanupService == null)
                    {
                        Format(args.TextView, document, selectionOpt: null, cancellationToken);
                    }
                    else
                    {
                        if (docOptions.GetOption(CodeCleanupOptions.AreCodeCleanupRulesConfigured))
                        {
                            // Code cleanup
                            var oldDoc             = document;
                            var codeCleanupChanges = GetCodeCleanupAndFormatChangesAsync(document, codeCleanupService, cancellationToken).WaitAndGetResult(cancellationToken);

                            if (codeCleanupChanges != null && codeCleanupChanges.Count() > 0)
                            {
                                ApplyChanges(oldDoc, codeCleanupChanges.ToList(), selectionOpt: null, cancellationToken);
                            }
                        }
                        else
                        {
                            Format(args.TextView, document, selectionOpt: null, cancellationToken);

                            if (!docOptions.GetOption(CodeCleanupOptions.NeverShowCodeCleanupInfoBarAgain))
                            {
                                ShowGoldBarForCodeCleanupConfiguration(document);
                            }
                        }
                    }

                    transaction.Complete();
                }
            }

            return(true);
        }
Ejemplo n.º 7
0
        public bool ExecuteCommand(FormatDocumentCommandArgs args, CommandExecutionContext context)
        {
            if (!CanExecuteCommand(args.SubjectBuffer))
            {
                return(false);
            }

            var document = args.SubjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();

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

            context.OperationContext.TakeOwnership();

            _waitIndicator.Wait(
                EditorFeaturesResources.Formatting_document,
                EditorFeaturesResources.Formatting_document,
                allowCancel: true,
                showProgress: true,
                c =>
            {
                var docOptions = document.GetOptionsAsync(c.CancellationToken).WaitAndGetResult(c.CancellationToken);

                using (Logger.LogBlock(FunctionId.FormatDocument, CodeCleanupLogMessage.Create(docOptions), c.CancellationToken))
                    using (var transaction = new CaretPreservingEditTransaction(
                               EditorFeaturesResources.Formatting, args.TextView, _undoHistoryRegistry, _editorOperationsFactoryService))
                    {
                        var codeCleanupService = document.GetLanguageService <ICodeCleanupService>();
                        if (codeCleanupService == null)
                        {
                            Format(args.TextView, document, selectionOpt: null, c.CancellationToken);
                        }
                        else
                        {
                            CodeCleanupOrFormat(args, document, c.ProgressTracker, c.CancellationToken);
                        }

                        transaction.Complete();
                    }
            });

            return(true);
        }
        private void CodeCleanupOrFormat(
            FormatDocumentCommandArgs args, Document document, ICodeCleanupService codeCleanupService,
            IProgressTracker progressTracker, CancellationToken cancellationToken)
        {
            var workspace = document.Project.Solution.Workspace;
            var isFeatureTurnedOnThroughABTest = TurnOnCodeCleanupForGroupBIfInABTest(document, workspace);
            var performAdditionalCodeCleanupDuringFormatting = workspace.Options.GetOption(CodeCleanupOptions.PerformAdditionalCodeCleanupDuringFormatting, document.Project.Language);

            // if feature is turned on through AB test, we need to show the Gold bar even if they set NeverShowCodeCleanupInfoBarAgain == true before
            if (isFeatureTurnedOnThroughABTest ||
                !workspace.Options.GetOption(CodeCleanupOptions.NeverShowCodeCleanupInfoBarAgain, document.Project.Language))
            {
                // Show different gold bar text depends on PerformAdditionalCodeCleanupDuringFormatting value
                ShowGoldBarForCodeCleanupConfiguration(document, performAdditionalCodeCleanupDuringFormatting);
            }

            if (performAdditionalCodeCleanupDuringFormatting)
            {
                // Start with a single progress item, which is the one to actually apply
                // the changes.
                progressTracker.AddItems(1);

                // Code cleanup
                var oldDoc = document;

                var codeCleanupChanges = GetCodeCleanupAndFormatChangesAsync(
                    document, codeCleanupService, progressTracker, cancellationToken).WaitAndGetResult(cancellationToken);

                if (codeCleanupChanges != null && codeCleanupChanges.Length > 0)
                {
                    progressTracker.Description = EditorFeaturesResources.Applying_changes;
                    ApplyChanges(oldDoc, codeCleanupChanges.ToList(), selectionOpt: null, cancellationToken);
                }

                progressTracker.ItemCompleted();
            }
            else
            {
                Format(args.TextView, document, selectionOpt: null, cancellationToken);
            }
        }
        private bool TryExecuteCommand(FormatDocumentCommandArgs args)
        {
            //if (!args.SubjectBuffer.CanApplyChangeDocumentToWorkspace())
            //{
            //    return false;
            //}

            var document = args.SubjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();

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

            var formattingService = document.GetLanguageService <IEditorFormattingService>();

            if (formattingService == null || !formattingService.SupportsFormatDocument)
            {
                return(false);
            }

            var result = false;

            _waitIndicator.Wait(
                title: EditorFeaturesResources.Format_Document,
                message: EditorFeaturesResources.Formatting_document,
                allowCancel: true,
                action: waitContext =>
            {
                Format(args.TextView, document, null, waitContext.CancellationToken);
                result = true;
            });

            // We don't call nextHandler, since we have handled this command.
            return(result);
        }
 public VSCommanding.CommandState GetCommandState(FormatDocumentCommandArgs args)
 {
     return(GetCommandState(args.SubjectBuffer));
 }
 public CommandState GetCommandState(FormatDocumentCommandArgs args, Func <CommandState> nextHandler)
 {
     return(GetCommandState(args.SubjectBuffer, nextHandler));
 }
 public bool ExecuteCommand(FormatDocumentCommandArgs args, CommandExecutionContext context)
 {
     return(TryExecuteCommand(args, context));
 }
Ejemplo n.º 13
0
 public CommandState GetCommandState(FormatDocumentCommandArgs args) =>
 GetCommandState(args.SubjectBuffer);