浏览代码

Implementing undo/redo to back tab key. (#1608)

BDisp 3 年之前
父节点
当前提交
e3b62cff38
共有 2 个文件被更改,包括 91 次插入0 次删除
  1. 6 0
      Terminal.Gui/Views/TextView.cs
  2. 85 0
      UnitTests/TextViewTests.cs

+ 6 - 0
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<List<Rune>> () { new List<Rune> (currentLine) }, CursorPosition);
+
 					currentLine.RemoveAt (currentColumn - 1);
 					currentColumn--;
+
+					historyText.Add (new List<List<Rune>> () { new List<Rune> (GetCurrentLine ()) }, CursorPosition,
+						HistoryText.LineStatus.Replaced);
 				}
 			}
 			DoNeededAction ();

+ 85 - 0
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 ()
 		{