Skip to content

Commit

Permalink
Make use_document_path equal to True when getting definitions and hov…
Browse files Browse the repository at this point in the history
…ers (#62)
  • Loading branch information
ccordoba12 committed Jul 28, 2021
1 parent f18f7ee commit 32bbc06
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
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
2 changes: 1 addition & 1 deletion pylsp/plugins/hover.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@hookimpl
def pylsp_hover(document, position):
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script().infer(**code_position)
definitions = document.jedi_script(use_document_path=True).infer(**code_position)
word = document.word_at_position(position)

# Find first exact matching definition
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)
27 changes: 27 additions & 0 deletions test/plugins/test_hover.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.hover import pylsp_hover
from pylsp.workspace import Document
Expand Down Expand Up @@ -72,3 +74,28 @@ def test_hover(workspace):
} == pylsp_hover(doc, hov_position)

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


def test_document_path_hover(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():
"""A docstring for foo."""
pass
'''

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

# Content of doc to test definition
doc_content = """from mymodule import foo
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)

cursor_pos = {'line': 1, 'character': 3}
contents = pylsp_hover(doc, cursor_pos)['contents']

assert contents[1] == 'A docstring for foo.'
1 change: 1 addition & 0 deletions test/test_language_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def client_exited_server():
assert client_server_pair.process.is_alive() is False


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

0 comments on commit 32bbc06

Please sign in to comment.