Skip to content

Separate signature from docstring on hover #623

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

Merged
merged 4 commits into from
Sep 11, 2019
Merged
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
1 change: 0 additions & 1 deletion pyls/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ def format_docstring(contents):
"""
contents = contents.replace('\t', u'\u00A0' * 4)
contents = contents.replace(' ', u'\u00A0' * 2)
contents = contents.replace('*', '\\*')
return contents


Expand Down
27 changes: 22 additions & 5 deletions pyls/plugins/hover.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,26 @@ def pyls_hover(document, position):
definitions = document.jedi_script(position).goto_definitions()
word = document.word_at_position(position)

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

return {'contents': ''}
if not definition:
return {'contents': ''}

# raw docstring returns only doc, without signature
doc = _utils.format_docstring(definition.docstring(raw=True))

# 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}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
'future>=0.14.0',
'futures; python_version<"3.2"',
'backports.functools_lru_cache; python_version<"3.2"',
'jedi>=0.14.1,<0.15',
'jedi>=0.15.0,<0.16',
'python-jsonrpc-server>=0.1.0',
'pluggy'
],
Expand Down
2 changes: 1 addition & 1 deletion test/plugins/test_hover.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_hover():
doc = Document(DOC_URI, DOC)

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

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