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:
BlackAngelTV committed 2026-11-13 08:03:03 +01:00
1 parent dc13716431
commit bfc97997a1
10 files changed
+418 -13

No files matched your search

+49
View File
@@ -279,3 +279,52 @@ class TestCommandPalette(BaseTest):
def test_unmapped_actions_sorted_order(self):
# Covered by test_collect_keys_bindings_sorted
pass
def test_leader_prefix_bindings(self):
from kittens.command_palette.main import collect_leader_data
from kitty.options.utils import parse_map, parse_shortcut
opts = self.set_options({'leader': 'ctrl+space'})
for spec in ('ctrl+space>n new_os_window', 'ctrl+space>w new_window', 'ctrl+space>n new_os_window'):
for kd in parse_map(spec):
kd = kd.resolve_and_copy(opts.kitty_mod)
opts.keyboard_modes[''].keymap.setdefault(kd.trigger, []).append(kd)
data = collect_leader_data(opts, parse_shortcut('ctrl+space'))
leader = data['leader']
self.ae(leader['prefix'], 'ctrl+space')
self.ae([b['key'] for b in leader['bindings']], ['n', 'w'], 'Duplicate continuations should collapse')
self.ae(leader['bindings'][0]['action'], 'new_os_window')
self.ae(leader['bindings'][1]['action'], 'new_window')
self.assertTrue(leader['bindings'][0]['help'], 'Bindings should carry their help text')
def test_leader_option(self):
from kitty.keys import leader_key_from_options
opts = self.set_options({'leader': 'none'})
self.assertIsNone(leader_key_from_options(opts))
self.assertIsNone(opts.leader_key)
opts = self.set_options({'leader': 'ctrl+space'})
self.assertIsNotNone(leader_key_from_options(opts))
self.assertIsNotNone(opts.leader_key)
def test_append_map_to_config(self):
import os
import tempfile
from kitty.config import append_map_to_config
with tempfile.TemporaryDirectory() as tdir:
path = os.path.join(tdir, 'kitty.conf')
self.ae(append_map_to_config('ctrl+u', 'new_os_window', path), path)
self.ae(append_map_to_config('ctrl+j', 'new_tab', path), path)
with open(path) as f:
self.ae(f.read().splitlines(), ['map ctrl+u new_os_window', 'map ctrl+j new_tab'])
# A config file without a trailing newline must not get a mangled line
with open(path, 'w') as f:
f.write('font_size 12')
append_map_to_config('ctrl+k', 'new_window', path)
with open(path) as f:
self.ae(f.read().splitlines(), ['font_size 12', 'map ctrl+k new_window'])