irb 1.17.0 → 1.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -255,6 +255,16 @@ module IRB
255
255
 
256
256
  class RelineInputMethod < StdioInputMethod
257
257
  HISTORY = Reline::HISTORY
258
+ ALT_KEY_NAME = RUBY_PLATFORM.match?(/darwin/) ? "Option" : "Alt"
259
+ PRESS_ALT_D_TO_READ_FULL_DOC = "Press #{ALT_KEY_NAME}+d to read the full document".freeze
260
+ PRESS_ALT_D_TO_SEE_MORE = "Press #{ALT_KEY_NAME}+d to see more".freeze
261
+ ALT_D_SEQUENCES = [
262
+ [27, 100], # Normal Alt+d when convert-meta isn't used.
263
+ # When option/alt is not configured as a meta key in terminal emulator,
264
+ # option/alt + d will send a unicode character depend on OS keyboard setting.
265
+ [195, 164], # "ä" in somewhere (FIXME: environment information is unknown).
266
+ [226, 136, 130] # "∂" Alt+d on Mac keyboard.
267
+ ].freeze
258
268
  include HistorySavingAbility
259
269
  # Creates a new input method object using Reline
260
270
  def initialize(completor)
@@ -305,9 +315,17 @@ module IRB
305
315
  @auto_indent_proc = block
306
316
  end
307
317
 
308
- def retrieve_doc_namespace(matched)
318
+ def retrieve_document_target(matched)
309
319
  preposing, _target, postposing, bind = @completion_params
310
- @completor.doc_namespace(preposing, matched, postposing, bind: bind)
320
+ result = @completor.doc_namespace(preposing, matched, postposing, bind: bind)
321
+ case result
322
+ when DocumentTarget, nil
323
+ result
324
+ when Array
325
+ MethodDocument.new(*result)
326
+ when String
327
+ MethodDocument.new(result)
328
+ end
311
329
  end
312
330
 
313
331
  def rdoc_ri_driver
@@ -328,146 +346,179 @@ module IRB
328
346
  input_method = self # self is changed in the lambda below.
329
347
  ->() {
330
348
  dialog.trap_key = nil
331
- alt_d = [
332
- [27, 100], # Normal Alt+d when convert-meta isn't used.
333
- # When option/alt is not configured as a meta key in terminal emulator,
334
- # option/alt + d will send a unicode character depend on OS keyboard setting.
335
- [195, 164], # "ä" in somewhere (FIXME: environment information is unknown).
336
- [226, 136, 130] # "∂" Alt+d on Mac keyboard.
337
- ]
338
-
339
- if just_cursor_moving and completion_journey_data.nil?
349
+
350
+ if just_cursor_moving && completion_journey_data.nil?
340
351
  return nil
341
352
  end
342
353
  cursor_pos_to_render, result, pointer, autocomplete_dialog = context.pop(4)
343
- return nil if result.nil? or pointer.nil? or pointer < 0
354
+ return nil if result.nil? || pointer.nil? || pointer < 0
344
355
 
345
- name = input_method.retrieve_doc_namespace(result[pointer])
346
- # Use first one because document dialog does not support multiple namespaces.
347
- name = name.first if name.is_a?(Array)
356
+ matched_text = result[pointer]
357
+ show_easter_egg = matched_text&.match?(/\ARubyVM/) && !ENV['RUBY_YES_I_AM_NOT_A_NORMAL_USER']
358
+ target = show_easter_egg ? nil : input_method.retrieve_document_target(matched_text)
348
359
 
349
- show_easter_egg = name&.match?(/\ARubyVM/) && !ENV['RUBY_YES_I_AM_NOT_A_NORMAL_USER']
360
+ x, width = input_method.dialog_doc_position(cursor_pos_to_render, autocomplete_dialog, screen_width)
361
+ return nil unless x
350
362
 
351
- driver = input_method.rdoc_ri_driver
363
+ dialog.trap_key = ALT_D_SEQUENCES
352
364
 
353
365
  if key.match?(dialog.name)
354
- if show_easter_egg
355
- IRB.__send__(:easter_egg)
356
- else
357
- # RDoc::RI::Driver#display_names uses pager command internally.
358
- # Some pager command like `more` doesn't use alternate screen
359
- # so we need to turn on and off alternate screen manually.
360
- begin
361
- print "\e[?1049h"
362
- driver.display_names([name])
363
- rescue RDoc::RI::Driver::NotFoundError
364
- ensure
365
- print "\e[?1049l"
366
- end
366
+ begin
367
+ print "\e[?1049h"
368
+ input_method.display_document(matched_text)
369
+ ensure
370
+ print "\e[?1049l"
367
371
  end
368
372
  end
369
373
 
370
- begin
371
- name = driver.expand_name(name)
372
- rescue RDoc::RI::Driver::NotFoundError
373
- return nil
374
- rescue
375
- return nil # unknown error
376
- end
377
- doc = nil
378
- used_for_class = false
379
- if not name =~ /#|\./
380
- found, klasses, includes, extends = driver.classes_and_includes_and_extends_for(name)
381
- if not found.empty?
382
- doc = driver.class_document(name, found, klasses, includes, extends)
383
- used_for_class = true
374
+ contents = case target
375
+ when CommandDocument
376
+ input_method.command_doc_dialog_contents(target.name, width)
377
+ when MethodDocument
378
+ input_method.rdoc_dialog_contents(target.name, width)
379
+ else
380
+ if show_easter_egg
381
+ input_method.easter_egg_dialog_contents
384
382
  end
385
383
  end
386
- unless used_for_class
387
- doc = RDoc::Markup::Document.new
388
- begin
389
- driver.add_method(doc, name)
390
- rescue RDoc::RI::Driver::NotFoundError
391
- doc = nil
392
- rescue
393
- return nil # unknown error
394
- end
384
+ return nil unless contents
385
+
386
+ contents = contents.take(preferred_dialog_height)
387
+ y = cursor_pos_to_render.y
388
+ Reline::DialogRenderInfo.new(pos: Reline::CursorPos.new(x, y), contents: contents, width: width, bg_color: '49')
389
+ }
390
+ end
391
+
392
+ def command_doc_dialog_contents(command_name, width)
393
+ command_class = IRB::Command.load_command(command_name)
394
+ return unless command_class
395
+
396
+ [PRESS_ALT_D_TO_READ_FULL_DOC, ""] + command_class.doc_dialog_content(command_name, width)
397
+ end
398
+
399
+ def easter_egg_dialog_contents
400
+ type = STDOUT.external_encoding == Encoding::UTF_8 ? :unicode : :ascii
401
+ lines = IRB.send(:easter_egg_logo, type).split("\n")
402
+ lines[0][0, PRESS_ALT_D_TO_SEE_MORE.size] = PRESS_ALT_D_TO_SEE_MORE
403
+ lines
404
+ end
405
+
406
+ def rdoc_dialog_contents(name, width)
407
+ formatter = RDoc::Markup::ToAnsi.new
408
+ formatter.width = width
409
+
410
+ begin
411
+ document = retrieve_rdoc_document(name)
412
+ rescue RDoc::RI::Driver::NotFoundError
413
+ return
414
+ rescue => e
415
+ raise if $DEBUG
416
+ return rdoc_error_document(e).accept(formatter).split("\n")
417
+ end
418
+ return unless document
419
+
420
+ [PRESS_ALT_D_TO_READ_FULL_DOC] + document.accept(formatter).split("\n")
421
+ end
422
+
423
+ def retrieve_rdoc_document(name)
424
+ driver = rdoc_ri_driver
425
+ return unless driver
426
+
427
+ name = driver.expand_name(name)
428
+
429
+ if name =~ /#|\./
430
+ d = RDoc::Markup::Document.new
431
+ driver.add_method(d, name)
432
+ d
433
+ else
434
+ found, klasses, includes, extends = driver.classes_and_includes_and_extends_for(name)
435
+ if found.empty?
436
+ d = RDoc::Markup::Document.new
437
+ driver.add_method(d, name)
438
+ d
439
+ else
440
+ driver.class_document(name, found, klasses, includes, extends)
395
441
  end
396
- return nil if doc.nil?
397
- width = 40
398
-
399
- right_x = cursor_pos_to_render.x + autocomplete_dialog.width
400
- if right_x + width > screen_width
401
- right_width = screen_width - (right_x + 1)
402
- left_x = autocomplete_dialog.column - width
403
- left_x = 0 if left_x < 0
404
- left_width = width > autocomplete_dialog.column ? autocomplete_dialog.column : width
405
- if right_width.positive? and left_width.positive?
406
- if right_width >= left_width
407
- width = right_width
408
- x = right_x
409
- else
410
- width = left_width
411
- x = left_x
412
- end
413
- elsif right_width.positive? and left_width <= 0
442
+ end
443
+ end
444
+
445
+ def rdoc_error_document(error)
446
+ document = RDoc::Markup::Document.new
447
+ document << RDoc::Markup::Paragraph.new("Failed to load the document:")
448
+ document << RDoc::Markup::Paragraph.new("#{error.class}: #{error.message}")
449
+ document << RDoc::Markup::BlankLine.new
450
+ document << RDoc::Markup::Paragraph.new("Restart IRB with -d to see the backtrace.")
451
+ document
452
+ end
453
+
454
+ def dialog_doc_position(cursor_pos_to_render, autocomplete_dialog, screen_width)
455
+ width = 40
456
+ right_x = cursor_pos_to_render.x + autocomplete_dialog.width
457
+ if right_x + width > screen_width
458
+ right_width = screen_width - (right_x + 1)
459
+ left_x = autocomplete_dialog.column - width
460
+ left_x = 0 if left_x < 0
461
+ left_width = width > autocomplete_dialog.column ? autocomplete_dialog.column : width
462
+ if right_width.positive? && left_width.positive?
463
+ if right_width >= left_width
414
464
  width = right_width
415
465
  x = right_x
416
- elsif right_width <= 0 and left_width.positive?
466
+ else
417
467
  width = left_width
418
468
  x = left_x
419
- else # Both are negative width.
420
- return nil
421
469
  end
422
- else
470
+ elsif right_width.positive? && left_width <= 0
471
+ width = right_width
423
472
  x = right_x
424
- end
425
- formatter = RDoc::Markup::ToAnsi.new
426
- formatter.width = width
427
- dialog.trap_key = alt_d
428
- mod_key = RUBY_PLATFORM.match?(/darwin/) ? "Option" : "Alt"
429
- if show_easter_egg
430
- type = STDOUT.external_encoding == Encoding::UTF_8 ? :unicode : :ascii
431
- contents = IRB.send(:easter_egg_logo, type).split("\n")
432
- message = "Press #{mod_key}+d to see more"
433
- contents[0][0, message.size] = message
473
+ elsif right_width <= 0 && left_width.positive?
474
+ width = left_width
475
+ x = left_x
434
476
  else
435
- message = "Press #{mod_key}+d to read the full document"
436
- contents = [message] + doc.accept(formatter).split("\n")
477
+ return nil
437
478
  end
438
- contents = contents.take(preferred_dialog_height)
439
-
440
- y = cursor_pos_to_render.y
441
- Reline::DialogRenderInfo.new(pos: Reline::CursorPos.new(x, y), contents: contents, width: width, bg_color: '49')
442
- }
479
+ else
480
+ x = right_x
481
+ end
482
+ [x, width]
443
483
  end
444
484
 
445
485
  def display_document(matched)
446
- driver = rdoc_ri_driver
447
- return unless driver
448
-
449
- if matched =~ /\A(?:::)?RubyVM/ and not ENV['RUBY_YES_I_AM_NOT_A_NORMAL_USER']
450
- IRB.__send__(:easter_egg)
451
- return
452
- end
486
+ target = retrieve_document_target(matched)
487
+ return unless target
488
+
489
+ case target
490
+ when CommandDocument
491
+ command_class = IRB::Command.load_command(target.name)
492
+ if command_class
493
+ content = command_class.help_message || command_class.description
494
+ Pager.page(retain_content: true) do |io|
495
+ io.puts content
496
+ end
497
+ end
498
+ when MethodDocument
499
+ driver = rdoc_ri_driver
500
+ return unless driver
453
501
 
454
- namespace = retrieve_doc_namespace(matched)
455
- return unless namespace
502
+ if matched =~ /\A(?:::)?RubyVM/ && !ENV['RUBY_YES_I_AM_NOT_A_NORMAL_USER']
503
+ IRB.__send__(:easter_egg)
504
+ return
505
+ end
456
506
 
457
- if namespace.is_a?(Array)
458
- out = RDoc::Markup::Document.new
459
- namespace.each do |m|
507
+ if target.names.length > 1
508
+ out = RDoc::Markup::Document.new
509
+ target.names.each do |m|
510
+ begin
511
+ driver.add_method(out, m)
512
+ rescue RDoc::RI::Driver::NotFoundError
513
+ end
514
+ end
515
+ driver.display(out)
516
+ else
460
517
  begin
461
- driver.add_method(out, m)
518
+ driver.display_names([target.name])
462
519
  rescue RDoc::RI::Driver::NotFoundError
463
520
  end
464
521
  end
465
- driver.display(out)
466
- else
467
- begin
468
- driver.display_names([namespace])
469
- rescue RDoc::RI::Driver::NotFoundError
470
- end
471
522
  end
472
523
  end
473
524
 
@@ -222,6 +222,7 @@ module IRB
222
222
  # Heredoc closing contains trailing newline. We need to exclude it
223
223
  close_location_start(node.closing_loc) if node.closing_loc && !node.closing.empty?
224
224
  elsif node.opening
225
+ return if node.opening == '?' && node.closing.nil? # Character literal has no closing
225
226
  open_location(node.location, type, node.opening)
226
227
  if node.closing && node.closing != ''
227
228
  # Closing of `"#{\n` is "\n". We need to treat it as not-closed.
data/lib/irb/pager.rb CHANGED
@@ -42,6 +42,14 @@ module IRB
42
42
  # SIGTERM not supported (windows)
43
43
  Process.kill("KILL", pid)
44
44
  end
45
+
46
+ begin
47
+ # Wait for the pager process to terminate.
48
+ # Reading next input from Reline before the pager process is fully terminated
49
+ # may cause issues like raw/cooked mode not being controlled properly.
50
+ Process.waitpid(pid) if pid
51
+ rescue Errno::ECHILD, Errno::ESRCH
52
+ end
45
53
  rescue Errno::ESRCH
46
54
  # Pager process already terminated
47
55
  end