module ErrorHighlight::CoreExt

Constants

SKIP_TO_S_FOR_SUPER_LOOKUP

This is a marker to let ‘DidYouMean::Correctable#original_message` skip the following method definition of `to_s`. See github.com/ruby/did_you_mean/pull/152

Public Instance Methods

detailed_message(highlight: false, error_highlight: true, **) click to toggle source
Calls superclass method
# File lib/error_highlight/core_ext.rb, line 39
def detailed_message(highlight: false, error_highlight: true, **)
  return super unless error_highlight
  snippet = generate_snippet
  if highlight
    snippet = snippet.gsub(/.+/) { "\e[1m" + $& + "\e[m" }
  end
  super + snippet
end
to_s() click to toggle source
Calls superclass method
# File lib/error_highlight/core_ext.rb, line 54
def to_s
  msg = super
  snippet = generate_snippet
  if snippet != "" && !msg.include?(snippet)
    msg + snippet
  else
    msg
  end
end

Private Instance Methods

generate_snippet() click to toggle source
# File lib/error_highlight/core_ext.rb, line 5
        def generate_snippet
  locs = backtrace_locations
  return "" unless locs

  loc = locs.first
  return "" unless loc

  begin
    node = RubyVM::AbstractSyntaxTree.of(loc, keep_script_lines: true)
    opts = {}

    case self
    when NoMethodError, NameError
      opts[:point_type] = :name
      opts[:name] = name
    when TypeError, ArgumentError
      opts[:point_type] = :args
    end

    spot = ErrorHighlight.spot(node, **opts)

  rescue SyntaxError
  rescue SystemCallError # file not found or something
  rescue ArgumentError   # eval'ed code
  end

  if spot
    return ErrorHighlight.formatter.message_for(spot)
  end

  ""
end