类 IRB::RelineInputMethod

常量

HISTORY

公共类方法

new(completor) 点击切换源代码

使用 Reline 创建一个新的输入方法对象

调用超类方法 IRB::StdioInputMethod::new
# File lib/irb/input-method.rb, line 237
def initialize(completor)
  IRB.__send__(:set_encoding, Reline.encoding_system_needs.name, override: false)

  super()

  @eof = false
  @completor = completor

  Reline.basic_word_break_characters = BASIC_WORD_BREAK_CHARACTERS
  Reline.completion_append_character = nil
  Reline.completer_quote_characters = ''
  Reline.completion_proc = ->(target, preposing, postposing) {
    bind = IRB.conf[:MAIN_CONTEXT].workspace.binding
    @completion_params = [preposing, target, postposing, bind]
    @completor.completion_candidates(preposing, target, postposing, bind: bind)
  }
  Reline.output_modifier_proc =
    if IRB.conf[:USE_COLORIZE]
      proc do |output, complete: |
        next unless IRB::Color.colorable?
        lvars = IRB.CurrentContext&.local_variables || []
        IRB::Color.colorize_code(output, complete: complete, local_variables: lvars)
      end
    else
      proc do |output|
        Reline::Unicode.escape_for_print(output)
      end
    end
  Reline.dig_perfect_match_proc = ->(matched) { display_document(matched) }
  Reline.autocompletion = IRB.conf[:USE_AUTOCOMPLETE]

  if IRB.conf[:USE_AUTOCOMPLETE]
    begin
      require 'rdoc'
      Reline.add_dialog_proc(:show_doc, show_doc_dialog_proc, Reline::DEFAULT_DIALOG_CONTEXT)
    rescue LoadError
    end
  end
end

公共实例方法

auto_indent(&block) 点击切换源代码
# File lib/irb/input-method.rb, line 290
def auto_indent(&block)
  @auto_indent_proc = block
end
check_termination(&block) 点击切换源代码
# File lib/irb/input-method.rb, line 282
def check_termination(&block)
  @check_termination_proc = block
end
completion_info() 点击切换源代码
# File lib/irb/input-method.rb, line 277
def completion_info
  autocomplete_message = Reline.autocompletion ? 'Autocomplete' : 'Tab Complete'
  "#{autocomplete_message}, #{@completor.inspect}"
end
display_document(matched, driver: nil) 点击切换源代码
# File lib/irb/input-method.rb, line 410
def display_document(matched, driver: nil)
  begin
    require 'rdoc'
  rescue LoadError
    return
  end

  if matched =~ /\A(?:::)?RubyVM/ and not ENV['RUBY_YES_I_AM_NOT_A_NORMAL_USER']
    IRB.__send__(:easter_egg)
    return
  end

  _target, preposing, postposing, bind = @completion_params
  namespace = @completor.doc_namespace(preposing, matched, postposing, bind: bind)
  return unless namespace

  driver ||= RDoc::RI::Driver.new
  if namespace.is_a?(Array)
    out = RDoc::Markup::Document.new
    namespace.each do |m|
      begin
        driver.add_method(out, m)
      rescue RDoc::RI::Driver::NotFoundError
      end
    end
    driver.display(out)
  else
    begin
      driver.display_names([namespace])
    rescue RDoc::RI::Driver::NotFoundError
    end
  end
end
dynamic_prompt(&block) 点击切换源代码
# File lib/irb/input-method.rb, line 286
def dynamic_prompt(&block)
  @prompt_proc = block
end
eof?() 点击切换源代码

判断此输入方法是否已到达结尾,如果不再有数据可读,则返回 true

有关更多信息,请参阅 IO#eof?

# File lib/irb/input-method.rb, line 465
def eof?
  @eof
end
gets() 点击切换源代码

从此输入方法读取下一行。

有关更多信息,请参阅 IO#gets

# File lib/irb/input-method.rb, line 447
def gets
  Reline.input = @stdin
  Reline.output = @stdout
  Reline.prompt_proc = @prompt_proc
  Reline.auto_indent_proc = @auto_indent_proc if @auto_indent_proc
  if l = Reline.readmultiline(@prompt, false, &@check_termination_proc)
    Reline::HISTORY.push(l) if !l.empty?
    @line[@line_no += 1] = l + "\n"
  else
    @eof = true
    l
  end
end
inspect() 点击切换源代码

用于调试消息

# File lib/irb/input-method.rb, line 470
def inspect
  config = Reline::Config.new
  str = "RelineInputMethod with Reline #{Reline::VERSION}"
  inputrc_path = File.expand_path(config.inputrc_path)
  str += " and #{inputrc_path}" if File.exist?(inputrc_path)
  str
end
show_doc_dialog_proc() 点击切换源代码
# File lib/irb/input-method.rb, line 294
def show_doc_dialog_proc
  doc_namespace = ->(matched) {
    preposing, _target, postposing, bind = @completion_params
    @completor.doc_namespace(preposing, matched, postposing, bind: bind)
  }
  ->() {
    dialog.trap_key = nil
    alt_d = [
      [Reline::Key.new(nil, 0xE4, true)], # Normal Alt+d.
      [27, 100], # Normal Alt+d when convert-meta isn't used.
      [195, 164], # The "ä" that appears when Alt+d is pressed on xterm.
      [226, 136, 130] # The "∂" that appears when Alt+d in pressed on iTerm2.
    ]

    if just_cursor_moving and completion_journey_data.nil?
      return nil
    end
    cursor_pos_to_render, result, pointer, autocomplete_dialog = context.pop(4)
    return nil if result.nil? or pointer.nil? or pointer < 0

    name = doc_namespace.call(result[pointer])
    # Use first one because document dialog does not support multiple namespaces.
    name = name.first if name.is_a?(Array)

    show_easter_egg = name&.match?(/\ARubyVM/) && !ENV['RUBY_YES_I_AM_NOT_A_NORMAL_USER']

    options = {}
    options[:extra_doc_dirs] = IRB.conf[:EXTRA_DOC_DIRS] unless IRB.conf[:EXTRA_DOC_DIRS].empty?
    driver = RDoc::RI::Driver.new(options)

    if key.match?(dialog.name)
      if show_easter_egg
        IRB.__send__(:easter_egg)
      else
        begin
          driver.display_names([name])
        rescue RDoc::RI::Driver::NotFoundError
        end
      end
    end

    begin
      name = driver.expand_name(name)
    rescue RDoc::RI::Driver::NotFoundError
      return nil
    rescue
      return nil # unknown error
    end
    doc = nil
    used_for_class = false
    if not name =~ /#|\./
      found, klasses, includes, extends = driver.classes_and_includes_and_extends_for(name)
      if not found.empty?
        doc = driver.class_document(name, found, klasses, includes, extends)
        used_for_class = true
      end
    end
    unless used_for_class
      doc = RDoc::Markup::Document.new
      begin
        driver.add_method(doc, name)
      rescue RDoc::RI::Driver::NotFoundError
        doc = nil
      rescue
        return nil # unknown error
      end
    end
    return nil if doc.nil?
    width = 40

    right_x = cursor_pos_to_render.x + autocomplete_dialog.width
    if right_x + width > screen_width
      right_width = screen_width - (right_x + 1)
      left_x = autocomplete_dialog.column - width
      left_x = 0 if left_x < 0
      left_width = width > autocomplete_dialog.column ? autocomplete_dialog.column : width
      if right_width.positive? and left_width.positive?
        if right_width >= left_width
          width = right_width
          x = right_x
        else
          width = left_width
          x = left_x
        end
      elsif right_width.positive? and left_width <= 0
        width = right_width
        x = right_x
      elsif right_width <= 0 and left_width.positive?
        width = left_width
        x = left_x
      else # Both are negative width.
        return nil
      end
    else
      x = right_x
    end
    formatter = RDoc::Markup::ToAnsi.new
    formatter.width = width
    dialog.trap_key = alt_d
    mod_key = RUBY_PLATFORM.match?(/darwin/) ? "Option" : "Alt"
    if show_easter_egg
      type = STDOUT.external_encoding == Encoding::UTF_8 ? :unicode : :ascii
      contents = IRB.send(:easter_egg_logo, type).split("\n")
      message = "Press #{mod_key}+d to see more"
      contents[0][0, message.size] = message
    else
      message = "Press #{mod_key}+d to read the full document"
      contents = [message] + doc.accept(formatter).split("\n")
    end
    contents = contents.take(preferred_dialog_height)

    y = cursor_pos_to_render.y
    Reline::DialogRenderInfo.new(pos: Reline::CursorPos.new(x, y), contents: contents, width: width, bg_color: '49')
  }
end