public void SingleLineTestUndoRedoCommand()
        {
            // Arrange
            ProjectBlockCollection blocks;
            BlockCommandSupervisor commands;
            BlockTypeSupervisor blockTypes;
            BlockCommandContext context;
            SetupMultilineTest(
                out context, out blocks, out blockTypes, out commands, lineCount: 1);

            var command = new DeleteBlockCommand(blocks[0].BlockKey);
            commands.Do(command, context);
            commands.Undo(context);

            // Act
            commands.Redo(context);

            // Assert
            Assert.AreEqual(1, blocks.Count);
            Assert.AreEqual(new BlockPosition(blocks[0], 0), commands.LastPosition);

            const int index = 0;
            Assert.AreEqual("", blocks[index].Text);
            Assert.AreEqual(blockTypes.Paragraph, blocks[index].BlockType);
        }
        public DeleteMultilineTextCommand(
            BlockCollection blocks,
            BlockPosition startPosition,
            BlockPosition stopPosition)
            : base(true, false)
        {
            // Save the variables so we can set the position.
            this.startPosition = startPosition;
            this.stopPosition  = stopPosition;

            // If we are in the same line, we have a modified command.
            if (startPosition.BlockKey == stopPosition.BlockKey)
            {
                // We have a single-line delete.
                var singleDeleteCommand = new DeleteTextCommand(
                    startPosition, stopPosition.TextIndex);

                Commands.Add(singleDeleteCommand);
                return;
            }

            // Start by removing the text to the right of the first line.
            var deleteTextCommand = new DeleteTextCommand(
                startPosition, CharacterPosition.End);

            Commands.Add(deleteTextCommand);

            // Copy the final line text, from beginning to position, into the first
            // line. This will merge the top and bottom lines.
            var insertTextCommand = new InsertTextFromBlock(
                startPosition,
                stopPosition.BlockKey,
                stopPosition.TextIndex,
                CharacterPosition.End);

            Commands.Add(insertTextCommand);

            // Once we have a merged line, then just delete the remaining lines.
            // Figure out line ranges we'll be deleting text from.
            removedBlocks = new List <Block>();

            Block startBlock = blocks[startPosition.BlockKey];
            Block stopBlock  = blocks[stopPosition.BlockKey];

            int startIndex = blocks.IndexOf(startBlock);
            int stopIndex  = blocks.IndexOf(stopBlock);

            // Go through the remaining lines.
            for (int i = startIndex + 1;
                 i <= stopIndex;
                 i++)
            {
                // Get the block we're removing and add it to the list.
                Block removeBlock = blocks[i];

                removedBlocks.Add(removeBlock);

                // Add in a command to remove the block.
                var deleteBlockCommand = new DeleteBlockCommand(removeBlock.BlockKey);

                Commands.Add(deleteBlockCommand);
            }
        }
        public override LineBufferOperationResults DeleteLines(
			int lineIndex,
			int count)
        {
            using (project.Blocks.AcquireLock(RequestLock.Write))
            {
                Block block = project.Blocks[lineIndex];
                var command = new DeleteBlockCommand(block.BlockKey);
                var context = new BlockCommandContext(project);
                project.Commands.Do(command, context);

                return GetOperationResults();
            }
        }
        public void TestUndoRedoUndoCommand()
        {
            // Arrange
            ProjectBlockCollection blocks;
            BlockCommandSupervisor commands;
            BlockTypeSupervisor blockTypes;
            BlockCommandContext context;
            SetupMultilineTest(out context, out blocks, out blockTypes, out commands);

            var command = new DeleteBlockCommand(blocks[0].BlockKey);
            commands.Do(command, context);
            commands.Undo(context);
            commands.Redo(context);

            // Act
            commands.Undo(context);

            // Assert
            Assert.AreEqual(4, blocks.Count);
            Assert.AreEqual(
                new BlockPosition(blocks[0], "Line 1".Length), commands.LastPosition);

            int index = 0;
            Assert.AreEqual("Line 1", blocks[index].Text);
            Assert.AreEqual(blockTypes.Chapter, blocks[index].BlockType);

            index++;
            Assert.AreEqual("Line 2", blocks[index].Text);
            Assert.AreEqual(blockTypes.Scene, blocks[index].BlockType);

            index++;
            Assert.AreEqual("Line 3", blocks[index].Text);
            Assert.AreEqual(blockTypes.Scene, blocks[index].BlockType);

            index++;
            Assert.AreEqual("Line 4", blocks[index].Text);
            Assert.AreEqual(blockTypes.Scene, blocks[index].BlockType);
        }