Skip to content
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

Replace RubyIndexer::Collector with RubyIndexer::DeclarationListener #2008

Merged
merged 1 commit into from
May 7, 2024

Conversation

st0012
Copy link
Member

@st0012 st0012 commented May 3, 2024

Motivation

This rewrite will give us a few benefits:

  • We can use the same pattern as we write request listeners, which will make maintenance easier.
  • It will make some future indexing features easier to implement. With collector, we don't have access to a node's leave events, which makes features like capturing visibility hard to implement.
  • Since Prism::Dispatcher visits all nodes in the AST, we can now
    collect nodes that are defined unconventionally, like behind conditionals
    or begin blocks, just by doing the switching.

Unfortunately, this change will make indexing slower. In Core, it increases the indexing time from ~32 seconds to ~41 seconds (~28%). But on the other hand, if in the future Prism::Dispatcher receives performance improvements, the indexer will benefit automatically from them too.

Implementation

  1. Created a new RubyIndexer::DeclarationListener class
  2. Besides having to take in a Prism::Dispatcher and register the relevant events, DeclarationListener can reuse most methods from Collector with minor tweaks and renaming.
  3. Replaced all Collector references with DeclarationListener.

Automated Tests

I added a few simple tests for the newly supported cases.

Manual Tests

@st0012 st0012 added chore Chore task server This pull request should be included in the server gem's release notes labels May 3, 2024
@st0012 st0012 requested a review from a team as a code owner May 3, 2024 16:08
@st0012 st0012 requested review from andyw8 and vinistock May 3, 2024 16:08
@st0012 st0012 force-pushed the rewrite-index-collector branch 2 times, most recently from 6739fe9 to 9584ebd Compare May 3, 2024 17:45
@st0012 st0012 added enhancement New feature or request and removed chore Chore task labels May 3, 2024
exe/ruby-lsp-check Outdated Show resolved Hide resolved
@st0012 st0012 self-assigned this May 3, 2024
Copy link
Member

@vinistock vinistock left a comment

Choose a reason for hiding this comment

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

Overall, looks good to me. I'm not super happy with the name Listener, though. Hopefully there's something more descriptive we can use.

exe/ruby-lsp-check Outdated Show resolved Hide resolved
@@ -185,9 +185,11 @@ def index_all(indexable_paths: RubyIndexer.configuration.indexables, &block)
sig { params(indexable_path: IndexablePath, source: T.nilable(String)).void }
def index_single(indexable_path, source = nil)
content = source || File.read(indexable_path.full_path)
dispatcher = Prism::Dispatcher.new
Copy link
Member

Choose a reason for hiding this comment

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

If we want to be able to index at the same time that we run semantic highlighting, document symbol and others, then we'll need to receiver the dispatcher as an argument. Maybe we can make it optional with the default of Prism::Dispatcher.new?

Copy link
Member Author

Choose a reason for hiding this comment

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

While I agree with the idea, I prefer leaving this change to the PR where we're actually doing so as it will make the change linked its dependencies. If we do it in this PR, it'd be hard to understand the intention without checking the PR discussions.

lib/ruby_indexer/lib/ruby_indexer/listener.rb Outdated Show resolved Hide resolved
lib/ruby_indexer/lib/ruby_indexer/listener.rb Outdated Show resolved Hide resolved
This rewrite will give us a few benefits:

- We can use the same pattern as we write request listeners, which will
  make maintenance easier.
- It will make some future indexing features easier to implement. With
  collector, we don't have access to a node's leave events, which makes
  features like capturing visibility hard to implement.
- Since `Prism::Dispatcher` visits all nodes in the AST, we can now
  collect nodes that are defined unconventionally, like behind conditionals
  or begin blocks, just by doing the switching.

Unfortunately, this change will make indexing slower. In Core, it increases
the indexing time from ~32 seconds to ~41 seconds (~28%). But on the other
hand, if in the future `Prism::Dispatcher` receives performance improvements,
the indexer will benefit automatically from them too.
@st0012 st0012 changed the title Replace RubyIndexer::Collector with RubyIndexer::Listener Replace RubyIndexer::Collector with RubyIndexer::DeclarationListener May 3, 2024
@st0012 st0012 merged commit 1f2c028 into main May 7, 2024
39 checks passed
@st0012 st0012 deleted the rewrite-index-collector branch May 7, 2024 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request server This pull request should be included in the server gem's release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants