From ff32bb064b38a0c95489f63a98a16147c95bc7ed Mon Sep 17 00:00:00 2001 From: Julius Kirsch Date: Fri, 22 Aug 2025 10:13:04 +0200 Subject: [PATCH] feat: delete lines, wip --- TextControlBox-TestApp/MainWindow.xaml.cs | 2 + .../Core/CoreTextControlBox.xaml.cs | 5 +++ TextControlBox/Core/Text/TextActionManager.cs | 39 ++++++++++++++++++- TextControlBox/Test/TextTests.cs | 36 ++++++++++++++++- TextControlBox/TextControlBox.cs | 12 ++++++ 5 files changed, 92 insertions(+), 2 deletions(-) diff --git a/TextControlBox-TestApp/MainWindow.xaml.cs b/TextControlBox-TestApp/MainWindow.xaml.cs index 98ea45f..d8a70e1 100644 --- a/TextControlBox-TestApp/MainWindow.xaml.cs +++ b/TextControlBox-TestApp/MainWindow.xaml.cs @@ -23,6 +23,8 @@ public MainWindow() textbox.NumberOfSpacesForTab = 4; textbox.Loaded += Textbox_Loaded; + textbox.DeleteLines(0, 5); + //ActionGrouping(); } diff --git a/TextControlBox/Core/CoreTextControlBox.xaml.cs b/TextControlBox/Core/CoreTextControlBox.xaml.cs index 78abb3a..9a98ecc 100644 --- a/TextControlBox/Core/CoreTextControlBox.xaml.cs +++ b/TextControlBox/Core/CoreTextControlBox.xaml.cs @@ -747,6 +747,11 @@ public bool DeleteLine(int line) return textActionManager.DeleteLine(line); } + public bool DeleteLines(int start, int count) + { + return textActionManager.DeleteLines(start, count); + } + public bool AddLine(int line, string text) { return textActionManager.AddLine(line, text); diff --git a/TextControlBox/Core/Text/TextActionManager.cs b/TextControlBox/Core/Text/TextActionManager.cs index 42d9f44..940e8e9 100644 --- a/TextControlBox/Core/Text/TextActionManager.cs +++ b/TextControlBox/Core/Text/TextActionManager.cs @@ -1,4 +1,5 @@ -using Microsoft.UI.Input; +using Collections.Pooled; +using Microsoft.UI.Input; using System; using System.Collections.Generic; using System.Diagnostics; @@ -508,6 +509,42 @@ public bool DeleteLine(int line) return true; } + public bool DeleteLines(int start, int count) + { + if (start < 0 || count <= 0 || start >= textManager.LinesCount) + return false; + + if (start + count > textManager.LinesCount) + count = textManager.LinesCount - start; + + if (longestLineManager.longestIndex >= start && longestLineManager.longestIndex < start + count) + longestLineManager.needsRecalculation = true; + + //whole text is selected: + if (textManager.LinesCount == count) + { + undoRedo.RecordUndoAction(() => + { + textManager.SetLineText(0, ""); + if (count > 1) + textManager.RemoveRange(start, count); + + }, start, start + count, 0); + } + else + { + undoRedo.RecordUndoAction(() => + { + textManager.RemoveRange(start, count); + }, start, start + count, 0); + } + + eventsManager.CallTextChanged(); + canvasUpdateManager.UpdateText(); + return true; + } + + public bool AddLine(int line, string text) { if (line > textManager.LinesCount || line < 0) diff --git a/TextControlBox/Test/TextTests.cs b/TextControlBox/Test/TextTests.cs index 5e612f2..f3cf5fa 100644 --- a/TextControlBox/Test/TextTests.cs +++ b/TextControlBox/Test/TextTests.cs @@ -55,6 +55,8 @@ public override Func[] GetAllTests() this.Test_33, this.Test_34, this.Test_35, + this.Test_36, + this.Test_37, ]; } @@ -570,7 +572,6 @@ public bool Test_33() return res.undo && res.redo && textBefore.Equals(coreTextbox.GetText()); } - public bool Test_34() { Debug.WriteLine("Undo Grouping"); @@ -623,5 +624,38 @@ public bool Test_35() return res.undo && res.redo && textBefore.Equals(coreTextbox.GetText()); } + + public bool Test_36() + { + Debug.WriteLine("Delete all Lines"); + + coreTextbox.SetText("Line1\nLine2\nLine3\nLine4\nLine5"); + string textBefore = coreTextbox.GetText(); + + coreTextbox.DeleteLines(0,5); + + var res = CheckUndoRedo(); + coreTextbox.Undo(); + + var count = coreTextbox.GetText().Split("\r\n"); + + return res.undo && res.redo && textBefore.Equals(coreTextbox.GetText()) && count.Length == 1; + } + public bool Test_37() + { + Debug.WriteLine("Delete 0-3 Lines"); + + coreTextbox.SetText("Line1\nLine2\nLine3\nLine4\nLine5"); + string textBefore = coreTextbox.GetText(); + + coreTextbox.DeleteLines(0, 3); + + var res = CheckUndoRedo(); + coreTextbox.Undo(); + + var count = coreTextbox.GetText().Split("\r\n"); + + return res.undo && res.redo && textBefore.Equals(coreTextbox.GetText()) && count.Length == 2; + } } } \ No newline at end of file diff --git a/TextControlBox/TextControlBox.cs b/TextControlBox/TextControlBox.cs index 041eb53..d7a56b3 100644 --- a/TextControlBox/TextControlBox.cs +++ b/TextControlBox/TextControlBox.cs @@ -345,6 +345,18 @@ public bool DeleteLine(int line) return coreTextBox.DeleteLine(line); } + /// + /// Deletes the lines specified by the zero based start from the textbox until the start + count line + /// This action will record an undo step + /// + /// The beginning of the line to delete + /// The amount of lines to delete + /// Returns true if the line was deleted successfully and false if not + public bool DeleteLines(int start, int count) + { + return coreTextBox.DeleteLines(start, count); + } + /// /// Adds a new line with the text specified /// This action will record an undo step