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

Improve support for `ActiveSupport::Concern`

Open andyw8 opened this issue 1 year ago • 1 comments

[!NOTE] This issue is aimed at those attending the RubyConf 2024 Hack Day

[!CAUTION] This will likely be a difficult issue, please discuss approaches with the maintainers before attempting.

For ActiveRecord model, you'll notice that you can hover over some DSLs, such as has_one:, to see its documentation, but if you others, such as validates:, nothing is shown.

This is because the ActiveModel::Validations::Callbacks module is mixed into the parent’s class using an included block:

https://github.com/rails/rails/blob/d4fff28caf25546dfef68087047af34927a3d5f0/activerecord/lib/active_record/callbacks.rb#L413

As this a Rails feature, it is not supported by Ruby LSP natively.

It should be possible to support this via an Indexing Enhancement in the Rails addon so that the index knows about ActiveModel::Validations::Callbacks, and Ruby LSP will then be able to show the documentation on hover.

Also, since included can be used in application code, this will improve Ruby's LSP's ability to provide features for your app.

There is partial support for concerns in indexing_enhancement.rb but it doesn't yet know about included.

(Note that there can also be a prepended block).

These articles may help with understanding the details of concerns:

  • https://guides.rubyonrails.org/getting_started.html#using-concerns
  • https://blog.jez.io/concern-inheritance/
  • https://blog.appsignal.com/2020/09/16/rails-concers-to-concern-or-not-to-concern.html
  • https://gmalette.dev/posts/short-rant-about-activesupport-concern/

andyw8 avatar Oct 22 '24 19:10 andyw8

Hi there!

I've been playing around with building an indexing enhancement for another package with a similar DSL, and I see a few roadblocks and was wondering what y'all were thinking around this topic.

Rails server boot

Today the rails server boots in a background thread. However, in order to contribute to indexing the Rails server likely needs to boot before indexing starts, so that it and dependent addons can register indexing enhancements that contribute to indexing and leverage the rails server during it. Otherwise (I think) you'll need to re-index after the server has loaded. The good news is that Ruby LSP likely already supports loading the server beforehand by only starting indexing after addons are loaded and so its mostly a rails LSP problem.

The obvious bad news is that this possibly increases the total time to index if the server needs to boot up.

Enhancement construction

The second issue I see is that unlike the overrides in Addon you can have for creating listeners, the enhancement registry manages initialization of enhancements so there's no clear injection point for additional dependencies.

Perhaps there's a way to reference global state that isn't passed, but it does seem a little gross.

Let me know what y'all think and if I'm on the right track for what are the major impediments here!

kassemsandarusi avatar Dec 17 '24 03:12 kassemsandarusi