Nicer formatting of mappings in the comment conf file

This commit is contained in:
Kovid Goyal 2021-11-18 09:21:24 +05:30
parent 3e3dc26e3f
commit d30ba9654f
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
2 changed files with 21 additions and 15 deletions

View file

@ -3,7 +3,9 @@
import builtins
import re
import textwrap
import typing
from functools import lru_cache
from importlib import import_module
from typing import (
Any, Callable, Dict, Iterable, Iterator, List, Match, Optional, Set, Tuple,
@ -78,30 +80,31 @@ def iter_blocks(lines: Iterable[str]) -> Iterator[Tuple[List[str], int]]:
yield current_block, indent_size
def wrapped_block(lines: Iterable[str]) -> Iterator[str]:
wrapper = getattr(wrapped_block, 'wrapper', None)
if wrapper is None:
import textwrap
wrapper = textwrap.TextWrapper(
initial_indent='#: ', subsequent_indent='#: ', width=70, break_long_words=False
@lru_cache(maxsize=8)
def block_wrapper(comment_symbol: str) -> textwrap.TextWrapper:
return textwrap.TextWrapper(
initial_indent=comment_symbol, subsequent_indent=comment_symbol, width=70, break_long_words=False
)
setattr(wrapped_block, 'wrapper', wrapper)
def wrapped_block(lines: Iterable[str], comment_symbol: str = '#: ') -> Iterator[str]:
wrapper = block_wrapper(comment_symbol)
for block, indent_size in iter_blocks(lines):
if indent_size > 0:
for line in block:
if not line:
yield line
else:
yield '#: ' + line
yield comment_symbol + line
else:
for line in wrapper.wrap('\n'.join(block)):
yield line
def render_block(text: str) -> str:
def render_block(text: str, comment_symbol: str = '#: ') -> str:
text = remove_markup(text)
lines = text.splitlines()
return '\n'.join(wrapped_block(lines))
return '\n'.join(wrapped_block(lines, comment_symbol))
class CoalescedIteratorData:
@ -288,12 +291,17 @@ class Mapping:
def as_conf(self, commented: bool = False, level: int = 0, action_group: List['Mapping'] = []) -> List[str]:
ans: List[str] = []
if not self.documented:
return ans
a = ans.append
if self.short_text:
a(render_block(self.short_text.strip())), a('')
for sc in [self] + action_group:
if sc.documented and sc.add_to_default:
a(sc.setting_name + ' ' + sc.parseable_text)
if self.documented and self.long_text:
a(''), a(render_block(self.long_text.strip())), a('')
if self.long_text:
a(''), a(render_block(self.long_text.strip(), '#:: '))
a('')
return ans
def as_rst(

View file

@ -619,9 +619,7 @@ mma('Extend the current selection even when grabbed',
mma('Show clicked command output in pager',
'show_clicked_cmd_output_ungrabbed ctrl+shift+right press ungrabbed mouse_show_command_output',
long_text='''
Requires :ref:`shell_integration` to work.
'''
long_text='Requires :ref:`shell_integration` to work'
)
egr() # }}}
egr() # }}}