Skip to content

Commit e962b88

Browse files
committed
feat: Support separate attribute signature
1 parent ac11970 commit e962b88

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/mkdocstrings_handlers/python/renderer.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,25 @@ def update_env(self, md: Markdown, config: dict) -> None: # noqa: D102 (ignore
135135
self.env.filters["crossref"] = self.do_crossref
136136
self.env.filters["multi_crossref"] = self.do_multi_crossref
137137
self.env.filters["order_members"] = self.do_order_members
138+
self.env.filters["format_code"] = self.do_format_code
138139
self.env.filters["format_signature"] = self.do_format_signature
139140

141+
def do_format_code(self, code: str, line_length: int) -> str:
142+
"""Format code using Black.
143+
144+
Parameters:
145+
code: The code to format.
146+
line_length: The line length to give to Black.
147+
148+
Returns:
149+
The same code, formatted.
150+
"""
151+
code = code.strip()
152+
if len(code) < line_length:
153+
return code
154+
formatter = _get_black_formatter()
155+
return formatter(code, line_length)
156+
140157
def do_format_signature(self, signature: str, line_length: int) -> str:
141158
"""Format a signature using Black.
142159
@@ -151,7 +168,9 @@ def do_format_signature(self, signature: str, line_length: int) -> str:
151168
if len(code) < line_length:
152169
return code
153170
formatter = _get_black_formatter()
154-
return formatter(code, line_length)
171+
formatted = formatter(f"def {code}: pass", line_length)
172+
# remove starting `def ` and trailing `: pass`
173+
return formatted[4:-5].strip()[:-1]
155174

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

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

226243
return formatter

src/mkdocstrings_handlers/python/templates/material/_base/attribute.html

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,32 @@
2222
class="doc doc-heading",
2323
toc_label=attribute.name) %}
2424

25-
{% filter highlight(language="python", inline=True) %}
25+
{% if config.separate_signature %}
2626
{% if show_full_path %}{{ attribute.path }}{% else %}{{ attribute.name }}{% endif %}
27-
{% if attribute.annotation %}: {{ attribute.annotation }}{% endif %}
28-
{% if attribute.value %} = {{ attribute.value }}{% endif %}
29-
{% endfilter %}
27+
{% else %}
28+
{% filter highlight(language="python", inline=True) %}
29+
{% if show_full_path %}{{ attribute.path }}{% else %}{{ attribute.name }}{% endif %}
30+
{% if attribute.annotation %}: {{ attribute.annotation }}{% endif %}
31+
{% if attribute.value %} = {{ attribute.value }}{% endif %}
32+
{% endfilter %}
33+
{% endif %}
3034

3135
{% with labels = attribute.labels %}
3236
{% include "labels.html" with context %}
3337
{% endwith %}
3438

3539
{% endfilter %}
3640

41+
{% if config.separate_signature %}
42+
{% filter highlight(language="python", inline=False) %}
43+
{% filter format_code(config.line_length) %}
44+
{% if show_full_path %}{{ attribute.path }}{% else %}{{ attribute.name }}{% endif %}
45+
{% if attribute.annotation %}: {{ attribute.annotation|safe }}{% endif %}
46+
{% if attribute.value %} = {{ attribute.value|safe }}{% endif %}
47+
{% endfilter %}
48+
{% endfilter %}
49+
{% endif %}
50+
3751
{% else %}
3852
{% if config.show_root_toc_entry %}
3953
{% filter heading(heading_level,

0 commit comments

Comments
 (0)