Skip to content

Commit

Permalink
Pass use_document_path to jedi_script when getting definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ccordoba12 committed Jul 28, 2021
1 parent 58b229b commit 4b42a24
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pylsp/plugins/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
def pylsp_definitions(config, document, position):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script().goto(
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)
Expand Down
35 changes: 35 additions & 0 deletions test/plugins/test_definitions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright 2017-2020 Palantir Technologies, Inc.
# Copyright 2021- Python Language Server Contributors.

import os

from pylsp import uris
from pylsp.plugins.definition import pylsp_definitions
from pylsp.workspace import Document
Expand Down Expand Up @@ -57,3 +59,36 @@ def test_assignment(config, workspace):

doc = Document(DOC_URI, workspace, DOC)
assert [{'uri': DOC_URI, 'range': def_range}] == pylsp_definitions(config, doc, cursor_pos)


def test_document_path_definitions(config, workspace_other_root_path, tmpdir):
# Create a dummy module out of the workspace's root_path and try to get
# a definition on it in another file placed next to it.
module_content = '''
def foo():
pass
'''

p = tmpdir.join("mymodule.py")
p.write(module_content)

# Content of doc to test definition
doc_content = """from mymodule import foo"""
doc_path = str(tmpdir) + os.path.sep + 'myfile.py'
doc_uri = uris.from_fs_path(doc_path)
doc = Document(doc_uri, workspace_other_root_path, doc_content)

# The range where is defined in mymodule.py
def_range = {
'start': {'line': 1, 'character': 4},
'end': {'line': 1, 'character': 7}
}

# The position where foo is called in myfile.py
cursor_pos = {'line': 0, 'character': 24}

# The uri for mymodule.py
module_path = str(p)
module_uri = uris.from_fs_path(module_path)

assert [{'uri': module_uri, 'range': def_range}] == pylsp_definitions(config, doc, cursor_pos)

0 comments on commit 4b42a24

Please sign in to comment.