module OpenSource::Utils

Public Instance Methods

get_command(file, line) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/open_source.rb, line 71
def open_location(loc)
  file, line = loc
  command = Utils.get_command file, line
  Runner.run *command
end