Skip to content

Commit

Permalink
Create instances of Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinDaugherty committed Nov 4, 2020
1 parent 5931137 commit b95d3c3
Showing 1 changed file with 17 additions and 31 deletions.
48 changes: 17 additions & 31 deletions lib/better_errors/editor.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "uri"

module BetterErrors
module Editor
class Editor
KNOWN_EDITORS = [
{ symbols: [:atom], sniff: /atom/i, url: "atom://core/open/file?filename=%{file}&line=%{line}" },
{ symbols: [:emacs, :emacsclient], sniff: /emacs/i, url: "emacs://open?url=file://%{file}&line=%{line}" },
Expand All @@ -14,40 +14,14 @@ module Editor
{ symbols: [:vscodium, :codium], sniff: /codium/i, url: "vscodium://file/%{file}:%{line}" },
]

class UsingFormattingString
def initialize(url_formatting_string)
@url_formatting_string = url_formatting_string
end

def url(file, line)
url_formatting_string % { file: URI.encode_www_form_component(file), file_unencoded: file, line: line }
end

private

attr_reader :url_formatting_string
end

class UsingProc
def initialize(url_proc)
@url_proc = url_proc
end

def url(file, line)
url_proc.call(file, line)
end

private

attr_reader :url_proc
end

def self.for_formatting_string(formatting_string)
UsingFormattingString.new(formatting_string)
new proc { |file, line|
formatting_string % { file: URI.encode_www_form_component(file), file_unencoded: file, line: line }
}
end

def self.for_proc(url_proc)
UsingProc.new(url_proc)
new url_proc
end

def self.for_symbol(symbol)
Expand Down Expand Up @@ -91,5 +65,17 @@ def self.editor_from_environment_formatting_string

for_formatting_string(ENV['BETTER_ERRORS_EDITOR_URL'])
end

def initialize(url_proc)
@url_proc = url_proc
end

def url(file, line)
url_proc.call(file, line)
end

private

attr_reader :url_proc
end
end

0 comments on commit b95d3c3

Please sign in to comment.