widget: delete whole words with key modifier

Delete entire words with key modifier, ie "ctrl + delete".

Signed-off-by: Jack Mordaunt <jackmordaunt@gmail.com>
This commit is contained in:
Jack Mordaunt
2020-09-17 15:48:32 +08:00
committed by Elias Naur
parent d27d1a989e
commit ef7b3e75f4
2 changed files with 112 additions and 2 deletions
+55
View File
@@ -92,6 +92,7 @@ const (
moveEnd
moveCoord
moveWord
deleteWord
moveLast // Mark end; never generated.
)
@@ -143,6 +144,8 @@ func TestEditorCaretConsistency(t *testing.T) {
e.moveCoord(image.Pt(int(x), int(y)))
case moveWord:
e.moveWord(int(distance))
case deleteWord:
e.deleteWord(int(distance))
default:
return false
}
@@ -203,6 +206,58 @@ func TestEditorMoveWord(t *testing.T) {
}
}
}
func TestEditorDeleteWord(t *testing.T) {
type Test struct {
Text string
Start int
Delete int
Want int
Result string
}
tests := []Test{
{"", 0, 0, 0, ""},
{"", 0, -1, 0, ""},
{"", 0, 1, 0, ""},
{"hello", 0, -1, 0, "hello"},
{"hello", 0, 1, 0, ""},
{"hello world", 3, 1, 3, "hel world"},
{"hello world", 3, -1, 0, "lo world"},
{"hello world", 8, -1, 6, "hello rld"},
{"hello world", 8, 1, 8, "hello wo"},
{"hello world", 3, 1, 3, "hel world"},
{"hello world", 3, 2, 3, "helworld"},
{"hello world", 8, 1, 8, "hello "},
{"hello world", 8, -1, 5, "hello world"},
{"hello brave new world", 0, 3, 0, " new world"},
}
setup := func(t string) *Editor {
e := new(Editor)
gtx := layout.Context{
Ops: new(op.Ops),
Constraints: layout.Exact(image.Pt(100, 100)),
}
cache := text.NewCache(gofont.Collection())
fontSize := unit.Px(10)
font := text.Font{}
e.SetText(t)
e.Layout(gtx, cache, font, fontSize)
return e
}
for ii, tt := range tests {
e := setup(tt.Text)
e.Move(tt.Start)
e.deleteWord(tt.Delete)
if e.rr.caret != tt.Want {
t.Fatalf("[%d] deleteWord: bad caret position: got %d, want %d", ii, e.rr.caret, tt.Want)
}
if e.Text() != tt.Result {
t.Fatalf("[%d] deleteWord: invalid result: got %q, want %q", ii, e.Text(), tt.Result)
}
}
}
func TestEditorNoLayout(t *testing.T) {
var e Editor
e.SetText("hi!\n")