Class: IRB::RelineInputMethod

Inherits:
StdioInputMethod show all
Includes:
HistorySavingAbility
Defined in:
lib/irb/input-method.rb

Direct Known Subclasses

ReidlineInputMethod

Constant Summary collapse

HISTORY =
Reline::HISTORY

Constants inherited from InputMethod

InputMethod::BASIC_WORD_BREAK_CHARACTERS

Instance Attribute Summary

Attributes inherited from InputMethod

#prompt

Instance Method Summary collapse

Methods included from HistorySavingAbility

#load_history, #reset_history_counter, #save_history, #support_history_saving?

Methods inherited from StdioInputMethod

#encoding, #line, #readable_after_eof?

Methods inherited from InputMethod

#readable_after_eof?, #support_history_saving?, #winsize

Constructor Details

#initialize(completor) ⇒ RelineInputMethod

Creates a new input method object using Reline



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/irb/input-method.rb', line 252

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 = proc do |input, complete:|
    IRB.CurrentContext.colorize_input(input, complete: complete)
  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

Instance Method Details

#auto_indent(&block) ⇒ Object



296
297
298
# File 'lib/irb/input-method.rb', line 296

def auto_indent(&block)
  @auto_indent_proc = block
end

#check_termination(&block) ⇒ Object



288
289
290
# File 'lib/irb/input-method.rb', line 288

def check_termination(&block)
  @check_termination_proc = block
end

#completion_infoObject



283
284
285
286
# File 'lib/irb/input-method.rb', line 283

def completion_info
  autocomplete_message = Reline.autocompletion ? 'Autocomplete' : 'Tab Complete'
  "#{autocomplete_message}, #{@completor.inspect}"
end

#display_document(matched) ⇒ Object



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/irb/input-method.rb', line 431

def display_document(matched)
  driver = rdoc_ri_driver
  return unless driver

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

  namespace = retrieve_doc_namespace(matched)
  return unless namespace

  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) ⇒ Object



292
293
294
# File 'lib/irb/input-method.rb', line 292

def dynamic_prompt(&block)
  @prompt_proc = block
end

#eof?Boolean

Whether the end of this input method has been reached, returns true if there is no more data to read.

See IO#eof? for more information.

Returns:

  • (Boolean)


481
482
483
# File 'lib/irb/input-method.rb', line 481

def eof?
  @eof
end

#getsObject

Reads the next line from this input method.

See IO#gets for more information.



463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/irb/input-method.rb', line 463

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

#inspectObject

For debug message



490
491
492
493
494
495
496
# File 'lib/irb/input-method.rb', line 490

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

#prompting?Boolean

Returns:

  • (Boolean)


485
486
487
# File 'lib/irb/input-method.rb', line 485

def prompting?
  true
end

#rdoc_ri_driverObject



305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/irb/input-method.rb', line 305

def rdoc_ri_driver
  return @rdoc_ri_driver if defined?(@rdoc_ri_driver)

  begin
    require 'rdoc'
  rescue LoadError
    @rdoc_ri_driver = nil
  else
    options = {}
    options[:extra_doc_dirs] = IRB.conf[:EXTRA_DOC_DIRS] unless IRB.conf[:EXTRA_DOC_DIRS].empty?
    @rdoc_ri_driver = RDoc::RI::Driver.new(options)
  end
end

#retrieve_doc_namespace(matched) ⇒ Object



300
301
302
303
# File 'lib/irb/input-method.rb', line 300

def retrieve_doc_namespace(matched)
  preposing, _target, postposing, bind = @completion_params
  @completor.doc_namespace(preposing, matched, postposing, bind: bind)
end

#show_doc_dialog_procObject



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/irb/input-method.rb', line 319

def show_doc_dialog_proc
  input_method = self # self is changed in the lambda below.
  ->() {
    dialog.trap_key = nil
    alt_d = [
      [27, 100], # Normal Alt+d when convert-meta isn't used.
      # When option/alt is not configured as a meta key in terminal emulator,
      # option/alt + d will send a unicode character depend on OS keyboard setting.
      [195, 164], # "ä" in somewhere (FIXME: environment information is unknown).
      [226, 136, 130] # "∂" Alt+d on Mac keyboard.
    ]

    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 = input_method.retrieve_doc_namespace(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']

    driver = input_method.rdoc_ri_driver

    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