Skip to content

Commit e1086a4

Browse files
authored
Fixes #4112. WordForward and WordBackward are not consistent with identical RuneType (#4131)
* Move parallelizable to new file * Add UseSameRuneTypeForWords property * Add SelectWordOnlyOnDoubleClick property and ProcessDoubleClickSelection method * Change IsSameRuneType method to also handle equivalent rune types * Fix WordBackward and WordForward to support properly handle rune types * Fix unit test to deal properly with the new roles of rune types * Add new unit tests * Remove duplicated unit test * Add UseSameRuneTypeForWords and SelectWordOnlyOnDoubleClick handling into Editor scenario
1 parent b59ac3b commit e1086a4

File tree

8 files changed

+3179
-2641
lines changed

8 files changed

+3179
-2641
lines changed

Examples/UICatalog/Scenarios/Editor.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ public override void Main ()
201201
CreateAutocomplete (),
202202
CreateAllowsTabChecked (),
203203
CreateReadOnlyChecked (),
204+
CreateUseSameRuneTypeForWords (),
205+
CreateSelectWordOnlyOnDoubleClick (),
204206
new MenuItem (
205207
"Colors",
206208
"",
@@ -776,6 +778,26 @@ private MenuItem [] CreateKeepChecked ()
776778
return new [] { item };
777779
}
778780

781+
private MenuItem CreateSelectWordOnlyOnDoubleClick ()
782+
{
783+
var item = new MenuItem { Title = "SelectWordOnlyOnDoubleClick" };
784+
item.CheckType |= MenuItemCheckStyle.Checked;
785+
item.Checked = _textView.SelectWordOnlyOnDoubleClick;
786+
item.Action += () => _textView.SelectWordOnlyOnDoubleClick = (bool)(item.Checked = !item.Checked);
787+
788+
return item;
789+
}
790+
791+
private MenuItem CreateUseSameRuneTypeForWords ()
792+
{
793+
var item = new MenuItem { Title = "UseSameRuneTypeForWords" };
794+
item.CheckType |= MenuItemCheckStyle.Checked;
795+
item.Checked = _textView.UseSameRuneTypeForWords;
796+
item.Action += () => _textView.UseSameRuneTypeForWords = (bool)(item.Checked = !item.Checked);
797+
798+
return item;
799+
}
800+
779801
private MenuItem CreateReadOnlyChecked ()
780802
{
781803
var item = new MenuItem { Title = "Read Only" };

Terminal.Gui/Views/TextInput/TextField.cs

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,19 @@ public string SelectedText
570570
/// </summary>
571571
public bool Used { get; set; }
572572

573+
/// <summary>
574+
/// Gets or sets whether the word forward and word backward navigation should use the same or equivalent rune type.
575+
/// Default is <c>false</c> meaning using equivalent rune type.
576+
/// </summary>
577+
public bool UseSameRuneTypeForWords { get; set; }
578+
579+
/// <summary>
580+
/// Gets or sets whether the word navigation should select only the word itself without spaces around it or with the
581+
/// spaces at right.
582+
/// Default is <c>false</c> meaning that the spaces at right are included in the selection.
583+
/// </summary>
584+
public bool SelectWordOnlyOnDoubleClick { get; set; }
585+
573586
/// <summary>Clear the selected text.</summary>
574587
public void ClearAllSelection ()
575588
{
@@ -754,7 +767,7 @@ public void InsertText (string toAdd, bool useOldCursorPos = true)
754767
public virtual void KillWordBackwards ()
755768
{
756769
ClearAllSelection ();
757-
(int col, int row)? newPos = GetModel ().WordBackward (_cursorPosition, 0);
770+
(int col, int row)? newPos = GetModel ().WordBackward (_cursorPosition, 0, UseSameRuneTypeForWords);
758771

759772
if (newPos is null)
760773
{
@@ -777,7 +790,7 @@ public virtual void KillWordBackwards ()
777790
public virtual void KillWordForwards ()
778791
{
779792
ClearAllSelection ();
780-
(int col, int row)? newPos = GetModel ().WordForward (_cursorPosition, 0);
793+
(int col, int row)? newPos = GetModel ().WordForward (_cursorPosition, 0, UseSameRuneTypeForWords);
781794

782795
if (newPos is null)
783796
{
@@ -857,43 +870,15 @@ protected override bool OnMouseEvent (MouseEventArgs ev)
857870
{
858871
EnsureHasFocus ();
859872
int x = PositionCursor (ev);
860-
int sbw = x;
873+
(int startCol, int col, int row)? newPos = GetModel ().ProcessDoubleClickSelection (x, x, 0, UseSameRuneTypeForWords, SelectWordOnlyOnDoubleClick);
861874

862-
if (x == _text.Count
863-
|| (x > 0 && (char)_text [x - 1].Value != ' ')
864-
|| (x > 0 && (char)_text [x].Value == ' '))
865-
{
866-
(int col, int row)? newPosBw = GetModel ().WordBackward (x, 0);
867-
868-
if (newPosBw is null)
869-
{
870-
return true;
871-
}
872-
873-
sbw = newPosBw.Value.col;
874-
}
875-
876-
if (sbw != -1)
877-
{
878-
x = sbw;
879-
PositionCursor (x);
880-
}
881-
882-
(int col, int row)? newPosFw = GetModel ().WordForward (x, 0);
883-
884-
if (newPosFw is null)
875+
if (newPos is null)
885876
{
886877
return true;
887878
}
888879

889-
ClearAllSelection ();
890-
891-
if (newPosFw.Value.col != -1 && sbw != -1)
892-
{
893-
_cursorPosition = newPosFw.Value.col;
894-
}
895-
896-
PrepareSelection (sbw, newPosFw.Value.col - sbw);
880+
SelectedStart = newPos.Value.startCol;
881+
CursorPosition = newPos.Value.col;
897882
}
898883
else if (ev.Flags == MouseFlags.Button1TripleClicked)
899884
{
@@ -1502,7 +1487,7 @@ private void MoveRightExtend ()
15021487
private void MoveWordLeft ()
15031488
{
15041489
ClearAllSelection ();
1505-
(int col, int row)? newPos = GetModel ().WordBackward (_cursorPosition, 0);
1490+
(int col, int row)? newPos = GetModel ().WordBackward (_cursorPosition, 0, UseSameRuneTypeForWords);
15061491

15071492
if (newPos is null)
15081493
{
@@ -1528,7 +1513,7 @@ private void MoveWordLeftExtend ()
15281513

15291514
if (x > 0)
15301515
{
1531-
(int col, int row)? newPos = GetModel ().WordBackward (x, 0);
1516+
(int col, int row)? newPos = GetModel ().WordBackward (x, 0, UseSameRuneTypeForWords);
15321517

15331518
if (newPos is null)
15341519
{
@@ -1548,7 +1533,7 @@ private void MoveWordLeftExtend ()
15481533
private void MoveWordRight ()
15491534
{
15501535
ClearAllSelection ();
1551-
(int col, int row)? newPos = GetModel ().WordForward (_cursorPosition, 0);
1536+
(int col, int row)? newPos = GetModel ().WordForward (_cursorPosition, 0, UseSameRuneTypeForWords);
15521537

15531538
if (newPos is null)
15541539
{
@@ -1568,7 +1553,7 @@ private void MoveWordRightExtend ()
15681553
if (_cursorPosition < _text.Count)
15691554
{
15701555
int x = _start > -1 && _start > _cursorPosition ? _start : _cursorPosition;
1571-
(int col, int row)? newPos = GetModel ().WordForward (x, 0);
1556+
(int col, int row)? newPos = GetModel ().WordForward (x, 0, UseSameRuneTypeForWords);
15721557

15731558
if (newPos is null)
15741559
{

0 commit comments

Comments
 (0)