-
Notifications
You must be signed in to change notification settings - Fork 221
Add support for type definition #645
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
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
902fb5b
Add textDocument/typeDefinition plugin
Hoblovski 86a91ff
Add tests for textDocument/typeDefinition
Hoblovski 75f6234
Add jedi_ plugin prefix for consistency.
Hoblovski a94ac27
Fix order in CONFIGURATION.md.
Hoblovski 18705e7
Merge branch 'develop' into feat/type_definition
krassowski fbe97d2
Merge branch 'develop' into feat/type_definition
krassowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright 2017-2020 Palantir Technologies, Inc. | ||
krassowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Copyright 2021- Python Language Server Contributors. | ||
|
||
import logging | ||
|
||
from pylsp import _utils, hookimpl | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def lsp_location(name): | ||
module_path = name.module_path | ||
if module_path is None or name.line is None or name.column is None: | ||
return None | ||
uri = module_path.as_uri() | ||
return { | ||
"uri": str(uri), | ||
"range": { | ||
"start": {"line": name.line - 1, "character": name.column}, | ||
"end": {"line": name.line - 1, "character": name.column + len(name.name)}, | ||
}, | ||
} | ||
|
||
|
||
@hookimpl | ||
def pylsp_type_definition(config, document, position): | ||
try: | ||
kwargs = _utils.position_to_jedi_linecolumn(document, position) | ||
script = document.jedi_script() | ||
names = script.infer(**kwargs) | ||
definitions = [ | ||
definition | ||
for definition in [lsp_location(name) for name in names] | ||
if definition is not None | ||
] | ||
return definitions | ||
except Exception as e: | ||
log.debug("Failed to run type_definition: %s", e) | ||
return [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# Copyright 2017-2020 Palantir Technologies, Inc. | ||
krassowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Copyright 2021- Python Language Server Contributors. | ||
|
||
import os | ||
|
||
from pylsp import uris | ||
from pylsp.plugins.type_definition import pylsp_type_definition | ||
from pylsp.workspace import Document | ||
|
||
DOC_URI = uris.from_fs_path(__file__) | ||
DOC = """\ | ||
from dataclasses import dataclass | ||
|
||
@dataclass | ||
class IntPair: | ||
a: int | ||
b: int | ||
|
||
def main() -> None: | ||
l0 = list(1, 2) | ||
|
||
my_pair = IntPair(a=10, b=20) | ||
print(f"Original pair: {my_pair}") | ||
""" | ||
|
||
|
||
def test_type_definitions(config, workspace) -> None: | ||
# Over 'IntPair' in 'main' | ||
cursor_pos = {"line": 10, "character": 14} | ||
|
||
# The definition of 'IntPair' | ||
def_range = { | ||
"start": {"line": 3, "character": 6}, | ||
"end": {"line": 3, "character": 13}, | ||
} | ||
|
||
doc = Document(DOC_URI, workspace, DOC) | ||
assert [{"uri": DOC_URI, "range": def_range}] == pylsp_type_definition( | ||
config, doc, cursor_pos | ||
) | ||
|
||
|
||
def test_builtin_definition(config, workspace) -> None: | ||
# Over 'list' in main | ||
cursor_pos = {"line": 8, "character": 9} | ||
|
||
doc = Document(DOC_URI, workspace, DOC) | ||
orig_settings = config.settings() | ||
|
||
defns = pylsp_type_definition(config, doc, cursor_pos) | ||
assert len(defns) == 1 | ||
assert defns[0]["uri"].endswith("builtins.pyi") | ||
|
||
|
||
def test_mutli_file_type_definitions(config, workspace, tmpdir) -> None: | ||
# 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 = """\ | ||
from dataclasses import dataclass | ||
|
||
@dataclass | ||
class IntPair: | ||
a: int | ||
b: int | ||
""" | ||
p1 = tmpdir.join("intpair.py") | ||
p1.write(module_content) | ||
# The uri for intpair.py | ||
module_path = str(p1) | ||
module_uri = uris.from_fs_path(module_path) | ||
|
||
# Content of doc to test type definition | ||
doc_content = """\ | ||
from intpair import IntPair | ||
|
||
def main() -> None: | ||
l0 = list(1, 2) | ||
|
||
my_pair = IntPair(a=10, b=20) | ||
print(f"Original pair: {my_pair}") | ||
""" | ||
p2 = tmpdir.join("main.py") | ||
p2.write(doc_content) | ||
doc_path = str(p2) | ||
doc_uri = uris.from_fs_path(doc_path) | ||
|
||
doc = Document(doc_uri, workspace, doc_content) | ||
|
||
# The range where IntPair is defined in intpair.py | ||
def_range = { | ||
"start": {"line": 3, "character": 6}, | ||
"end": {"line": 3, "character": 13}, | ||
} | ||
|
||
# The position where IntPair is called in main.py | ||
cursor_pos = {"line": 5, "character": 14} | ||
|
||
print("!URI", module_uri) | ||
print("!URI", doc_uri) | ||
krassowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
assert [{"uri": module_uri, "range": def_range}] == pylsp_type_definition( | ||
config, doc, cursor_pos | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.