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);
        }
Exemplo n.º 2
0
        public JoinNextParagraphCommand(
            ITextEditingCommandController <TContext> controller,
            LinePosition line)
            : base(true, false)
        {
            // Joining a paragraph consists of inserting the text of the current
            // paragraph into the previous one with a space and then moving the
            // cursor to the end of the original first paragraph (and space).

            // Insert the text from the line into the prvious line.
            var joinedLine = new LinePosition(line);

            IInsertTextFromTextRangeCommand <TContext> insertCommand =
                controller.CreateInsertTextFromTextRangeCommand(
                    new TextPosition(joinedLine, CharacterPosition.End),
                    new SingleLineTextRange(
                        (int)line + 1, CharacterPosition.Begin, CharacterPosition.End));

            // Finally, delete the current line since we merged it.
            IDeleteLineCommand <TContext> deleteCommand =
                controller.CreateDeleteLineCommand((int)line + 1);

            // Add the commands into the composite and indicate that the whitespace
            // command controls where the text position will end up.
            Commands.Add(insertCommand);
            Commands.Add(deleteCommand);
        }
Exemplo n.º 3
0
        public JoinPreviousParagraphCommand(
            ITextEditingCommandController <TContext> controller,
            LinePosition line)
            : base(true, false)
        {
            // Establish our code contracts.
            if (line.Index <= 0)
            {
                throw new InvalidOperationException(
                          "Cannot join the paragraph on the first line.");
            }

            // Joining a paragraph consists of inserting the text of the current
            // paragraph into the previous one with a space and then moving the
            // cursor to the end of the original first paragraph (and space).

            // Insert the text from the line into the prvious line.
            var joinedLine = new LinePosition((int)line - 1);

            IInsertTextFromTextRangeCommand <TContext> insertCommand =
                controller.CreateInsertTextFromTextRangeCommand(
                    new TextPosition(joinedLine, CharacterPosition.End),
                    new SingleLineTextRange(
                        line, CharacterPosition.Begin, CharacterPosition.End));

            insertCommand.UpdateTextPosition = DoTypes.All;

            // Finally, delete the current line since we merged it.
            IDeleteLineCommand <TContext> deleteCommand =
                controller.CreateDeleteLineCommand(line);

            deleteCommand.UpdateTextPosition = DoTypes.None;

            // Add the commands into the composite and indicate that the whitespace
            // command controls where the text position will end up.
            Commands.Add(insertCommand);
            Commands.Add(deleteCommand);
        }
Exemplo n.º 4
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);
        }