protected override void Do(
            object context,
            CommandFactoryManager <OperationContext> commandFactory,
            object commandData,
            OperationContext operationContext,
            EditorViewController controller,
            IDisplayContext displayContext,
            TextPosition position)
        {
            IDeleteTextCommand <OperationContext> deleteCommand =
                controller.CommandController.CreateDeleteTextCommand(
                    new SingleLineTextRange(
                        displayContext.Caret.Position.LinePosition,
                        displayContext.Caret.Position.CharacterPosition,
                        CharacterPosition.Word));

            deleteCommand.UpdateTextPosition = DoTypes.All;

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

            // If we have a text position, we need to set it.
            if (operationContext.Results.HasValue)
            {
                displayContext.Caret.SetAndScrollToPosition(
                    operationContext.Results.Value.TextPosition);
            }
        }
        public SplitParagraphCommand(
            ITextEditingCommandController <TContext> controller,
            TextPosition position)
            : base(true, false)
        {
            // The split paragraph consists of inserting a new line, pasting the
            // text to the right of the position into that one, and then removing
            // the text from the current line.
            var line = (int)position.LinePosition;

            // Start by inserting the new line.
            IInsertLineCommand <TContext> insertLineCommand =
                controller.CreateInsertLineCommand((int)position.LinePosition + 1);

            insertLineCommand.UpdateTextPosition = DoTypes.All;

            // Insert the text from the line into the nmew line.
            IInsertTextFromTextRangeCommand <TContext> insertTextCommand =
                controller.CreateInsertTextFromTextRangeCommand(
                    new TextPosition((line + 1), CharacterPosition.Begin),
                    new SingleLineTextRange(
                        position.LinePosition, position.CharacterPosition, CharacterPosition.End));

            // Delete the text from the current line.
            IDeleteTextCommand <TContext> deleteTextCommand =
                controller.CreateDeleteTextCommand(
                    new SingleLineTextRange(
                        line, position.CharacterPosition, CharacterPosition.End));

            // Add the commands into the composite and indicate that the whitespace
            // command controls where the text position will end up.
            Commands.Add(insertLineCommand);
            Commands.Add(insertTextCommand);
            Commands.Add(deleteTextCommand);
        }
示例#3
0
        /// <summary>
        /// Creates the command needed for the delete selection.
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="displayContext"></param>
        /// <returns></returns>
        public static IUndoableCommand <OperationContext> CreateCommand(
            EditorViewController controller,
            IDisplayContext displayContext)
        {
            // Grab the selection and determine if we are a single line or
            // multiple line selection.
            IUndoableCommand <OperationContext> command;
            TextRange selection = displayContext.Caret.Selection;

            if (selection.IsSameLine)
            {
                IDeleteTextCommand <OperationContext> deleteCommand =
                    controller.CommandController.CreateDeleteTextCommand(
                        new SingleLineTextRange(
                            selection.BeginTextPosition.LinePosition,
                            selection.BeginTextPosition.CharacterPosition,
                            selection.EndTextPosition.CharacterPosition));
                deleteCommand.UpdateTextPosition = DoTypes.All;
                command = deleteCommand;
            }
            else
            {
                // We have a multiple line delete which requires some additional
                // processing since the core operation is a single line only.
                var compositeCommand = new CompositeCommand <OperationContext>(true, false);

                // Delete the text from the first line.
                IDeleteTextCommand <OperationContext> firstLineCommand =
                    controller.CommandController.CreateDeleteTextCommand(
                        new SingleLineTextRange(
                            selection.BeginTextPosition.LinePosition,
                            selection.BeginTextPosition.CharacterPosition,
                            CharacterPosition.End));
                firstLineCommand.UpdateTextPosition = DoTypes.All;

                compositeCommand.Commands.Add(firstLineCommand);

                // Insert the text from the last into into the first.
                IInsertTextFromTextRangeCommand <OperationContext> secondLineCommand =
                    controller.CommandController.CreateInsertTextFromTextRangeCommand(
                        selection.BeginTextPosition,
                        new SingleLineTextRange(
                            selection.EndTextPosition.LinePosition,
                            selection.EndTextPosition.CharacterPosition,
                            CharacterPosition.End));

                compositeCommand.Commands.Add(secondLineCommand);

                // Delete all the lines between the two.
                int startLineIndex =
                    selection.FirstLinePosition.GetLineIndex(displayContext.LineBuffer);
                int endLineIndex =
                    selection.LastLinePosition.GetLineIndex(displayContext.LineBuffer);

                for (int lineIndex = endLineIndex;
                     lineIndex > startLineIndex;
                     lineIndex--)
                {
                    IDeleteLineCommand <OperationContext> deleteLineCommand =
                        controller.CommandController.CreateDeleteLineCommand(lineIndex);
                    compositeCommand.Commands.Add(deleteLineCommand);
                }

                // Execute the composite command.
                command = compositeCommand;
            }

            // Return the resulting command.
            return(command);
        }