newedit/editutil: In BasicHandler, only insert the character when the key is a character.

This commit is contained in:
Qi Xiao 2019-04-19 23:02:44 +01:00
parent f798ef1e98
commit 5adf29b15f
2 changed files with 17 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package editutil
import (
"unicode"
"unicode/utf8"
"github.com/elves/elvish/edit/tty"
@ -57,7 +58,7 @@ func BasicHandler(e tty.Event, st *types.State) types.HandlerAction {
_, skip := utf8.DecodeRuneInString(raw.Code[raw.Dot:])
raw.Dot += skip
default:
if k.Mod == 0 {
if IsChar(k) {
s := string(k.Rune)
raw.Code = raw.Code[:raw.Dot] + s + raw.Code[raw.Dot:]
raw.Dot += len(s)
@ -67,3 +68,8 @@ func BasicHandler(e tty.Event, st *types.State) types.HandlerAction {
}
return types.NoAction
}
// IsChar returns whether the given key is not a character insertion.
func IsChar(k ui.Key) bool {
return k.Mod == 0 && k.Rune > 0 && unicode.IsGraphic(k.Rune)
}

View File

@ -8,6 +8,7 @@ import (
"github.com/elves/elvish/edit/tty"
"github.com/elves/elvish/edit/ui"
"github.com/elves/elvish/newedit/types"
"github.com/elves/elvish/tt"
)
var basicHandlerKeyEventsTests = []struct {
@ -85,3 +86,12 @@ func TestBasicHandler_IgnoresOtherEvents(t *testing.T) {
})
}
}
func TestIsChar(t *testing.T) {
tt.Test(t, tt.Fn("IsChar", IsChar), tt.Table{
Args(ui.K(ui.Up)).Rets(false),
Args(ui.K('A', ui.Ctrl)).Rets(false),
Args(ui.K('a')).Rets(true),
Args(ui.K('好')).Rets(true),
})
}