From e3b62cff38a8ed0a460a26d73951710aea348bbd Mon Sep 17 00:00:00 2001 From: BDisp Date: Thu, 24 Feb 2022 13:49:05 +0000 Subject: [PATCH] Implementing undo/redo to back tab key. (#1608) --- Terminal.Gui/Views/TextView.cs | 6 +++ UnitTests/TextViewTests.cs | 85 ++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/Terminal.Gui/Views/TextView.cs b/Terminal.Gui/Views/TextView.cs index 6427d41d8..ff9770fec 100644 --- a/Terminal.Gui/Views/TextView.cs +++ b/Terminal.Gui/Views/TextView.cs @@ -2900,8 +2900,14 @@ namespace Terminal.Gui { if (currentColumn > 0) { var currentLine = GetCurrentLine (); if (currentLine.Count > 0 && currentLine [currentColumn - 1] == '\t') { + + historyText.Add (new List> () { new List (currentLine) }, CursorPosition); + currentLine.RemoveAt (currentColumn - 1); currentColumn--; + + historyText.Add (new List> () { new List (GetCurrentLine ()) }, CursorPosition, + HistoryText.LineStatus.Replaced); } } DoNeededAction (); diff --git a/UnitTests/TextViewTests.cs b/UnitTests/TextViewTests.cs index 394518371..858f7e3b1 100644 --- a/UnitTests/TextViewTests.cs +++ b/UnitTests/TextViewTests.cs @@ -5575,6 +5575,91 @@ line. Assert.Equal (new Point (1, 1), tv.CursorPosition); } + [Fact] + public void HistoryText_Undo_Redo_Multiline_Simples_Tab_BackTab () + { + var text = "First line.\nSecond line.\nThird line."; + var tv = new TextView () { Width = 80, Height = 5, Text = text }; + + Assert.True (tv.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()))); + Assert.Equal ($"\tFirst line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.", tv.Text); + Assert.Equal (3, tv.Lines); + Assert.Equal (new Point (1, 0), tv.CursorPosition); + + Assert.True (tv.ProcessKey (new KeyEvent (Key.BackTab, new KeyModifiers ()))); + Assert.Equal ($"First line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.", tv.Text); + Assert.Equal (3, tv.Lines); + Assert.Equal (new Point (0, 0), tv.CursorPosition); + + // Undo + Assert.True (tv.ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ()))); + Assert.Equal ($"\tFirst line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.", tv.Text); + Assert.Equal (3, tv.Lines); + Assert.Equal (new Point (1, 0), tv.CursorPosition); + Assert.True (tv.IsDirty); + + Assert.True (tv.ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ()))); + Assert.Equal ($"First line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.", tv.Text); + Assert.Equal (3, tv.Lines); + Assert.Equal (new Point (0, 0), tv.CursorPosition); + Assert.False (tv.IsDirty); + + // Redo + Assert.True (tv.ProcessKey (new KeyEvent (Key.R | Key.CtrlMask, new KeyModifiers ()))); + Assert.Equal ($"\tFirst line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.", tv.Text); + Assert.Equal (3, tv.Lines); + Assert.Equal (new Point (1, 0), tv.CursorPosition); + + Assert.True (tv.ProcessKey (new KeyEvent (Key.R | Key.CtrlMask, new KeyModifiers ()))); + Assert.Equal ($"First line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.", tv.Text); + Assert.Equal (3, tv.Lines); + Assert.Equal (new Point (0, 0), tv.CursorPosition); + } + + [Fact] + public void HistoryText_Undo_Redo_Multiline_Selected_Tab_BackTab () + { + var text = "First line.\nSecond line.\nThird line."; + var tv = new TextView () { Width = 80, Height = 5, Text = text }; + + tv.SelectionStartColumn = 6; + tv.CursorPosition = new Point (6, 2); + + Assert.True (tv.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()))); + Assert.Equal ("First \tline.", tv.Text); + Assert.Equal (1, tv.Lines); + Assert.Equal (new Point (7, 0), tv.CursorPosition); + + Assert.True (tv.ProcessKey (new KeyEvent (Key.BackTab, new KeyModifiers ()))); + Assert.Equal ("First line.", tv.Text); + Assert.Equal (1, tv.Lines); + Assert.Equal (new Point (6, 0), tv.CursorPosition); + + // Undo + Assert.True (tv.ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ()))); + Assert.Equal ("First \tline.", tv.Text); + Assert.Equal (1, tv.Lines); + Assert.Equal (new Point (7, 0), tv.CursorPosition); + Assert.True (tv.IsDirty); + + Assert.True (tv.ProcessKey (new KeyEvent (Key.Z | Key.CtrlMask, new KeyModifiers ()))); + Assert.Equal ($"First line.{Environment.NewLine}Second line.{Environment.NewLine}Third line.", tv.Text); + Assert.Equal (3, tv.Lines); + Assert.Equal (new Point (6, 2), tv.CursorPosition); + Assert.False (tv.IsDirty); + + // Redo + Assert.True (tv.ProcessKey (new KeyEvent (Key.R | Key.CtrlMask, new KeyModifiers ()))); + Assert.Equal ("First \tline.", tv.Text); + Assert.Equal (1, tv.Lines); + Assert.Equal (new Point (7, 0), tv.CursorPosition); + + Assert.True (tv.ProcessKey (new KeyEvent (Key.R | Key.CtrlMask, new KeyModifiers ()))); + Assert.Equal ("First line.", tv.Text); + Assert.Equal (1, tv.Lines); + Assert.Equal (new Point (6, 0), tv.CursorPosition); + } + [Fact] public void HistoryText_ClearHistoryChanges () {