Skip to content
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

Merged
merged 7 commits into from
Jan 28, 2017

Conversation

martindemello
Copy link
Contributor

@martindemello martindemello commented Jan 26, 2017

Fixes #96 by adding an optional @parentsection directive to be used with the @section directive.

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
Copy link
Contributor

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.

Copy link
Contributor Author

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']:
Copy link
Contributor

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.)

Copy link
Contributor Author

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
Copy link
Contributor

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?

Copy link
Contributor Author

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 :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
Oh yeah, so they are…

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):
Copy link
Contributor

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.

Copy link
Contributor Author

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*
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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).

Copy link
Contributor Author

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

Copy link
Contributor

@dbarnett dbarnett left a 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
Copy link
Contributor

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.

Copy link
Contributor Author

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.
Copy link
Contributor

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.

Copy link
Contributor Author

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:
Copy link
Contributor

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.

Copy link
Contributor Author

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.
Copy link
Contributor

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?

Copy link
Contributor Author

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
Copy link
Contributor

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='.')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice idea. done.

Copy link
Contributor

@dbarnett dbarnett left a 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.

@martindemello martindemello merged commit 5eb63f6 into google:master Jan 28, 2017
@martindemello martindemello deleted the parent-section branch January 28, 2017 06:45
@wsdjeg
Copy link

wsdjeg commented Jan 28, 2017

Nice work, thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants