castwide / solargraph

A Ruby language server.
https://solargraph.org
MIT License
1.9k stars 157 forks source link

Inconsistent loading and autocomplete from generated YARD for ActiveRecord attributes #332

Closed iftheshoefritz closed 2 years ago

iftheshoefritz commented 4 years ago

I wrote this rake task to generate YARD docs for model attributes in a Rails 6 app. It's working for some models (attributes are present in autocomplete) but not others. Wondering if there are any glaringly obvious issues you can tell me about:

namespace :solargraph do
  task update_model_definitions: :environment do
    if Rails.env.development?
      Rails.application.eager_load!
      def_file = Rails.root.join('config', 'model_definitions.rb')
      File.open(def_file, 'w') do |file|
        ApplicationRecord.descendants.each do |model|
          file << "class #{model.name}\n"
          klass = model.name.constantize
          klass.attribute_names.each do |name|
            file << "#   @!attribute [rw] #{name}\n"
            file << "#     @return [#{ruby_type[klass.type_for_attribute(name).type]}]\n"
          end
          file << "end\n"
        end
      end
    end
  end
end

def ruby_type
  {
    array:       'Array',
    boolean:     'Boolean',
    date:        'Date',
    datetime:    'DateTime',
    decimal:     'Decimal',
    float:       'Float',
    integer:     'Integer',
    string:      'String',
    text:        'String',
    time:        'Time',
  }
end

Two problems occur:

  1. Some models load get the attributes I want, e.g.

image

(exactly what I want) from this generated config:

class Period
#   @!attribute [rw] id
#     @return [Integer]
# ... lots more config

but no luck on another model:

image

with what looks like the same config:

class School
#   @!attribute [rw] id
#     @return [Integer]
# ... lots more config

Here I'm expecting to see the id attribute as an option in auto-complete. Unfortunately it and all other attributes defined in this config are missing.

Is it possible that I'm generating broken config that prevents the completion?

  1. I'm not sure what causes Solargraph to reload definitions from the generated config file. A few scenarios:

I guess that asking for certainty on emacs behaviour is difficult. I'm just wondering what needs to happen with the solargraph process - is there a hot reload, do I need to kill and restart it... or what?

iftheshoefritz commented 4 years ago

I found that generating the YARD docs as one file per class being reflected fixed this for me. Interested to know why that might be.

The other issues (how my editor reloads things) must be an Emacs problem, because VSCode handles it perfectly.

arbox commented 4 years ago

A bit of offtopic, but @iftheshoefritz is it possible to look at your init.el to learn from your experience?

iftheshoefritz commented 4 years ago

@arbox I'm using Spacemacs, and pretty close to vanilla at that. Check out the LSP layer here: https://github.com/syl20bnr/spacemacs/tree/develop/layers/%2Btools/lsp

castwide commented 2 years ago

Closing this as presumed resolved in solargraph-rails. Please feel free to open a new issue if necessary.