Ejemplo n.º 1
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();
            }
        }
        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 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();
            }
        }