ruby-lsp-rails-routes 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d9cd218d807199a52c4a777b85f9fdf417317b5ec41b63fecd79aa53cb842630
4
+ data.tar.gz: 4ac4413993b99273f45c89e0d53398dab22a474d63eee5b4a705f0346842fff7
5
+ SHA512:
6
+ metadata.gz: 7469386b93318ff3b35a26380b1a1105ff7995df6585a43c8f7d1a37fedc4f1597deb3948e0294fdd4d413577c23eeb87f80ec908ebba76f6f2f3a8ebbc4d9b3
7
+ data.tar.gz: db3cc968b08917d3565fc3805cf0d0730df0b72a675a02dc9c5f52bce52d591ca087bffa304ea6067b8ce4467268b79b22f5fcddba11dc5f5483a75337cda38c
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## [Unreleased]
4
+
5
+ - Initial version: go-to-definition from `*_path` / `*_url` route helpers to the controller action.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 aki77
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.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # ruby-lsp-rails-routes
2
+
3
+ A [Ruby LSP](https://github.com/Shopify/ruby-lsp) add-on that adds **go to definition** from Rails
4
+ route helpers to the controller action they map to.
5
+
6
+ ruby-lsp-rails already lets you jump from a `*_path` / `*_url` helper to its declaration in
7
+ `config/routes.rb`. This add-on goes one step further: it resolves the named route to its
8
+ controller action at runtime and offers the action's `def` line as an additional candidate. The
9
+ routes.rb declaration is still offered, so both show up in the definition picker.
10
+
11
+ ```ruby
12
+ redirect_to users_path # go to definition -> UsersController#index (and config/routes.rb)
13
+ ```
14
+
15
+ ## Requirements
16
+
17
+ - [ruby-lsp](https://github.com/Shopify/ruby-lsp) `>= 0.26.0, < 0.27.0`
18
+ - [ruby-lsp-rails](https://github.com/Shopify/ruby-lsp-rails) — this add-on talks to its rails
19
+ runner to resolve routes, so both must be installed.
20
+
21
+ ## Installation
22
+
23
+ Add the gem to your application's `Gemfile`:
24
+
25
+ ```ruby
26
+ group :development do
27
+ gem 'ruby-lsp-rails-routes', require: false
28
+ end
29
+ ```
30
+
31
+ Then run `bundle install` and restart the Ruby LSP server in your editor. The add-on is discovered
32
+ automatically by Ruby LSP via the `lib/ruby_lsp/**/addon.rb` convention.
33
+
34
+ ## Scope
35
+
36
+ The route helper must be called with a self (or implicit) receiver. Route helpers reached through
37
+ an explicit receiver (`some_object.users_path`), `url_for`, and polymorphic routes are not
38
+ supported. Routes that do not map to a controller action (`redirect`, mounted Rack apps, mounted
39
+ engines) are skipped.
40
+
41
+ ## License
42
+
43
+ MIT
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "version"
4
+ require_relative "util"
5
+ require_relative "definition"
6
+ # NOTE: server_addon.rb is intentionally NOT required here. Its base class
7
+ # RubyLsp::Rails::ServerAddon only exists in the rails runner process, which loads the file via
8
+ # register_server_addon. Requiring it here would fail to load this add-on in the LSP process.
9
+
10
+ module RubyLsp
11
+ module RailsRoutes
12
+ # Ruby LSP add-on that adds go-to-definition from *_path / *_url route helpers to the
13
+ # controller action the named route maps to. Works alongside ruby-lsp-rails, whose own
14
+ # Definition listener contributes the routes.rb declaration; this add-on only adds the action
15
+ # location as an extra candidate.
16
+ class Addon < ::RubyLsp::Addon
17
+ SERVER_ADDON_NAME = "Rails Routes"
18
+
19
+ def activate(_global_state, _outgoing_queue)
20
+ @server_addon_registered = false
21
+ end
22
+
23
+ def deactivate; end
24
+
25
+ def name
26
+ "Rails Routes"
27
+ end
28
+
29
+ def version
30
+ VERSION
31
+ end
32
+
33
+ # Invoked on every definition request. The ruby-lsp-rails client is resolved lazily here
34
+ # (not in #activate) because add-on activation order is not guaranteed, and because the
35
+ # runner client boots asynchronously — it is a NullClient until ready.
36
+ def create_definition_listener(response_builder, _uri, node_context, dispatcher)
37
+ client = rails_runner_client
38
+ return unless client
39
+
40
+ ensure_server_addon_registered(client)
41
+
42
+ Definition.new(client, SERVER_ADDON_NAME, response_builder, node_context, dispatcher)
43
+ end
44
+
45
+ private
46
+
47
+ # Returns the ruby-lsp-rails RunnerClient, or nil if the add-on is unavailable/incompatible.
48
+ def rails_runner_client
49
+ ::RubyLsp::Addon.get("Ruby LSP Rails", ">= 0.4.0").rails_runner_client
50
+ rescue ::RubyLsp::Addon::AddonNotFoundError, ::RubyLsp::Addon::IncompatibleApiError
51
+ nil
52
+ end
53
+
54
+ # Registers our server add-on exactly once. register_server_addon is a no-op on NullClient,
55
+ # so registering before the runner finishes booting would silently be lost. We therefore
56
+ # only register once the client is connected, guarded by an idempotency flag.
57
+ def ensure_server_addon_registered(client)
58
+ return if @server_addon_registered
59
+ return unless client.connected? # false for NullClient
60
+
61
+ client.register_server_addon(File.expand_path("server_addon.rb", __dir__))
62
+ @server_addon_registered = true
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLsp
4
+ module RailsRoutes
5
+ # When the cursor is on a *_path / *_url route helper call, asks the rails runner process to
6
+ # resolve the named route to its controller action, and adds that action's source location as
7
+ # a definition candidate. ruby-lsp-rails' own Definition listener still contributes the
8
+ # routes.rb declaration to the same response builder, so both candidates coexist.
9
+ class Definition
10
+ include ::RubyLsp::Requests::Support::Common
11
+
12
+ def initialize(client, server_addon_name, response_builder, node_context, dispatcher)
13
+ @client = client
14
+ @server_addon_name = server_addon_name
15
+ @response_builder = response_builder
16
+ @node_context = node_context
17
+
18
+ dispatcher.register(self, :on_call_node_enter)
19
+ end
20
+
21
+ # Mirrors ruby-lsp-rails' own detection: self (or implicit) receiver, message ends with
22
+ # _path / _url.
23
+ def on_call_node_enter(node)
24
+ return unless self_receiver?(node)
25
+
26
+ message = node.message
27
+ return unless message
28
+ return unless message.end_with?("_path") || message.end_with?("_url")
29
+
30
+ result = @client.delegate_request(
31
+ server_addon_name: @server_addon_name,
32
+ request_name: "action_location",
33
+ name: message
34
+ )
35
+ return unless result && result[:location]
36
+
37
+ @response_builder << Util.location_from_s(result[:location])
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is loaded inside the rails runner process via RunnerClient#register_server_addon.
4
+ # The base class RubyLsp::Rails::ServerAddon only exists in that process, so this file must never
5
+ # be required from the LSP process (do not require_relative it from addon.rb).
6
+ module RubyLsp
7
+ module RailsRoutes
8
+ # Runs inside the rails runner process. Resolves a named route to its controller action's
9
+ # source location by inspecting the application's routes, and returns it to the LSP process.
10
+ class ServerAddon < ::RubyLsp::Rails::ServerAddon
11
+ def name
12
+ "Rails Routes"
13
+ end
14
+
15
+ def execute(request, params)
16
+ case request
17
+ when "action_location"
18
+ send_result(action_location(params[:name]))
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ # Resolves a route helper name (e.g. "users_path") to its controller action's source
25
+ # location, returned as { location: "file:line" }. Returns nil when the route does not map
26
+ # to a resolvable controller action. Mirrors ruby-lsp-rails Server#route_location for the
27
+ # route lookup, then continues on to the action method's source_location.
28
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
29
+ def action_location(name)
30
+ # Rails 8 does not populate named_routes until the routes are loaded.
31
+ ::Rails.application.reload_routes_unless_loaded if ::Rails.application.respond_to?(:reload_routes_unless_loaded)
32
+
33
+ match_data = name.match(/^(.+)(_path|_url)$/)
34
+ return unless match_data
35
+
36
+ route = ::Rails.application.routes.named_routes.get(match_data[1])
37
+ return unless route
38
+
39
+ controller = route.defaults[:controller]
40
+ action = route.defaults[:action]
41
+ return unless controller && action
42
+
43
+ # camelize also converts a namespaced controller ("members/x") into "Members::X".
44
+ controller_class = "#{controller}_controller".camelize.constantize
45
+ # MVP assumes the action has an explicit `def` in the controller. instance_method may
46
+ # resolve to an inherited method; refining this is deferred to a later phase.
47
+ source_location = controller_class.instance_method(action).source_location
48
+ return unless source_location
49
+
50
+ file, line = source_location
51
+ { location: "#{file}:#{line}" }
52
+ rescue NameError
53
+ nil
54
+ end
55
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+
5
+ module RubyLsp
6
+ module RailsRoutes
7
+ # Helpers shared by the LSP-side listeners. Kept dependency-light so they can be unit tested
8
+ # without booting Ruby LSP.
9
+ module Util
10
+ class << self
11
+ # Converts a "path:line" string (1-based line, as returned by Method#source_location) into
12
+ # an Interface::Location (0-based line). The last segment is treated as the line only when
13
+ # it is numeric, so Windows drive letters ("C:/foo.rb:42") survive the split and a string
14
+ # without a line suffix falls back to line 0.
15
+ def location_from_s(location_string)
16
+ file_path, _, line = location_string.rpartition(":")
17
+ if file_path.empty? || line !~ /\A\d+\z/
18
+ file_path = location_string
19
+ line = nil
20
+ end
21
+ line_as_number = line ? line.to_i - 1 : 0
22
+
23
+ ::RubyLsp::Interface::Location.new(
24
+ uri: URI::Generic.from_path(path: file_path).to_s,
25
+ range: ::RubyLsp::Interface::Range.new(
26
+ start: ::RubyLsp::Interface::Position.new(line: line_as_number, character: 0),
27
+ end: ::RubyLsp::Interface::Position.new(line: line_as_number, character: 0)
28
+ )
29
+ )
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLsp
4
+ module RailsRoutes
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-lsp-rails-routes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - aki77
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: ruby-lsp
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: 0.26.0
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: 0.27.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: 0.26.0
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: 0.27.0
32
+ description: Provides go-to-definition from *_path / *_url route helpers to the controller
33
+ action the named route maps to.
34
+ email:
35
+ - 163168+aki77@users.noreply.github.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - CHANGELOG.md
41
+ - LICENSE.txt
42
+ - README.md
43
+ - lib/ruby_lsp/ruby_lsp_rails_routes/addon.rb
44
+ - lib/ruby_lsp/ruby_lsp_rails_routes/definition.rb
45
+ - lib/ruby_lsp/ruby_lsp_rails_routes/server_addon.rb
46
+ - lib/ruby_lsp/ruby_lsp_rails_routes/util.rb
47
+ - lib/ruby_lsp/ruby_lsp_rails_routes/version.rb
48
+ homepage: https://github.com/aki77/ruby-lsp-rails-routes
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ rubygems_mfa_required: 'true'
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '3.4'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 4.0.10
68
+ specification_version: 4
69
+ summary: Ruby LSP add-on for Rails route helpers
70
+ test_files: []