diff --git a/lib/ruby_lsp/ruby_lsp_rails/definition.rb b/lib/ruby_lsp/ruby_lsp_rails/definition.rb index 8dc12f93..d460ac3a 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/definition.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/definition.rb @@ -9,8 +9,6 @@ module Rails # request](https://microsoft.github.io/language-server-protocol/specification#textDocument_definition) jumps to the # definition of the symbol under the cursor. # - # It is available only Rails 7.1 or newer. - # # Currently supported targets: # - Callbacks # - Named routes (e.g. `users_path`) @@ -22,6 +20,7 @@ module Rails # ``` # # Notes for named routes: + # - It is available only in Rails 7.1 or newer. # - Route may be defined across multiple files, e.g. using `draw`, rather than in `routes.rb`. # - Routes won't be found if not defined for the Rails development environment. # - If using `constraints`, the route can only be found if the constraints are met. @@ -58,7 +57,7 @@ def on_call_node_enter(node) if Support::Callbacks::ALL.include?(message) handle_callback(node) - elsif message.match?(/^([a-zA-Z0-9_]+)(_path|_url)$/) + elsif message.end_with?("_path") || message.end_with?("_url") handle_route(node) end end diff --git a/lib/ruby_lsp/ruby_lsp_rails/server.rb b/lib/ruby_lsp/ruby_lsp_rails/server.rb index 56270563..a9308b95 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/server.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/server.rb @@ -90,15 +90,14 @@ def route_location(name) return { result: { location: nil } } end - key = T.must(name.match(/^([a-zA-Z0-9_]+)(_path|_url)$/))[1] + key = T.must(name.match(/(^.+)(_path|_url)$/))[1] # A token could match the _path or _url pattern, but not be an actual route. - unless ::Rails.application.routes.named_routes.key?(key) + route = ::Rails.application.routes.named_routes.get(key) + unless route return { result: { location: nil } } end - route = ::Rails.application.routes.named_routes.get(key) - unless route&.source_location return { result: { location: nil } } end