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

Testing @grncdr's hacks #43

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8bda871
Remove Rails 5 & Rails 6 specs
grncdr May 13, 2023
d2774d0
Don't require solargraph when loading test Rails app
grncdr May 14, 2023
ca1ea40
Configure test app to behave like a real app
grncdr May 14, 2023
47769b3
Use ruby/setup-ruby action and test app Gemfile for CI
grncdr May 14, 2023
ccb477d
Add a plugin that generates yard docs after bundle install
grncdr May 15, 2023
99ac892
Bump tested rails version to 7.0.4
grncdr May 20, 2023
6e31acf
Use rails7 workspace in schema spec
grncdr May 20, 2023
b479fdb
Clean up spec/definitions/**/*.yml
grncdr May 14, 2023
37fb11d
Report all non-matching definitions when running specs
grncdr May 20, 2023
fd95daa
Fix order-dependence of annotate_spec
grncdr May 20, 2023
38d7f99
Default unknown database types to String
grncdr Sep 3, 2022
e1291d6
Generate more attribute methods
grncdr Sep 10, 2022
3c57dc1
Ignore modules when finding the namespace to process
grncdr Oct 7, 2022
6dc8c4c
Extract Util.extract_option helper
grncdr Sep 23, 2022
de6986e
Use Solargraph::Pin::DelegatedMethod to define delegate methods
grncdr Sep 23, 2022
3c12d41
Resolve annotations.rb to a real path
grncdr Sep 23, 2022
76798b6
Generate per-model Relation types
grncdr Sep 3, 2022
a252d69
Rebind blocks passed to ActiveRecord::Base.scope
grncdr Sep 6, 2022
8bfa7a8
Include overloads and parameter types for ActiveRecord methods
grncdr Sep 28, 2022
fb26c77
Restore ds instance variable
grncdr Nov 3, 2022
309a7b4
Add type annotations for ActionMailer
grncdr Jan 7, 2023
97f3f2b
Fix undefined closure for generated parameter pins
grncdr Feb 8, 2023
008163d
Fix hack that sets binder for scope blocks
grncdr May 7, 2023
7001ba4
Fix undefined variable
grncdr May 7, 2023
714532a
Fix: actually return pin for ActiveRecord method overrrides
grncdr May 9, 2023
4360eae
Feature detect DelegatedMethod pin support
grncdr May 14, 2023
45456df
Use normal Ruby source for extra YARD annotations
grncdr May 14, 2023
c6b9fa7
Update README introduction
grncdr May 14, 2023
a0b63e2
Rubocop
grncdr May 14, 2023
9f1d4e7
Switch to stdlib debug gem
grncdr May 14, 2023
9574d0e
Update model spec to expect private relation types
grncdr May 14, 2023
5890127
Fix autocompletion in routes files
grncdr May 14, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby

name: Ruby

on:
Expand All @@ -23,20 +16,21 @@ jobs:
solargraph-version:
- "0.48.0"
- "0.49.0"
rails-version:
- "7"
fail-fast: false

steps:
- uses: actions/checkout@v2
- name: Cache RVM
uses: actions/cache@v3
id: rvm
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true
- uses: ruby/setup-ruby@v1
with:
key: ${{ runner.os }}-rvm-${{ matrix.ruby-version }}
path: |
/home/runner/.rvm
/home/runner/.yardoc
#/usr/local/rvm/
#- name: Setup upterm session
# uses: lhotari/action-upterm@v1
- name: Test
run: "/bin/bash ./ci.sh ${{ matrix.ruby-version }} ${{ matrix.solargraph-version}}"
working-directory: spec/rails${{ matrix.rails-version }}
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true
- name: RSpec
run: bundle exec rspec spec/solargraph-rails

4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
/spec/reports/
/tmp/
.DS_Store
spec/rails5/log/
spec/rails5/tmp/
spec/rails6/log/
spec/rails6/tmp/
spec/rails7/log/
spec/rails7/tmp/
.projections.json
Expand Down
26 changes: 5 additions & 21 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,20 @@

1. create fork and clone the repo
2. install gem deps `bundle install`
3. install dummy rails5 app deps and build its yard cache

```
$ cd spec/rails5
$ bundle install && yard gems
$ cd ../../
```

3. install dummy rails6 app deps and build its yard cache

```
$ cd spec/rails6
$ bundle install && yard gems
$ cd ../../
```

4. install dummy rails7 app deps and build its yard cache
3. install dummy Rails app deps and build its yard cache

```
$ cd spec/rails7
$ bundle install && yard gems
$ cd ../../
```
5. now tests should pass locally and you can try different changes
6. sumbit PR
4. now tests should pass locally and you can try different changes
5. sumbit PR

## Completion coverage tracking

Solargraph-Rails uses a [set of yaml files](https://github.com/iftheshoefritz/solargraph-rails/tree/master/spec/definitions) to track coverage of found completions.
Those yaml files are generated at runtime from a dummy [rails5](https://github.com/iftheshoefritz/solargraph-rails/tree/master/spec/rails5) or [rails6](https://github.com/iftheshoefritz/solargraph-rails/tree/master/spec/rails6) app.
Those yaml files are generated at runtime from a dummy [Rails 7 app](https://github.com/iftheshoefritz/solargraph-rails/tree/master/spec/rails7).

The main goal is to catch any regressions in case of any change. In case a method completion is marked completed and it is not found in solargraph completions, the tests will fail.

Expand Down Expand Up @@ -85,7 +69,7 @@ In case a new set of assertion files has to be created (for a new Rails version
All you have to do is execute the script and pass it a path to rails app:

```
ruby script/generate_definitions.rb spec/rails6
ruby script/generate_definitions.rb spec/rails7
```

Make sure to review the script and uncomment relevant parts
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

group :development, :test do
gem 'byebug'
gem 'bundler-audit'
gem 'debug'
end
# Specify your gem's dependencies in solargraph_rails.gemspec
gemspec
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
# Solargraph::Rails - Help solargraph with Rails

## Models
Given a typical Rails model like this:
Consider pair of typical Rails models like this:

```sh
rails g model Author lastname:string firstnames:string
rails g model Book title:string isbn:string author:belongs_to
```

```ruby
# == 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"
class Author < ApplicationRecord
has_many :books

def sortable_name
"#{lastname}, #{firstnames}"
end
end

...
class Book < ApplicationRecord
belongs_to :book

def label
[author.sortable_name, title, isbn].join("\n")
end
end
```

The various Ruby intellisense tools are ok at knowing that there is a `MyBook` constant, and some (including Solargraph) are 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.
The various Ruby intellisense tools are ok at knowing that there are `Book` and `Author` constants, and some (including Solargraph) are aware that objects like `Book.new` have a `.label` method. But what about those "magical" dynamic methods that ActiveRecord creates like `.title`, or `.author`?

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.
Since these attributes are only created at runtime, a simple static analysis of the `Book` class 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 database schema and YARD docs of various gems to give Solargraph some extra hints. For instance database attributes:

Expand Down Expand Up @@ -74,13 +76,11 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/ifthes
3. install dummy rails app deps and build the yard cache:

```
$ cd spec/rails5
$ cd spec/rails7
$ bundle install && yard gems
$ cd ../../
```

(and the same for rails 6 and rails 7)

4. now tests should pass locally and you can try different changes

5. sumbit PR
Expand Down
34 changes: 0 additions & 34 deletions ci.sh

This file was deleted.

38 changes: 20 additions & 18 deletions lib/solargraph-rails.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
require 'solargraph'
require 'active_support/core_ext/string/inflections'

require_relative 'solargraph/rails/util.rb'
require_relative 'solargraph/rails/schema.rb'
require_relative 'solargraph/rails/annotate.rb'
require_relative 'solargraph/rails/autoload.rb'
require_relative 'solargraph/rails/model.rb'
require_relative 'solargraph/rails/devise.rb'
require_relative 'solargraph/rails/walker.rb'
require_relative 'solargraph/rails/rails_api.rb'
require_relative 'solargraph/rails/delegate.rb'
require_relative 'solargraph/rails/storage.rb'
require_relative 'solargraph/rails/debug.rb'
require_relative 'solargraph/rails/version.rb'
require_relative 'solargraph/rails/util'
require_relative 'solargraph/rails/schema'
require_relative 'solargraph/rails/annotate'
require_relative 'solargraph/rails/autoload'
require_relative 'solargraph/rails/model'
require_relative 'solargraph/rails/devise'
require_relative 'solargraph/rails/walker'
require_relative 'solargraph/rails/rails_api'
require_relative 'solargraph/rails/routes_dsl'
require_relative 'solargraph/rails/delegate'
require_relative 'solargraph/rails/storage'
require_relative 'solargraph/rails/debug'
require_relative 'solargraph/rails/version'

module Solargraph
module Rails
Expand All @@ -33,12 +34,13 @@ def global(yard_map)
end

def local(source_map)
run_feature { RoutesDsl.local(source_map) }

pins = []
ds =
source_map.document_symbols.select do |n|
n.is_a?(Solargraph::Pin::Namespace)
end
ns = ds.first
ds = source_map.document_symbols.select do |n|
n.is_a?(Solargraph::Pin::Namespace)
end
ns = ds.find { |s| s.type == :class }

return EMPTY_ENVIRON unless ns

Expand All @@ -48,7 +50,7 @@ def local(source_map)
pins += run_feature { Storage.instance.process(source_map, ns) }
pins += run_feature { Autoload.instance.process(source_map, ns, ds) }
pins += run_feature { Devise.instance.process(source_map, ns) }
pins += run_feature { Delegate.instance.process(source_map, ns) }
pins += run_feature { Delegate.instance.process(source_map, ns) } if Delegate.supported?
pins += run_feature { RailsApi.instance.local(source_map, ns) }

Solargraph::Environ.new(pins: pins)
Expand Down
50 changes: 0 additions & 50 deletions lib/solargraph/rails/annotations.rb

This file was deleted.

5 changes: 5 additions & 0 deletions lib/solargraph/rails/annotations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# annotations

Code in this directory is _not_ loaded or evaluated at runtime, either by solargraph or your application.

The code in [rails_api.rb](../rails_api.rb) loads each of these files inside a YARD `@!parse` directive.
29 changes: 29 additions & 0 deletions lib/solargraph/rails/annotations/action_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class ActionController::Base
include ActionController::MimeResponds
include ActionController::Redirecting
include ActionController::Cookies
include AbstractController::Rendering
extend ActiveSupport::Callbacks::ClassMethods
extend ActiveSupport::Rescuable::ClassMethods
extend AbstractController::Callbacks::ClassMethods
extend ActionController::RequestForgeryProtection::ClassMethods

# @return [ActionDispatch::Response]
def response; end
# @return [ActionDispatch::Request]
def request; end
# @return [ActionDispatch::Request::Session]
def session; end
# @return [ActionDispatch::Flash::FlashHash]
def flash; end
end

class ActionController::Metal
# @return [ActionController::Parameters]
def params; end
end

class ActionController::Cookies
# @return [ActionDispatch::Cookies::CookieJar]
def cookies; end
end
8 changes: 8 additions & 0 deletions lib/solargraph/rails/annotations/action_dispatch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module ActionDispatch
module Flash
class FlashHash
# @return [ActionDispatch::Flash::FlashNow]
def now; end
end
end
end
7 changes: 7 additions & 0 deletions lib/solargraph/rails/annotations/action_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ActionMailer::Base
# @return [self]
def self.with(**params); end
#
# @return [ActionMailer::MessageDelivery]
def mail(**params); end
end
Loading