In Files

  • error_highlight/base.rb

Parent

Methods

ErrorHighlight::Spotter

Public Class Methods

new(node, point_type: :name, name: nil) click to toggle source
 
               # File error_highlight/base.rb, line 27
def initialize(node, point_type: :name, name: nil)
  @node = node
  @point_type = point_type
  @name = name

  # Not-implemented-yet options
  @arg = nil # Specify the index or keyword at which argument caused the TypeError/ArgumentError
  @multiline = false # Allow multiline spot

  @fetch = -> (lineno, last_lineno = lineno) do
    snippet = @node.script_lines[lineno - 1 .. last_lineno - 1].join("")
    snippet += "\n" unless snippet.end_with?("\n")

    # It require some work to support Unicode (or multibyte) characters.
    # Tentatively, we stop highlighting if the code snippet has non-ascii characters.
    # See https://github.com/ruby/error_highlight/issues/4
    raise NonAscii unless snippet.ascii_only?

    snippet
  end
end
            

Public Instance Methods

spot() click to toggle source
 
               # File error_highlight/base.rb, line 49
def spot
  return nil unless @node

  case @node.type

  when :CALL, :QCALL
    case @point_type
    when :name
      spot_call_for_name
    when :args
      spot_call_for_args
    end

  when :ATTRASGN
    case @point_type
    when :name
      spot_attrasgn_for_name
    when :args
      spot_attrasgn_for_args
    end

  when :OPCALL
    case @point_type
    when :name
      spot_opcall_for_name
    when :args
      spot_opcall_for_args
    end

  when :FCALL
    case @point_type
    when :name
      spot_fcall_for_name
    when :args
      spot_fcall_for_args
    end

  when :VCALL
    spot_vcall

  when :OP_ASGN1
    case @point_type
    when :name
      spot_op_asgn1_for_name
    when :args
      spot_op_asgn1_for_args
    end

  when :OP_ASGN2
    case @point_type
    when :name
      spot_op_asgn2_for_name
    when :args
      spot_op_asgn2_for_args
    end

  when :CONST
    spot_vcall

  when :COLON2
    spot_colon2

  when :COLON3
    spot_vcall

  when :OP_CDECL
    spot_op_cdecl
  end

  if @snippet && @beg_column && @end_column && @beg_column < @end_column
    return {
      first_lineno: @beg_lineno,
      first_column: @beg_column,
      last_lineno: @end_lineno,
      last_column: @end_column,
      snippet: @snippet,
    }
  else
    return nil
  end

rescue NonAscii
  nil
end
            
There is an updated format of the API docs for this version here.