Skip to content

Commit 4b42a24

Browse files
committed
Pass use_document_path to jedi_script when getting definitions
1 parent 58b229b commit 4b42a24

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

Diff for: pylsp/plugins/definition.py

+1-1
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)

Diff for: test/plugins/test_definitions.py

+35
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)

0 commit comments

Comments
 (0)