Skip to content

Commit

Permalink
docstring: move child handling under _CompoundDocstring
Browse files Browse the repository at this point in the history
Only _CompoundDocstring and its subclasses can have children.
  • Loading branch information
jnikula committed Oct 3, 2023
1 parent 1cf07cd commit 2ff9338
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions src/hawkmoth/docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ def __init__(self, domain='c', text=None, name=None,
self._domain = domain
self._children = []

def add_child(self, comment):
self._children.append(comment)

def add_children(self, comments):
self._children.extend(comments)

def _match(self, filter_types=None, filter_names=None):
if filter_types is not None and type(self) not in filter_types:
return False
Expand All @@ -80,23 +74,9 @@ def _match(self, filter_types=None, filter_names=None):
return True

def walk(self, recurse=True, filter_types=None, filter_names=None):
# Note: The filtering is pretty specialized for our use case here. It
# only filters the immediate children, not this comment, nor
# grandchildren.

# The contents of the parent will always be before children.
if self._text:
yield self

# Sort the children by order of appearance. We may add other sort
# options later.
for comment in sorted(self._children, key=lambda c: c.get_line()):
if comment._match(filter_types=filter_types, filter_names=filter_names):
if recurse:
yield from comment.walk()
else:
yield comment

@staticmethod
def is_doc(comment):
"""Test if comment is a C documentation comment."""
Expand Down Expand Up @@ -256,6 +236,30 @@ def _get_decl_name(self):

return self._decl_name

def add_child(self, comment):
self._children.append(comment)

def add_children(self, comments):
self._children.extend(comments)

def walk(self, recurse=True, filter_types=None, filter_names=None):
# Note: The filtering is pretty specialized for our use case here. It
# only filters the immediate children, not this comment, nor
# grandchildren.

# The contents of the parent will always be before children.
if self._text:
yield self

# Sort the children by order of appearance. We may add other sort
# options later.
for comment in sorted(self._children, key=lambda c: c.get_line()):
if comment._match(filter_types=filter_types, filter_names=filter_names):
if recurse:
yield from comment.walk()
else:
yield comment

class RootDocstring(_CompoundDocstring):
def __init__(self, filename=None, domain='c', clang_args=None):
super().__init__(domain=domain)
Expand Down

0 comments on commit 2ff9338

Please sign in to comment.