Giter Site home page Giter Site logo

solargraph-rails's Introduction

Solargraph::Rails - Help solargraph with Rails

Work in progress - here be dragons

There are significant rough edges to this gem still.

Models

Given a typical Rails model like this:

# == Schema Information
#
# Table name: my_books
#
#  id         :integer          not null, primary key
#  author     :string
#  name       :string
#  created_at :datetime         not null
#  updated_at :datetime         not null
#
class MyBook < ApplicationRecord
  def my_method
    "hello"
  end

  ...

end

The various Ruby intellisense tools are ok at knowing that there is a MyBook constant, and some (including Solargraph) are even aware that objects like MyBook.new have a method .my_method. But what about those magical dynamic attributes that ActiveRecord creates when Rails starts up? You can see these listed at the top of the file under # == Schema Information, the comments helpfully added by the Annotate gem.

Since these attributes are only created at runtime, static analysis alone can't identify them. Your editor has no idea that these attributes exist, but they're amongst the most common things that you will work with in any Rails app.

That's where this plugin for Solargraph comes in: it parses the schema comments left by Annotate and uses those to give Solargraph some extra hints.

With this you get autocompletion on ActiveRecord attributes:

Autocompletion of dynamic attributes like created_at

... and go to definition commands take you to the schema comment for that column:

Go to definition of dynamic attributes like created_at

... and peek commands show you documentation about the attribute:

Peek at documentation of attributes like created_at, author, etc.

Reload workspace after migrations

Solargraph won't know about attributes that you add during a session. Restart your LSP workspace to get the new attributes.

For my setup with Emacs, that means running M-x lsp-workspace-restart, YMMV in other editors.

Associations (experimental)

There is simplistic support for belongs_to and has_many macros:

Experimental autocomplete and go to definition of associations

Known issues

This project is being used to write production code by the maintainer, but it is still WIP. Check out the issues tab and contribute if you are interested.

Association support is slightly less functional in Rails 7.

Installation

Install solargraph and solargraph-rails locally

Typically gems like these are not installed via the Gemfile, because most projects have more than one contributor and other contributors might have different setups for their editors in mind. Instead you need to use gem install.

gem install solargraph-rails

Alternative: using bundler

If you do want to use bundler, add gem 'solargraph-rails'

Add solargraph-rails to your .solargraph.yml

(if you don't have a .solargraph.yml in your project root, you can run solargraph config to add one)

plugins:
  - solargraph-rails

Add annotate

Add schema comments your model files using Annotate. At the moment Solargraph::Rails assumes your schema comments are at the top of the source file.

Development

Fork the project, start hacking, put up a PR :).

When you make changes, you probably need to shut down solargraph and restart it (maybe that requires you to shut down your whole editor?). You can speed up the feedback loop by running

api_map = Solargraph::ApiMap.load(Rails.root.to_s)

in the console of the Rails project where solargraph-rails is installed. This may require restarting the rails console each time, and possibly killing Spring.

Once you have an instance of Solargraph::ApiMap, you can interrogate it with Solargraph code like:

pins = api_map.get_methods('MyBook')

More examples here: https://solargraph.org/guides/code-examples

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/iftheshoefritz/solargraph_rails.

License

The gem is available as open source under the terms of the MIT License.

solargraph-rails's People

Contributors

iftheshoefritz avatar julianrubisch avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.