Ejemplo n.º 1
0
        void IChainedCommandHandler <ReturnKeyCommandArgs> .ExecuteCommand(ReturnKeyCommandArgs args, Action nextHandler, CommandExecutionContext context)
        {
            AssertIsForeground();

            if (sessionOpt == null)
            {
                // No computation.  Nothing to do.  Just let the editor handle this.
                nextHandler();
                return;
            }

            CommitOnEnter(out var sendThrough, out var committed);

            // Always stop completion after enter has been typed.
            DismissSessionIfActive();

            // Enter has different behavior amongst languages, so we need to actually defer to
            // the individual language item to determine what to do.  For example, in VB, enter
            // always commits the item and then sends the enter through so that later features can
            // handle it (i.e. indentation).  In C# enter only commits, although there is an option
            // to send the newline along if the item was completely typed.
            if (sendThrough)
            {
                nextHandler();
            }
        }
Ejemplo n.º 2
0
        void ICommandHandler <ReturnKeyCommandArgs> .ExecuteCommand(ReturnKeyCommandArgs args, Action nextHandler)
        {
            AssertIsForeground();

            if (sessionOpt == null)
            {
                // No computation.  Nothing to do.  Just let the editor handle this.
                nextHandler();
                return;
            }

            // We are computing a model.  Commit it if we compute any selected item.
            bool sendThrough, committed;

            CommitOnEnter(out sendThrough, out committed);

            // We did not commit based on enter.  So our computation will still be running.  Stop it now.
            if (!committed)
            {
                this.StopModelComputation();
            }

            // Enter has different behavior amongst languages, so we need to actually defer to
            // the individual language item to determine what to do.  For example, in VB, enter
            // always commits the item and then sends the enter through so that later features can
            // handle it (i.e. indentation).  In C# enter only commits, although there is an option
            // to send the newline along if the item was completely typed.
            if (sendThrough)
            {
                nextHandler();
            }
        }
        protected void VerifyPressingEnter(
            string initialMarkup,
            string expectedMarkup,
            bool useTabs = false,
            bool autoGenerateXmlDocComments      = true,
            Action <TestWorkspace> setOptionsOpt = null
            )
        {
            Verify(
                initialMarkup,
                expectedMarkup,
                useTabs,
                autoGenerateXmlDocComments,
                setOptionsOpt: setOptionsOpt,
                execute: (workspace, view, editorOperationsFactoryService) =>
            {
                var commandHandler = CreateCommandHandler(workspace);

                var commandArgs = new ReturnKeyCommandArgs(view, view.TextBuffer);
                var nextHandler = CreateInsertTextHandler(view, "\r\n");
                commandHandler.ExecuteCommand(
                    commandArgs,
                    nextHandler,
                    TestCommandExecutionContext.Create()
                    );
            }
                );
        }
        protected void Verify(string initialMarkup, string expectedMarkup)
        {
            using (var workspace = CreateTestWorkspace(initialMarkup))
            {
                var testDocument = workspace.Documents.Single();
                var view         = testDocument.GetTextView();
                view.Caret.MoveTo(new SnapshotPoint(view.TextSnapshot, testDocument.CursorPosition.Value));

                var commandHandler = CreateCommandHandler(workspace.GetService <ITextUndoHistoryRegistry>(), workspace.GetService <IEditorOperationsFactoryService>());

                var args        = new ReturnKeyCommandArgs(view, view.TextBuffer);
                var nextHandler = CreateInsertTextHandler(view, "\r\n");

                if (!commandHandler.ExecuteCommand(args, TestCommandExecutionContext.Create()))
                {
                    nextHandler();
                }

                MarkupTestFile.GetPosition(expectedMarkup, out var expectedCode, out int expectedPosition);

                Assert.Equal(expectedCode, 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(ReturnKeyCommandArgs args, CommandExecutionContext executionContext)
        {
            var cocoaTextView = (ICocoaTextView)args.TextView;

            cocoaTextView.VisualElement.ResignFirstResponder();

            return(true);
        }
        public bool ExecuteCommand(ReturnKeyCommandArgs args, CommandExecutionContext executionContext)
        {
            if (_snippetManager.IsInSession(args.TextView))
            {
                return(_snippetManager.EndSession(args.TextView, leaveCaret: false));
            }

            return(false);
        }
Ejemplo n.º 7
0
        public void ExecuteCommand(ReturnKeyCommandArgs args, Action nextHandler)
        {
            // Check to see if the current line starts with exterior trivia. If so, we'll take over.
            // If not, let the nextHandler run.

            int originalPosition = -1;

            // The original position should be a position that is consistent with the syntax tree, even
            // after Enter is pressed. Thus, we use the start of the first selection if there is one.
            // Otherwise, getting the tokens to the right or the left might return unexpected results.

            if (args.TextView.Selection.SelectedSpans.Count > 0)
            {
                var selectedSpan = args.TextView.Selection
                                   .GetSnapshotSpansOnBuffer(args.SubjectBuffer)
                                   .FirstOrNullable();

                originalPosition = selectedSpan != null
                    ? selectedSpan.Value.Start
                    : args.TextView.GetCaretPoint(args.SubjectBuffer) ?? -1;
            }

            if (originalPosition < 0)
            {
                nextHandler();
                return;
            }

            if (!CurrentLineStartsWithExteriorTrivia(args.SubjectBuffer, originalPosition))
            {
                nextHandler();
                return;
            }

            // Finally, wait and see if completion is computing. If it is, we want to allow
            // the list to pop up rather than insert a blank line in the buffer.
            if (_completionService.WaitForComputation(args.TextView, args.SubjectBuffer))
            {
                nextHandler();
                return;
            }

            // According to JasonMal, the text undo history is associated with the surface buffer
            // in projection buffer scenarios, so the following line's usage of the surface buffer
            // is correct.
            using (var transaction = _undoHistoryRegistry.GetHistory(args.TextView.TextBuffer).CreateTransaction(EditorFeaturesResources.Insert_new_line))
            {
                var editorOperations = _editorOperationsFactoryService.GetEditorOperations(args.TextView);
                editorOperations.InsertNewLine();

                CompleteComment(args.SubjectBuffer, args.TextView, originalPosition, InsertOnEnterTyped, CancellationToken.None);

                // Since we're wrapping the ENTER key undo transaction, we always complete
                // the transaction -- even if we didn't generate anything.
                transaction.Complete();
            }
        }
Ejemplo n.º 8
0
 public void ExecuteCommand(
     ReturnKeyCommandArgs args,
     Action nextHandler,
     CommandExecutionContext context
     ) =>
 ExecuteReturnOrTypeCommand(
     args,
     nextHandler,
     context.OperationContext.UserCancellationToken
     );
Ejemplo n.º 9
0
        public CommandState GetCommandState(ReturnKeyCommandArgs args, Func <CommandState> nextHandler)
        {
            AssertIsForeground();
            Workspace workspace;

            if (!Workspace.TryGetWorkspace(args.SubjectBuffer.AsTextContainer(), out workspace))
            {
                return(nextHandler());
            }

            return(CommandState.Available);
        }
        protected void VerifyPressingEnter(string initialMarkup, string expectedMarkup)
        {
            Verify(initialMarkup, expectedMarkup, (view, undoHistoryRegistry, editorOperationsFactoryService, completionService) =>
            {
                var commandHandler = CreateCommandHandler(TestWaitIndicator.Default, undoHistoryRegistry, editorOperationsFactoryService, completionService) as ICommandHandler <ReturnKeyCommandArgs>;

                var commandArgs = new ReturnKeyCommandArgs(view, view.TextBuffer);
                var nextHandler = CreateInsertTextHandler(view, "\r\n");

                commandHandler.ExecuteCommand(commandArgs, nextHandler);
            });
        }
        protected async Task VerifyPressingEnterAsync(string initialMarkup, string expectedMarkup, bool useTabs = false, bool autoGenerateXmlDocComments = true)
        {
            await VerifyAsync(initialMarkup, expectedMarkup, useTabs, autoGenerateXmlDocComments,
                              execute : (view, undoHistoryRegistry, editorOperationsFactoryService, completionService) =>
            {
                var commandHandler = CreateCommandHandler(TestWaitIndicator.Default, undoHistoryRegistry, editorOperationsFactoryService) as ICommandHandler <ReturnKeyCommandArgs>;

                var commandArgs = new ReturnKeyCommandArgs(view, view.TextBuffer);
                var nextHandler = CreateInsertTextHandler(view, "\r\n");

                commandHandler.ExecuteCommand(commandArgs, nextHandler);
            });
        }
        internal void VerifyPressingEnter(string initialMarkup, string expectedMarkup, bool useTabs = false, string newLine = "\r\n", bool trimTrailingWhiteSpace = false, OptionsCollection globalOptions = null)
        {
            Verify(initialMarkup, expectedMarkup,
                   execute: (workspace, view, editorOperationsFactoryService) =>
            {
                var commandHandler = CreateCommandHandler(workspace);

                var commandArgs = new ReturnKeyCommandArgs(view, view.TextBuffer);
                var nextHandler = CreateInsertTextHandler(view, "\r\n");
                commandHandler.ExecuteCommand(commandArgs, nextHandler, TestCommandExecutionContext.Create());
            },
                   useTabs, newLine, trimTrailingWhiteSpace, globalOptions);
        }
Ejemplo n.º 13
0
        protected void VerifyPressingEnter(string initialMarkup, string expectedMarkup, bool useTabs = false, bool autoGenerateXmlDocComments = true,
                                           Action <TestWorkspace> setOptionsOpt = null)
        {
            Verify(initialMarkup, expectedMarkup, useTabs, autoGenerateXmlDocComments,
                   setOptionsOpt: setOptionsOpt,
                   execute: (view, undoHistoryRegistry, editorOperationsFactoryService, completionService) =>
            {
                var commandHandler = CreateCommandHandler(TestWaitIndicator.Default, undoHistoryRegistry, editorOperationsFactoryService) as ICommandHandler <ReturnKeyCommandArgs>;

                var commandArgs = new ReturnKeyCommandArgs(view, view.TextBuffer);
                var nextHandler = CreateInsertTextHandler(view, "\r\n");
                commandHandler.ExecuteCommand(commandArgs, nextHandler);
            });
        }
        public CommandState GetCommandState(ReturnKeyCommandArgs args)
        {
            AssertIsForeground();

            if (!AreSnippetsEnabled(args))
            {
                return(CommandState.Unspecified);
            }

            if (!Workspace.TryGetWorkspace(args.SubjectBuffer.AsTextContainer(), out var workspace))
            {
                return(CommandState.Unspecified);
            }

            return(CommandState.Available);
        }
        public CommandState GetCommandState(ReturnKeyCommandArgs args)
        {
            ThreadingContext.ThrowIfNotOnUIThread();

            if (!AreSnippetsEnabled(args))
            {
                return(CommandState.Unspecified);
            }

            if (!Workspace.TryGetWorkspace(args.SubjectBuffer.AsTextContainer(), out _))
            {
                return(CommandState.Unspecified);
            }

            return(CommandState.Available);
        }
        public bool ExecuteCommand(ReturnKeyCommandArgs args, CommandExecutionContext context)
        {
            AssertIsForeground();
            if (!AreSnippetsEnabled(args))
            {
                return(false);
            }

            if (args.TextView.Properties.TryGetProperty(typeof(AbstractSnippetExpansionClient), out AbstractSnippetExpansionClient snippetExpansionClient) &&
                snippetExpansionClient.TryHandleReturn())
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 17
0
        public void ExecuteCommand(ReturnKeyCommandArgs args, Action nextHandler)
        {
            AssertIsForeground();
            if (!AreSnippetsEnabled(args))
            {
                nextHandler();
                return;
            }

            if (args.TextView.Properties.TryGetProperty(typeof(AbstractSnippetExpansionClient), out AbstractSnippetExpansionClient snippetExpansionClient) &&
                snippetExpansionClient.TryHandleReturn())
            {
                return;
            }

            nextHandler();
        }
        protected static void AssertFormatWithPasteOrReturn(string expectedWithMarker, string codeWithMarker, bool allowDocumentChanges, bool isPaste = true)
        {
            using (var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromLines(codeWithMarker))
            {
                workspace.CanApplyChangeDocument = allowDocumentChanges;

                // 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 optionService = workspace.Services.GetService <IOptionService>();
                if (isPaste)
                {
                    optionService.SetOptions(optionService.GetOptions().WithChangedOption(FeatureOnOffOptions.FormatOnPaste, LanguageNames.CSharp, true));
                    var commandHandler = new FormatCommandHandler(TestWaitIndicator.Default, null, null);
                    var commandArgs    = new PasteCommandArgs(view, view.TextBuffer);
                    commandHandler.ExecuteCommand(commandArgs, () => { });
                }
                else
                {
                    // Return Key Command
                    var textUndoHistory         = new Mock <ITextUndoHistoryRegistry>();
                    var editorOperationsFactory = new Mock <IEditorOperationsFactoryService>();
                    var editorOperations        = new Mock <IEditorOperations>();
                    editorOperationsFactory.Setup(x => x.GetEditorOperations(testDocument.GetTextView())).Returns(editorOperations.Object);
                    var commandHandler = new FormatCommandHandler(TestWaitIndicator.Default, textUndoHistory.Object, editorOperationsFactory.Object);
                    var commandArgs    = new ReturnKeyCommandArgs(view, view.TextBuffer);
                    commandHandler.ExecuteCommand(commandArgs, () => { });
                }

                string expected;
                int    expectedPosition;
                MarkupTestFile.GetPosition(expectedWithMarker, out expected, out 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 CommandState GetCommandState(ReturnKeyCommandArgs args, Func <CommandState> nextHandler)
        {
            AssertIsForeground();

            if (!args.SubjectBuffer.GetOption(InternalFeatureOnOffOptions.Snippets))
            {
                return(nextHandler());
            }

            Workspace workspace;

            if (!Workspace.TryGetWorkspace(args.SubjectBuffer.AsTextContainer(), out workspace))
            {
                return(nextHandler());
            }

            return(CommandState.Available);
        }
Ejemplo n.º 20
0
        public void ExecuteCommand(ReturnKeyCommandArgs args, Action nextHandler)
        {
            AssertIsForeground();
            if (!args.SubjectBuffer.GetOption(InternalFeatureOnOffOptions.Snippets))
            {
                nextHandler();
                return;
            }

            AbstractSnippetExpansionClient snippetExpansionClient;

            if (args.TextView.Properties.TryGetProperty(typeof(AbstractSnippetExpansionClient), out snippetExpansionClient) &&
                snippetExpansionClient.TryHandleReturn())
            {
                return;
            }

            nextHandler();
        }
        public void ExecuteCommand(ReturnKeyCommandArgs args, Action nextHandler)
        {
            // Check to see if the current line starts with exterior trivia. If so, we'll take over.
            // If not, let the nextHandler run.

            var caretPosition = args.TextView.GetCaretPoint(args.SubjectBuffer) ?? -1;

            if (caretPosition < 0)
            {
                nextHandler();
                return;
            }

            if (!CurrentLineStartsWithExteriorTrivia(args.SubjectBuffer, caretPosition))
            {
                nextHandler();
                return;
            }

            // Finally, wait and see if completion is computing. If it is, we want to allow
            // the list to pop up rather than insert a blank line in the buffer.
            if (_completionService.WaitForComputation(args.TextView, args.SubjectBuffer))
            {
                nextHandler();
                return;
            }

            // According to JasonMal, the text undo history is associated with the surface buffer
            // in projection buffer scenarios, so the following line's usage of the surface buffer
            // is correct.
            using (var transaction = _undoHistoryRegistry.GetHistory(args.TextView.TextBuffer).CreateTransaction(EditorFeaturesResources.InsertNewLine))
            {
                var editorOperations = _editorOperationsFactoryService.GetEditorOperations(args.TextView);
                editorOperations.InsertNewLine();

                CompleteComment(args.SubjectBuffer, args.TextView, caretPosition, InsertOnEnterTyped, CancellationToken.None);

                // Since we're wrapping the ENTER key undo transaction, we always complete
                // the transaction -- even if we didn't generate anything.
                transaction.Complete();
            }
        }
        public void ExecuteCommand(ReturnKeyCommandArgs args, Action nextCommandHandler, CommandExecutionContext executionContext)
        {
            if (Enabled(args.TextView) && Manager(args.TextView).HasActiveSessions)
            {
                bool handledCommand = false;

                Manager(args.TextView).PreReturn(out handledCommand);

                if (handledCommand)
                {
                    return;
                }

                nextCommandHandler();

                Manager(args.TextView).PostReturn();

                return;
            }
            nextCommandHandler();
        }
Ejemplo n.º 23
0
        protected static void AssertFormatWithPasteOrReturn(string expectedWithMarker, string codeWithMarker, bool allowDocumentChanges, bool isPaste = true)
        {
            using (var workspace = TestWorkspace.CreateCSharp(codeWithMarker))
            {
                workspace.CanApplyChangeDocument = allowDocumentChanges;

                // 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();

                if (isPaste)
                {
                    var commandHandler = workspace.GetService <FormatCommandHandler>();
                    var commandArgs    = new PasteCommandArgs(view, view.TextBuffer);
                    commandHandler.ExecuteCommand(commandArgs, () => { }, TestCommandExecutionContext.Create());
                }
                else
                {
                    // Return Key Command
                    var commandHandler = workspace.GetService <FormatCommandHandler>();
                    var commandArgs    = new ReturnKeyCommandArgs(view, view.TextBuffer);
                    commandHandler.ExecuteCommand(commandArgs, () => { }, TestCommandExecutionContext.Create());
                }

                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));
            }
        }
Ejemplo n.º 24
0
 CommandState ICommandHandler <ReturnKeyCommandArgs> .GetCommandState(ReturnKeyCommandArgs args, Func <CommandState> nextHandler)
 {
     AssertIsForeground();
     return(nextHandler());
 }
Ejemplo n.º 25
0
 public CommandState GetCommandState(ReturnKeyCommandArgs args)
 => CommandState.Unspecified;
Ejemplo n.º 26
0
 public void ExecuteCommand(ReturnKeyCommandArgs args, Action nextHandler)
 {
     ExecuteReturnOrTypeCommand(args, nextHandler, CancellationToken.None);
 }
Ejemplo n.º 27
0
 public CommandState GetCommandState(ReturnKeyCommandArgs args, Func <CommandState> nextHandler)
 {
     return(nextHandler());
 }
 public VSCommanding.CommandState GetCommandState(ReturnKeyCommandArgs args)
 {
     return(VSCommanding.CommandState.Unspecified);
 }
 public CommandState GetCommandState(ReturnKeyCommandArgs args)
 {
     return(CommandState.Unspecified);
 }
        public void ExecuteCommand(ReturnKeyCommandArgs args, Action nextHandler)
        {
            // Check to see if the current line starts with exterior trivia. If so, we'll take over.
            // If not, let the nextHandler run.

            var caretPosition = args.TextView.GetCaretPoint(args.SubjectBuffer) ?? -1;

            if (caretPosition < 0)
            {
                nextHandler();
                return;
            }

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

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

            var text = document
                       .GetTextAsync(CancellationToken.None)
                       .WaitAndGetResult(CancellationToken.None);

            var currentLine     = text.Lines.GetLineFromPosition(caretPosition);
            var currentLineText = currentLine.ToString();
            var offset          = currentLineText.GetFirstNonWhitespaceOffset();

            if (offset == null)
            {
                nextHandler();
                return;
            }

            if (currentLineText.IndexOf(ExteriorTriviaText, StringComparison.Ordinal) != offset)
            {
                nextHandler();
                return;
            }

            // Finally, wait and see if completion is computing. If it is, we want to allow
            // the list to pop up rather than insert a blank line in the buffer.
            if (_completionService.WaitForComputation(args.TextView, args.SubjectBuffer))
            {
                nextHandler();
                return;
            }

            // According to JasonMal, the text undo history is associated with the surface buffer
            // in projection buffer scenarios, so the following line's usage of the surface buffer
            // is correct.
            using (var transaction = _undoHistoryRegistry.GetHistory(args.TextView.TextBuffer).CreateTransaction(EditorFeaturesResources.InsertNewLine))
            {
                var editorOperations = _editorOperationsFactoryService.GetEditorOperations(args.TextView);
                editorOperations.InsertNewLine();

                CompleteComment(args.SubjectBuffer, args.TextView, caretPosition, InsertOnEnterTyped, CancellationToken.None);

                // Since we're wrapping the ENTER key undo transaction, we always complete
                // the transaction -- even if we didn't generate anything.
                transaction.Complete();
            }
        }