Skip to content

Commit 5a50b50

Browse files
committed
Allow *signature* to hide the docstring.
1 parent 6a24326 commit 5a50b50

File tree

8 files changed

+84
-14
lines changed

8 files changed

+84
-14
lines changed

CONFIGURATION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ This server can be configured using the `workspace/didChangeConfiguration` metho
7777
| `pylsp.rope.extensionModules` | `string` | Builtin and c-extension modules that are allowed to be imported and inspected by rope. | `null` |
7878
| `pylsp.rope.ropeFolder` | `array` of unique `string` items | The name of the folder in which rope stores project configurations and data. Pass `null` for not using such a folder at all. | `null` |
7979
| `pylsp.signature.formatter` | `string` (one of: `'black'`, `'ruff'`, `None`) | Formatter to use for reformatting signatures in docstrings. | `"black"` |
80+
| `pylsp.signature.include_docstring` | `boolean` | Include signature docstring. | `true` |
8081
| `pylsp.signature.line_length` | `number` | Maximum line length in signatures. | `88` |
8182

8283
This documentation was generated from `pylsp/config/schema.json`. Please do not edit this file directly.

pylsp/_utils.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -315,17 +315,24 @@ def format_docstring(
315315
contents = ""
316316

317317
if markup_kind == "markdown":
318-
try:
319-
value = docstring_to_markdown.convert(contents)
320-
except docstring_to_markdown.UnknownFormatError:
321-
# try to escape the Markdown syntax instead:
322-
value = escape_markdown(contents)
323-
324-
if signatures:
325-
wrapped_signatures = convert_signatures_to_markdown(
326-
signatures, config=signature_config or {}
327-
)
328-
value = wrapped_signatures + "\n\n" + value
318+
wrapped_signatures = convert_signatures_to_markdown(
319+
signatures if signatures is not None else [], config=signature_config or {}
320+
)
321+
322+
if contents != "":
323+
try:
324+
value = docstring_to_markdown.convert(contents)
325+
except docstring_to_markdown.UnknownFormatError:
326+
# try to escape the Markdown syntax instead:
327+
value = escape_markdown(contents)
328+
329+
if signatures:
330+
value = wrapped_signatures + "\n\n" + value
331+
else:
332+
value = contents
333+
334+
if signatures:
335+
value = wrapped_signatures
329336

330337
return {"kind": "markdown", "value": value}
331338
value = contents

pylsp/config/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,11 @@
530530
"default": "black",
531531
"description": "Formatter to use for reformatting signatures in docstrings."
532532
},
533+
"pylsp.signature.include_docstring": {
534+
"type": "boolean",
535+
"default": true,
536+
"description": "Include signature docstring."
537+
},
533538
"pylsp.signature.line_length": {
534539
"type": "number",
535540
"default": 88,

pylsp/plugins/hover.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,19 @@ def pylsp_hover(config, document, position):
4141
"",
4242
)
4343

44+
include_docstring = signature_config.get("include_docstring", True)
45+
46+
# raw docstring returns only doc, without signature
47+
docstring = definition.docstring(raw=True)
48+
if include_docstring is False:
49+
if signature:
50+
docstring = ""
51+
else:
52+
docstring = docstring.strip().split("\n")[0].strip()
53+
4454
return {
4555
"contents": _utils.format_docstring(
46-
# raw docstring returns only doc, without signature
47-
definition.docstring(raw=True),
56+
docstring,
4857
preferred_markup_kind,
4958
signatures=[signature] if signature else None,
5059
signature_config=signature_config,

pylsp/plugins/signature.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
@hookimpl
1919
def pylsp_signature_help(config, document, position):
20+
signature_config = config.settings().get("signature", {})
2021
code_position = _utils.position_to_jedi_linecolumn(document, position)
2122
signatures = document.jedi_script().get_signatures(**code_position)
2223

@@ -41,10 +42,15 @@ def pylsp_signature_help(config, document, position):
4142
# Docstring contains one or more lines of signature, followed by empty line, followed by docstring
4243
function_sig_lines = (docstring.split("\n\n") or [""])[0].splitlines()
4344
function_sig = " ".join([line.strip() for line in function_sig_lines])
45+
46+
signature_docstring = s.docstring(raw=True)
47+
if signature_config.get("include_docstring", True) is False:
48+
signature_docstring = ""
49+
4450
sig = {
4551
"label": function_sig,
4652
"documentation": _utils.format_docstring(
47-
s.docstring(raw=True), markup_kind=preferred_markup_kind
53+
signature_docstring, markup_kind=preferred_markup_kind
4854
),
4955
}
5056

test/fixtures.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,17 @@ def client_server_pair() -> None:
177177
).result(timeout=CALL_TIMEOUT_IN_SECONDS)
178178
assert shutdown_response is None
179179
client_server_pair_obj.client._endpoint.notify("exit")
180+
181+
182+
@pytest.fixture
183+
def workspace_with_signature_docstring_disabled(workspace) -> None:
184+
workspace._config.update(
185+
{
186+
"signature": {
187+
**workspace._config.settings().get("signature", {}),
188+
"include_docstring": False,
189+
},
190+
}
191+
)
192+
193+
yield workspace

test/plugins/test_hover.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,17 @@ def foo():
143143
contents = pylsp_hover(doc._config, doc, cursor_pos)["contents"]
144144

145145
assert "A docstring for foo." in contents["value"]
146+
147+
148+
def test_hover_without_docstring(workspace_with_signature_docstring_disabled) -> None:
149+
# Over 'main' in def main():
150+
hov_position = {"line": 2, "character": 6}
151+
152+
doc = Document(DOC_URI, workspace_with_signature_docstring_disabled, DOC)
153+
154+
contents = {
155+
"kind": "markdown",
156+
"value": "```python\nmain(a: float, b: float)\n```\n",
157+
}
158+
159+
assert {"contents": contents} == pylsp_hover(doc._config, doc, hov_position)

test/plugins/test_signature.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,17 @@ def test_docstring_params(regex, doc) -> None:
104104
m = regex.match(doc)
105105
assert m.group("param") == "test"
106106
assert m.group("doc") == "parameter docstring"
107+
108+
109+
def test_signature_without_docstring(
110+
workspace_with_signature_docstring_disabled,
111+
) -> None:
112+
# Over '( ' in main(
113+
sig_position = {"line": 10, "character": 5}
114+
doc = Document(DOC_URI, workspace_with_signature_docstring_disabled, DOC)
115+
116+
sig_info = signature.pylsp_signature_help(doc._config, doc, sig_position)
117+
118+
sigs = sig_info["signatures"]
119+
assert len(sigs) == 1
120+
assert sigs[0]["documentation"] == {"kind": "markdown", "value": ""}

0 commit comments

Comments
 (0)