Skip to content

Migrate away from deprecated LSP MarkedString type for hover #4

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 2 commits into from
Feb 18, 2021
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
13 changes: 10 additions & 3 deletions pyls/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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',
Expand Down
25 changes: 5 additions & 20 deletions pyls/plugins/hover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
6 changes: 3 additions & 3 deletions test/plugins/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
18 changes: 9 additions & 9 deletions test/plugins/test_hover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand All @@ -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)