Skip to content

Commit ecf3dbe

Browse files
committed
Update pyls hover to specify language of signature.
LSP supports `MarkedString[]` to specify render engine. We can definitely say that function signature should be rendered as python. For the docstring, as we have no good way of parsing it to guess the engine, we can pass it as markdown.
1 parent 35979d3 commit ecf3dbe

3 files changed

Lines changed: 25 additions & 7 deletions

File tree

pyls/plugins/hover.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright 2017 Palantir Technologies, Inc.
22
import logging
3+
from jedi.api.classes import Signature
34
from pyls import hookimpl, _utils
45

56
log = logging.getLogger(__name__)
@@ -10,9 +11,26 @@ def pyls_hover(document, position):
1011
definitions = document.jedi_script(position).goto_definitions()
1112
word = document.word_at_position(position)
1213

13-
# Find an exact match for a completion
14-
for d in definitions:
15-
if d.name == word:
16-
return {'contents': _utils.format_docstring(d.docstring()) or ''}
14+
# Find first exact matching definition
15+
definition = next((x for x in definitions if x.name == word), None)
1716

18-
return {'contents': ''}
17+
if not definition:
18+
return {'contents': ''}
19+
20+
# raw docstring returns only doc, without signature
21+
doc = _utils.format_docstring(definition.docstring(raw=True))
22+
23+
# Find first exact matching signature
24+
signature = next((x.to_string() for x in definition.get_signatures() if x.name == word), '')
25+
26+
contents = []
27+
if signature:
28+
contents.append({
29+
'language': 'python',
30+
'value': signature,
31+
})
32+
if doc:
33+
contents.append(doc)
34+
if not contents:
35+
return {'contents': ''}
36+
return {'contents': contents}

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
'future>=0.14.0',
3737
'futures; python_version<"3.2"',
3838
'backports.functools_lru_cache; python_version<"3.2"',
39-
'jedi>=0.13.2,<0.15,!=0.14.0',
39+
'jedi>=0.15.0',
4040
'python-jsonrpc-server>=0.1.0',
4141
'pluggy'
4242
],

test/plugins/test_hover.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_hover():
2121
doc = Document(DOC_URI, DOC)
2222

2323
assert {
24-
'contents': 'main()\n\nhello world'
24+
'contents': [{'language': 'python', 'value': 'main()'}, 'hello world']
2525
} == pyls_hover(doc, hov_position)
2626

2727
assert {'contents': ''} == pyls_hover(doc, no_hov_position)

0 commit comments

Comments
 (0)