Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/hawkmoth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ def process_docstring(lines): self.__process_docstring(lines)

num_matches = 0
for docstrings in root.walk(recurse=False, filter_types=self._docstring_types,
filter_names=self._get_names()):
filter_names=self._get_names(),
filter_func=self._get_filter()):
num_matches += 1
for docstr in docstrings.walk(filter_names=self._get_members()):
lines, line_number = docstr.get_docstring(process_docstring=process_docstring)
Expand Down Expand Up @@ -164,6 +165,9 @@ def _get_names(self):
def _get_members(self):
return None

def _get_filter(self):
return lambda comment: False

def _get_filenames(self):
raise NotImplementedError(self.__class__.__name__ + '._get_filenames')

Expand Down Expand Up @@ -246,8 +250,30 @@ def _get_members(self):
# By default use [] as a filter that does not match any members.
return self.options.get('members', [])

def _filter_private_members(comment: docstring.Docstring):
if comment.is_static():
return True

if isinstance(comment, docstring.MacroDocstring):
return True

if isinstance(comment, docstring.TypedefDocstring):
return True

return False

class CAutoDocDirective(_AutoDocDirective):
_domain = 'c'
option_spec = _AutoDocDirective.option_spec.copy()
option_spec.update({
'exclude-private-members': lambda arg: True,
})

def _get_filter(self):
if self.options.get('exclude-private-members', False):
return _filter_private_members
else:
return super()._get_filter()

class CAutoSectionDirective(_AutoSymbolDirective):
# Allow spaces in the directive argument (the name)
Expand Down
12 changes: 9 additions & 3 deletions src/hawkmoth/docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,16 @@ def __init__(self, cursor=None, text=None, meta=None, nest=0):
self._nest = nest
self._children = []

def _match(self, filter_types=None, filter_names=None):
def _match(self, filter_types=None, filter_names=None, filter_func=None):
if filter_types is not None and type(self) not in filter_types:
return False

if filter_names is not None and self.get_name() not in filter_names:
return False

if filter_func is not None and filter_func(self):
return False

return True

def walk(self, recurse=True, filter_types=None, filter_names=None):
Expand Down Expand Up @@ -181,6 +184,9 @@ def get_name(self):
def get_line(self):
return self._meta['line']

def is_static(self):
return 'static' in self._ttype

class TextDocstring(Docstring):
_indent = 0
_fmt = ''
Expand Down Expand Up @@ -294,7 +300,7 @@ def add_child(self, comment):
def add_children(self, comments):
self._children.extend(comments)

def walk(self, recurse=True, filter_types=None, filter_names=None):
def walk(self, recurse=True, filter_types=None, filter_names=None, filter_func=None):
# Note: The filtering is pretty specialized for our use case here. It
# only filters the immediate children, not this comment, nor
# grandchildren.
Expand All @@ -306,7 +312,7 @@ def walk(self, recurse=True, filter_types=None, filter_names=None):
# 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 comment._match(filter_types=filter_types, filter_names=filter_names, filter_func=filter_func):
if recurse:
yield from comment.walk()
else:
Expand Down
Loading