feat(shortcuts): add a leader key tree and palette remapping
Expose the multi-key mappings starting at the configured leader key in a which-key panel, so that continuations are discoverable as soon as the prefix is pressed. Both the panel and the command palette read the same structured mapping data, and the palette can now write a new mapping for the selected action.
This commit is contained in:
10 files changed
+418
-13
No files matched your search
@@ -5,6 +5,8 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kovidgoyal/kitty/tools/tui/loop"
|
||||
)
|
||||
|
||||
// testBinding creates a Binding where Action, ActionDisplay, and Definition all
|
||||
@@ -120,6 +122,48 @@ func newTestHandler() *Handler {
|
||||
build()
|
||||
}
|
||||
|
||||
func TestLeaderPanel(t *testing.T) {
|
||||
h := &Handler{}
|
||||
data := []byte(`{"leader":{"prefix":"ctrl+space","title":"Leader keys","bindings":[{"key":"n","action":"new_os_window","action_display":"new_os_window","definition":"new_os_window"},{"key":"shift+w","action":"new_window","action_display":"new_window","definition":"new_window"}]}}`)
|
||||
if err := h.parseData(data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if h.leader == nil {
|
||||
t.Fatal("expected leader mode to be active")
|
||||
}
|
||||
if len(h.all_items) != 2 {
|
||||
t.Fatalf("expected 2 leader entries, got %d", len(h.all_items))
|
||||
}
|
||||
if b := h.matchLeaderBinding(&loop.KeyEvent{Type: loop.PRESS, Key: "n"}); b == nil || b.Definition != "new_os_window" {
|
||||
t.Fatalf("expected n to run new_os_window, got %+v", b)
|
||||
}
|
||||
if b := h.matchLeaderBinding(&loop.KeyEvent{Type: loop.PRESS, Key: "w", Mods: loop.SHIFT, ShiftedKey: "W"}); b == nil || b.Definition != "new_window" {
|
||||
t.Fatalf("expected shift+w to run new_window, got %+v", b)
|
||||
}
|
||||
if b := h.matchLeaderBinding(&loop.KeyEvent{Type: loop.PRESS, Key: "x"}); b != nil {
|
||||
t.Fatalf("expected x to match nothing, got %+v", b)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeySpecForRemap(t *testing.T) {
|
||||
cases := []struct {
|
||||
ev loop.KeyEvent
|
||||
expected string
|
||||
}{
|
||||
{loop.KeyEvent{Type: loop.PRESS, Key: "a"}, "a"},
|
||||
{loop.KeyEvent{Type: loop.PRESS, Key: "a", Mods: loop.SHIFT}, "shift+a"},
|
||||
{loop.KeyEvent{Type: loop.PRESS, Key: "a", Mods: loop.CTRL | loop.SHIFT}, "ctrl+shift+a"},
|
||||
{loop.KeyEvent{Type: loop.PRESS, Key: "a", Mods: loop.CTRL | loop.CAPS_LOCK}, "ctrl+a"},
|
||||
{loop.KeyEvent{Type: loop.PRESS, Key: "", Text: "é"}, "é"},
|
||||
{loop.KeyEvent{Type: loop.PRESS, Key: "", Text: ""}, ""},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := keySpecForRemap(&c.ev); got != c.expected {
|
||||
t.Fatalf("keySpecForRemap(%+v) = %q, expected %q", c.ev, got, c.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlattenAllBindings(t *testing.T) {
|
||||
h := newTestHandler()
|
||||
// 5 default mode + 2 mw mode + 2 mouse = 9
|
||||
|
||||
Reference in new issue
Block a user