editor_opener 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: 97e3fda047f6c339f6b1e5c85ac7a6e7442438adf1be5698cc74509dc277cab2
4
+ data.tar.gz: 86a624869961333ebb914c16f5c8e4a9aa27d2fd11959a4c43ca04eebeeb1782
5
+ SHA512:
6
+ metadata.gz: '039d8837645d9a93163a08693939eabeebc8179176ead4bc8e113906bc80a1b219e57a7c47cdab904e8571bbd005fac2cdd91ecb87d7529d154c2ed59cd99e76'
7
+ data.tar.gz: 2598ff96c36081386a3d8ec4692c9aff666d322a268e11ec47cfe92c625816d9516baaaea8b866779ab7130a4aa9c8f6723c1a8d6c09d1a6e445995a32e07ccf
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright Igor Kasyanchuk
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,28 @@
1
+ # EditorOpener
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "editor_opener"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install editor_opener
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,70 @@
1
+ <% names = traces.keys %>
2
+ <% error_index = local_assigns[:error_index] || 0 %>
3
+
4
+ <p><code>Rails.root: <%= defined?(Rails) && Rails.respond_to?(:root) ? Rails.root : "unset" %></code></p>
5
+
6
+ <div id="traces-<%= error_index %>">
7
+ <% names.each do |name| %>
8
+ <% show = "show('#{name.gsub(/\s/, "-")}-#{error_index}');"
9
+ hide = (names - [name]).collect { |hide_name| "hide('#{hide_name.gsub(/\s/, "-")}-#{error_index}');" } %>
10
+ <a href="#" onclick="<%= hide.join %><%= show %>; return false;"><%= name %></a> <%= "|" unless names.last == name %>
11
+ <% end %>
12
+
13
+ <% traces.each do |name, trace| %>
14
+ <div id="<%= "#{name.gsub(/\s/, "-")}-#{error_index}" %>" class="trace-container" style="display: <%= (name == trace_to_show) ? "block" : "none" %>;">
15
+ <code class="traces">
16
+ <% trace.each do |frame| %>
17
+ <div class="trace">
18
+ <% file_url = ActionDispatch::TraceToFileExtractor.open_in_editor? && ActionDispatch::TraceToFileExtractor.new(frame[:trace]).call %>
19
+ <% if file_url.present? %>
20
+ <a href="<%= file_url %>" class="edit-icon">
21
+ <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
22
+ <path stroke-linecap="round" stroke-linejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L10.582 16.07a4.5 4.5 0 0 1-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 0 1 1.13-1.897l8.932-8.931Zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0 1 15.75 21H5.25A2.25 2.25 0 0 1 3 18.75V8.25A2.25 2.25 0 0 1 5.25 6H10" />
23
+ </svg>
24
+ </a>
25
+ <% end %>
26
+ <a class="trace-frames trace-frames-<%= error_index %>" data-exception-object-id="<%= frame[:exception_object_id] %>" data-frame-id="<%= frame[:id] %>" href="#">
27
+ <%= frame[:trace] %>
28
+ </a>
29
+ <br>
30
+ </div>
31
+ <% end %>
32
+ </code>
33
+ </div>
34
+ <% end %>
35
+
36
+ <script>
37
+ (function() {
38
+ var traceFrames = document.getElementsByClassName('trace-frames-<%= error_index %>');
39
+ var selectedFrame, currentSource = document.getElementById('frame-source-<%= error_index %>-0');
40
+
41
+ // Add click listeners for all stack frames
42
+ for (var i = 0; i < traceFrames.length; i++) {
43
+ traceFrames[i].addEventListener('click', function(e) {
44
+ e.preventDefault();
45
+ var target = e.target;
46
+ var frame_id = target.dataset.frameId;
47
+
48
+ if (selectedFrame) {
49
+ selectedFrame.className = selectedFrame.className.replace("selected", "");
50
+ }
51
+
52
+ target.className += " selected";
53
+ selectedFrame = target;
54
+
55
+ // Change the extracted source code
56
+ changeSourceExtract(frame_id);
57
+ });
58
+
59
+ function changeSourceExtract(frame_id) {
60
+ var el = document.getElementById('frame-source-<%= error_index %>-' + frame_id);
61
+ if (currentSource && el) {
62
+ currentSource.className += " hidden";
63
+ el.className = el.className.replace(" hidden", "");
64
+ currentSource = el;
65
+ }
66
+ }
67
+ }
68
+ })();
69
+ </script>
70
+ </div>
@@ -0,0 +1,303 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <meta name="turbo-visit-control" content="reload">
7
+ <title>Action Controller: Exception caught</title>
8
+ <style>
9
+ body {
10
+ background-color: #FAFAFA;
11
+ color: #333;
12
+ color-scheme: light dark;
13
+ supported-color-schemes: light dark;
14
+ margin: 0px;
15
+ }
16
+
17
+ body, p, ol, ul, td {
18
+ font-family: helvetica, verdana, arial, sans-serif;
19
+ font-size: 13px;
20
+ line-height: 18px;
21
+ }
22
+
23
+ pre {
24
+ font-size: 11px;
25
+ white-space: pre-wrap;
26
+ }
27
+
28
+ pre.box {
29
+ border: 1px solid #EEE;
30
+ padding: 10px;
31
+ margin: 0px;
32
+ width: 958px;
33
+ }
34
+
35
+ header {
36
+ color: #F0F0F0;
37
+ background: #C00;
38
+ padding: 0.5em 1.5em;
39
+ }
40
+
41
+ h1 {
42
+ overflow-wrap: break-word;
43
+ margin: 0.2em 0;
44
+ line-height: 1.1em;
45
+ font-size: 2em;
46
+ }
47
+
48
+ h2 {
49
+ color: #C00;
50
+ line-height: 25px;
51
+ }
52
+
53
+ code.traces {
54
+ font-size: 11px;
55
+ }
56
+
57
+ .trace-container {
58
+ margin-top: 10px;
59
+ }
60
+
61
+ code.traces .trace {
62
+ display: flex;
63
+ align-items: center;
64
+ gap: 2px;
65
+ }
66
+
67
+ .edit-icon {
68
+ width: 16px;
69
+ height: 16px;
70
+ display: flex;
71
+ align-items: center;
72
+ justify-content: center;
73
+ color: #B0B0B0;
74
+ }
75
+
76
+ .response-heading, .request-heading {
77
+ margin-top: 30px;
78
+ }
79
+
80
+ .exception-message {
81
+ padding: 8px 0;
82
+ }
83
+
84
+ .exception-message .message {
85
+ margin-bottom: 8px;
86
+ line-height: 25px;
87
+ font-size: 1.5em;
88
+ font-weight: bold;
89
+ color: #C00;
90
+ }
91
+
92
+ .details {
93
+ border: 1px solid #D0D0D0;
94
+ border-radius: 4px;
95
+ margin: 1em 0px;
96
+ display: block;
97
+ max-width: 978px;
98
+ }
99
+
100
+ .summary {
101
+ padding: 8px 15px;
102
+ border-bottom: 1px solid #D0D0D0;
103
+ display: block;
104
+ }
105
+
106
+ a.summary {
107
+ color: #F0F0F0;
108
+ text-decoration: none;
109
+ background: #C52F24;
110
+ border-bottom: none;
111
+ }
112
+
113
+ .details pre {
114
+ margin: 5px;
115
+ border: none;
116
+ }
117
+
118
+ #container {
119
+ box-sizing: border-box;
120
+ width: 100%;
121
+ padding: 0 1.5em;
122
+ }
123
+
124
+ .source * {
125
+ margin: 0px;
126
+ padding: 0px;
127
+ }
128
+
129
+ .source {
130
+ border: 1px solid #D9D9D9;
131
+ background: #ECECEC;
132
+ max-width: 978px;
133
+ }
134
+
135
+ .source pre {
136
+ padding: 10px 0px;
137
+ border: none;
138
+ }
139
+
140
+ .source .data {
141
+ font-size: 80%;
142
+ overflow: auto;
143
+ background-color: #FFF;
144
+ }
145
+
146
+ .info {
147
+ padding: 0.5em;
148
+ }
149
+
150
+ .source .data .line_numbers {
151
+ background-color: #ECECEC;
152
+ color: #555;
153
+ padding: 1em .5em;
154
+ border-right: 1px solid #DDD;
155
+ text-align: right;
156
+ }
157
+
158
+ .line {
159
+ padding-left: 10px;
160
+ white-space: pre;
161
+ }
162
+
163
+ .line:hover {
164
+ background-color: #F6F6F6;
165
+ }
166
+
167
+ .line.active {
168
+ background-color: #FCC;
169
+ }
170
+
171
+ .error_highlight {
172
+ display: inline-block;
173
+ background-color: #FF9;
174
+ text-decoration: #F00 wavy underline;
175
+ }
176
+
177
+ .error_highlight_tip {
178
+ color: #666;
179
+ padding: 2px 2px;
180
+ font-size: 10px;
181
+ }
182
+
183
+ .button_to {
184
+ display: inline-block;
185
+ margin-top: 0.75em;
186
+ margin-bottom: 0.75em;
187
+ }
188
+
189
+ .hidden {
190
+ display: none;
191
+ }
192
+
193
+ .correction {
194
+ list-style-type: none;
195
+ }
196
+
197
+ input[type="submit"] {
198
+ color: white;
199
+ background-color: #C00;
200
+ border: none;
201
+ border-radius: 12px;
202
+ box-shadow: 0 3px #F99;
203
+ font-size: 13px;
204
+ font-weight: bold;
205
+ margin: 0;
206
+ padding: 10px 18px;
207
+ cursor: pointer;
208
+ -webkit-appearance: none;
209
+ }
210
+ input[type="submit"]:focus,
211
+ input[type="submit"]:hover {
212
+ opacity: 0.8;
213
+ }
214
+ input[type="submit"]:active {
215
+ box-shadow: 0 2px #F99;
216
+ transform: translateY(1px)
217
+ }
218
+
219
+ a { color: #980905; }
220
+ a:visited { color: #666; }
221
+ a.trace-frames {
222
+ color: #666;
223
+ overflow-wrap: break-word;
224
+ }
225
+ a:hover, a.trace-frames.selected { color: #C00; }
226
+ a.summary:hover { color: #FFF; }
227
+
228
+ @media (prefers-color-scheme: dark) {
229
+ body {
230
+ background-color: #222;
231
+ color: #ECECEC;
232
+ }
233
+
234
+ .details, .summary {
235
+ border-color: #666;
236
+ }
237
+
238
+ .source {
239
+ border-color: #555;
240
+ background-color: #333;
241
+ }
242
+
243
+ .source .data {
244
+ background: #444;
245
+ }
246
+
247
+ .source .data .line_numbers {
248
+ background: #333;
249
+ border-color: #222;
250
+ }
251
+
252
+ .line:hover {
253
+ background: #666;
254
+ }
255
+
256
+ .line.active {
257
+ background-color: #900;
258
+ }
259
+
260
+ .error_highlight {
261
+ color: #333;
262
+ }
263
+
264
+ input[type="submit"] {
265
+ box-shadow: 0 3px #800;
266
+ }
267
+ input[type="submit"]:active {
268
+ box-shadow: 0 2px #800;
269
+ }
270
+
271
+ a { color: #C00; }
272
+ a.trace-frames { color: #999; }
273
+ a:hover, a.trace-frames.selected { color: #E9382B; }
274
+ }
275
+
276
+ <%= yield :style %>
277
+ </style>
278
+
279
+ <script>
280
+ var toggle = function(id) {
281
+ document.getElementById(id).classList.toggle('hidden');
282
+ return false;
283
+ }
284
+ var show = function(id) {
285
+ document.getElementById(id).style.display = 'block';
286
+ }
287
+ var hide = function(id) {
288
+ document.getElementById(id).style.display = 'none';
289
+ }
290
+ var toggleSessionDump = function() {
291
+ return toggle('session_dump');
292
+ }
293
+ var toggleEnvDump = function() {
294
+ return toggle('env_dump');
295
+ }
296
+ </script>
297
+ </head>
298
+ <body>
299
+
300
+ <%= yield %>
301
+
302
+ </body>
303
+ </html>
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionDispatch
4
+ # # Extract file and line from a trace and open it in the editor
5
+ #
6
+ # This is used to open the file in the editor when the user clicks on the edit icon. You have to set the `EDITOR` environment variable to the editor you want to use.
7
+ class TraceToFileExtractor
8
+ KNOWN_EDITORS = [
9
+ { symbols: [ :atom ], sniff: /atom/i, url: "atom://core/open/file?filename=%{file}&line=%{line}" },
10
+ { symbols: [ :emacs, :emacsclient ], sniff: /emacs/i, url: "emacs://open?url=file://%{file}&line=%{line}" },
11
+ { symbols: [ :idea ], sniff: /idea/i, url: "idea://open?file=%{file}&line=%{line}" },
12
+ { symbols: [ :macvim, :mvim ], sniff: /vim/i, url: "mvim://open?url=file://%{file_unencoded}&line=%{line}" },
13
+ { symbols: [ :rubymine ], sniff: /mine/i, url: "x-mine://open?file=%{file}&line=%{line}" },
14
+ { symbols: [ :sublime, :subl, :st ], sniff: /subl/i, url: "subl://open?url=file://%{file}&line=%{line}" },
15
+ { symbols: [ :textmate, :txmt, :tm ], sniff: /mate/i, url: "txmt://open?url=file://%{file}&line=%{line}" },
16
+ { symbols: [ :vscode, :code ], sniff: /code/i, url: "vscode://file/%{file}:%{line}" },
17
+ { symbols: [ :vscodium, :codium ], sniff: /codium/i, url: "vscodium://file/%{file}:%{line}" },
18
+ { symbols: [ :windsurf ], sniff: /windsurf/i, url: "windsurf://file/%{file}:%{line}" },
19
+ { symbols: [ :zed ], sniff: /zed/i, url: "zed://file/%{file}:%{line}" },
20
+ { symbols: [ :nova ], sniff: /nova/i, url: "nova://open?path=%{file}&line=%{line}" },
21
+ { symbols: [ :cursor ], sniff: /cursor/i, url: "cursor://file/%{file}:%{line}" }
22
+ ]
23
+
24
+ class << self
25
+ def open_in_editor?
26
+ editor.present?
27
+ end
28
+
29
+ def editor
30
+ @editor ||= ENV["EDITOR"].present? && KNOWN_EDITORS.find { |editor| editor[:symbols].include?(ENV["EDITOR"].to_sym) }
31
+ end
32
+ end
33
+
34
+ def initialize(trace)
35
+ @trace = trace.to_s.strip
36
+ end
37
+
38
+ def call
39
+ return nil unless self.class.open_in_editor?
40
+
41
+ file_name = file_name_in_the_trace
42
+ return nil if file_name.blank?
43
+
44
+ case detect_trace_type
45
+ when :gem_reference
46
+ gem_name = trace.split(" ").first
47
+ gem_spec = Gem.loaded_specs[gem_name]
48
+ file_name = gem_spec.present? ? "#{gem_spec.full_gem_path}/#{file_name}" : nil
49
+ when :app_code
50
+ file_name = "#{Rails.root}/#{file_name}"
51
+ when :direct_path
52
+ # do nothing, we already have the full path
53
+ end
54
+
55
+ if file_name
56
+ file, line = file_name.split(":")
57
+
58
+ self.class.editor[:url] % { file: file, line: line }
59
+ else
60
+ raise @trace
61
+ end
62
+ end
63
+
64
+ private
65
+ attr_reader :trace
66
+
67
+ def detect_trace_type
68
+ if trace[0] == "/" || trace.match?(/^[A-Z]:/)
69
+ # to match linux and windows paths
70
+ :direct_path
71
+ elsif trace.split(" ")[1].present? && trace.split(" ")[1].match?(/\(([0-9.]*\))/)
72
+ :gem_reference
73
+ else
74
+ :app_code
75
+ end
76
+ end
77
+
78
+ def file_name_in_the_trace
79
+ @file_name_in_the_trace ||= trace.split(" ").find { |part| part.match?(/\.\w+:\d+/) }.to_s.split(":in")[0]
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,14 @@
1
+ require_relative "action_dispatch/trace_to_file_extractor"
2
+
3
+ module EditorOpener
4
+ class Railtie < ::Rails::Railtie
5
+ initializer "editor_opener.action_dispatch" do
6
+ ActiveSupport.on_load(:action_controller) do
7
+ path = File.expand_path("../../app/views", __dir__)
8
+
9
+ # DebugView has custom templates path, that cannot be overridden by prepend_view_path
10
+ ActionDispatch::DebugView::RESCUES_TEMPLATE_PATHS.unshift(path)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module EditorOpener
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "editor_opener/version"
2
+ require "editor_opener/railtie"
3
+
4
+ module EditorOpener
5
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: editor_opener
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Igor Kasyanchuk
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2025-07-03 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: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: debug
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: wrapped_print
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: minitest
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ description: Open source file in the editor from the Rails error page
69
+ email:
70
+ - igorkasyanchuk@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - MIT-LICENSE
76
+ - README.md
77
+ - Rakefile
78
+ - app/views/rescues/_trace.html.erb
79
+ - app/views/rescues/layout.erb
80
+ - lib/editor_opener.rb
81
+ - lib/editor_opener/action_dispatch/trace_to_file_extractor.rb
82
+ - lib/editor_opener/railtie.rb
83
+ - lib/editor_opener/version.rb
84
+ homepage: https://github.com/igorkasyanchuk/editor_opener
85
+ licenses:
86
+ - MIT
87
+ metadata:
88
+ homepage_uri: https://github.com/igorkasyanchuk/editor_opener
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubygems_version: 3.6.3
104
+ specification_version: 4
105
+ summary: Open source file in the editor from the Rails error page
106
+ test_files: []