@@ -467,10 +467,83 @@ def name; end
467467 assert_equal ( 15 , response . range . end . character )
468468 end
469469
470+ test "returns 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 (
488+ source ,
489+ { line : 4 , character : 45 } ,
490+ uri : URI ( "file:///config/routes.rb" ) ,
491+ )
492+
493+ assert_equal ( 1 , response . size )
494+
495+ location = response . first
496+
497+ expected_path = File . expand_path ( "test/dummy/app/controllers/users_controller.rb" )
498+ assert_equal ( "file://#{ expected_path } " , location . uri )
499+ assert_equal ( 7 , location . range . start . line )
500+ assert_equal ( 7 , location . range . end . line )
501+ end
502+
503+ test "returns all matching controller actions when multiple controllers exist in different namespaces" do
504+ source = <<~RUBY
505+ Rails.application.routes.draw do
506+ # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
507+ resources :users do
508+ get :archive, on: :collection, to: "users#archive"
509+ get :unarchive, on: :collection, to: "users#unarchive"
510+ end
511+
512+ scope module: "admin" do
513+ resources :users do
514+ get :archive, on: :collection, to: "users#archive"
515+ end
516+ end
517+ end
518+ RUBY
519+
520+ response = generate_definitions_for_source (
521+ source ,
522+ { line : 3 , character : 45 } ,
523+ uri : URI ( "file:///config/routes.rb" ) ,
524+ )
525+
526+ assert_equal ( 2 , response . size )
527+
528+ location = response . first
529+
530+ expected_path = File . expand_path ( "test/dummy/app/controllers/users_controller.rb" )
531+ assert_equal ( "file://#{ expected_path } " , location . uri )
532+ assert_equal ( 5 , location . range . start . line )
533+ assert_equal ( 5 , location . range . end . line )
534+
535+ location = response . second
536+
537+ expected_path = File . expand_path ( "test/dummy/app/controllers/admin/users_controller.rb" )
538+ assert_equal ( "file://#{ expected_path } " , location . uri )
539+ assert_equal ( 6 , location . range . start . line )
540+ assert_equal ( 6 , location . range . end . line )
541+ end
542+
470543 private
471544
472- def generate_definitions_for_source ( source , position )
473- with_server ( source ) do |server , uri |
545+ def generate_definitions_for_source ( source , position , uri : nil )
546+ with_server ( source , * [ uri ] . compact ) do |server , uri |
474547 sleep ( 0.1 ) while RubyLsp ::Addon . addons . first . instance_variable_get ( :@rails_runner_client ) . is_a? ( NullClient )
475548
476549 server . process_message (
0 commit comments