Module: OpenSource::Utils

Defined in:
lib/open_source.rb

Defined Under Namespace

Classes: Runner

Class Method Summary collapse

Class Method Details

.get_command(file, line) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/open_source.rb', line 77

def get_command(file, line)
  editor = ENV["OPEN_SOURCE_GEM_EDITOR"] || ENV["EDITOR"]

  # different editors handle going to specific lines differently
  case editor
  when nil
    raise NoEditorError, "the EDITOR env variable must be set to use this feature"
  when /\s?(?:n|m|g)?vim?(\s|\z)/
    [editor, "+#{line}", file]
  when /\s?code(\s|\z)/
    [editor, "--goto", "#{file}:#{line}"]
  else
    # fallback - ignore line number
    # TODO: add support for other editors
    [editor, file]
  end
end

.handle_constant!(const) ⇒ Object

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/open_source.rb', line 41

def handle_constant!(const)
  raise UnopenableError, "Cannot open an anonymous #{const.class}" unless const.class.name

  loc = Object.const_source_location const.to_s

  if !loc
    raise UnopenableError, "Cannot open #{const} in an editor"
  elsif loc.empty?
    raise UnknownLocationError, "Cannot find the location of #{const} - perhaps it is defined in native code."
  else
    open_location loc
  end
end

.handle_method_or_name!(meth) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/open_source.rb', line 55

def handle_method_or_name!(meth)
  case meth
  when Symbol, String
    meth = method meth
  end

  loc = meth.source_location
  if loc
    open_location loc
  else
    raise UnknownLocationError, "Cannot find the location of #{meth} - perhaps it is defined in native code."
  end
rescue NameError
  raise UnopenableError, "Cannot open #{meth.inspect} (#{meth.class}) in an editor"
end

.open_location(loc) ⇒ Object



71
72
73
74
75
# File 'lib/open_source.rb', line 71

def open_location(loc)
  file, line = loc
  command = Utils.get_command file, line
  Runner.run *command
end