コード例 #1
0
        protected override void Do(
            object context,
            CommandFactoryManager <OperationContext> commandFactory,
            object commandData,
            OperationContext operationContext,
            EditorViewController controller,
            IDisplayContext displayContext,
            TextPosition position)
        {
            // If we don't have a selection, this is a simple insert command.
            TextPosition bufferPosition = displayContext.Caret.Position;
            TextRange    selection      = displayContext.Caret.Selection;

            if (!selection.IsEmpty)
            {
                // Create and execute the delete command. We do this separately
                // so they show up as a different undo item.
                IUndoableCommand <OperationContext> deleteCommand =
                    DeleteSelectionCommandFactory.CreateCommand(controller, displayContext);

                controller.CommandController.Do(deleteCommand, operationContext);

                // We have to reset the position so the insert happens as if
                // the text doesn't exist.
                bufferPosition   = selection.FirstTextPosition;
                operationContext = new OperationContext(
                    operationContext.LineBuffer, bufferPosition);
            }

            // Create the insert command using the (potentially) modified selection.
            string text = commandData.ToString();
            IInsertTextCommand <OperationContext> insertCommand =
                controller.CommandController.CreateInsertTextCommand(bufferPosition, text);

            insertCommand.UpdateTextPosition = DoTypes.All;

            controller.CommandController.Do(insertCommand, operationContext);

            // If we have a text position, we need to set it.
            if (operationContext.Results.HasValue)
            {
                displayContext.Caret.SetAndScrollToPosition(
                    operationContext.Results.Value.TextPosition);
            }
        }
コード例 #2
0
        protected override void Do(
            object context,
            CommandFactoryManager <OperationContext> commandFactory,
            object commandData,
            OperationContext operationContext,
            EditorViewController controller,
            IDisplayContext displayContext,
            TextPosition position)
        {
            // Get the text from the clipboard.
            Clipboard clipboard = displayContext.Clipboard;

            clipboard.RequestText(null);

            string clipboardText = clipboard.WaitForText();

            if (string.IsNullOrEmpty(clipboardText))
            {
                return;
            }

            // Figure out the position of this paste.
            TextPosition textPosition;

            if (displayContext.Caret.Selection.IsEmpty)
            {
                textPosition = new TextPosition(
                    displayContext.Caret.Position.LinePosition,
                    displayContext.Caret.Position.CharacterPosition);
            }
            else
            {
                textPosition = displayContext.Caret.Selection.FirstTextPosition;
            }

            // Create the paste command for the text.
            CompositeCommand <OperationContext> command =
                new PasteCommand <OperationContext>(
                    controller.CommandController, textPosition, clipboardText);

            // If we have a selection, we need to delete the selection first.
            if (!displayContext.Caret.Selection.IsEmpty)
            {
                // Create the composite command for the delete.
                var composite = new CompositeCommand <OperationContext>(true, false);
                IUndoableCommand <OperationContext> selection =
                    DeleteSelectionCommandFactory.CreateCommand(controller, displayContext);

                composite.Commands.Add(selection);
                composite.Commands.Add(command);

                // Move the composite over to the command.
                command = composite;
            }

            // Execute the command.
            controller.CommandController.Do(command, operationContext);

            // If we have a text position, we need to set it.
            if (operationContext.Results.HasValue)
            {
                displayContext.Caret.SetAndScrollToPosition(
                    operationContext.Results.Value.TextPosition);
            }
        }