commit a7f7c5a

Alexey Yerin  ·  2021-04-26 10:18:01 +0000 UTC
parent 5e2bbb5
editor: add ^W delete word binding

It allows to delete the current word (any string of characters until a
space). Also, all spaces at the start are cleared to allow doing
something like:

    Hello world|
    Hello | <- the cursor is at a space
    | <- nothing left
3 files changed,  +40, -0
M app.go
M app.go
+6, -0
 1@@ -448,6 +448,12 @@ func (app *App) handleKeyEvent(ev *tcell.EventKey) {
 2 			app.typing()
 3 			app.updatePrompt()
 4 		}
 5+	case tcell.KeyCtrlW:
 6+		ok := app.win.InputDeleteWord()
 7+		if ok {
 8+			app.typing()
 9+			app.updatePrompt()
10+		}
11 	case tcell.KeyTab:
12 		ok := app.win.InputAutoComplete(1)
13 		if ok {
+30, -0
 1@@ -116,6 +116,36 @@ func (e *Editor) remRuneAt(idx int) {
 2 	e.text[e.lineIdx] = e.text[e.lineIdx][:len(e.text[e.lineIdx])-1]
 3 }
 4 
 5+func (e *Editor) RemWord() (ok bool) {
 6+	ok = 0 < e.cursorIdx
 7+	if !ok {
 8+		return
 9+	}
10+
11+	line := e.text[e.lineIdx]
12+
13+	// To allow doing something like this (| is the cursor):
14+	// Hello world|
15+	// Hello |
16+	// |
17+	for line[e.cursorIdx - 1] == ' ' {
18+		e.remRuneAt(e.cursorIdx - 1)
19+		e.Left()
20+	}
21+
22+	for i := e.cursorIdx - 1; i >= 0; i -= 1 {
23+		if line[i] == ' ' {
24+			break
25+		}
26+		e.remRuneAt(i)
27+		e.Left()
28+	}
29+
30+	e.autoCache = nil
31+	return
32+}
33+
34+
35 func (e *Editor) Flush() (content string) {
36 	content = string(e.text[e.lineIdx])
37 	if len(e.text[len(e.text)-1]) == 0 {
+4, -0
 1@@ -192,6 +192,10 @@ func (ui *UI) InputDelete() (ok bool) {
 2 	return ui.e.RemRuneForward()
 3 }
 4 
 5+func (ui *UI) InputDeleteWord() (ok bool) {
 6+	return ui.e.RemWord()
 7+}
 8+
 9 func (ui *UI) InputAutoComplete(offset int) (ok bool) {
10 	return ui.e.AutoComplete(offset)
11 }