class DEBUGGER__::LineBreakpoint

def self.copy bp, root_iseq

def self.copy bp, root_iseq
  nbp = LineBreakpoint.new bp.path, bp.line,
                           cond: bp.cond, oneshot: bp.oneshot, hook_call: bp.hook_call,
                           command: bp.command, skip_activate: true
  nbp.try_activate root_iseq
  nbp
end

def activate iseq, event, line

def activate iseq, event, line
  @iseq = iseq
  @type = event
  @line = line
  @path = iseq.absolute_path
  @key = [@path, @line].freeze
  SESSION.rehash_bps
  setup
  enable
  if @pending && !@oneshot
    DEBUGGER__.info "#{self} is activated."
  end
  @pending = false
end

def activate_exact iseq, events, line

def activate_exact iseq, events, line
  case
  when events.include?(:RUBY_EVENT_CALL)
    # "def foo" line set bp on the beginning of method foo
    activate(iseq, :call, line)
  when events.include?(:RUBY_EVENT_LINE)
    activate(iseq, :line, line)
  when events.include?(:RUBY_EVENT_RETURN)
    activate(iseq, :return, line)
  when events.include?(:RUBY_EVENT_B_RETURN)
    activate(iseq, :b_return, line)
  when events.include?(:RUBY_EVENT_END)
    activate(iseq, :end, line)
  else
    # not activated
  end
end

def duplicable?

def duplicable?
  @oneshot
end

def enable

def enable
  return unless @iseq
  if @type == :line
    @tp.enable(target: @iseq, target_line: @line)
  else
    @tp.enable(target: @iseq)
  end
rescue ArgumentError
  puts @iseq.disasm # for debug
  raise
end

def initialize path, line, cond: nil, oneshot: false, hook_call: true, command: nil, skip_activate: false, skip_src: false

def initialize path, line, cond: nil, oneshot: false, hook_call: true, command: nil, skip_activate: false, skip_src: false
  @line = line
  @oneshot = oneshot
  @hook_call = hook_call
  @skip_src = skip_src
  @pending = false
  @iseq = nil
  @type = nil
  @key = [path, @line].freeze
  super(cond, command, path)
  try_activate unless skip_activate
  @pending = !@iseq
end

def inspect

def inspect
  "<#{self.class.name} #{self.to_s}>"
end

def iterate_iseq root_iseq

def iterate_iseq root_iseq
  if root_iseq
    is = [root_iseq]
    while iseq = is.pop
      yield iseq
      iseq.each_child do |child_iseq|
        is << child_iseq
      end
    end
  else
    ObjectSpace.each_iseq do |iseq|
      if DEBUGGER__.compare_path((iseq.absolute_path || iseq.path), self.path) &&
         iseq.first_lineno <= self.line &&
         iseq.type != :ensure # ensure iseq is copied (duplicated)
        yield iseq
      end
    end
  end
end

def path_is? path

def path_is? path
  DEBUGGER__.compare_path(@path, path)
end

def setup

def setup
  return unless @type
  @tp = TracePoint.new(@type) do |tp|
    if @cond
      next unless safe_eval tp.binding, @cond
    end
    delete if @oneshot
    suspend
  end
end

def to_s

def to_s
  oneshot = @oneshot ? " (oneshot)" : ""
  if @iseq
    "#{generate_label("Line")} #{@path}:#{@line} (#{@type})#{oneshot}" + super
  else
    "#{generate_label("Line (pending)")} #{@path}:#{@line}#{oneshot}" + super
  end
end

def try_activate root_iseq = nil

def try_activate root_iseq = nil
  nearest = nil # NearestISeq
  iterate_iseq root_iseq do |iseq|
    iseq.traceable_lines_norec(line_events = {})
    lines = line_events.keys.sort
    if !lines.empty? && lines.last >= line
      nline = lines.bsearch{|l| line <= l}
      events = line_events[nline]
      next if events == [:RUBY_EVENT_B_CALL]
      if @hook_call &&
        events.include?(:RUBY_EVENT_CALL) &&
        self.line == iseq.first_lineno
        nline = iseq.first_lineno
      end
      if !nearest || ((line - nline).abs < (line - nearest.line).abs)
        nearest = NearestISeq.new(iseq, nline, events)
      elsif @hook_call &&
            nearest.line == iseq.first_line &&
            events.include?(:RUBY_EVENT_CALL)
        nearest = NearestISeq.new(iseq, nline, events)
      end
    end
  end
  if nearest
    activate_exact nearest.iseq, nearest.events, nearest.line
  end
end