-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add a parent directive to @section #97
Conversation
if ident is None: | ||
# If omitted, it's the name in lowercase, with spaces converted to dashes. | ||
ident = self.name.lower().replace(' ', '-') | ||
self.id = ident | ||
|
||
# This is potentially a problem if the parent layer name used spaces and an | ||
# id was autogenerated; the parent_id needs to match the hyphenated form. | ||
# We could enforce an explicit id field for sections that are used as a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enforcing an explicit ID sounds reasonable to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
realised this was not possible because we don't know if the parent has been read at the time we read the child, and when we assemble the tree we don't know if the id was explicit or autogenerated. i added a hint in the error message instead.
to_delete = [] | ||
for key in self.sections: | ||
section = self.sections[key] | ||
if 'parent_id' in section.locals and section.locals['parent_id']: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd avoid the try
/ except KeyError
over the whole block when it's just one of several lookups you're trying to catch (a typo in 'children' or something could result a misleading NoSuchSection(parent_id)
. Also you can use setdefault and other python idioms to tighten up the code so it's easier to understand at a glance (to my eyes, anyway):
parent_id = section.locals.get('parent_id', None)
if parent_id:
if parent_id not in self.sections:
raise error.NoSuchSection(parent_id)
parent = self.sections[parent_id]
parent.locals.setdefault('children', []).append(section)
to_delete.append(key)
(That's one suggested alternative. Feel free to pick and chooses parts of that to suit your taste.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, didn't know about setdefault. done.
if 'children' in section.locals: | ||
sort_key = lambda s: s.locals['name'] | ||
for child in sorted(section.locals['children'], key = sort_key): | ||
child.locals['level'] = section.locals['level'] + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason not to use += 1
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they're two different objects :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
section.locals['level'] = 0 | ||
if 'children' in section.locals: | ||
sort_key = lambda s: s.locals['name'] | ||
for child in sorted(section.locals['children'], key = sort_key): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: key=sort_key
w/o spaces per https://google.github.io/styleguide/pyguide.html?showone=Whitespace#Whitespace:
Don't use spaces around the '=' sign when used to indicate a keyword argument or a default parameter value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
) | ||
# Optional identifier | ||
(?: | ||
# Separated by comma and whitespace. | ||
,\s* | ||
\s*,\s* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems unnecessary to me accepting extra whitespace before the comma here. Is that intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, there was an issue where the regexp was slurping spaces after the name in the case "name < parent", so i enforced the name ending with a nonspace and then i figured we might as well allow whitespaces around both the , and < separators for consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well FWIW, I would expect it allowed around the < but expect it not allowed around , (just the way commas work typographically).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, makes sense. taking that bit back out
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for implementing this! (And for bearing with all my comments.)
@@ -114,12 +114,15 @@ Available block directives include: | |||
- `@public` marks a function public. In most plugins, functions are private by | |||
default, though this default may be overridden on a per-plugin basis. | |||
- `@private` marks a function private. | |||
- `@section name[, id]` allows you to write a new section for the helpfile. The | |||
id will be a lowercased version of name if omitted. | |||
- `@section name[, id]` allows you to write a new section for the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Looks like there's a spurious edit wrapping this line early even though it's otherwise identical.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
- `@section name[, id]` allows you to write a new section for the | ||
helpfile. The id will be a lowercased version of name if omitted. | ||
- `@parentsection id` defines the current section as a child of the given | ||
section. Must be contained within a @section block. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add backticks around this @section
for code font, and the other @parentsection
below. GitHub seems to be highlighting them strangely and code font is a little clearer anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
# We add a 'level' variable to locals so that WriteTableOfContents can keep | ||
# track of the nesting. | ||
def _AddChildSections(section): | ||
if not 'level' in section.locals: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could also use section.locals.setdefault('level', 0)
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -211,7 +215,8 @@ def _DelimitedRegex(pattern): | |||
# MATCH GROUP 1: The Name | |||
( | |||
# Non-commas or escaped commas or escaped escapes. | |||
(?:[^\\,]|\\.)+ | |||
# Must not end with a space. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this change leftover from the other approach, or valuable in itself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
valuable in itself - it prevents problems with trailing spaces being changed to dashes.
@@ -56,12 +56,22 @@ def WriteTableOfContents(self): | |||
"""Writes the table of contents.""" | |||
self.WriteRow() | |||
self.WriteLine('CONTENTS', right=self.Tag(self.Slug('contents'))) | |||
for i, block in enumerate(self.module.sections.values()): | |||
# We need to keep track of section numbering on a per-level basis |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to extract out a generator function as a helper that tracks the indexing? Then the code in this method could look like:
for index, block in self._EnumerateIndices(self.module.sections.values()):
self.WriteLine('%d. %s' % (index, block.locals['name']),
indent=2 * block.locals['level'] + 1,
right=self.Link(self.Slug(block.locals['id'])),
fill='.')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice idea. done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking this task on!
We can bump the vimdoc version after this change and I can get an updated deb built after that. Then we can update vim-codefmt and vim-coverage to use it while wsdjeg is updating SpaceVim.
Nice work, thanks a lot. |
Fixes #96 by adding an optional
@parentsection
directive to be used with the@section
directive.