/// <summary> /// Executes command. /// </summary> /// <param name="document">Document insert to.</param> public void Execute(ITextEditorDocument document) { if (this.text.Count == 0 || document == null) { return; } this.line = document.LineNumberByIndex(this.caretIndex); this.position = document.CaretPositionInLineByIndex(this.caretIndex); this.changedDocument = document; if (this.line == -1) { this.line = 0; document.AddLine(string.Empty); } string paragraph = document.AllLines[this.line]; string partToMove = paragraph.Substring(this.position); if (paragraph.Length > this.position) { paragraph = paragraph.Remove(this.position); } document.ChangeLineAtIndex(this.line, paragraph.Insert(this.position, this.text.First())); List<string> newLines = new List<string>(this.text); newLines.RemoveAt(0); for (int i = 0; i < newLines.Count; i++) { document.InsertLineAtIndex(this.line + i + 1, newLines[i]); } string lastLine = document.AllLines[this.line + this.text.Count - 1]; document.ChangeLineAtIndex(this.line + this.text.Count - 1, lastLine + partToMove); }
public void TextEditorFileManager_Save_Open() { document = fileManager.OpenFileUsingEncoding(@"Resources\DocumentExample.txt", Encoding.Default); Assert.IsNotNull(this.document, "FileManager couldn't open document"); document.AddLine("Hello"); fileManager.SaveDocument(document); document = fileManager.OpenFileUsingEncoding(@"Resources\DocumentExample.txt", Encoding.Default); Assert.IsTrue(document.AllLines[document.AllLines.Count - 1] == "Hello", "Changes to document haven't been saved by FileManager"); document.RemoveLineAtIndex(document.AllLines.Count - 1); fileManager.SaveDocument(document); }
/// <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); this.changedDocument = document; if (this.line == -1) { this.line = 0; document.AddLine(string.Empty); } string paragraph = document.AllLines[this.line]; document.ChangeLineAtIndex(this.line, paragraph.Insert(this.position, this.text)); }