-
Notifications
You must be signed in to change notification settings - Fork 216
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?
Add basic Goto Implementation Request support #644
Conversation
282f64b
to
8604c25
Compare
These don't work because Rope's find_implementations doesn't take a `source` argument (unlike e.g. `code_assist`) and hence *always* loads the file from disk, which means we can't use a "fake" module unless we want to use `unittest.mock.patch` to override some of this behavior, which is hacky & bad.
8604c25
to
6dd3500
Compare
6dd3500
to
41a8852
Compare
pylsp/plugins/rope_implementation.py
Outdated
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 comment
The 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
try: | |
definitions = code_assist( | |
rope_project, document.source, offset, document_rope, maxfixes=3 | |
) | |
except Exception as e: | |
log.debug("Failed to run Rope code assist: %s", e) | |
return [] |
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.
Done
|
||
from rope.base.project import Project | ||
from rope.base.resources import Resource | ||
from rope.contrib.findit import Location, find_implementations |
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 LSP textDocument/definition
request, that one is already implemented in python-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:
- I think that jedi's implementation will return both implementation and definitions
- I believe that users who would want to use rope for this would likely want to disable the jedi implementation if they could have a matching rope implementation for consistency
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 also textDocument/definition
which would be my choice if I was adding a rope version for 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.
(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 like
class A:
def f(self): ...
class B(A):
def f(self): ...
a: A = A()
a.f()
using Rope's find_implementations
on the a.f()
call will go to B.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.
It looks like a few of the tests are failing:
|
{ | ||
"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 comment
The reason will be displayed to describe this comment to others. Learn more.
Under certain circumstances I seem to get URIs like ///example.py
here for a file that is actually in another directory - will have to investigate & fix.
Description
As pointed out in #97, support for the LSP Goto Implementation Request (
textDocument/implementation
) akaimplementationProvider
capability is still missing.This PR adds basic support for this feature using Rope's
rope.findit.find_implementations
function (so it only works if therope
extra is installed).Rope's
find_implementations
function seems a bit brittle, e.g. it doesn't appear to work for some slightly more complicated inheritance hierarchies (multiple levels & multiple inheritance & generics, but not sure which of these is the issue), but these things should be fixed upstream & in any case it's better than nothing.Status
findit.find_implementations
on method call finds implementation for different subclass python-rope/rope#812