@@ -467,10 +467,75 @@ def name; end
467467 assert_equal ( 15 , response . range . end . character )
468468 end
469469
470+ test "finds the controller action definition when only one controller matches" do
471+ source = <<~RUBY
472+ Rails.application.routes.draw do
473+ # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
474+ resources :users do
475+ get :archive, on: :collection, to: "users#archive"
476+ get :unarchive, on: :collection, to: "users#unarchive"
477+ end
478+
479+ scope module: "admin" do
480+ resources :users do
481+ get :archive, on: :collection, to: "users#archive"
482+ end
483+ end
484+ end
485+ RUBY
486+
487+ response = generate_definitions_for_source ( source , { line : 4 , character : 45 } , uri : URI ( "file:///config/routes.rb" ) )
488+
489+ assert_equal ( 1 , response . size )
490+
491+ location = response . first
492+
493+ expected_path = File . expand_path ( "test/dummy/app/controllers/users_controller.rb" )
494+ assert_equal ( "file://#{ expected_path } " , location . uri )
495+ assert_equal ( 7 , location . range . start . line )
496+ assert_equal ( 7 , location . range . end . line )
497+ end
498+
499+ test "finds all matching controller actions when multiple controllers exist in different namespaces" do
500+ source = <<~RUBY
501+ Rails.application.routes.draw do
502+ # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
503+ resources :users do
504+ get :archive, on: :collection, to: "users#archive"
505+ get :unarchive, on: :collection, to: "users#unarchive"
506+ end
507+
508+ scope module: "admin" do
509+ resources :users do
510+ get :archive, on: :collection, to: "users#archive"
511+ end
512+ end
513+ end
514+ RUBY
515+
516+ response = generate_definitions_for_source ( source , { line : 3 , character : 45 } , uri : URI ( "file:///config/routes.rb" ) )
517+
518+ assert_equal ( 2 , response . size )
519+
520+ location = response . first
521+
522+ expected_path = File . expand_path ( "test/dummy/app/controllers/users_controller.rb" )
523+ assert_equal ( "file://#{ expected_path } " , location . uri )
524+ assert_equal ( 5 , location . range . start . line )
525+ assert_equal ( 5 , location . range . end . line )
526+
527+ location = response . second
528+
529+ expected_path = File . expand_path ( "test/dummy/app/controllers/admin/users_controller.rb" )
530+ assert_equal ( "file://#{ expected_path } " , location . uri )
531+ assert_equal ( 6 , location . range . start . line )
532+ assert_equal ( 6 , location . range . end . line )
533+ end
534+
470535 private
471536
472- def generate_definitions_for_source ( source , position )
473- with_server ( source ) do |server , uri |
537+ def generate_definitions_for_source ( source , position , uri : nil )
538+ with_server ( source , * [ uri ] . compact ) do |server , uri |
474539 sleep ( 0.1 ) while RubyLsp ::Addon . addons . first . instance_variable_get ( :@rails_runner_client ) . is_a? ( NullClient )
475540
476541 server . process_message (
0 commit comments