-
Notifications
You must be signed in to change notification settings - Fork 217
Add basic Goto Implementation Request support #644
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
base: develop
Are you sure you want to change the base?
Changes from 4 commits
84cb3a5
b7fb666
41a8852
9c715d1
c1437e8
f38d3ef
b460f31
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,70 @@ | ||||||||||||||||
# Copyright 2017-2020 Palantir Technologies, Inc. | ||||||||||||||||
# Copyright 2021- Python Language Server Contributors. | ||||||||||||||||
import logging | ||||||||||||||||
import os | ||||||||||||||||
from typing import Any | ||||||||||||||||
|
||||||||||||||||
from rope.base.project import Project | ||||||||||||||||
from rope.base.resources import Resource | ||||||||||||||||
from rope.contrib.findit import Location, find_implementations | ||||||||||||||||
|
||||||||||||||||
from pylsp import hookimpl, uris | ||||||||||||||||
|
||||||||||||||||
log = logging.getLogger(__name__) | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
@hookimpl | ||||||||||||||||
def pylsp_settings(): | ||||||||||||||||
# Default to enabled (no reason not to) | ||||||||||||||||
return {"plugins": {"rope_implementation": {"enabled": True}}} | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
@hookimpl | ||||||||||||||||
def pylsp_implementations(config, workspace, document, position): | ||||||||||||||||
offset = document.offset_at_position(position) | ||||||||||||||||
rope_config = config.settings(document_path=document.path).get("rope", {}) | ||||||||||||||||
rope_project = workspace._rope_project_builder(rope_config) | ||||||||||||||||
rope_resource = document._rope_resource(rope_config) | ||||||||||||||||
|
||||||||||||||||
impls = find_implementations(rope_project, rope_resource, offset) | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should it be wrapped in try-except to handle exceptions, as in: python-lsp-server/pylsp/plugins/rope_completion.py Lines 59 to 65 in 04fa3e5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||||||||||||
|
||||||||||||||||
return [ | ||||||||||||||||
{ | ||||||||||||||||
"uri": uris.uri_with( | ||||||||||||||||
document.uri, | ||||||||||||||||
path=os.path.join(workspace.root_path, impl.resource.path), | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Under certain circumstances I seem to get URIs like |
||||||||||||||||
), | ||||||||||||||||
"range": _rope_location_to_range(impl, rope_project), | ||||||||||||||||
} | ||||||||||||||||
for impl in impls | ||||||||||||||||
] | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
def _rope_location_to_range( | ||||||||||||||||
location: Location, rope_project: Project | ||||||||||||||||
) -> dict[str, Any]: | ||||||||||||||||
# NOTE: This assumes the result is confined to a single line, which should | ||||||||||||||||
# always be the case here because Python doesn't allow splitting up | ||||||||||||||||
# identifiers across more than one line. | ||||||||||||||||
start_column, end_column = _rope_region_to_columns( | ||||||||||||||||
location.region, location.lineno, location.resource, rope_project | ||||||||||||||||
) | ||||||||||||||||
return { | ||||||||||||||||
"start": {"line": location.lineno - 1, "character": start_column}, | ||||||||||||||||
"end": {"line": location.lineno - 1, "character": end_column}, | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
def _rope_region_to_columns( | ||||||||||||||||
offsets: tuple[int, int], line: int, rope_resource: Resource, rope_project: Project | ||||||||||||||||
) -> tuple[int, int]: | ||||||||||||||||
""" | ||||||||||||||||
Convert pair of offsets from start of file to columns within line. | ||||||||||||||||
|
||||||||||||||||
Assumes both offsets reside within the same line and will return nonsense | ||||||||||||||||
for the end offset if this isn't the case. | ||||||||||||||||
""" | ||||||||||||||||
line_start_offset = rope_project.get_pymodule(rope_resource).lines.get_line_start( | ||||||||||||||||
line | ||||||||||||||||
) | ||||||||||||||||
return offsets[0] - line_start_offset, offsets[1] - line_start_offset |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
class Animal(ABC): | ||
@abstractmethod | ||
def breathe(self): | ||
pass | ||
|
||
@property | ||
@abstractmethod | ||
def size(self) -> str: | ||
pass | ||
|
||
class WingedAnimal(Animal): | ||
@abstractmethod | ||
def fly(self, destination): | ||
pass | ||
|
||
class Bird(WingedAnimal): | ||
def breathe(self): | ||
print("*inhales like a bird*") | ||
|
||
def fly(self, destination): | ||
print("*flies like a bird*") | ||
|
||
@property | ||
def size(self) -> str: | ||
return "bird-sized" | ||
|
||
print("not a method at all") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# Copyright 2017-2020 Palantir Technologies, Inc. | ||
# Copyright 2021- Python Language Server Contributors. | ||
|
||
from collections.abc import Iterable | ||
from importlib.resources import as_file, files | ||
from pathlib import Path | ||
|
||
import pytest | ||
from rope.base.exceptions import BadIdentifierError | ||
|
||
from pylsp import uris | ||
from pylsp.config.config import Config | ||
from pylsp.plugins.rope_implementation import pylsp_implementations | ||
from pylsp.workspace import Workspace | ||
|
||
|
||
# We use a real file because the part of Rope that this feature uses | ||
# (`rope.findit.find_implementations`) *always* loads files from the | ||
# filesystem, in contrast to e.g. `code_assist` which takes a `source` argument | ||
# that can be more easily faked. | ||
# An alternative to using real files would be `unittest.mock.patch`, but that | ||
# ends up being more trouble than it's worth... | ||
@pytest.fixture | ||
def examples_dir_path() -> Iterable[Path]: | ||
with as_file(files("test.data.implementations_examples")) as path: | ||
smheidrich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
yield path | ||
|
||
|
||
@pytest.fixture | ||
def doc_uri(examples_dir_path: Path) -> str: | ||
return uris.from_fs_path(str(examples_dir_path / "example.py")) | ||
|
||
|
||
# Similarly to the above, we need our workspace to point to the actual location | ||
# on the filesystem containing the example modules, so we override the fixture: | ||
@pytest.fixture | ||
def workspace(examples_dir_path: Path, endpoint) -> None: | ||
ws = Workspace(uris.from_fs_path(str(examples_dir_path)), endpoint) | ||
ws._config = Config(ws.root_uri, {}, 0, {}) | ||
yield ws | ||
ws.close() | ||
|
||
|
||
def test_implementations(config, workspace, doc_uri) -> None: | ||
# Over 'fly' in WingedAnimal.fly | ||
cursor_pos = {"line": 14, "character": 8} | ||
|
||
# The implementation of 'Bird.fly' | ||
def_range = { | ||
"start": {"line": 21, "character": 8}, | ||
"end": {"line": 21, "character": 11}, | ||
} | ||
|
||
doc = workspace.get_document(doc_uri) | ||
assert [{"uri": doc_uri, "range": def_range}] == pylsp_implementations( | ||
config, workspace, doc, cursor_pos | ||
) | ||
|
||
|
||
def test_implementations_skipping_one_class(config, workspace, doc_uri) -> None: | ||
# Over 'Animal.breathe' | ||
cursor_pos = {"line": 4, "character": 8} | ||
|
||
# The implementation of 'breathe', skipping intermediate classes | ||
def_range = { | ||
"start": {"line": 18, "character": 8}, | ||
"end": {"line": 18, "character": 15}, | ||
} | ||
|
||
doc = workspace.get_document(doc_uri) | ||
assert [{"uri": doc_uri, "range": def_range}] == pylsp_implementations( | ||
config, workspace, doc, cursor_pos | ||
) | ||
|
||
|
||
@pytest.mark.xfail( | ||
reason="not implemented upstream (Rope)", strict=True, raises=BadIdentifierError | ||
) | ||
def test_property_implementations(config, workspace, doc_uri) -> None: | ||
# Over 'Animal.size' | ||
cursor_pos = {"line": 9, "character": 9} | ||
|
||
# The property implementation 'Bird.size' | ||
def_range = { | ||
"start": {"line": 25, "character": 8}, | ||
"end": {"line": 25, "character": 12}, | ||
} | ||
|
||
doc = workspace.get_document(doc_uri) | ||
assert [{"uri": doc_uri, "range": def_range}] == pylsp_implementations( | ||
config, workspace, doc, cursor_pos | ||
) | ||
|
||
|
||
def test_implementations_not_a_method(config, workspace, doc_uri) -> None: | ||
# Over 'print(...)' call => Rope error because not a method. | ||
cursor_pos = {"line": 28, "character": 0} | ||
|
||
doc = workspace.get_document(doc_uri) | ||
|
||
# This exception is turned into an empty result set automatically in upper | ||
# layers, so we just check that it is raised to document this behavior: | ||
with pytest.raises(BadIdentifierError): | ||
pylsp_implementations(config, workspace, doc, cursor_pos) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it would also make sense to add
find_definition
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you mean using
rope.contrib.findit.find_definition
to implement the LSPtextDocument/definition
request, that one is already implemented inpython-lsp-server
using Jedi, so I don't think adding an additional Rope-powered implementation makes sense. But maybe I misunderstand it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's what I meant. My reasoning is twofold:
Of note, rope is disabled by default in general because it is hard to guarantee it works well and does not conflict with jedi (e.g. by de-duplciating responses).
For the best UX for an average user I think it would be best to add
textDocument/implementation
using jedi, but I would not object to adding one with rope as in this PR.I was just curious as to your reasoning for adding only
textDocument/implementation
and not alsotextDocument/definition
which would be my choice if I was adding a rope version for it.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Comment deleted because it doesn't make sense, give me a minute while I write a new one)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally wrote a response disagreeing but there was a glaring mistake in it because I overlooked that the current Rope
find_implementations
implementation is much buggier than I thought: E.g. for a file likeusing Rope's
find_implementations
on thea.f()
call will go toB.f
for some reason 😕It works well for moving between overridden methods within a class hierarchy, where it nicely complements the Jedi
textDocument/definition
, which always moves "up", by moving "down" instead.But my guess is that it would take quite a lot of effort to fix Rope's
find_implementations
so it works correctly when used on method calls as well 😔I'll open an issue but this PR should be on hold until it's fixed, no point merging something that's fundamentally buggy from day 1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rope issue created:
findit.find_implementations
on method call finds implementation for different subclass python-rope/rope#812