Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add link to external editor to rescue view #46378

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

pedz
Copy link
Contributor

@pedz pedz commented Oct 29, 2022

This is a somewhat trivial diff. Currently, when an exception is thrown, a page is rendered and part of the page has the paths and line numbers from the exception stack. The lines are clickable and when clicked, the upper section changes to show more context of the line in the original source code.

The proposed change is to add an additional link whose text is the Unicode link symbol after the existing link. The more common glyph was rejected by Unicode.

The href for the link symbol comes from Ruby code that first checks, at render time, to see if ActionDispatch::DebugView#ext_editor is defined. If it is, it is called passing the "frame". Otherwise, ActionDispatch::DebugView#int_editor is called.

int_editor creates a URL using the file: scheme. While most browsers won't follow the link, it will show to the developer what is possible. My example of ext_editor creates a URL using the emacs: scheme.

I've added a tool tip to try and tell the developers how they can configure the URL to invoke their favorite editor.

I thought the approach of checking to see if a method is defined and calling it would be preferred to having the user redefine the definition of an existing method.

Another piece of work to do would be to add something like the sample I have to the templates so that config/initializers/debug_view.rb would be created when the Rails app is created.

Having both links, the currently existing link that shows the code snippets and the new link that opens the file in the external editor, gives the users the best of both worlds. They can view in the browser what happened and then click to get them placed in their favorite editor at the line number for the issue.

The key here is "their favorite editor" can be customized to their liking via the ext_editor method.

Motivation / Background

This Pull Request has been created because adding a link to the exception rescue page to the files and lines that are in the exception stack will help the developer edit the area that caused the fault.

Detail

This Pull Request changes:
M actionpack/lib/action_dispatch.rb
M actionpack/lib/action_dispatch/middleware/debug_view.rb
M actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
A actionpack/lib/action_dispatch/locale/en.yml

Additional information

My example for what a developer can add to config/initializers/debug_view.rb:

# frozen_string_literal: true

module ActionDispatch
  class DebugView < ActionView::Base # :nodoc:
    def ext_editor(frame)
      frame[:trace].sub(/([^:]+):([0-9]+)(:.*)?/, 'emacs:///Users/pedz/Source/hatred/\\1#\\2')
    end
  end
end

I don't know the official way to request a feature in RoR.  I hope
this pull request is reasonably complete and acceptable.

This is a somewhat trivial diff.  Currently, when an exception is
thrown, a page is rendered and part of the page has the paths and line
numbers from the exception stack.  The lines are clickable and when
clicked, the upper section changes to show more context of the line in
the original source code.

The proposed change is to add an additional link whose text is the
Unicode link symbol after the existing link.  The more common glyph
was rejected by Unicode.

The href for the link symbol comes from Ruby code that first checks,
at render time, to see if ActionDispatch::DebugView#ext_editor is
defined.  If it is, it is called passing the "frame".  Otherwise,
ActionDispatch::DebugView#int_editor is called.

int_editor creates a URL using the `file:` scheme.  While most
browsers won't follow the link, it will show to the developer what is
possible.  My example of ext_editor creates a URL using the `emacs:`
scheme.

I've added a tool tip to try and tell the developers how they can
configure the URL to invoke their favorite editor.

I thought the approach of checking to see if a method is defined
and calling it would be preferred to having the user redefine the
definition of an existing method.

Another piece of work to do would be to add something like the sample
I have to the templates so that config/initializers/debug_view.rb
would be created when the Rails app is created.

Having both links, the currently existing link that shows the code
snippets and the new link that opens the file in the external editor,
gives the users the best of both worlds.  They can view in the browser
what happened and then click to get them placed in their favorite
editor at the line number for the issue.

The key here is "their favorite editor" can be customized to their
liking via the ext_editor method.
@rails-bot rails-bot bot added the actionpack label Oct 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant