Skip to content

Commit

Permalink
Fix autocompletion in routes files
Browse files Browse the repository at this point in the history
  • Loading branch information
grncdr committed May 20, 2023
1 parent 9574d0e commit 5890127
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 56 deletions.
3 changes: 3 additions & 0 deletions lib/solargraph-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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'
Expand All @@ -33,6 +34,8 @@ 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)
Expand Down
16 changes: 0 additions & 16 deletions lib/solargraph/rails/annotations/action_dispatch.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
module ActionDispatch
module Routing
class RouteSet
# @!method draw(&block)
# @yieldself [Mapper]
end

class Mapper
include Resources
include Concerns
include Scoping
include Redirection
include HttpHelpers
include Base
end
end

module Flash
class FlashHash
# @return [ActionDispatch::Flash::FlashNow]
Expand Down
15 changes: 15 additions & 0 deletions lib/solargraph/rails/routes_dsl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Solargraph
module Rails
module RoutesDsl
def self.local(source_map)
return unless source_map.filename.end_with?('routes.rb')

block = source_map.pins.find do |pin|
pin.is_a?(Pin::Block) && pin.receiver&.source&.end_with?('.draw')
end

block.instance_variable_set(:@binder, ComplexType.parse('ActionDispatch::Routing::Mapper')) if block
end
end
end
end
62 changes: 22 additions & 40 deletions spec/solargraph-rails/rails7_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,17 @@ def index
)
end

it 'can auto-complete inside routes' do
map =
use_workspace './spec/rails7' do |root|
root.write_file 'config/routes.rb', <<~EOS
Rails.application.routes.draw do
res
resource :things do
res
end
end
EOS
end

filename = './config/routes.rb'
expect(completion_at(filename, [1, 5], map)).to include('resources')
expect(completion_at(filename, [3, 7], map)).to include('resources')
end

it 'can auto-complete inside mailers' do
map =
use_workspace './spec/rails7' do |root|
root.write_file 'app/mailers/test_mailer.rb', <<~EOS
class TestMailer < ActionMailer::Base
defa
def welcome_email
ma
class TestMailer < ActionMailer::Base
defa
def welcome_email
ma
end
end
end
EOS
EOS
end

filename = './app/mailers/test_mailer.rb'
Expand All @@ -66,25 +48,25 @@ def welcome_email
map =
use_workspace './spec/rails7' do |root|
root.write_file 'db/migrate/20130502114652_create_things.rb', <<~EOS
class CreateThings < ActiveRecord::Migration[7.0]
def self.up
crea
end
def change
crea
create_table :things do |t|
t.col
class CreateThings < ActiveRecord::Migration[7.0]
def self.up
crea
end
change_table :things do |t|
t.col
end
create_join_table :things do |t|
t.col
def change
crea
create_table :things do |t|
t.col
end
change_table :things do |t|
t.col
end
create_join_table :things do |t|
t.col
end
end
end
end
EOS
EOS
end

filename = './db/migrate/20130502114652_create_things.rb'
Expand Down

0 comments on commit 5890127

Please sign in to comment.