Shopify / Shopify/ruby-lsp

Lazy Index Entries

Open
#3,545 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement pinned
Dominant language
Ruby
Stars
2k
Forks
281
Avg merge
2h 14m
Merged PRs (30d)
6

Description

I have checked that this feature is not already implemented
  • This feature does not exist
Use case

For every Ruby DSL I can think of, false positives could be avoided by querying the index to understand the context of the call. For example, the belongs_to DSL method in Rails is only available on descendants of ActiveRecord::Base.

Unfortunately, it is not possible to query the index to check if the current context inherits from ActiveRecord::Base while still building the index.

Description

I think this problem could be solved by allowing you to store lazy “potential” entries in the index. Let’s say you pick up the method call belongs_to :user. You want to define the method user as a result of this, but only if the class inherits from ActiveRecord::Base.

What if you could add an entry like this?

@listener.add_lazy_method("user") do |index|
  if index.linearized_ancestors_of(owner).include?("ActiveRecord::Base")
    RubyIndex::Entry::Method.new(
      # ...
    )
  end
end

The index could store that there is potentially a method called user and its signature is in this Proc.

At lookup time, once the indexing is completed, the proc could be executed and if it returns nil, the entry is ignored. Otherwise, the returned Method entry is used.

Implementation

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the index-building and lookup paths around the proposed @listener.add_lazy_method and linearized_ancestors_of calls. Review RubyIndex::Entry::Method and existing index entry handling; done means lazy entries can be stored during indexing, evaluated after indexing, and omitted when the proc returns nil.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.