diff --git a/pyls/_utils.py b/pyls/_utils.py index ae8258ac..6b305435 100644 --- a/pyls/_utils.py +++ b/pyls/_utils.py @@ -5,7 +5,7 @@ import os import sys import threading -from typing import List +from typing import List, Dict import docstring_to_markdown import jedi @@ -146,7 +146,7 @@ def wrap_signature(signature): return '```python\n' + signature + '\n```\n' -def format_docstring(contents, signatures: List[str] = None): +def format_docstring(contents, signatures: List[str] = None) -> Dict: """Python doc strings come in a number of formats, but LSP wants markdown.""" if not contents: return contents @@ -167,7 +167,14 @@ def format_docstring(contents, signatures: List[str] = None): value = value[len(signature):] value = prefix + '\n\n' + value except docstring_to_markdown.UnknownFormatError: - return contents.replace('\t', u'\u00A0' * 4).replace(' ', u'\u00A0' * 2) + plain = '' + if signatures: + plain += '\n'.join(signatures) + '\n\n' + plain += contents.replace('\t', u'\u00A0' * 4).replace(' ', u'\u00A0' * 2) + return { + 'kind': 'plaintext', + 'value': plain + } return { 'kind': 'markdown', diff --git a/pyls/plugins/hover.py b/pyls/plugins/hover.py index 9332a52d..985c54b5 100644 --- a/pyls/plugins/hover.py +++ b/pyls/plugins/hover.py @@ -25,24 +25,9 @@ def pyls_hover(document, position): if not definition: return {'contents': ''} - # raw docstring returns only doc, without signature - doc = _utils.format_docstring(definition.docstring(raw=True)) + doc = _utils.format_docstring( + definition.docstring(raw=True), + signatures=[d.to_string() for d in definition.get_signatures()] + ) - # Find first exact matching signature - signature = next((x.to_string() for x in definition.get_signatures() - if x.name == word), '') - - contents = [] - if signature: - contents.append({ - 'language': 'python', - 'value': signature, - }) - - if doc: - contents.append(doc) - - if not contents: - return {'contents': ''} - - return {'contents': contents} + return {'contents': doc} diff --git a/test/plugins/test_completion.py b/test/plugins/test_completion.py index 21acf33b..b2d92b85 100644 --- a/test/plugins/test_completion.py +++ b/test/plugins/test_completion.py @@ -87,7 +87,7 @@ def test_jedi_completion_item_resolve(config, workspace): resolved_documented_hello = pyls_jedi_completion_item_resolve( completion_item=documented_hello_item ) - assert 'Sends a polite greeting' in resolved_documented_hello['documentation'] + assert 'Sends a polite greeting' in resolved_documented_hello['documentation']['value'] def test_jedi_completion_with_fuzzy_enabled(config, workspace): @@ -350,7 +350,7 @@ def spam(): assert completions[0]['label'] == 'spam()' -@pytest.mark.skipif(PY2 or not LINUX or not CI, reason="tested on linux and python 3 only") +@pytest.mark.skipif(not LINUX or not CI, reason="tested on linux and CI") def test_jedi_completion_environment(workspace): # Content of doc to test completion doc_content = '''import logh @@ -377,7 +377,7 @@ def test_jedi_completion_environment(workspace): assert completions[0]['label'] == 'loghub' resolved = pyls_jedi_completion_item_resolve(completions[0]) - assert 'changelog generator' in resolved['documentation'].lower() + assert 'changelog generator' in resolved['documentation']['value'].lower() def test_document_path_completions(tmpdir, workspace_other_root_path): diff --git a/test/plugins/test_hover.py b/test/plugins/test_hover.py index 9deb5a3b..d6b33a91 100644 --- a/test/plugins/test_hover.py +++ b/test/plugins/test_hover.py @@ -38,22 +38,22 @@ def test_numpy_hover(workspace): assert contents in pyls_hover(doc, no_hov_position)['contents'] contents = 'NumPy\n=====\n\nProvides\n' - hov_1 = pyls_hover(doc, numpy_hov_position_1)['contents'][0] + hov_1 = pyls_hover(doc, numpy_hov_position_1)['contents'] assert hov_1['kind'] == 'markdown' assert contents in hov_1['value'] contents = 'NumPy\n=====\n\nProvides\n' - hov_2 = pyls_hover(doc, numpy_hov_position_2)['contents'][0] + hov_2 = pyls_hover(doc, numpy_hov_position_2)['contents'] assert hov_2['kind'] == 'markdown' assert contents in hov_2['value'] contents = 'NumPy\n=====\n\nProvides\n' - hov_3 = pyls_hover(doc, numpy_hov_position_3)['contents'][0] + hov_3 = pyls_hover(doc, numpy_hov_position_3)['contents'] assert hov_3['kind'] == 'markdown' assert contents in hov_3['value'] contents = 'Trigonometric sine, element-wise.\n\n' - hov_sin = pyls_hover(doc, numpy_sin_hov_position)['contents'][0] + hov_sin = pyls_hover(doc, numpy_sin_hov_position)['contents'] assert hov_sin['kind'] == 'markdown' assert contents in hov_sin['value'] @@ -66,10 +66,10 @@ def test_hover(workspace): doc = Document(DOC_URI, workspace, DOC) - contents = [{'language': 'python', 'value': 'main()'}, 'hello world'] - - assert { - 'contents': contents - } == pyls_hover(doc, hov_position) + contents = { + 'kind': 'plaintext', + 'value': 'main()\n\nhello world' + } + assert {'contents': contents} == pyls_hover(doc, hov_position) assert {'contents': ''} == pyls_hover(doc, no_hov_position)