Skip to content

Commit

Permalink
fix: Fix separate signature feature
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Nov 30, 2021
1 parent b34ead0 commit da6e81c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
19 changes: 11 additions & 8 deletions src/mkdocstrings/handlers/python/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,27 @@ 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 the code using Black.
def do_format_signature(self, signature: str, line_length: int) -> str:
"""Format a signature using Black.
Parameters:
code: The code to format.
signature: The signature to format.
line_length: The line length to give to Black.
Returns:
The same code, formatted.
"""
from black import Mode, format_str

if len(code) >= line_length:
mode = Mode(line_length=line_length)
return format_str(f"def {code}: pass", mode=mode)[4:-6].strip()
return code
code = signature.strip()
if len(code) < line_length:
return code
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]

def do_order_members(self, members: Sequence[Object | Alias], order: Order) -> Sequence[Object | Alias]:
"""Order members given an ordering method.
Expand Down
19 changes: 10 additions & 9 deletions src/mkdocstrings/templates/python/material/_base/function.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,7 @@
toc_label=function.name ~ "()") %}

{% if config.separate_signature %}
<details>
<summary>Signature</summary>
{% filter highlight(language="python", inline=False) %}
{% filter format_code(config.line_length) %}
{% if show_full_path %}{{ function.path }}{% else %}{{ function.name }}{% endif %}
{% include "signature.html" with context %}
{% endfilter %}
{% endfilter %}
</details>
{% if show_full_path %}{{ function.path }}{% else %}{{ function.name }}{% endif %}()
{% else %}
{% filter highlight(language="python", inline=True) %}
{% if show_full_path %}{{ function.path }}{% else %}{{ function.name }}{% endif %}
Expand All @@ -45,6 +37,15 @@

{% endfilter %}

{% if config.separate_signature %}
{% filter highlight(language="python", inline=False) %}
{% filter format_signature(config.line_length) %}
{% if show_full_path %}{{ function.path }}{% else %}{{ function.name }}{% endif %}
{% include "signature.html" with context %}
{% endfilter %}
{% endfilter %}
{% endif %}

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

0 comments on commit da6e81c

Please sign in to comment.