コード例 #1
0
        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);
            }
        }