Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails support #87

Open
philevans opened this issue Sep 17, 2018 · 130 comments
Open

Rails support #87

philevans opened this issue Sep 17, 2018 · 130 comments

Comments

@philevans
Copy link

I know Rails support is only partial at the moment, but I have installed the Solargraph gem, and am using the VSCode extension, and when I am in a Rails application, I am not getting any Rails-related completions/suggestions whatsoever.

I don't even get a suggestion for the "Rails" class. I just want to make sure I am not getting the wrong end of the stick in terms of what amount of Rails support Solargraph actually has? Solargraph itself is working fine - I am getting standard Ruby suggestions no problem.

@castwide
Copy link
Owner

There are a couple of things you can try.

  1. Make sure you have gem documentation installed by running yard gems.

  2. Add a .solargraph.yml file to the app's root directory (you can generate a default one by running solargraph config) and make the following change to the require section:

require:
- actioncable
- actionmailer
- actionpack
- actionview
- activejob
- activemodel
- activerecord
- activestorage
- activesupport

That should get you closer, but it's still far from perfect. Improving Rails support has been on the roadmap for a while. The amount of runtime magic involved makes it challenging, but I have some updates in the works that should help.

@andersennl
Copy link

I hope it's fine if I add my thoughts here. First of all, thanks for your work! I've found this thread after noticing that Solargraph wouldn't know how to handle a goto definition click on has_many :users, where I'd expect it to open the User class. I'm not sure how much runtime magic is involved here but having this would be a big improvement.

@tvallois
Copy link

Hi, any news on this?

Is there any dedicated branch for Rails where we could help you to implement it?

@castwide
Copy link
Owner

Solargraph's YARD support is at a point where some of the missing Rails intellisense can be filled with a small @!parse directive. I created a gist for it here: https://gist.github.com/castwide/28b349566a223dfb439a337aea29713e

I'm looking for ways to improve it, including ways to make it easy to install and maintain. Any suggestions are welcome.

@mathieushopify
Copy link

mathieushopify commented Jun 18, 2019

@castwide Using the gist above, it works well in many cases... thank you! However, in the 'activesupport' gem, there is a method blank? which is not getting picked up... is there a workaround? (.gem/ruby/2.5.3/gems/activesupport-5.2.2.1/lib/active_support/core_ext/object/blank.rb)

@castwide
Copy link
Owner

@mathieushopify It works in circumstances where a type can be inferred:

obj = Object.new
obj.b # <= suggests blank?

str = 'str'
str.b # <= suggests blank?

If those don't work, make sure you have the latest gem version (0.33.0 was released this morning) and run yard gems to make sure they're documented.

Completion won't work on on unknown types, which is unfortunately the status of most of the Rails API; for example, completion will suggest ApplicationRecord.find, but doesn't know what type find returns, so it won't make any suggestions for methods chained to it.

@mathieushopify
Copy link

Makes sense, thanks for the prompt reply! :-)

@dgutov
Copy link

dgutov commented Jun 18, 2019

@castwide Don't most types inherit from Object, though?

Although the lack of completions it can be a useful indicator that the type is in not inferred. "Some completions" is not always better than "no completions".

@castwide
Copy link
Owner

@dgutov Yeah, my personal preference is for undefined types to return no completions instead of assuming Object. It's less ambiguous about whether a type was detected.

If anyone finds it more useful to assume Object for untagged methods, we might be able to make it a configuration option.

@castwide
Copy link
Owner

Version 0.34.0 introduces some new features that should add some improvements to Rails support.

Quick Start

  1. Install the latest Solargraph gem.
  2. Run solargraph bundle from your Rails app's root directory.
  3. Add the most recent version of this file: https://gist.github.com/castwide/28b349566a223dfb439a337aea29713e

How It Works

  • solargraph bundle makes sure that YARD documentation is installed for all your gems. Rails gems get parsed with RDoc first, then converted to YARD and saved in Solargraph's cache directory.
  • The YardMap detects that the code calls Bundler.require and automatically adds gem dependencies to the map, so the @!parse require directives are no longer necessary.
  • The gist file adds explicit references to a few more modules that Rails automagically includes and extends.
  • The @!override in the gist applies overloaded methods with return types to ActiveRecord::FinderMethods#find.

Known Issues

  • Although this process fills in a lot more methods, practically none of them have documented return types. The @!override directive might be a way to fix that, but I'm not sure if it'll be a long-term solution.
  • Using go-to-definition on a symbol with custom documentation should open the correct file, but it always goes to the first line. (Go-to-definition on other gems still works correctly.)
  • The RDoc-to-YARD conversion is necessary so Rails magic methods documented in RDoc will be visible in Solargraph. It might be a good idea to implement this feature as a YARD plugin.
  • If the custom documentation gives you any trouble, you can run solargraph clear && solargraph download-core to reset it.
  • There are a lot more includes and extends that could be added to the gist. Any help is appreciated.

@minkir014
Copy link

When you intend to fix this issue?

Using go-to-definition on a symbol with custom documentation should open the correct file, but it always goes to the first line. (Go-to-definition on other gems still works correctly.)

I think it should go to the correct line.

@mathieushopify
Copy link

@castwide thanks for the update!

@dirksierd
Copy link

Great stuff! Thanks!

@minkir014
Copy link

@castwide can you tell me that if that was an ordinary work or it have an issue??

@castwide
Copy link
Owner

@minkir014 I'm sorry, I don't know what you mean.

@castwide
Copy link
Owner

Gem 0.34.1 fixes some issues that occurred in certain bundler environments.

@minkir014
Copy link

minkir014 commented Jun 28, 2019

As I know about go to definition that it must go to a certain line that the class or any other thing is defined and you said

Using go-to-definition on a symbol with custom documentation should open the correct file, but it always goes to the first line. (Go-to-definition on other gems still works correctly.)

That means that some gems have problems with go to definition. So, when do you intend to fix this issue???

@castwide
Copy link
Owner

castwide commented Jun 29, 2019

@minkir014 I don't know when it will be fixed because I don't even know what the root cause is. When the RdocToYard component generates an RDoc store, all the code objects have nil line numbers.

@minkir014
Copy link

You could file an issue against RdocToYard gem. It maybe a problem with it.

@tvallois
Copy link

tvallois commented Sep 5, 2019

Is there a way to manage models values with Solargraph?
E.g for the User model, to have autocompletion on user.id, user.created_at, etc...

@castwide
Copy link
Owner

castwide commented Sep 7, 2019

@tvallois I'm working on a Rails convention that should be able to map model attributes from the database schema.

Right now the only other option is to document them yourself, e.g., with the @!attribute directive:

class User < ActiveRecord::Base
  # @!attribute id
  #   @return [Integer]
  # @!attribute name
  #   @return [String]
end

@Zeko369
Copy link

Zeko369 commented Sep 24, 2019

@castwide Hey, I was thinking of building a script that uses annotations built by annotate gem to add types to models. You said that you're working on something similar? Have you pushed any code towards that? I would like to help and start building it myself if you haven't 👨‍💻

# == Schema Information
#
# Table name: groups
#
#  id           :bigint(8)        not null, primary key
#  name         :string
#  created_at   :datetime         not null
#  updated_at   :datetime         not null
#  namespace_id :bigint(8)        indexed
#

class Group < ApplicationRecord
  # @!attribute id
  #   @return [Integer]
  # @!attribute name
  #   @return [String]
  # @!attribute created_at
  #   @return [Date]
  # @!attribute updated_at
  #   @return [Date]
  # @!attribute namespace_id
  #   @return [Integer]

  ...
end

@castwide
Copy link
Owner

castwide commented Sep 25, 2019

@Zeko369 I don't have anything substantial. If you can build something, that would be awesome. I'm not sure how integration will work yet, but we can worry about those details after we have a working implementation.

@jhirn
Copy link

jhirn commented Oct 20, 2021

Turns out that solargraph bundle for some reason messes things up. If I run all 4 commands above and then save the output of solargraph scan -v to a file, this is what I get when I search for ActiveRecord::Base

100%. In fact just doing a solargraph clear brought back ActiveSupport methods that weren't present after running solargraph bundle. Don't have time to troubleshoot now but there's definitely something not working right with solargraph bundle

@iftheshoefritz
Copy link

@jhirn this is covered #473, if you do get to troubleshooting please add your feedback there.

@jhirn
Copy link

jhirn commented Oct 20, 2021

@jhirn this is covered #473, if you do get to troubleshooting please add your feedback there.

Thanks again @iftheshoefritz!

@iftheshoefritz
Copy link

I've released v0.3.0 of solargraph-rails which brings Rails 7 support and moves association support into the "non-pre-release" versions.

I can see that there are still teething issues with association support, particularly with Rails 7 and (separately) #473, but I would rather have people try this and report bugs than it just live in obscurity.

@jhirn
Copy link

jhirn commented Jan 21, 2022

Nice. Can't wait to try it!

@kaspergrubbe
Copy link

Thank you so much for working on this <3

@kevink-sq
Copy link

Thanks a lot!

@asok
Copy link

asok commented Feb 5, 2022

For anyone interested, there's also https://github.com/alisnic/solargraph-arc which seems more feature rich than solargraph-rails.

@iftheshoefritz
Copy link

iftheshoefritz commented Mar 18, 2022

I'm pleased to announce that solargraph-arc has merged into solargraph-rails and you can benefit from all of @alisnic's hard work right now by installing v1.0.0.pre.1.

See the changelog here for all the new features that this brings.

Also: please create issues for the bugs you find and the features you want over in the solargraph-rails project. Let's make this thing work!

@kaspergrubbe
Copy link

This is exciting, thank you for your hard work!

@iftheshoefritz
Copy link

solargraph-rails v1.0.1 released on Saturday, it includes everything that was sitting in 1.0.0.pre.1 for so long while some admin remained undone. Thanks again to @alisnic for all of his contributions which have turned this chewing gum and regex gem into something much more mature. Please try it out and raise issues!

@gastonmorixe
Copy link
Contributor

@iftheshoefritz after upgrading from 0.3.1 I am seeing this error and can't upgrade to 1.0.1, is the dependency wrong or it's not supporting solargraph 0.47.2 yet? thank you

Bundler could not find compatible versions for gem "solargraph":
  In Gemfile:
    solargraph (~> 0.47.2)

    solargraph-rails (~> 1.0.1) was resolved to 1.0.1, which depends on
      solargraph (~> 0.44.2)

@Samuelodan
Copy link

Samuelodan commented Nov 12, 2022

Hello,
running yard gems fails for me with the following error.

/Users/samuelodan/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/yard-0.9.28/lib/yard/parser/ruby/ast_node.rb:274:in `first': cannot get the first element of beginless range (RangeError)

A lot of the output is just errors and warnings for me. I don't know if that's normal.
Sadly, I still don't get rails suggestions despite installing solargraph-rails. Maybe it's cos building the Yard docs fails?

EDIT: Woah! It suddenly started working for me after messing around so much that I can't even describe what exactly I did that made it start working. I'm just gonna leave it this way and not bother about the yard gems issue. Man, I was really tempted to start using RubyMine.
Thanks everyone for all your contributions. I ended up trying eveything. The gist file, adding gems to the require option in the solargraph config file, installing solargraph-rails (which means I now have two versions of solargraph), everything I could find.

@gwillcox-r7
Copy link
Contributor

gwillcox-r7 commented Nov 23, 2022

Edit: There is a PR over at iftheshoefritz/solargraph-rails#49 that should be attempting to fix this now, though looks like there may be some issues still to work through r.e testing before it gets landed.

@iftheshoefritz
Copy link

In case you haven't seen it, solargraph-rails no longer pins the version of solargraph as of 1.1.0. I can confirm the main features work with 0.48.0, although I admit that experiences like @Samuelodan's ("... error... Woah! It suddenly started working") are something I share and want to improve on.

@nimaai
Copy link

nimaai commented Dec 4, 2023

Integration with Rails doesn't work for me. I am using neovim with coc-solargraph plugin. Hover command does not give me any of the Rails documentation for its methods. Ruby-Core etc. work fine. solargraph bundle is deprecated by now (a no-op).

Any updates on how to make Rails work with solargraph at this point in time?

@gastonmorixe
Copy link
Contributor

@nimaai have you tried Shopify/ruby-lsp instead?

@Gasol
Copy link

Gasol commented Dec 4, 2023

@nimaai Have you tried the command yard gems?

@nimaai
Copy link

nimaai commented Dec 4, 2023

@Gasol yes, i have. does rails work for you? what setup and versions of the gems are you using?

@nimaai
Copy link

nimaai commented Dec 4, 2023

@gastonmorixe yes, i have shortly but requires separate setup from coc which i am using for other filetypes. little messy.

also the hover action opens a new buffer instead of a popup.

@Gasol
Copy link

Gasol commented Dec 4, 2023

@nimaai Yes. I have the same setup just like you using Neovim. Except the one I didn't use Coc.

$ bundle show | grep solargraph
  * solargraph (0.49.0)
  * solargraph-rails (1.1.0)
$ cat .solargraph.yml
---
include:
  - '**/*.rb'
exclude:
  - spec/**/*
  - test/**/*
  - vendor/**/*
  - '.bundle/**/*'
require:
  - actioncable
  - actionmailer
  - actionpack
  - actionview
  - activejob
  - activemodel
  - activerecord
  - activestorage
  - activesupport
domains: []
reporters:
  - rubocop
  - require_not_found
formatter:
  rubocop:
    cops: safe
    except: []
    only: []
    extra_args: []
require_paths: []
plugins:
  - solargraph-rails
max_files: 5000
Screenshot 2023-12-04 at 9 01 24 PM Screenshot 2023-12-04 at 9 01 43 PM

@nimaai
Copy link

nimaai commented Dec 4, 2023

@Gasol thanks. i was missing require'lspconfig'.solargraph.setup{}. solargraph lsp was not started before.

now i get the diagnostics, but the documentation for the methods etc. is still not being displayed. must have something to do with configuration for coc-solargraph extensions.

btw, what autocomplete plugin are you using for neovim?

@Gasol
Copy link

Gasol commented Dec 4, 2023

@nimaai I use LazyVim.

@PedroSena
Copy link

@Gasol Can you please show me your LSP config on LazyVim? I'm using a very similar setup to your but not having the desired result here

@Gasol
Copy link

Gasol commented Mar 31, 2024

@Gasol Can you please show me your LSP config on LazyVim? I'm using a very similar setup to your but not having the desired result here

Here is the link. I would say there's nothing special about it.

@PedroSena
Copy link

Thank you @Gasol . I was expecting to see some configuration specific to Solargraph (specially w/r/t to its cmd path) but apparently you used Mason for installing it, right ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests