From cd9f3af5822ab561970c5b62a4efff92b0c8ab02 Mon Sep 17 00:00:00 2001 From: Jani Nikula Date: Mon, 25 Sep 2023 19:21:26 +0300 Subject: [PATCH] docstring: move child handling under _CompoundDocstring Only _CompoundDocstring and its subclasses can have children. --- src/hawkmoth/docstring.py | 44 +++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/hawkmoth/docstring.py b/src/hawkmoth/docstring.py index 9efd8495..f70ae01f 100644 --- a/src/hawkmoth/docstring.py +++ b/src/hawkmoth/docstring.py @@ -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 @@ -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.""" @@ -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)