ruby-lsp-rails icon indicating copy to clipboard operation
ruby-lsp-rails copied to clipboard

Add methods generated through metaprogramming to the index

Open Earlopain opened this issue 1 year ago • 7 comments

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 avatar Jun 30 '24 09:06 Earlopain

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.

Earlopain avatar Jul 01 '24 10:07 Earlopain

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.

vinistock avatar Jul 02 '24 19:07 vinistock

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 😄)

Earlopain avatar Jul 02 '24 20:07 Earlopain

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.

vinistock avatar Jul 02 '24 20:07 vinistock

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

andyw8 avatar Jul 02 '24 21:07 andyw8

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?

vinistock avatar Jul 02 '24 21:07 vinistock

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.

Earlopain avatar Jul 03 '24 11:07 Earlopain

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

andyw8 avatar Oct 23 '24 17:10 andyw8