zed-rails-jumper 0.1.3

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: e3cfa97946fff56ec96843f304309a74a2f28d16fb59e052d046fd92ec1975d7
4
+ data.tar.gz: 94ee5fa6638496adc92bf478d412c0a3b4660212af07403c929d6b8549ef19dd
5
+ SHA512:
6
+ metadata.gz: 5bde8595dcb51ff98f0fb788f25c4da89f4cb4021f77fbc9c86c6b579905c2863796cba66b73fa9a72ed0f9f47f5e1b6732142fb7b4f8b8c8ac2da418ff4d609
7
+ data.tar.gz: 89b0a52d6154b78c8d77210f3d44e6cfc1833533f0baa9b8eb94d59c13aa8d8b2efbaae8e7f57a8fbae371a4e2d00ee24043e4b381643c8b2336c0a1ac6f15eb
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright Tonksthebear
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,140 @@
1
+ # Zed Rails Jumper
2
+
3
+ A CLI gem for the Zed editor that helps developers quickly jump between Rails controllers and their associated views. Inspired by [zed-test-toggle](https://github.com/MoskitoHero/zed-test-toggle).
4
+
5
+ ## Installation
6
+
7
+ Ensure the [Zed CLI is installed](https://zed.dev/docs/getting-started?highlight=cli#cli)
8
+
9
+ Install the gem:
10
+
11
+ ```bash
12
+ gem install zed-rails-jumper
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ This tool is designed to be called from Zed tasks. It can jump in both directions:
18
+ - From controller methods to associated view files
19
+ - From view files to their corresponding controller and action
20
+
21
+ **Note**: The tool automatically opens the target file in Zed using `system("zed", file_path)`.
22
+
23
+ ### Zed Task Configuration
24
+
25
+ Add these tasks to your Zed tasks configuration:
26
+
27
+ ```json
28
+ [
29
+ {
30
+ "label": "Jump to Rails View",
31
+ "command": "bundle exec zed-rails-jumper",
32
+ "args": [
33
+ "lookup",
34
+ "-p",
35
+ "\"$ZED_RELATIVE_FILE\"",
36
+ "-r",
37
+ "./",
38
+ "-l",
39
+ "\"$ZED_ROW\""
40
+ ],
41
+ "hide": "always",
42
+ "use_new_terminal": false,
43
+ "reveal": "never"
44
+ },
45
+ {
46
+ "label": "Jump to Rails Controller",
47
+ "command": "bundle exec zed-rails-jumper",
48
+ "args": ["controller", "-p", "\"$ZED_RELATIVE_FILE\"", "-r", "./"],
49
+ "hide": "always",
50
+ "use_new_terminal": false,
51
+ "reveal": "never"
52
+ }
53
+ ]
54
+ ```
55
+
56
+ ### Keybinding Configuration
57
+
58
+ Add these keybindings to your Zed keybindings:
59
+
60
+ ```json
61
+ [
62
+ {
63
+ "bindings": {
64
+ "cmd-shift-v": [
65
+ "task::Spawn",
66
+ {
67
+ "task_name": "Jump to Rails View",
68
+ "reevaluate_context": true
69
+ }
70
+ ],
71
+ "cmd-shift-c": [
72
+ "task::Spawn",
73
+ {
74
+ "task_name": "Jump to Rails Controller",
75
+ "reevaluate_context": true
76
+ }
77
+ ]
78
+ }
79
+ }
80
+ ]
81
+ ```
82
+
83
+ ## How it Works
84
+
85
+ ### Controller to View Jumping
86
+ 1. **Controller Detection**: The tool detects if you're in a Rails controller file
87
+ 2. **Method Detection**: Based on cursor position, it finds the current controller method
88
+ 3. **View Discovery**: It searches for view files in the corresponding `app/views` directory
89
+ 4. **File Opening**: Opens the first matching view file in Zed using `system("zed", file_path)`
90
+ 5. **Multiple Formats**: Supports various view formats:
91
+ - `.html.erb`
92
+ - `.erb`
93
+ - `.js.erb`
94
+ - `.json.erb`
95
+ - `.json.jbuilder`
96
+ - `.xml.builder`
97
+
98
+ ### View to Controller Jumping
99
+ 1. **View Detection**: The tool detects if you're in a Rails view file
100
+ 2. **Controller Mapping**: It maps the view path to the corresponding controller
101
+ 3. **Action Detection**: It extracts the action name from the view filename
102
+ 4. **Method Verification**: It checks if the action method exists in the controller
103
+ 5. **File Opening**: Opens the controller file in Zed using `system("zed", file_path)`
104
+
105
+ ## Examples
106
+
107
+ ### Controller to View
108
+ If you're in `app/controllers/users_controller.rb` at line 5 (inside the `index` method), the tool will:
109
+ 1. Find `app/views/users/index.html.erb`
110
+ 2. Automatically open it in Zed
111
+
112
+ ### View to Controller
113
+ If you're in `app/views/users/show.html.erb`, the tool will:
114
+ 1. Find `app/controllers/users_controller.rb`
115
+ 2. Automatically open it in Zed
116
+
117
+ ## CLI Commands
118
+
119
+ ### `lookup` - Find and open views for current controller method
120
+ ```bash
121
+ zed-rails-jumper lookup -p "app/controllers/users_controller.rb" -r "/path/to/rails/app" -l 5
122
+ ```
123
+
124
+ ### `controller` - Find and open controller for current view
125
+ ```bash
126
+ zed-rails-jumper controller -p "app/views/users/show.html.erb" -r "/path/to/rails/app"
127
+ ```
128
+
129
+ ## Development
130
+
131
+ To set up the development environment:
132
+
133
+ ```bash
134
+ bundle install
135
+ bin/test
136
+ ```
137
+
138
+ ## License
139
+
140
+ MIT License - see the [LICENSE](MIT-LICENSE) file for details.
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "zed/rails/jumper/cli"
5
+
6
+ Zed::Rails::Jumper::CLI.start(ARGV)
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :zed_rails_jumper do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,265 @@
1
+ require "thor"
2
+ require "pathname"
3
+ require "fileutils"
4
+
5
+ module Zed
6
+ module Rails
7
+ module Jumper
8
+ class CLI < Thor
9
+ def self.start(*)
10
+ super
11
+ rescue Thor::InvocationError, ArgumentError, Errno::ENOENT, IOError => exc
12
+ warn String(exc)
13
+ exit(1)
14
+ end
15
+
16
+ def self.exit_on_failure?
17
+ true
18
+ end
19
+
20
+ desc "lookup", "Find associated Rails views for the current controller method"
21
+ option :path, aliases: "-p", required: true, desc: "Path to the current file"
22
+ option :root, aliases: "-r", required: true, desc: "Rails application root directory"
23
+ option :line, aliases: "-l", type: :numeric, desc: "Current line number (for cursor position)"
24
+ def lookup
25
+ current_file = options[:path]
26
+ rails_root = options[:root]
27
+ current_line = options[:line]
28
+
29
+ view_finder = ViewFinder.new(current_file, rails_root, current_line)
30
+ associated_views = view_finder.find_associated_views
31
+
32
+ if associated_views.any?
33
+ # Open the first view file found in Zed
34
+ system("zed", associated_views.first)
35
+ else
36
+ warn "No associated views found"
37
+ exit 1
38
+ end
39
+ end
40
+
41
+ desc "controller", "Find the controller and action for a given Rails view file"
42
+ option :path, aliases: "-p", required: true, desc: "Path to the current view file"
43
+ option :root, aliases: "-r", required: true, desc: "Rails application root directory"
44
+ def controller
45
+ view_file = options[:path]
46
+ rails_root = options[:root]
47
+
48
+ controller_finder = ControllerFinder.new(view_file, rails_root)
49
+ result = controller_finder.find_controller_and_action
50
+
51
+ if result
52
+ # Open the controller file in Zed
53
+ system("zed", result[:controller_file])
54
+ else
55
+ warn "No corresponding controller/action found"
56
+ exit 1
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ class ViewFinder
63
+ def initialize(current_file, rails_root, current_line = nil)
64
+ @current_file = Pathname.new(current_file)
65
+ @rails_root = Pathname.new(rails_root)
66
+ @current_line = current_line
67
+ end
68
+
69
+ def find_associated_views
70
+ return [] unless controller_file?
71
+
72
+ controller_name = extract_controller_name
73
+ action_name = extract_action_name
74
+
75
+ return [] unless controller_name && action_name
76
+
77
+ find_views_for_action(controller_name, action_name)
78
+ end
79
+
80
+ private
81
+
82
+ def controller_file?
83
+ @current_file.to_s.include?("controllers") && @current_file.extname == ".rb"
84
+ end
85
+
86
+ def extract_controller_name
87
+ # Extract controller name from file path
88
+ # e.g., app/controllers/users_controller.rb -> users
89
+ # e.g., app/controllers/users/archives_controller.rb -> users/archives
90
+ relative_path = @current_file.relative_path_from(@rails_root.join("app", "controllers"))
91
+ return nil unless relative_path.to_s.end_with?("_controller.rb")
92
+
93
+ relative_path.to_s.sub("_controller.rb", "")
94
+ end
95
+
96
+ def extract_action_name
97
+ return "index" unless @current_line && @current_file.exist?
98
+
99
+ # Read the file and find the method at the current line
100
+ lines = @current_file.readlines
101
+ current_line_content = lines[@current_line - 1] if @current_line <= lines.length
102
+
103
+ # Look for method definitions around the current line
104
+ method_name = find_method_at_line(lines, @current_line)
105
+
106
+ method_name || "index"
107
+ end
108
+
109
+ def find_method_at_line(lines, target_line)
110
+ # Find the method definition that contains the target line
111
+ current_method = nil
112
+
113
+ lines.each_with_index do |line, line_num|
114
+ line_index = line_num + 1
115
+
116
+ # Check if this line is a method definition
117
+ match = line.strip.match(/^\s*def\s+(\w+)/)
118
+ if match
119
+ current_method = match[1]
120
+ end
121
+
122
+ # If we've reached the target line, return the current method
123
+ if line_index == target_line
124
+ return current_method
125
+ end
126
+ end
127
+
128
+ nil
129
+ end
130
+
131
+ def find_views_for_action(controller_name, action_name)
132
+ view_files = []
133
+
134
+ # First, try the current controller's view directory
135
+ views_dir = @rails_root.join("app", "views", controller_name)
136
+ view_files.concat(find_view_files_in_directory(views_dir, action_name)) if views_dir.exist?
137
+
138
+ # If no views found, check inherited controllers
139
+ if view_files.empty?
140
+ inherited_controllers = find_inherited_controllers(controller_name)
141
+ inherited_controllers.each do |inherited_controller|
142
+ inherited_views_dir = @rails_root.join("app", "views", inherited_controller)
143
+ view_files.concat(find_view_files_in_directory(inherited_views_dir, action_name)) if inherited_views_dir.exist?
144
+ end
145
+ end
146
+
147
+ view_files
148
+ end
149
+
150
+ def find_view_files_in_directory(views_dir, action_name)
151
+ view_files = []
152
+
153
+ # Check for .erb files
154
+ erb_file = views_dir.join("#{action_name}.erb")
155
+ view_files << erb_file.to_s if erb_file.exist?
156
+
157
+ # Check for .html.erb files
158
+ html_erb_file = views_dir.join("#{action_name}.html.erb")
159
+ view_files << html_erb_file.to_s if html_erb_file.exist?
160
+
161
+ # Check for .js.erb files
162
+ js_erb_file = views_dir.join("#{action_name}.js.erb")
163
+ view_files << js_erb_file.to_s if js_erb_file.exist?
164
+
165
+ # Check for .json.jbuilder files
166
+ jbuilder_file = views_dir.join("#{action_name}.json.jbuilder")
167
+ view_files << jbuilder_file.to_s if jbuilder_file.exist?
168
+
169
+ # Check for .json.erb files
170
+ json_erb_file = views_dir.join("#{action_name}.json.erb")
171
+ view_files << json_erb_file.to_s if json_erb_file.exist?
172
+
173
+ # Check for .xml.builder files
174
+ xml_builder_file = views_dir.join("#{action_name}.xml.builder")
175
+ view_files << xml_builder_file.to_s if xml_builder_file.exist?
176
+
177
+ view_files
178
+ end
179
+
180
+ def find_inherited_controllers(controller_name)
181
+ inherited_controllers = []
182
+
183
+ # Read the controller file to find inheritance
184
+ controller_file = @rails_root.join("app", "controllers", "#{controller_name}_controller.rb")
185
+ return inherited_controllers unless controller_file.exist?
186
+
187
+ lines = controller_file.readlines
188
+ lines.each do |line|
189
+ # Look for class definition with inheritance
190
+ # e.g., class Users::ArchivesController < UsersController
191
+ # e.g., class Admin::UsersController < ApplicationController
192
+ if line.strip.match(/class\s+([A-Z][A-Za-z0-9:]*Controller)\s*<\s*([A-Z][A-Za-z0-9:]*Controller)/)
193
+ parent_controller = $2
194
+ # Convert class name to path (e.g., UsersController -> users)
195
+ parent_path = convert_class_name_to_path(parent_controller)
196
+ inherited_controllers << parent_path if parent_path
197
+ end
198
+ end
199
+
200
+ inherited_controllers
201
+ end
202
+
203
+ def convert_class_name_to_path(class_name)
204
+ return nil if class_name == "ApplicationController"
205
+ # Remove "Controller" suffix
206
+ path = class_name.sub(/Controller$/, "")
207
+ # Replace :: with /
208
+ path = path.gsub(/::/, "/")
209
+ # Convert CamelCase to snake_case for each segment
210
+ path = path.split("/").map { |seg| seg.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase }.join("/")
211
+ path
212
+ end
213
+ end
214
+
215
+ class ControllerFinder
216
+ def initialize(view_file, rails_root)
217
+ @view_file = Pathname.new(view_file)
218
+ @rails_root = Pathname.new(rails_root)
219
+ end
220
+
221
+ def find_controller_and_action
222
+ # Extract the path relative to app/views
223
+ # e.g., app/views/users/archives/show.html.erb -> users/archives/show
224
+ relative_path = @view_file.relative_path_from(@rails_root.join("app", "views"))
225
+ return nil unless relative_path.to_s.match?(/\.(erb|jbuilder|builder)$/)
226
+
227
+ # Remove all possible Rails view extensions
228
+ path_without_ext = relative_path.to_s.sub(/\.(html|json|js|xml)?\.(erb|jbuilder|builder)$/, '').sub(/\.(erb|jbuilder|builder)$/, '')
229
+ path_parts = path_without_ext.split('/')
230
+
231
+ return nil if path_parts.empty?
232
+
233
+ # The last part is the action name
234
+ action_name = path_parts.pop
235
+ # The remaining parts form the controller path
236
+ controller_parts = path_parts
237
+
238
+ return nil if controller_parts.empty?
239
+
240
+ # Build the controller file path
241
+ # e.g., users/archives -> app/controllers/users/archives_controller.rb
242
+ controller_path = controller_parts.join('/')
243
+ controller_file = @rails_root.join("app", "controllers", "#{controller_path}_controller.rb")
244
+
245
+ return nil unless controller_file.exist?
246
+
247
+ # Optionally, check if the method exists in the controller
248
+ if method_defined_in_controller?(controller_file, action_name)
249
+ { controller_file: controller_file.to_s, action_name: action_name }
250
+ else
251
+ { controller_file: controller_file.to_s, action_name: action_name, warning: "Method not found in controller" }
252
+ end
253
+ end
254
+
255
+ private
256
+
257
+ def method_defined_in_controller?(controller_file, action_name)
258
+ lines = controller_file.readlines
259
+ lines.any? { |line| line.strip.match(/^def\s+#{action_name}\b/) }
260
+ end
261
+ end
262
+ end
263
+ end
264
+ end
265
+ end
@@ -0,0 +1,8 @@
1
+ module Zed
2
+ module Rails
3
+ module Jumper
4
+ class Railtie < ::Rails::Railtie
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module Zed
2
+ module Rails
3
+ module Jumper
4
+ VERSION = "0.1.3"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ require "zed/rails/jumper/version"
2
+ require "zed/rails/jumper/railtie"
3
+ require "zed/rails/jumper/cli"
4
+
5
+ module Zed
6
+ module Rails
7
+ module Jumper
8
+ # Your code goes here...
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zed-rails-jumper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Tonksthebear
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2025-06-30 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rails
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: 7.0.0
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: 7.0.0
26
+ - !ruby/object:Gem::Dependency
27
+ name: thor
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '1.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.0'
40
+ description: A CLI gem for the Zed editor that helps developers quickly jump to Rails
41
+ views associated with the current controller method.
42
+ executables:
43
+ - zed-rails-jumper
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - MIT-LICENSE
48
+ - README.md
49
+ - Rakefile
50
+ - exe/zed-rails-jumper
51
+ - lib/tasks/zed/rails/jumper_tasks.rake
52
+ - lib/zed/rails/jumper.rb
53
+ - lib/zed/rails/jumper/cli.rb
54
+ - lib/zed/rails/jumper/railtie.rb
55
+ - lib/zed/rails/jumper/version.rb
56
+ homepage: https://github.com/tonksthebear/zed-rails-jumper
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ allowed_push_host: https://rubygems.org
61
+ homepage_uri: https://github.com/tonksthebear/zed-rails-jumper
62
+ source_code_uri: https://github.com/tonksthebear/zed-rails-jumper
63
+ changelog_uri: https://github.com/tonksthebear/zed-rails-jumper/blob/main/CHANGELOG.md
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.6.2
79
+ specification_version: 4
80
+ summary: A CLI gem for the Zed editor that helps developers quickly jump to Rails
81
+ views associated with the current controller method.
82
+ test_files: []