public void InsertLinesCommand_Insert() { List<string> lines = new List<string>() { "insert", "some", " ", "lines" }; InsertLinesCommand command = new InsertLinesCommand(lines, 9); command.Execute(this.document); List<string> expected = new List<string>() { "hello", "worinsert", "some", " ", "linesld", "", "123" }; string expectedString = string.Join("\n", expected); Assert.AreEqual(expectedString, this.document.Text); command.Undo(); Assert.AreEqual(this.initialDocument.Text, this.document.Text); command = new InsertLinesCommand(lines, 16); command.Execute(this.document); expected = new List<string>() { "hello", "world", "", "123insert", "some", " ", "lines" }; expectedString = string.Join("\n", expected); Assert.AreEqual(expectedString, this.document.Text); command.Undo(); Assert.AreEqual(this.initialDocument.Text, this.document.Text); command = new InsertLinesCommand(lines, 0); command.Execute(this.document); expected = new List<string>() { "insert", "some", " ", "lineshello", "world", "", "123" }; expectedString = string.Join("\n", expected); Assert.AreEqual(expectedString, this.document.Text); command.Undo(); Assert.AreEqual(this.initialDocument.Text, this.document.Text); }
/// <summary> /// Executes command. /// </summary> /// <param name="document">Document to run command.</param> public void Execute(ITextEditorDocument document) { if (document == null) { return; } this.line = document.LineNumberByIndex(this.caretIndex); this.position = document.CaretPositionInLineByIndex(this.caretIndex); string paragrapgh = document.AllLines[this.line]; int paragraphIndex = this.position; int length = 0; while (paragraphIndex < paragrapgh.Length && paragrapgh[paragraphIndex] == this.snippet.Name[length]) { paragraphIndex++; length++; } this.removeCommand = new RemoveRangeCommand(this.caretIndex, length); this.removeCommand.Execute(document); this.insertCommand = new InsertLinesCommand(this.snippet.Content, this.caretIndex); this.insertCommand.Execute(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); }