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
2 changed files with 21 additions and 15 deletions

View File

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

View File

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