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
@@ -318,6 +318,52 @@ def collect_keys_data(opts: Any) -> dict[str, Any]:
|
||||
}
|
||||
|
||||
|
||||
def collect_leader_data(opts: Any, prefix: Any, definitions: Any = None) -> dict[str, Any]:
|
||||
"""Build the data for the leader/which-key panel from sequence mappings.
|
||||
|
||||
Only the first key after the leader is listed: that is the key the user has
|
||||
to press next, matching how which-key panels present prefix trees. When the
|
||||
mapping needs more keys, the full definition is still shown in the action
|
||||
column and selecting it runs the action directly.
|
||||
"""
|
||||
from kitty.types import Shortcut
|
||||
|
||||
action_to_group, action_to_help, action_to_long_help = build_action_lookups()
|
||||
if definitions is None:
|
||||
definitions = []
|
||||
for defns in opts.keyboard_modes[''].keymap.values():
|
||||
for d in defns:
|
||||
if d.is_sequence and d.trigger == prefix:
|
||||
definitions.append(d)
|
||||
|
||||
bindings: list[dict[str, str]] = []
|
||||
seen: set[str] = set()
|
||||
for d in definitions:
|
||||
if not d.is_sequence or not d.rest:
|
||||
continue
|
||||
key_repr = Shortcut((d.rest[0],)).human_repr(opts.kitty_mod)
|
||||
if key_repr in seen:
|
||||
continue
|
||||
seen.add(key_repr)
|
||||
action_name, category, alias = classify_action(d.definition, opts.alias_map, action_to_group)
|
||||
entry: dict[str, str] = {
|
||||
'key': key_repr,
|
||||
'action': action_name,
|
||||
'action_display': d.human_repr(),
|
||||
'definition': d.definition,
|
||||
'help': action_to_help.get(action_name, ''),
|
||||
'long_help': action_to_long_help.get(action_name, ''),
|
||||
'category': category,
|
||||
}
|
||||
if alias:
|
||||
entry['alias'] = alias
|
||||
bindings.append(entry)
|
||||
bindings.sort(key=lambda b: b['key'])
|
||||
prefix_repr = Shortcut((prefix,)).human_repr(opts.kitty_mod) if prefix is not None else ''
|
||||
title = f'Leader keys ({prefix_repr})' if prefix_repr else 'Leader keys'
|
||||
return {'leader': {'prefix': prefix_repr, 'title': title, 'bindings': bindings}}
|
||||
|
||||
|
||||
def main(args: list[str]) -> None:
|
||||
raise SystemExit('This kitten must be used only from a kitty.conf mapping')
|
||||
|
||||
@@ -330,7 +376,21 @@ def callback(target_window_id: int, action: str, timer_id: int | None) -> None:
|
||||
|
||||
@result_handler(has_ready_notification=True)
|
||||
def handle_result(args: list[str], data: dict[str, Any], target_window_id: int, boss: BossType) -> None:
|
||||
if data and (action := data.get('action')):
|
||||
if not data:
|
||||
return
|
||||
remap_key, remap_action = data.get('remap_key'), data.get('remap_action')
|
||||
if remap_key and remap_action:
|
||||
from kitty.config import append_map_to_config
|
||||
|
||||
try:
|
||||
path = append_map_to_config(remap_key, remap_action)
|
||||
boss.load_config_file()
|
||||
except Exception as err:
|
||||
boss.show_error('Failed to remap shortcut', f'Could not update {remap_key} in the config file: {err}')
|
||||
else:
|
||||
print(f'Mapped {remap_key} to {remap_action} in {path}', flush=True)
|
||||
return
|
||||
if action := data.get('action'):
|
||||
# run action after event loop tick so command palette overlay is closed
|
||||
add_timer(partial(callback, target_window_id, action), 0, False)
|
||||
|
||||
|
||||
Reference in new issue
Block a user