mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-19 22:55:29 +00:00
Use a per instance cache for resolved action aliases
This commit is contained in:
parent
7a19b8c408
commit
1adf1fbb76
4 changed files with 19 additions and 8 deletions
|
|
@ -139,7 +139,7 @@ from .keys import Mappings
|
|||
from .layout.base import set_layout_options
|
||||
from .notifications import NotificationManager
|
||||
from .options.types import Options, nullable_colors
|
||||
from .options.utils import KeyboardMode, KeyDefinition, clamp_font_size
|
||||
from .options.utils import KeyboardMode, KeyDefinition, MapType, clamp_font_size
|
||||
from .os_window_size import initial_window_size_func
|
||||
from .session import (
|
||||
Session,
|
||||
|
|
@ -2095,7 +2095,7 @@ class Boss:
|
|||
consumed = False
|
||||
if action_definition:
|
||||
try:
|
||||
actions = get_options().alias_map.resolve_aliases(action_definition, 'map' if dispatch_type == 'KeyPress' else 'mouse_map')
|
||||
actions = get_options().alias_map.resolve_aliases(action_definition, MapType.MAP if dispatch_type == 'KeyPress' else MapType.MOUSE_MAP)
|
||||
except Exception as e:
|
||||
self.show_error('Failed to parse action', f'{action_definition}\n{e}')
|
||||
return True
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ from collections import defaultdict
|
|||
from collections.abc import Callable, Container, Iterable, Iterator, Sequence
|
||||
from contextlib import suppress
|
||||
from dataclasses import dataclass, fields
|
||||
from functools import lru_cache
|
||||
from typing import (
|
||||
Any,
|
||||
Generic,
|
||||
|
|
@ -1278,16 +1277,23 @@ class ActionAlias(NamedTuple):
|
|||
class AliasMap:
|
||||
def __init__(self) -> None:
|
||||
self.aliases: dict[str, list[ActionAlias]] = {}
|
||||
self.resolved: dict[tuple[str, MapType], tuple[KeyAction, ...]] = {}
|
||||
|
||||
def append(self, name: str, aa: ActionAlias) -> None:
|
||||
self.aliases.setdefault(name, []).append(aa)
|
||||
self.resolved.clear()
|
||||
|
||||
def update(self, aa: 'AliasMap') -> None:
|
||||
self.aliases.update(aa.aliases)
|
||||
self.resolved.clear()
|
||||
|
||||
@lru_cache(maxsize=256)
|
||||
def resolve_aliases(self, definition: str, map_type: MapType = MapType.MAP) -> tuple[KeyAction, ...]:
|
||||
return tuple(resolve_aliases_and_parse_actions(definition, self.aliases, map_type))
|
||||
key = definition, map_type
|
||||
if (ans := self.resolved.get(key)) is None:
|
||||
if len(self.resolved) >= 256:
|
||||
self.resolved.clear()
|
||||
ans = self.resolved[key] = tuple(resolve_aliases_and_parse_actions(definition, self.aliases, map_type))
|
||||
return ans
|
||||
|
||||
|
||||
def build_action_aliases(raw: dict[str, str], first_arg_replacement: str = '') -> AliasMap:
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ from .constants import config_dir, unserialize_launch_flag
|
|||
from .fast_data_types import get_options
|
||||
from .layout.interface import all_layouts
|
||||
from .options.types import Options
|
||||
from .options.utils import resize_window, to_layout_names, window_size
|
||||
from .options.utils import MapType, resize_window, to_layout_names, window_size
|
||||
from .os_window_size import WindowSize, WindowSizeData, WindowSizes
|
||||
from .typing_compat import BossType, SpecialWindowInstance, WindowType
|
||||
from .utils import expandvars, log_error, resolve_custom_file, resolved_shell, shlex_split
|
||||
|
|
@ -445,7 +445,7 @@ def get_all_known_sessions() -> dict[str, str]:
|
|||
for km in opts.keyboard_modes.values():
|
||||
for kdefs in km.keymap.values():
|
||||
for kd in kdefs:
|
||||
for key_action in opts.alias_map.resolve_aliases(kd.definition, 'map'):
|
||||
for key_action in opts.alias_map.resolve_aliases(kd.definition, MapType.MAP):
|
||||
if key_action.func == 'goto_session':
|
||||
cmdline: list[str] = []
|
||||
for arg in key_action.args:
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import tempfile
|
|||
|
||||
from kitty.constants import kitty_exe
|
||||
from kitty.fast_data_types import Color, test_cursor_blink_easing_function
|
||||
from kitty.options.utils import DELETE_ENV_VAR, EasingFunction, to_color
|
||||
from kitty.options.utils import DELETE_ENV_VAR, EasingFunction, build_action_aliases, to_color
|
||||
from kitty.utils import log_error, shlex_split
|
||||
|
||||
from .base import BaseTest
|
||||
|
|
@ -471,6 +471,11 @@ def conf_parsing(self):
|
|||
opts = p('clear_all_shortcuts y', 'action_alias ss kitten "space 1"', 'map f1 ss "space 2"')
|
||||
self.ae(ac().args, ('space 1', 'space 2'))
|
||||
|
||||
am = build_action_aliases({'la': 'launch --moo'})
|
||||
self.ae(am.resolve_aliases('la XXX')[0].args, ('--moo', 'XXX'))
|
||||
am.update(build_action_aliases({'la': 'launch --baa'}))
|
||||
self.ae(am.resolve_aliases('la XXX')[0].args, ('--baa', 'XXX'))
|
||||
|
||||
opts = p('kitty_mod alt')
|
||||
self.ae(opts.kitty_mod, to_modifiers('alt'))
|
||||
self.ae(next(keys_for_func(opts, 'next_layout')).mods, opts.kitty_mod)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue