solargraph-rails 0.2.0.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 10a5f0ba2496f8b79f4e008ae3ca68f4cb193c31fdbdf95d18b65fc3850e703e
4
+ data.tar.gz: e21cff4b322b4b8547b9b06709f30fbd883108983570dfb3d44c50e83f335ecf
5
+ SHA512:
6
+ metadata.gz: 6be898e5f949bb5e683dd5057bd85ae88ebdf19a8d5662b3d107c444ec54b24c71f6026c0877bcf0e1c59e6f7aa2ae21713c6067e0f1c9d2f8f3bb93e60813b6
7
+ data.tar.gz: a26cbfc730799c6308773ee01a900356227d624d3b0090992ae21a915f01f42964fba3a53ba6fd29ee19c49bad4ef4f45a81592de999b3f48cd82c23519df954
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ group :development, :test do
6
+ gem 'byebug'
7
+ end
8
+ # Specify your gem's dependencies in solargraph_rails.gemspec
9
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Fritz Meissner
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,111 @@
1
+ # Solargraph::Rails - Help solargraph with Rails
2
+
3
+ ## Work in progress - here be dragons
4
+ There are significant rough edges to this gem still. Don't use it if you're not willing to do things like build gems from source and install them locally. See `Installation` below for more info.
5
+
6
+ ## Models
7
+ Given a typical Rails model like this:
8
+
9
+ ```ruby
10
+ # == Schema Information
11
+ #
12
+ # Table name: my_books
13
+ #
14
+ # id :integer not null, primary key
15
+ # author :string
16
+ # name :string
17
+ # created_at :datetime not null
18
+ # updated_at :datetime not null
19
+ #
20
+ class MyBook < ApplicationRecord
21
+ def my_method
22
+ "hello"
23
+ end
24
+
25
+ ...
26
+
27
+ end
28
+ ```
29
+
30
+ 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.
31
+
32
+ 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.
33
+
34
+ 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.
35
+
36
+ With this you get autocompletion on ActiveRecord attributes:
37
+
38
+ ![Autocompletion of dynamic attributes like created_at](assets/solar_rails_autocomplete.gif)
39
+
40
+ ... and go to definition commands take you to the schema comment for that column:
41
+
42
+
43
+ ![Go to definition of dynamic attributes like created_at](assets/solar_rails_goto.gif)
44
+
45
+ ... and peek commands show you documentation about the attribute:
46
+
47
+ ![Peek at documentation of attributes like created_at, author, etc.](assets/peek.png)
48
+
49
+ ### Reload workspace after migrations
50
+ Solargraph won't know about attributes that you add during a session. Restart your LSP workspace to get the new attributes.
51
+
52
+ For my setup with Emacs, that means running `M-x lsp-workspace-restart`, YMMV in other editors.
53
+
54
+ ## Associations
55
+ This is coming soon.
56
+
57
+ ## Known issues
58
+ 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.
59
+
60
+ ## Installation
61
+
62
+ ### Install `solargraph` v0.40+ and `solargraph_rails` locally
63
+
64
+ SG v0.40 and solargraph_rails are unreleased at time of writing. You'll need to install both locally:
65
+
66
+ 1. Clone solargraph and update version.rb to 0.40.0
67
+ 2. Run `gem build` in the root of your clone to create `solargraph-0.40.0.gem`
68
+ 3. Clone solargraph_rails
69
+ 4. Run `gem build` in the root of your clone to create `solargraph_rails-0.1.1.gem`
70
+ 5. From the root of your Rails app, install solargraph locally: `gem install --local /path/to/solargraph-0.40.0.gem`
71
+ 6. From the root of your Rails app, install solargraph_rails locally: `gem install --local /path/to/solargraph_rails-0.1.1.gem`
72
+
73
+ ### Install gems outside of bundler
74
+ This is the path described above. 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`.
75
+
76
+ #### Alternative: using bundler
77
+ If you do want to use bundler, add `gem "solargraph", path: "/path/to/my_solargraph"` and `gem "solargraph_rails", path: "/path/to/my_solargraph_rails"` instead of steps 5 & 6 above.
78
+
79
+ ### Add `solargraph_rails` to your `.solargraph.yml`
80
+
81
+ ```
82
+ plugins:
83
+ - solargraph_rails
84
+ ```
85
+
86
+ ### Add annotate
87
+ Add schema comments your model files using [Annotate](https://github.com/ctran/annotate_models/). At the moment Solargraph::Rails assumes your schema comments are at the top of the source file.
88
+
89
+ ## Development
90
+
91
+ Fork the project, start hacking, put up a PR :).
92
+
93
+ 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
94
+
95
+ `api_map = Solargraph::ApiMap.load(Rails.root)`
96
+
97
+ 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.
98
+
99
+ Once you have an instance of `Solargraph::ApiMap`, you can interrogate it with Solargraph code like:
100
+
101
+ `pins = api_map.get_methods('MyBook')`
102
+
103
+ More examples here: https://solargraph.org/guides/code-examples
104
+
105
+ ## Contributing
106
+
107
+ Bug reports and pull requests are welcome on GitHub at https://github.com/iftheshoefritz/solargraph_rails.
108
+
109
+ ## License
110
+
111
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
Binary file
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "solargraph-rails"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'solargraph'
4
+ require 'solargraph/rails/version'
5
+ require_relative 'solargraph/rails/pin_creator'
6
+ require_relative 'solargraph/rails/ruby_parser'
7
+ require_relative 'solargraph/rails/files_loader'
8
+
9
+ module Solargraph
10
+ module Rails
11
+ class DynamicAttributes < Solargraph::Convention::Base
12
+ def global yard_map
13
+ Solargraph::Environ.new(pins: parse_models)
14
+ end
15
+
16
+ private
17
+
18
+ def parse_models
19
+ pins = []
20
+
21
+ FilesLoader.new(
22
+ Dir[File.join(Dir.pwd, 'app', 'models', '**', '*.rb')]
23
+ ).each { |file, contents| pins.push *PinCreator.new(file, contents).create_pins }
24
+
25
+ pins
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+
32
+ Solargraph::Convention.register Solargraph::Rails::DynamicAttributes
@@ -0,0 +1,16 @@
1
+ module Solargraph
2
+ module Rails
3
+ class FilesLoader
4
+ def initialize(file_names)
5
+ @file_names = file_names
6
+ end
7
+
8
+ def each(&blk)
9
+ @file_names.each do |file_name|
10
+ Solargraph::Logging.logger.info "loading from #{file_name}"
11
+ blk.call(file_name, File.read(file_name))
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Solargraph
4
+ module Rails
5
+ class PinCreator
6
+ attr_reader :contents, :path
7
+
8
+ def initialize(path, contents)
9
+ @path = path
10
+ @contents = contents
11
+ end
12
+
13
+ def create_pins
14
+ model_attrs = []
15
+ model_name = nil
16
+ module_names = []
17
+ parser = RubyParser.new(file_contents: contents)
18
+
19
+ parser.on_comment do |comment|
20
+ Solargraph::Logging.logger.info "found comment #{comment}"
21
+ col_name, col_type = col_with_type(comment)
22
+ if type_translation.keys.include?(col_type)
23
+ loc = Solargraph::Location.new(
24
+ path,
25
+ Solargraph::Range.from_to(
26
+ parser.current_line_number,
27
+ 0,
28
+ parser.current_line_number,
29
+ parser.current_line_length - 1
30
+ )
31
+ )
32
+ model_attrs << { name: col_name, type: col_type, location: loc }
33
+ else
34
+ Solargraph::Logging.logger.info 'could not find annotation in comment'
35
+ end
36
+ end
37
+
38
+ parser.on_module do |mod_name|
39
+ Solargraph::Logging.logger.info "found module #{mod_name}"
40
+ module_names << mod_name
41
+ end
42
+
43
+ parser.on_class do |klass, superklass|
44
+ Solargraph::Logging.logger.info "found class: #{klass} < #{superklass}"
45
+ if ['ActiveRecord::Base', 'ApplicationRecord'].include?(superklass)
46
+ model_name = klass
47
+ else
48
+ Solargraph::Logging.logger.info "Unable to find ActiveRecord model from #{klass} #{superklass}"
49
+ model_attrs = [] # don't include anything from this file
50
+ end
51
+ end
52
+
53
+ parser.parse
54
+
55
+ Solargraph::Logging.logger.info "Adding #{model_attrs.count} attributes as pins"
56
+ model_attrs.map do |attr|
57
+ Solargraph::Pin::Method.new(
58
+ name: attr[:name],
59
+ comments: "@return [#{type_translation[attr[:type]]}]",
60
+ location: attr[:location],
61
+ closure: Solargraph::Pin::Namespace.new(name: module_names.join('::') + "::#{model_name}"),
62
+ scope: :instance,
63
+ attribute: true
64
+ )
65
+ end
66
+ end
67
+
68
+ def col_with_type(line)
69
+ line
70
+ .gsub(/[\(\),:\d]/, '')
71
+ .split
72
+ .first(2)
73
+ end
74
+
75
+ def type_translation
76
+ {
77
+ 'decimal' => 'BigDecimal',
78
+ 'integer' => 'Integer',
79
+ 'date' => 'Date',
80
+ 'datetime' => 'ActiveSupport::TimeWithZone',
81
+ 'string' => 'String',
82
+ 'boolean' => 'Boolean',
83
+ 'text' => 'String'
84
+ }
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,70 @@
1
+ module Solargraph
2
+ module Rails
3
+ class RubyParser
4
+ attr_reader :current_line_number, :current_line_length
5
+
6
+ def initialize(file_contents: '')
7
+ @lines = file_contents.lines
8
+ @comment_handlers = []
9
+ @class_handlers = []
10
+ @module_handlers = []
11
+ end
12
+
13
+ def on_comment(&blk)
14
+ @comment_handlers << blk
15
+ end
16
+
17
+ def on_class(&blk)
18
+ @class_handlers << blk
19
+ end
20
+
21
+ def on_module(&blk)
22
+ @module_handlers << blk
23
+ end
24
+
25
+ def parse
26
+ @lines
27
+ .map(&:rstrip)
28
+ .each_with_index do |line, i|
29
+ @current_line_number = i
30
+ @current_line_length = line.length
31
+
32
+ if is_comment?(line)
33
+ comment_content = line.gsub(/#\s*/, '')
34
+ @comment_handlers.each { |handler| handler.call(comment_content) }
35
+ end
36
+
37
+ if is_class?(line)
38
+ line.scan(/(?:(?<!<\s)(?:(\b\w+\b)\:\:))/).flatten.each do |inline_module_name|
39
+ @module_handlers.each { | handler| handler.call(inline_module_name) }
40
+ end
41
+ line.match(/class\s+(?:\w*?(?:\:\:))*([A-Z]\w*)/)
42
+ klass_name = $1
43
+ line.match(/(?:<\s+)((?:[A-Z]\w*(?:\:\:)?)*)/)
44
+ superklass_name = $1
45
+ @class_handlers.each { |handler| handler.call(klass_name, superklass_name) }
46
+ end
47
+
48
+ if is_module?(line)
49
+ module_name = line.match(/^\s*module\s*?([A-Z]\w+)/)[1]
50
+ @module_handlers.each { |handler| handler.call(module_name) }
51
+ end
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def is_comment?(line)
58
+ line =~ (/^\s*#/)
59
+ end
60
+
61
+ def is_class?(line)
62
+ line =~ /^[^#]?.*?class\s+?[A-Z]/
63
+ end
64
+
65
+ def is_module?(line)
66
+ line =~ (/^\s*module\s*?([A-Z]\w+)/)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,5 @@
1
+ module Solargraph
2
+ module Rails
3
+ VERSION = '0.2.0.pre'
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "solargraph/rails/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "solargraph-rails"
8
+ spec.version = Solargraph::Rails::VERSION
9
+ spec.authors = ["Fritz Meissner"]
10
+ spec.email = ["fritz.meissner@gmail.com"]
11
+
12
+ spec.summary = %q{Solargraph plugin that adds Rails-specific code through a Convention}
13
+ spec.description = %q{Add reflection on ActiveModel dynamic attributes that will be created at runtime}
14
+ spec.homepage = 'https://github.com/iftheshoefritz/solargraph-rails'
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 2.1.4"
25
+ spec.add_development_dependency "rake", "~> 12.3.3"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+
28
+ spec.add_runtime_dependency "solargraph", "~> 0.40.0"
29
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: solargraph-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0.pre
5
+ platform: ruby
6
+ authors:
7
+ - Fritz Meissner
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-01-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.4
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 12.3.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 12.3.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: solargraph
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.40.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.40.0
69
+ description: Add reflection on ActiveModel dynamic attributes that will be created
70
+ at runtime
71
+ email:
72
+ - fritz.meissner@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - assets/peek.png
85
+ - assets/solar_rails_autocomplete.gif
86
+ - assets/solar_rails_goto.gif
87
+ - bin/console
88
+ - bin/setup
89
+ - lib/solargraph-rails.rb
90
+ - lib/solargraph/rails/files_loader.rb
91
+ - lib/solargraph/rails/pin_creator.rb
92
+ - lib/solargraph/rails/ruby_parser.rb
93
+ - lib/solargraph/rails/version.rb
94
+ - solargraph-rails.gemspec
95
+ homepage: https://github.com/iftheshoefritz/solargraph-rails
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">"
111
+ - !ruby/object:Gem::Version
112
+ version: 1.3.1
113
+ requirements: []
114
+ rubygems_version: 3.1.4
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Solargraph plugin that adds Rails-specific code through a Convention
118
+ test_files: []