Shopify / ruby-lsp-rails

A Ruby LSP add-on for Rails
https://shopify.github.io/ruby-lsp/rails-add-on
MIT License
581 stars 27 forks source link

Add methods generated through metaprogramming to the index #403

Open Earlopain opened 5 months ago

Earlopain commented 5 months ago

I make occasional use of delegate in my code, which is rails specific: https://api.rubyonrails.org/classes/Module.html#method-i-delegate

Are addons able to contribute to the index? I don't think this addon currently does that. From the code in ruby-lsp, DeclarationListener.on_call_node_enter seems like the place where it handles buildin things like that.

Perhaps addons should to be able to hook into index_single to register a dispatcher of their own and contribute to the index?

### Tasks
- [ ] delegate
- [ ] has_many
- [ ] has_one
- [ ] belongs_to
- [ ] scope
- [ ] enum
- [ ] has_one_attached
- [ ] has_many_attached
- [ ] has_secure_token
- [ ] attribute
Earlopain commented 5 months ago

Gave this one a quick shot but I quickly realized that my simple suggestion above doesn't really cut it. Addons would have to duplicate a large chunk of what DeclarationListener does which I don't think would be particularly great, plus it wouldn't benefit from fixes/enhancements from ruby-lsp in that area.

vinistock commented 5 months ago

We want to allow addons to also contribute to the index by providing another extension point in the indexing mechanism, which is the way to move forward with support for stuff like this. We will be exploring it soon.

Essentially, we will need to allow addons to register for handling DSLs during indexing and then invoke the addons exactly in that spot in on_call_node_enter with enough context to make it useful.

Earlopain commented 5 months ago

Looking forward to that. Rails provides some methods where this would be very useful (like enum declarations) and with the index continuosly getting better the cases where it doesn't work start to feel weird (this is a compliment 😄)

vinistock commented 5 months ago

Yeah, definitely! The idea is that addons will have the ability to bake the logic for configurations style DSLs, improving the knowledge in the index.

A good example is association methods. When we find a belongs_to :user, we know which methods have to be inserted in the index.

andyw8 commented 5 months ago

@vinistock are you thinking this would be a new kind of Entry, e.g. Entry::DynamicMethod ?

vinistock commented 5 months ago

Ideally not because then everything else should work by default (resolution, completion candidates, etc). Do you see a reason for having a separate entry for it?

Earlopain commented 5 months ago

I've repurposed the issue for general metaprogramming stuff. scope is one I also remembered existing, and two from activestorage. Surely there are more, feel free to add.

andyw8 commented 1 month ago

Add-ons can now contribute to the index: https://shopify.github.io/ruby-lsp/add-ons.html#enhancing-features