dedup choice literals

This commit is contained in:
Kovid Goyal 2023-10-15 10:10:05 +05:30
parent 0e11174aa5
commit 9375e671bc
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 17 additions and 5 deletions

View file

@ -70,6 +70,8 @@ def option_type_data(option: Union[Option, MultiOption]) -> Tuple[Callable[[Any]
is_mutiple_vars = {}
option_names = set()
color_table = list(map(str, range(256)))
choice_dedup: Dict[str, str] = {}
choice_parser_dedup: Dict[str, str] = {}
def parser_function_declaration(option_name: str) -> None:
t('')
@ -100,6 +102,10 @@ def parser_function_declaration(option_name: str) -> None:
if option.choices:
typ = 'typing.Literal[{}]'.format(', '.join(repr(x) for x in option.choices))
ename = f'choices_for_{option.name}'
if typ in choice_dedup:
typ = choice_dedup[typ]
else:
choice_dedup[typ] = ename
choices[ename] = typ
typ = ename
func = str
@ -134,12 +140,18 @@ def parser_function_declaration(option_name: str) -> None:
imports.add(('kitty.constants', 'is_macos'))
a(f' {option.name}: {typ} = {defval}')
if option.choices:
ecname = f'choices_for_{option.name}'
crepr = f'frozenset({option.choices!r})'
if crepr in choice_parser_dedup:
crepr = choice_parser_dedup[crepr]
else:
choice_parser_dedup[crepr] = ecname
t(' val = val.lower()')
t(f' if val not in self.choices_for_{option.name}:')
t(f' raise ValueError(f"The value {{val}} is not a valid choice for {option.name}")')
t(f' ans["{option.name}"] = val')
t('')
t(f' choices_for_{option.name} = frozenset({option.choices!r})')
t(f' {ecname} = {crepr}')
for option_name, (typ, mval) in is_mutiple_vars.items():
a(f' {option_name}: {typ} = ' '{}')

View file

@ -1140,7 +1140,7 @@ def pointer_shape_when_dragging(self, val: str, ans: typing.Dict[str, typing.Any
raise ValueError(f"The value {val} is not a valid choice for pointer_shape_when_dragging")
ans["pointer_shape_when_dragging"] = val
choices_for_pointer_shape_when_dragging = frozenset(('arrow', 'beam', 'text', 'pointer', 'hand', 'help', 'wait', 'progress', 'crosshair', 'vertical-text', 'move', 'e-resize', 'ne-resize', 'nw-resize', 'n-resize', 'se-resize', 'sw-resize', 's-resize', 'w-resize', 'ew-resize', 'ns-resize', 'nesw-resize', 'nwse-resize', 'zoom-in', 'zoom-out', 'alias', 'copy', 'not-allowed', 'no-drop', 'grab', 'grabbing'))
choices_for_pointer_shape_when_dragging = choices_for_default_pointer_shape
def pointer_shape_when_grabbed(self, val: str, ans: typing.Dict[str, typing.Any]) -> None:
val = val.lower()
@ -1148,7 +1148,7 @@ def pointer_shape_when_grabbed(self, val: str, ans: typing.Dict[str, typing.Any]
raise ValueError(f"The value {val} is not a valid choice for pointer_shape_when_grabbed")
ans["pointer_shape_when_grabbed"] = val
choices_for_pointer_shape_when_grabbed = frozenset(('arrow', 'beam', 'text', 'pointer', 'hand', 'help', 'wait', 'progress', 'crosshair', 'vertical-text', 'move', 'e-resize', 'ne-resize', 'nw-resize', 'n-resize', 'se-resize', 'sw-resize', 's-resize', 'w-resize', 'ew-resize', 'ns-resize', 'nesw-resize', 'nwse-resize', 'zoom-in', 'zoom-out', 'alias', 'copy', 'not-allowed', 'no-drop', 'grab', 'grabbing'))
choices_for_pointer_shape_when_grabbed = choices_for_default_pointer_shape
def remember_window_size(self, val: str, ans: typing.Dict[str, typing.Any]) -> None:
ans['remember_window_size'] = to_bool(val)

View file

@ -21,8 +21,8 @@
choices_for_macos_colorspace = typing.Literal['srgb', 'default', 'displayp3']
choices_for_macos_show_window_title_in = typing.Literal['all', 'menubar', 'none', 'window']
choices_for_placement_strategy = typing.Literal['center', 'top-left']
choices_for_pointer_shape_when_dragging = typing.Literal['arrow', 'beam', 'text', 'pointer', 'hand', 'help', 'wait', 'progress', 'crosshair', 'vertical-text', 'move', 'e-resize', 'ne-resize', 'nw-resize', 'n-resize', 'se-resize', 'sw-resize', 's-resize', 'w-resize', 'ew-resize', 'ns-resize', 'nesw-resize', 'nwse-resize', 'zoom-in', 'zoom-out', 'alias', 'copy', 'not-allowed', 'no-drop', 'grab', 'grabbing']
choices_for_pointer_shape_when_grabbed = typing.Literal['arrow', 'beam', 'text', 'pointer', 'hand', 'help', 'wait', 'progress', 'crosshair', 'vertical-text', 'move', 'e-resize', 'ne-resize', 'nw-resize', 'n-resize', 'se-resize', 'sw-resize', 's-resize', 'w-resize', 'ew-resize', 'ns-resize', 'nesw-resize', 'nwse-resize', 'zoom-in', 'zoom-out', 'alias', 'copy', 'not-allowed', 'no-drop', 'grab', 'grabbing']
choices_for_pointer_shape_when_dragging = choices_for_default_pointer_shape
choices_for_pointer_shape_when_grabbed = choices_for_default_pointer_shape
choices_for_strip_trailing_spaces = typing.Literal['always', 'never', 'smart']
choices_for_tab_bar_align = typing.Literal['left', 'center', 'right']
choices_for_tab_bar_style = typing.Literal['fade', 'hidden', 'powerline', 'separator', 'slant', 'custom']