Coalesce shortcuts as well

This commit is contained in:
Kovid Goyal 2021-05-31 16:17:37 +05:30
parent e0261e925e
commit 0bbc4462fd
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 51 additions and 59 deletions

View File

@ -13,10 +13,7 @@ import subprocess
import sys
import time
from functools import partial
from typing import (
Any, Callable, Dict, Iterable, List, Match, Optional, Sequence, Tuple,
Union
)
from typing import Any, Callable, Dict, Iterable, List, Match, Optional, Tuple
from docutils import nodes
from docutils.parsers.rst.roles import set_classes
@ -391,12 +388,14 @@ class SessionLexer(RegexLexer):
def link_role(name: str, rawtext: str, text: str, lineno: int, inliner: Any, options: Any = {}, content: Any = []) -> Tuple[List, List]:
text = text.replace('\n', ' ')
m = re.match(r'(.+)\s+<(.+?)>', text)
if m is None:
msg = inliner.reporter.error(f'link "{text}" not recognized', line=lineno)
prb = inliner.problematic(rawtext, rawtext, msg)
return [prb], [msg]
text, url = m.group(1, 2)
url = url.replace(' ', '')
set_classes(options)
node = nodes.reference(rawtext, text, refuri=url, **options)
return [node], []
@ -491,6 +490,7 @@ def write_conf_docs(app: Any, all_kitten_names: Iterable[str]) -> None:
sc_role = app.registry.domain_roles['std']['sc']
sc_role.warn_dangling = True
sc_role.process_link = process_shortcut_link
shortcut_slugs.clear()
def generate_default_config(definition: Definition, name: str) -> None:
with open(f'generated/conf-{name}.rst', 'w', encoding='utf-8') as f:

View File

@ -101,6 +101,7 @@ def render_block(text: str) -> str:
class CoalescedIteratorData:
option_groups: Dict[int, List['Option']] = {}
action_groups: Dict[str, List['Mapping']] = {}
coalesced: Set[int] = set()
initialized: bool = False
kitty_mod: str = 'kitty_mod'
@ -111,6 +112,8 @@ class CoalescedIteratorData:
self.root = root
option_groups = self.option_groups = {}
current_group: List[Option] = []
action_groups: Dict[str, List[Mapping]] = {}
self.action_groups = action_groups
coalesced = self.coalesced = set()
self.kitty_mod = 'kitty_mod'
for item in root.iter_all_non_groups():
@ -126,12 +129,21 @@ class CoalescedIteratorData:
current_group = [item]
else:
current_group.append(item)
elif isinstance(item, Mapping):
if item.name in action_groups:
coalesced.add(id(item))
action_groups[item.name].append(item)
else:
action_groups[item.name] = []
if current_group:
option_groups[id(current_group[0])] = current_group[1:]
def option_group_for_option(self, opt: 'Option') -> List['Option']:
return self.option_groups.get(id(opt), [])
def action_group_for_action(self, ac: 'Mapping') -> List['Mapping']:
return self.action_groups.get(ac.name, [])
class Option:
@ -252,6 +264,7 @@ class Mapping:
documented: bool
setting_name: str
name: str
only: Only
@property
def parseable_text(self) -> str:
@ -271,21 +284,37 @@ class Mapping:
a(''), a(render_block(self.long_text.strip())), a('')
return ans
def as_rst(self, conf_name: str, shortcut_slugs: Dict[str, Tuple[str, str]], kitty_mod: str, level: int = 0) -> List[str]:
def as_rst(
self, conf_name: str, shortcut_slugs: Dict[str, Tuple[str, str]],
kitty_mod: str, level: int = 0, action_group: List['Mapping'] = []
) -> List[str]:
ans: List[str] = []
a = ans.append
if not self.documented:
return ans
if not self.short_text:
raise ValueError(f'The shortcut for {self.name} has no short_text')
sc_text = f'{conf_name}.{self.short_text}'
shortcut_slugs[f'{conf_name}.{self.name}'] = (sc_text, self.key_text.replace('kitty_mod', kitty_mod))
if self.documented:
a = ans.append
a('.. shortcut:: ' + sc_text)
if self.add_to_default:
a('.. code-block:: conf')
a('')
a(' ' + self.setting_name + ' ' + self.parseable_text.replace('kitty_mod', kitty_mod))
a('.. shortcut:: ' + sc_text)
block_started = False
for sc in [self] + action_group:
if sc.add_to_default and sc.documented:
if not block_started:
a('.. code-block:: conf')
a('')
block_started = True
suffix = ''
if sc.only == 'macos':
suffix = ' 🍎'
elif sc.only == 'linux':
suffix = ' 🐧'
a(f' {sc.setting_name} {sc.parseable_text.replace("kitty_mod", kitty_mod)}{suffix}')
a('')
if self.long_text:
a('')
a(expand_opt_references(conf_name, self.long_text))
a('')
if self.long_text:
a(expand_opt_references(conf_name, self.long_text))
a('')
return ans
@ -406,6 +435,8 @@ class Group:
for item in self.iter_with_coalesced_options():
if isinstance(item, Option):
lines = item.as_rst(conf_name, shortcut_slugs, kitty_mod, option_group=self.coalesced_iterator_data.option_group_for_option(item))
elif isinstance(item, Mapping):
lines = item.as_rst(conf_name, shortcut_slugs, kitty_mod, level + 1, action_group=self.coalesced_iterator_data.action_group_for_action(item))
else:
lines = item.as_rst(conf_name, shortcut_slugs, kitty_mod, level + 1)
ans.extend(lines)

View File

@ -2724,35 +2724,31 @@ agr('shortcuts.scrolling', 'Scrolling')
map('Scroll line up',
'scroll_line_up kitty_mod+up scroll_line_up',
)
map('Scroll line up',
'scroll_line_up kitty_mod+k scroll_line_up',
)
map('Scroll line up',
'scroll_line_up alt+cmd+page_up scroll_line_up',
only="macos",
documented=False,
)
map('Scroll line up',
'scroll_line_up cmd+up scroll_line_up',
only="macos",
documented=False,
)
map('',
'scroll_line_up kitty_mod+k scroll_line_up',
)
map('Scroll line down',
'scroll_line_down kitty_mod+down scroll_line_down',
)
map('',
map('Scroll line down',
'scroll_line_down kitty_mod+j scroll_line_down',
)
map('Scroll line down',
'scroll_line_down alt+cmd+page_down scroll_line_down',
only="macos",
documented=False,
)
map('Scroll line down',
'scroll_line_down cmd+down scroll_line_down',
only="macos",
documented=False,
)
map('Scroll page up',
@ -2761,7 +2757,6 @@ map('Scroll page up',
map('Scroll page up',
'scroll_page_up cmd+page_up scroll_page_up',
only="macos",
documented=False,
)
map('Scroll page down',
@ -2770,7 +2765,6 @@ map('Scroll page down',
map('Scroll page down',
'scroll_page_down cmd+page_down scroll_page_down',
only="macos",
documented=False,
)
map('Scroll to top',
@ -2779,7 +2773,6 @@ map('Scroll to top',
map('Scroll to top',
'scroll_home cmd+home scroll_home',
only="macos",
documented=False,
)
map('Scroll to bottom',
@ -2788,7 +2781,6 @@ map('Scroll to bottom',
map('Scroll to bottom',
'scroll_end cmd+end scroll_end',
only="macos",
documented=False,
)
map('Browse scrollback buffer in less',
@ -2810,7 +2802,7 @@ egr() # }}}
# shortcuts.window {{{
agr('shortcuts.window', 'Window management')
map('',
map('New window',
'new_window kitty_mod+enter new_window',
long_text='''
You can open a new window running an arbitrary program, for example::
@ -2841,7 +2833,6 @@ For more details, see :doc:`launch`.
map('New window',
'new_window cmd+enter new_window',
only="macos",
documented=False,
)
map('New OS window',
@ -2855,7 +2846,6 @@ current working directory.
map('New OS window',
'new_os_window cmd+n new_os_window',
only="macos",
documented=False,
)
map('Close window',
@ -2864,7 +2854,6 @@ map('Close window',
map('Close window',
'close_window shift+cmd+d close_window',
only="macos",
documented=False,
)
map('Next window',
@ -2893,7 +2882,6 @@ map('Start resizing window',
map('Start resizing window',
'start_resizing_window cmd+r start_resizing_window',
only="macos",
documented=False,
)
map('First window',
@ -2902,7 +2890,6 @@ map('First window',
map('First window',
'first_window cmd+1 first_window',
only="macos",
documented=False,
)
map('Second window',
@ -2911,7 +2898,6 @@ map('Second window',
map('Second window',
'second_window cmd+2 second_window',
only="macos",
documented=False,
)
map('Third window',
@ -2920,7 +2906,6 @@ map('Third window',
map('Third window',
'third_window cmd+3 third_window',
only="macos",
documented=False,
)
map('Fourth window',
@ -2929,7 +2914,6 @@ map('Fourth window',
map('Fourth window',
'fourth_window cmd+4 fourth_window',
only="macos",
documented=False,
)
map('Fifth window',
@ -2938,7 +2922,6 @@ map('Fifth window',
map('Fifth window',
'fifth_window cmd+5 fifth_window',
only="macos",
documented=False,
)
map('Sixth window',
@ -2947,7 +2930,6 @@ map('Sixth window',
map('Sixth window',
'sixth_window cmd+6 sixth_window',
only="macos",
documented=False,
)
map('Seventh window',
@ -2956,7 +2938,6 @@ map('Seventh window',
map('Seventh window',
'seventh_window cmd+7 seventh_window',
only="macos",
documented=False,
)
map('Eight window',
@ -2965,7 +2946,6 @@ map('Eight window',
map('Eight window',
'eighth_window cmd+8 eighth_window',
only="macos",
documented=False,
)
map('Ninth window',
@ -2974,7 +2954,6 @@ map('Ninth window',
map('Ninth window',
'ninth_window cmd+9 ninth_window',
only="macos",
documented=False,
)
map('Tenth window',
@ -2992,11 +2971,9 @@ map('Next tab',
map('Next tab',
'next_tab shift+cmd+] next_tab',
only="macos",
documented=False,
)
map('Next tab',
'next_tab ctrl+tab next_tab',
documented=False,
)
map('Previous tab',
@ -3005,11 +2982,9 @@ map('Previous tab',
map('Previous tab',
'previous_tab shift+cmd+[ previous_tab',
only="macos",
documented=False,
)
map('Previous tab',
'previous_tab shift+ctrl+tab previous_tab',
documented=False,
)
map('New tab',
@ -3018,7 +2993,6 @@ map('New tab',
map('New tab',
'new_tab cmd+t new_tab',
only="macos",
documented=False,
)
map('Close tab',
@ -3027,13 +3001,11 @@ map('Close tab',
map('Close tab',
'close_tab cmd+w close_tab',
only="macos",
documented=False,
)
map('Close OS window',
'close_os_window shift+cmd+w close_os_window',
only="macos",
documented=False,
)
map('Move tab forward',
@ -3050,7 +3022,6 @@ map('Set tab title',
map('Set tab title',
'set_tab_title shift+cmd+i set_tab_title',
only="macos",
documented=False,
)
egr('''
You can also create shortcuts to go to specific tabs, with 1 being the first
@ -3098,26 +3069,21 @@ map('Increase font size',
)
map('Increase font size',
'increase_font_size kitty_mod+plus change_font_size all +2.0',
documented=True,
)
map('Increase font size',
'increase_font_size kitty_mod+kp_add change_font_size all +2.0',
documented=True,
)
map('Increase font size',
'increase_font_size cmd+plus change_font_size all +2.0',
only="macos",
documented=True,
)
map('Increase font size',
'increase_font_size cmd+equal change_font_size all +2.0',
only="macos",
documented=True,
)
map('Increase font size',
'increase_font_size cmd+shift+equal change_font_size all +2.0',
only="macos",
documented=True,
)
map('Decrease font size',
@ -3129,12 +3095,10 @@ map('Decrease font size',
map('Decrease font size',
'decrease_font_size cmd+minus change_font_size all -2.0',
only="macos",
documented=True,
)
map('Decrease font size',
'decrease_font_size cmd+shift+minus change_font_size all -2.0',
only="macos",
documented=True,
)
map('Reset font size',
@ -3143,7 +3107,6 @@ map('Reset font size',
map('Reset font size',
'reset_font_size cmd+0 change_font_size all 0',
only="macos",
documented=True,
)
egr('''
To setup shortcuts for specific font sizes::
@ -3242,7 +3205,6 @@ map('Unicode input',
map('Unicode input',
'input_unicode_character cmd+ctrl+space kitten unicode_input',
only="macos",
documented=False,
)
map('Edit config file',
@ -3251,7 +3213,6 @@ map('Edit config file',
map('Edit config file',
'edit_config_file cmd+, edit_config_file',
only="macos",
documented=False,
)
map('Open the kitty command shell',