public void InsertStringCommand_InsertCharacter() { InsertStringCommand command = new InsertStringCommand("a", 0); command.Execute(this.document); Assert.AreEqual("ahello", this.document.Lines[0]); command.Undo(); Assert.AreEqual(this.initialDocument.Text, this.document.Text); command = new InsertStringCommand("a", 5); command.Execute(this.document); Assert.AreEqual("helloa", this.document.Lines[0]); command.Undo(); Assert.AreEqual(this.initialDocument.Text, this.document.Text); command = new InsertStringCommand("a", 6); command.Execute(this.document); Assert.AreEqual("aworld", this.document.Lines[1]); command.Undo(); Assert.AreEqual(this.initialDocument.Text, this.document.Text); command = new InsertStringCommand("a", 11); command.Execute(this.document); Assert.AreEqual("worlda", this.document.Lines[1]); command.Undo(); Assert.AreEqual(this.initialDocument.Text, this.document.Text); command = new InsertStringCommand("a", 12); command.Execute(this.document); Assert.AreEqual("a", this.document.Lines[2]); command.Undo(); Assert.AreEqual(this.initialDocument.Text, this.document.Text); command = new InsertStringCommand("a", 13); command.Execute(this.document); Assert.AreEqual("a123", this.document.Lines[3]); command.Undo(); Assert.AreEqual(this.initialDocument.Text, this.document.Text); command = new InsertStringCommand("a", 16); command.Execute(this.document); Assert.AreEqual("123a", this.document.Lines[3]); command.Undo(); Assert.AreEqual(this.initialDocument.Text, this.document.Text); }
public void InsertStringCommand_InsertCharacter_InalidCaretIndex() { InsertStringCommand command = new InsertStringCommand("a", 17); command.Execute(this.document); }
/// <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); }