Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow jedi_hover and jedi_signature_help plugins to hide docstrings. #589

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ This server can be configured using the `workspace/didChangeConfiguration` metho
| `pylsp.plugins.jedi_definition.follow_builtin_imports` | `boolean` | If follow_imports is True will decide if it follow builtin imports. | `true` |
| `pylsp.plugins.jedi_definition.follow_builtin_definitions` | `boolean` | Follow builtin and extension definitions to stubs. | `true` |
| `pylsp.plugins.jedi_hover.enabled` | `boolean` | Enable or disable the plugin. | `true` |
| `pylsp.plugins.jedi_hover.include_docstring` | `boolean` | Include signature docstring in the output. | `true` |
| `pylsp.plugins.jedi_references.enabled` | `boolean` | Enable or disable the plugin. | `true` |
| `pylsp.plugins.jedi_signature_help.enabled` | `boolean` | Enable or disable the plugin. | `true` |
| `pylsp.plugins.jedi_signature_help.include_docstring` | `boolean` | Include signature docstring in the output. | `true` |
| `pylsp.plugins.jedi_symbols.enabled` | `boolean` | Enable or disable the plugin. | `true` |
| `pylsp.plugins.jedi_symbols.all_scopes` | `boolean` | If True lists the names of all scopes instead of only the module namespace. | `true` |
| `pylsp.plugins.jedi_symbols.include_import_symbols` | `boolean` | If True includes symbols imported from other libraries. | `true` |
Expand Down
22 changes: 14 additions & 8 deletions pylsp/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,20 @@ def format_docstring(
contents = ""

if markup_kind == "markdown":
try:
value = docstring_to_markdown.convert(contents)
except docstring_to_markdown.UnknownFormatError:
# try to escape the Markdown syntax instead:
value = escape_markdown(contents)

if signatures:
value = wrap_signature("\n".join(signatures)) + "\n\n" + value
if contents != "":
try:
value = docstring_to_markdown.convert(contents)
except docstring_to_markdown.UnknownFormatError:
# try to escape the Markdown syntax instead:
value = escape_markdown(contents)

if signatures:
value = wrap_signature("\n".join(signatures)) + "\n\n" + value
else:
value = contents

if signatures:
value = wrap_signature("\n".join(signatures))

return {"kind": "markdown", "value": value}
value = contents
Expand Down
14 changes: 12 additions & 2 deletions pylsp/plugins/hover.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

@hookimpl
def pylsp_hover(config, document, position):
settings = config.plugin_settings("jedi_hover", document_path=document.path)
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).infer(**code_position)
word = document.word_at_position(position)
Expand Down Expand Up @@ -40,10 +41,19 @@ def pylsp_hover(config, document, position):
"",
)

include_docstring = settings.get("include_docstring", True)

# raw docstring returns only doc, without signature
docstring = definition.docstring(raw=True)
if include_docstring is False:
if signature:
docstring = ""
else:
docstring = docstring.strip().split("\n")[0].strip()

return {
"contents": _utils.format_docstring(
# raw docstring returns only doc, without signature
definition.docstring(raw=True),
docstring,
preferred_markup_kind,
signatures=[signature] if signature else None,
)
Expand Down
10 changes: 9 additions & 1 deletion pylsp/plugins/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

@hookimpl
def pylsp_signature_help(config, document, position):
settings = config.plugin_settings(
"jedi_signature_help", document_path=document.path
)
code_position = _utils.position_to_jedi_linecolumn(document, position)
signatures = document.jedi_script().get_signatures(**code_position)

Expand All @@ -41,10 +44,15 @@ def pylsp_signature_help(config, document, position):
# Docstring contains one or more lines of signature, followed by empty line, followed by docstring
function_sig_lines = (docstring.split("\n\n") or [""])[0].splitlines()
function_sig = " ".join([line.strip() for line in function_sig_lines])

signature_docstring = s.docstring(raw=True)
if settings.get("include_docstring", True) is False:
signature_docstring = ""

sig = {
"label": function_sig,
"documentation": _utils.format_docstring(
s.docstring(raw=True), markup_kind=preferred_markup_kind
signature_docstring, markup_kind=preferred_markup_kind
),
}

Expand Down
24 changes: 24 additions & 0 deletions test/plugins/test_hover.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import os

import pytest

from pylsp import uris
from pylsp.plugins.hover import pylsp_hover
from pylsp.workspace import Document
Expand All @@ -23,6 +25,17 @@ def main():
"""


@pytest.fixture
def workspace_with_hover_docstring_disabled(workspace) -> None:
workspace._config._settings["plugins"] = {
"jedi_hover": {
"include_docstring": False,
},
}

yield workspace


def test_numpy_hover(workspace) -> None:
# Over the blank line
no_hov_position = {"line": 1, "character": 0}
Expand Down Expand Up @@ -109,3 +122,14 @@ def foo():
contents = pylsp_hover(doc._config, doc, cursor_pos)["contents"]

assert "A docstring for foo." in contents["value"]


def test_hover_without_docstring(workspace_with_hover_docstring_disabled) -> None:
# Over 'main' in def main():
hov_position = {"line": 2, "character": 6}

doc = Document(DOC_URI, workspace_with_hover_docstring_disabled, DOC)

contents = {"kind": "markdown", "value": "```python\nmain()\n```\n"}

assert {"contents": contents} == pylsp_hover(doc._config, doc, hov_position)
25 changes: 25 additions & 0 deletions test/plugins/test_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ def main(param1=None,
"""


@pytest.fixture
def workspace_with_signature_docstring_disabled(workspace) -> None:
workspace._config._settings["plugins"] = {
"jedi_signature_help": {
"include_docstring": False,
},
}

yield workspace


def test_no_signature(workspace) -> None:
# Over blank line
sig_position = {"line": 9, "character": 0}
Expand Down Expand Up @@ -104,3 +115,17 @@ def test_docstring_params(regex, doc) -> None:
m = regex.match(doc)
assert m.group("param") == "test"
assert m.group("doc") == "parameter docstring"


def test_signature_without_docstring(
workspace_with_signature_docstring_disabled,
) -> None:
# Over '( ' in main(
sig_position = {"line": 10, "character": 5}
doc = Document(DOC_URI, workspace_with_signature_docstring_disabled, DOC)

sig_info = signature.pylsp_signature_help(doc._config, doc, sig_position)

sigs = sig_info["signatures"]
assert len(sigs) == 1
assert sigs[0]["documentation"] == {"kind": "markdown", "value": ""}