Skip to content

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

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

smheidrich
Copy link

@smheidrich smheidrich commented May 11, 2025

Description

As pointed out in #97, support for the LSP Goto Implementation Request (textDocument/implementation) aka implementationProvider 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 the rope 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

@smheidrich smheidrich force-pushed the implement-basic-goto-implementation branch 3 times, most recently from 282f64b to 8604c25 Compare May 11, 2025 16:10
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.
@smheidrich smheidrich force-pushed the implement-basic-goto-implementation branch from 8604c25 to 6dd3500 Compare May 11, 2025 20:32
@smheidrich smheidrich force-pushed the implement-basic-goto-implementation branch from 6dd3500 to 41a8852 Compare May 11, 2025 20:35
@smheidrich smheidrich marked this pull request as ready for review May 11, 2025 20:38
rope_project = workspace._rope_project_builder(rope_config)
rope_resource = document._rope_resource(rope_config)

impls = find_implementations(rope_project, rope_resource, offset)
Copy link
Member

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:

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 []

Copy link
Author

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
Copy link
Member

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?

Copy link
Author

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?

Copy link
Member

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.

Copy link
Author

@smheidrich smheidrich May 12, 2025

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)

Copy link
Author

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@krassowski
Copy link
Member

It looks like a few of the tests are failing:

ERROR test/plugins/test_implementations.py::test_implementations - TypeError: expected str, bytes or os.PathLike object, not NoneType
ERROR test/plugins/test_implementations.py::test_implementations_skipping_one_class - TypeError: expected str, bytes or os.PathLike object, not NoneType
ERROR test/plugins/test_implementations.py::test_property_implementations - TypeError: expected str, bytes or os.PathLike object, not NoneType
ERROR test/plugins/test_implementations.py::test_implementations_not_a_method - TypeError: expected str, bytes or os.PathLike object, not NoneType

{
"uri": uris.uri_with(
document.uri,
path=os.path.join(workspace.root_path, impl.resource.path),
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants