1111require "better_errors/raised_exception"
1212require "better_errors/repl"
1313require "better_errors/stack_frame"
14+ require "better_errors/editor"
1415
1516module BetterErrors
16- POSSIBLE_EDITOR_PRESETS = [
17- { symbols : [ :emacs , :emacsclient ] , sniff : /emacs/i , url : "emacs://open?url=file://%{file}&line=%{line}" } ,
18- { symbols : [ :macvim , :mvim ] , sniff : /vim/i , url : proc { |file , line | "mvim://open?url=file://#{ file } &line=#{ line } " } } ,
19- { symbols : [ :sublime , :subl , :st ] , sniff : /subl/i , url : "subl://open?url=file://%{file}&line=%{line}" } ,
20- { symbols : [ :textmate , :txmt , :tm ] , sniff : /mate/i , url : "txmt://open?url=file://%{file}&line=%{line}" } ,
21- { symbols : [ :idea ] , sniff : /idea/i , url : "idea://open?file=%{file}&line=%{line}" } ,
22- { symbols : [ :rubymine ] , sniff : /mine/i , url : "x-mine://open?file=%{file}&line=%{line}" } ,
23- { symbols : [ :vscode , :code ] , sniff : /code/i , url : "vscode://file/%{file}:%{line}" } ,
24- { symbols : [ :vscodium , :codium ] , sniff : /codium/i , url : "vscodium://file/%{file}:%{line}" } ,
25- { symbols : [ :atom ] , sniff : /atom/i , url : "atom://core/open/file?filename=%{file}&line=%{line}" } ,
26- ]
27-
2817 class << self
2918 # The path to the root of the application. Better Errors uses this property
3019 # to determine if a file in a backtrace should be considered an application
@@ -64,17 +53,18 @@ class << self
6453 @maximum_variable_inspect_size = 100_000
6554 @ignored_classes = [ 'ActionDispatch::Request' , 'ActionDispatch::Response' ]
6655
67- # Returns a proc, which when called with a filename and line number argument,
56+ # Returns an object which responds to #url, which when called with
57+ # a filename and line number argument,
6858 # returns a URL to open the filename and line in the selected editor.
6959 #
7060 # Generates TextMate URLs by default.
7161 #
72- # BetterErrors.editor[ "/some/file", 123]
62+ # BetterErrors.editor.url( "/some/file", 123)
7363 # # => txmt://open?url=file:///some/file&line=123
7464 #
7565 # @return [Proc]
7666 def self . editor
77- @editor
67+ @editor ||= default_editor
7868 end
7969
8070 # Configures how Better Errors generates open-in-editor URLs.
@@ -115,20 +105,15 @@ def self.editor
115105 # @param [Proc] proc
116106 #
117107 def self . editor = ( editor )
118- POSSIBLE_EDITOR_PRESETS . each do |config |
119- if config [ :symbols ] . include? ( editor )
120- return self . editor = config [ :url ]
121- end
122- end
123-
124- if editor . is_a? String
125- self . editor = proc { |file , line | editor % { file : URI . encode_www_form_component ( file ) , line : line } }
108+ if editor . is_a? Symbol
109+ @editor = Editor . for_symbol ( editor )
110+ raise ( ArgumentError , "Symbol #{ editor } is not a symbol in the list of supported errors." ) unless editor
111+ elsif editor . is_a? String
112+ @editor = Editor . for_formatting_string ( editor )
113+ elsif editor . respond_to? :call
114+ @editor = Editor . for_proc ( editor )
126115 else
127- if editor . respond_to? :call
128- @editor = editor
129- else
130- raise TypeError , "Expected editor to be a valid editor key, a format string or a callable."
131- end
116+ raise ArgumentError , "Expected editor to be a valid editor key, a format string or a callable."
132117 end
133118 end
134119
@@ -145,12 +130,8 @@ def self.use_pry!
145130 #
146131 # @return [Symbol]
147132 def self . default_editor
148- POSSIBLE_EDITOR_PRESETS . detect ( -> { { } } ) { |config |
149- ENV [ "EDITOR" ] =~ config [ :sniff ]
150- } [ :url ] || :textmate
133+ Editor . default_editor
151134 end
152-
153- BetterErrors . editor = default_editor
154135end
155136
156137begin
0 commit comments