Skip to content

Commit 32bbc06

Browse files
authored
Make use_document_path equal to True when getting definitions and hovers (#62)
1 parent f18f7ee commit 32bbc06

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

pylsp/plugins/definition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
def pylsp_definitions(config, document, position):
1212
settings = config.plugin_settings('jedi_definition')
1313
code_position = _utils.position_to_jedi_linecolumn(document, position)
14-
definitions = document.jedi_script().goto(
14+
definitions = document.jedi_script(use_document_path=True).goto(
1515
follow_imports=settings.get('follow_imports', True),
1616
follow_builtin_imports=settings.get('follow_builtin_imports', True),
1717
**code_position)

pylsp/plugins/hover.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@hookimpl
1212
def pylsp_hover(document, position):
1313
code_position = _utils.position_to_jedi_linecolumn(document, position)
14-
definitions = document.jedi_script().infer(**code_position)
14+
definitions = document.jedi_script(use_document_path=True).infer(**code_position)
1515
word = document.word_at_position(position)
1616

1717
# Find first exact matching definition

test/plugins/test_definitions.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright 2017-2020 Palantir Technologies, Inc.
22
# Copyright 2021- Python Language Server Contributors.
33

4+
import os
5+
46
from pylsp import uris
57
from pylsp.plugins.definition import pylsp_definitions
68
from pylsp.workspace import Document
@@ -57,3 +59,36 @@ def test_assignment(config, workspace):
5759

5860
doc = Document(DOC_URI, workspace, DOC)
5961
assert [{'uri': DOC_URI, 'range': def_range}] == pylsp_definitions(config, doc, cursor_pos)
62+
63+
64+
def test_document_path_definitions(config, workspace_other_root_path, tmpdir):
65+
# Create a dummy module out of the workspace's root_path and try to get
66+
# a definition on it in another file placed next to it.
67+
module_content = '''
68+
def foo():
69+
pass
70+
'''
71+
72+
p = tmpdir.join("mymodule.py")
73+
p.write(module_content)
74+
75+
# Content of doc to test definition
76+
doc_content = """from mymodule import foo"""
77+
doc_path = str(tmpdir) + os.path.sep + 'myfile.py'
78+
doc_uri = uris.from_fs_path(doc_path)
79+
doc = Document(doc_uri, workspace_other_root_path, doc_content)
80+
81+
# The range where is defined in mymodule.py
82+
def_range = {
83+
'start': {'line': 1, 'character': 4},
84+
'end': {'line': 1, 'character': 7}
85+
}
86+
87+
# The position where foo is called in myfile.py
88+
cursor_pos = {'line': 0, 'character': 24}
89+
90+
# The uri for mymodule.py
91+
module_path = str(p)
92+
module_uri = uris.from_fs_path(module_path)
93+
94+
assert [{'uri': module_uri, 'range': def_range}] == pylsp_definitions(config, doc, cursor_pos)

test/plugins/test_hover.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright 2017-2020 Palantir Technologies, Inc.
22
# Copyright 2021- Python Language Server Contributors.
33

4+
import os
5+
46
from pylsp import uris
57
from pylsp.plugins.hover import pylsp_hover
68
from pylsp.workspace import Document
@@ -72,3 +74,28 @@ def test_hover(workspace):
7274
} == pylsp_hover(doc, hov_position)
7375

7476
assert {'contents': ''} == pylsp_hover(doc, no_hov_position)
77+
78+
79+
def test_document_path_hover(workspace_other_root_path, tmpdir):
80+
# Create a dummy module out of the workspace's root_path and try to get
81+
# a definition on it in another file placed next to it.
82+
module_content = '''
83+
def foo():
84+
"""A docstring for foo."""
85+
pass
86+
'''
87+
88+
p = tmpdir.join("mymodule.py")
89+
p.write(module_content)
90+
91+
# Content of doc to test definition
92+
doc_content = """from mymodule import foo
93+
foo"""
94+
doc_path = str(tmpdir) + os.path.sep + 'myfile.py'
95+
doc_uri = uris.from_fs_path(doc_path)
96+
doc = Document(doc_uri, workspace_other_root_path, doc_content)
97+
98+
cursor_pos = {'line': 1, 'character': 3}
99+
contents = pylsp_hover(doc, cursor_pos)['contents']
100+
101+
assert contents[1] == 'A docstring for foo.'

test/test_language_server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def client_exited_server():
7575
assert client_server_pair.process.is_alive() is False
7676

7777

78+
@pytest.mark.skipif(sys.platform == 'darwin', reason='Too flaky on Mac')
7879
def test_initialize(client_server): # pylint: disable=redefined-outer-name
7980
response = client_server._endpoint.request('initialize', {
8081
'rootPath': os.path.dirname(__file__),

0 commit comments

Comments
 (0)