-
Notifications
You must be signed in to change notification settings - Fork 161
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
Add find_references to index #1044
Conversation
@@ -164,6 +164,16 @@ def index_single(indexable_path, source = nil) | |||
# If `path` is a directory, just ignore it and continue indexing | |||
end | |||
|
|||
# Finds references to a fully qualified in all indexed files |
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.
Is this going to run for every request to find the references of a constant? How fast is it on a big project with a bunch of dependencies?
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.
Yeah, that's the idea. I'll collect some data so that we know roughly what to expect.
Given that the operation of asking for references is user initiated and not on the critical performance path, I think it should be fine. There's also the option to show a loading dialogue while we search.
@index = index | ||
@path = path | ||
@fully_qualified_name = fully_qualified_name | ||
@nesting = T.let([], T::Array[String]) |
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.
This seems to be a common behavior for a bunch of visitors, I wonder if we could provide this feature through a super class?
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.
That's fair, we can have a parent class 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.
Actually, I tried this refactor and it makes things a little weird for our indexing visitor, since we need to stuff to happen between the push and pop of the stack. I think this might be a bit premature.
Let's wait a bit until we have a clearer picture and then we can refactor with more confidence.
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 think the superclass can introduce a visit_class_body
and visit_module_body
methods for example that are to be redefined in the subclasses.
So the subclasses do not need to touch visit_class_node
and get the nesting behavior for free.
c9e1410
to
a480134
Compare
a480134
to
4f905fe
Compare
I'm going to put this on hold for the moment. Finding references like this takes around 40 seconds, which also slows down Sorbet's response because VS Code keeps waiting for all LSPs to return before showing results. I did extensive profiling of our indexing mechanism and the biggest gains we can have come from making Prism instantiate When we can make those instantiations lazily, indexing will be significantly sped up. Then we can make a better decision and maybe even collect references in the first indexing pass. |
Motivation
First step towards #202. At least for classes, modules and constants.
This PR adds a method to find references to a fully qualified name in the indexer, so that we can provide the references request.
Implementation
The idea is that we go through all indexed file paths, using a new visitor to find references that match the given fully qualified name. We collect everything and return.
I find this implementation really interesting because it surfaces references in gems too. So, if you want to know how
ActiveRecord::Base
is used inside Rails, you can just ask for references on the class and it'll bring you results.Automated Tests
Added tests.