Class: EditorKicker::ExceptionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/editor_kicker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExceptionHandler

Returns a new instance of ExceptionHandler.



32
33
34
35
# File 'lib/editor_kicker.rb', line 32

def initialize
  @kicker = self   # you can set Proc object to @kicker
  @writable_check = false
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



37
38
39
# File 'lib/editor_kicker.rb', line 37

def command
  @command
end

#kickerObject

Returns the value of attribute kicker.



37
38
39
# File 'lib/editor_kicker.rb', line 37

def kicker
  @kicker
end

#writable_checkObject

Returns the value of attribute writable_check.



37
38
39
# File 'lib/editor_kicker.rb', line 37

def writable_check
  @writable_check
end

Instance Method Details

#call(filepath, linenum) ⇒ Object

default activity of kick()



90
91
92
93
94
# File 'lib/editor_kicker.rb', line 90

def call(filepath, linenum)     # should separate to a class?
  command = (@command || detect_command()) % [linenum, filepath]  # or [filepath, linenum]
  log(command)
  `#{command.untaint}`
end

#detect_commandObject

detect command to invoke editor



72
73
74
75
76
77
78
# File 'lib/editor_kicker.rb', line 72

def detect_command
  command = ENV['EDITOR_KICKER']
  return command if command
  bin = '/Applications/TextMate.app/Contents/Resources/mate'
  return "#{bin} -l %s '%s'" if test(?f, bin)
  return "emacsclient -n +%s '%s'"
end

#detect_location(ex, backtrace = nil) ⇒ Object

get filename and linenum from error



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/editor_kicker.rb', line 46

def detect_location(ex, backtrace=nil)
  filepath = linenum = nil
  backtrace ||= ex.backtrace
  if backtrace && !backtrace.empty?
    tuple = nil
    if backtrace.find {|str| tuple = get_location(str) }
      filepath, linenum = tuple
    end
  #elsif ex.is_a?(SyntaxEx)
  #  if ex.to_s =~ /^(.+):(\d+): syntax error,/
  #    filepath, linenum = $1, $2.to_i
  #  end
  elsif ex.to_s =~ /\A(.+):(\d+): /  # for SyntaxError
    filepath, linenum = $1, $2.to_i
  end
  return filepath, linenum
end

#get_location(str) ⇒ Object

get filepath and linenum from string



65
66
67
68
69
# File 'lib/editor_kicker.rb', line 65

def get_location(str)
  return nil if str !~ /^(.+):(\d+)(:in `.+'|$)/
  return nil if @writable_check && !File.writable?($1)
  return [$1, $2.to_i]
end

#handle(ex) ⇒ Object

detect error location from error and open related file



40
41
42
43
# File 'lib/editor_kicker.rb', line 40

def handle(ex)
  filepath, linenum = detect_location(ex)
  kick(filepath, linenum) if filepath && linenum
end

#kick(filepath, linenum) ⇒ Object

open file with editor



81
82
83
84
85
86
87
# File 'lib/editor_kicker.rb', line 81

def kick(filepath, linenum)
  if File.exists?(filepath)
    @kicker.call(filepath, linenum)
  else
    log("#{filepath}: not found.")
  end
end

#log(message) ⇒ Object



96
97
98
# File 'lib/editor_kicker.rb', line 96

def log(message)
  $stderr.puts "** [EditorKicker] #{message}"
end