Skip to content

Commit

Permalink
feat: Support separate attribute signature
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Feb 13, 2022
1 parent ac11970 commit e962b88
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
25 changes: 21 additions & 4 deletions src/mkdocstrings_handlers/python/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,25 @@ def update_env(self, md: Markdown, config: dict) -> None: # noqa: D102 (ignore
self.env.filters["crossref"] = self.do_crossref
self.env.filters["multi_crossref"] = self.do_multi_crossref
self.env.filters["order_members"] = self.do_order_members
self.env.filters["format_code"] = self.do_format_code
self.env.filters["format_signature"] = self.do_format_signature

def do_format_code(self, code: str, line_length: int) -> str:
"""Format code using Black.
Parameters:
code: The code to format.
line_length: The line length to give to Black.
Returns:
The same code, formatted.
"""
code = code.strip()
if len(code) < line_length:
return code
formatter = _get_black_formatter()
return formatter(code, line_length)

def do_format_signature(self, signature: str, line_length: int) -> str:
"""Format a signature using Black.
Expand All @@ -151,7 +168,9 @@ def do_format_signature(self, signature: str, line_length: int) -> str:
if len(code) < line_length:
return code
formatter = _get_black_formatter()
return formatter(code, line_length)
formatted = formatter(f"def {code}: pass", line_length)
# remove starting `def ` and trailing `: pass`
return formatted[4:-5].strip()[:-1]

def do_order_members(self, members: Sequence[Object | Alias], order: Order) -> Sequence[Object | Alias]:
"""Order members given an ordering method.
Expand Down Expand Up @@ -219,8 +238,6 @@ def _get_black_formatter():

def formatter(code, line_length): # noqa: WPS430
mode = Mode(line_length=line_length)
formatted = format_str(f"def {code}: pass", mode=mode)
# remove starting `def ` and trailing `: pass`
return formatted[4:-5].strip()[:-1]
return format_str(code, mode=mode)

return formatter
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,32 @@
class="doc doc-heading",
toc_label=attribute.name) %}

{% filter highlight(language="python", inline=True) %}
{% if config.separate_signature %}
{% if show_full_path %}{{ attribute.path }}{% else %}{{ attribute.name }}{% endif %}
{% if attribute.annotation %}: {{ attribute.annotation }}{% endif %}
{% if attribute.value %} = {{ attribute.value }}{% endif %}
{% endfilter %}
{% else %}
{% filter highlight(language="python", inline=True) %}
{% if show_full_path %}{{ attribute.path }}{% else %}{{ attribute.name }}{% endif %}
{% if attribute.annotation %}: {{ attribute.annotation }}{% endif %}
{% if attribute.value %} = {{ attribute.value }}{% endif %}
{% endfilter %}
{% endif %}

{% with labels = attribute.labels %}
{% include "labels.html" with context %}
{% endwith %}

{% endfilter %}

{% if config.separate_signature %}
{% filter highlight(language="python", inline=False) %}
{% filter format_code(config.line_length) %}
{% if show_full_path %}{{ attribute.path }}{% else %}{{ attribute.name }}{% endif %}
{% if attribute.annotation %}: {{ attribute.annotation|safe }}{% endif %}
{% if attribute.value %} = {{ attribute.value|safe }}{% endif %}
{% endfilter %}
{% endfilter %}
{% endif %}

{% else %}
{% if config.show_root_toc_entry %}
{% filter heading(heading_level,
Expand Down

0 comments on commit e962b88

Please sign in to comment.