public void NewLineCommand_AddNewLine()
        {
            NewLineCommand command = new NewLineCommand(0, 0);
            command.Execute(this.document);
            Assert.AreEqual(5, this.document.Lines.Count);
            Assert.AreEqual("", this.document.Lines[0]);
            Assert.AreEqual(this.initialDocument.Lines[0], this.document.Lines[1]);
            command.Undo();
            Assert.AreEqual(this.initialDocument.Text, this.document.Text);

            command = new NewLineCommand(0, 2);
            command.Execute(this.document);
            Assert.AreEqual(5, this.document.Lines.Count);
            Assert.AreEqual("he", this.document.Lines[0]);
            Assert.AreEqual("llo", this.document.Lines[1]);
            command.Undo();
            Assert.AreEqual(this.initialDocument.Text, this.document.Text);

            command = new NewLineCommand(0, 5);
            command.Execute(this.document);
            Assert.AreEqual(5, this.document.Lines.Count);
            Assert.AreEqual("hello", this.document.Lines[0]);
            Assert.AreEqual("", this.document.Lines[1]);
            command.Undo();
            Assert.AreEqual(this.initialDocument.Text, this.document.Text);

            command = new NewLineCommand(0, 6);
            command.Execute(this.document);
            Assert.AreEqual(5, this.document.Lines.Count);
            Assert.AreEqual("", this.document.Lines[1]);
            Assert.AreEqual("world", this.document.Lines[2]);
            command.Undo();
            Assert.AreEqual(this.initialDocument.Text, this.document.Text);

            command = new NewLineCommand(0, 11);
            command.Execute(this.document);
            Assert.AreEqual(5, this.document.Lines.Count);
            Assert.AreEqual("", this.document.Lines[2]);
            Assert.AreEqual("", this.document.Lines[3]);
            command.Undo();
            Assert.AreEqual(this.initialDocument.Text, this.document.Text);

            command = new NewLineCommand(0, this.document.Text.Length);
            command.Execute(this.document);
            Assert.AreEqual(5, this.document.Lines.Count);
            Assert.AreEqual("123", this.document.Lines[3]);
            Assert.AreEqual("", this.document.Lines[4]);
            command.Undo();
            Assert.AreEqual(this.initialDocument.Text, this.document.Text);
        }
        /// <summary>
        /// Handles KeyDown.
        /// </summary>
        /// <param name="e">Key event arguments.</param>
        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            if (e == null)
            {
                return;
            }
            else if (this.Document == null)
            {
                e.Handled = true;
                return;
            }

            char pressedChar = e.Key.GetChar();

            // CTRL + Z
            if (e.Key == Key.Z && Keyboard.Modifiers == ModifierKeys.Control)
            {
                this.lastCarretIndex = this.CaretIndex - this.document.CaretPositionInLineByIndex(this.CaretIndex);
                this.commandManager.Undo();
                this.UpdateUi();
                e.Handled = true;
            }

            // Backspace
            else if (e.Key == Key.Back)
            {
                RemoveRangeCommand removeCommand;
                if (this.SelectionLength > 0)
                {
                    this.lastCarretIndex = this.SelectionStart;
                    removeCommand = new RemoveRangeCommand(this.SelectionStart, this.SelectionLength);
                }
                else
                {
                    if (this.CaretIndex == 0)
                    {
                        return;
                    }

                    this.lastCarretIndex = this.CaretIndex - 1;
                    removeCommand = new RemoveRangeCommand(this.CaretIndex - 1, 1);
                }

                this.RunCommand(removeCommand);
                e.Handled = true;
            }

            // Tab
            else if (e.Key == Key.Tab)
            {
                this.lastCarretIndex = this.CaretIndex + 2;
                InsertStringCommand insertCommand = new InsertStringCommand("  ", this.CaretIndex);
                this.RunCommand(insertCommand);

                e.Handled = true;
            }

            // Enter
            else if (e.Key == Key.Return)
            {
                int line = this.document.LineNumberByIndex(this.CaretIndex);

                if (line == -1)
                {
                    line = 0;
                    this.document.AddLine(string.Empty);
                }

                string paragraph = this.document.AllLines[line];
                int spaceCount = 0;
                while (spaceCount < paragraph.Length && paragraph[spaceCount] == ' ')
                {
                    spaceCount++;
                }

                NewLineCommand newLineCommand = new NewLineCommand(spaceCount, this.CaretIndex);
                this.lastCarretIndex = this.CaretIndex + 1 + spaceCount;
                this.RunCommand(newLineCommand);
                e.Handled = true;
            }

            // Ctrl+V
            else if (e.Key == Key.V && Keyboard.Modifiers == ModifierKeys.Control)
            {
                string clipboardText = Clipboard.GetText();
                List<string> clipboardLines = clipboardText.Split('\n').Select(l => l.Replace("\r", string.Empty)).ToList();
                this.lastCarretIndex += clipboardText.Length;

                InsertLinesCommand command = new InsertLinesCommand(clipboardLines, this.CaretIndex);
                this.RunCommand(command);
            }

            // Some symbol
            else if ((!char.IsControl(pressedChar) && !pressedChar.Equals(' ')) || e.Key == Key.Space)
            {
                this.lastCarretIndex = this.CaretIndex + 1;
                InsertStringCommand insertCommand = new InsertStringCommand(e.Key.GetChar().ToString(), this.CaretIndex);
                this.RunCommand(insertCommand);

                // Autocompletion analyzing
                string currentWord = this.document.GetWordByCaretIndex(this.CaretIndex);

                List<string> snippetsNames = SnippetLibrary.Names.Where(s => s.StartsWith(currentWord)).ToList();
                if (snippetsNames.Count > 0)
                {
                    this.OnLibraryWordEntered(
                        new LibraryWordEnteredEventArgs(snippetsNames, this.GetRectFromCharacterIndex(this.CaretIndex)));
                }

                e.Handled = true;
            }

            base.OnPreviewKeyDown(e);
        }
 public void NewLineCommand_AddNewLine_WrongCaretIndex()
 {
     NewLineCommand command = new NewLineCommand(0, this.document.Text.Length + 1);
     command.Execute(this.document);
 }