Put option comments right above the option value

Problem: I like to edit `~/.config/qutebrowser/qutebrowser.conf`
manually with Vim. This works great, except that the current format is a
bit of a pain to deal with:

	[section-name]
	# section description
	#
	# [ Description of all the options]

	actual options

So if I want to know the description or what the default value is, I
need to scroll up and back down.

Solution: change the order of the comments to:

	# section description
	[section-name]

	# Option description
	option = value

	# Option description two
	optiontwo = value

	# Hello, world!
	[section-two]
	...

Which is much more convenient (and also what almost any other program
does).

(This patch changes much less code than it looks in the diff; I just
de-looped and moved `_str_option_desc` below `_str_items` as that makes
more sense since it gets called by `_str_items`).
This commit is contained in:
Martin Tournoij 2017-03-30 17:45:51 +01:00
parent 6939f81de7
commit 5efca15594
No known key found for this signature in database
GPG Key ID: A6258419189EE585

View File

@ -471,10 +471,9 @@ class ConfigManager(QObject):
"""Get the whole config as a string.""" """Get the whole config as a string."""
lines = configdata.FIRST_COMMENT.strip('\n').splitlines() lines = configdata.FIRST_COMMENT.strip('\n').splitlines()
for sectname, sect in self.sections.items(): for sectname, sect in self.sections.items():
lines.append('\n[{}]'.format(sectname)) lines += ['\n'] + self._str_section_desc(sectname)
lines += self._str_section_desc(sectname) lines.append('[{}]'.format(sectname))
lines += self._str_option_desc(sectname, sect) lines += self._str_items(sectname, sect)
lines += self._str_items(sect)
return '\n'.join(lines) + '\n' return '\n'.join(lines) + '\n'
def _str_section_desc(self, sectname): def _str_section_desc(self, sectname):
@ -489,17 +488,30 @@ class ConfigManager(QObject):
lines += wrapper.wrap(secline) lines += wrapper.wrap(secline)
return lines return lines
def _str_option_desc(self, sectname, sect): def _str_items(self, sectname, sect):
"""Get the option description strings for sect/sectname.""" """Get the option items as string for sect."""
lines = []
for optname, option in sect.items():
value = option.value(startlayer='conf')
for c in self.KEY_ESCAPE:
if optname.startswith(c):
optname = optname.replace(c, self.ESCAPE_CHAR + c, 1)
# configparser can't handle = in keys :(
optname = optname.replace('=', '<eq>')
keyval = '{} = {}'.format(optname, value)
lines += self._str_option_desc(sectname, sect, optname, option)
lines.append(keyval)
return lines
def _str_option_desc(self, sectname, sect, optname, option):
"""Get the option description strings for a single option."""
wrapper = textwrapper.TextWrapper(initial_indent='#' + ' ' * 5, wrapper = textwrapper.TextWrapper(initial_indent='#' + ' ' * 5,
subsequent_indent='#' + ' ' * 5) subsequent_indent='#' + ' ' * 5)
lines = [] lines = []
if not getattr(sect, 'descriptions', None): if not getattr(sect, 'descriptions', None):
return lines return lines
for optname, option in sect.items(): lines.append('')
lines.append('#')
typestr = ' ({})'.format(option.typ.get_name()) typestr = ' ({})'.format(option.typ.get_name())
lines.append("# {}{}:".format(optname, typestr)) lines.append("# {}{}:".format(optname, typestr))
@ -508,7 +520,7 @@ class ConfigManager(QObject):
except KeyError: except KeyError:
log.config.exception("No description for {}.{}!".format( log.config.exception("No description for {}.{}!".format(
sectname, optname)) sectname, optname))
continue return []
for descline in desc.splitlines(): for descline in desc.splitlines():
lines += wrapper.wrap(descline) lines += wrapper.wrap(descline)
valid_values = option.typ.get_valid_values() valid_values = option.typ.get_valid_values()
@ -524,20 +536,6 @@ class ConfigManager(QObject):
option.values['default'])) option.values['default']))
return lines return lines
def _str_items(self, sect):
"""Get the option items as string for sect."""
lines = []
for optname, option in sect.items():
value = option.value(startlayer='conf')
for c in self.KEY_ESCAPE:
if optname.startswith(c):
optname = optname.replace(c, self.ESCAPE_CHAR + c, 1)
# configparser can't handle = in keys :(
optname = optname.replace('=', '<eq>')
keyval = '{} = {}'.format(optname, value)
lines.append(keyval)
return lines
def _get_real_sectname(self, cp, sectname): def _get_real_sectname(self, cp, sectname):
"""Get an old or new section name based on a configparser. """Get an old or new section name based on a configparser.