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.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/doc/Index.md +2 -2
- data/lib/irb/color.rb +71 -8
- data/lib/irb/command/base.rb +35 -0
- data/lib/irb/command/internal_helpers.rb +6 -3
- data/lib/irb/command/load.rb +4 -4
- data/lib/irb/command/ls.rb +6 -4
- data/lib/irb/command/measure.rb +8 -2
- data/lib/irb/command/pushws.rb +10 -4
- data/lib/irb/completion.rb +48 -22
- data/lib/irb/context.rb +2 -2
- data/lib/irb/ext/eval_history.rb +3 -3
- data/lib/irb/ext/loader.rb +0 -3
- data/lib/irb/history.rb +25 -25
- data/lib/irb/init.rb +9 -3
- data/lib/irb/input-method.rb +162 -111
- data/lib/irb/nesting_parser.rb +1 -0
- data/lib/irb/pager.rb +8 -0
- data/lib/irb/ruby-lex.rb +164 -264
- data/lib/irb/ruby_logo.aa +4 -0
- data/lib/irb/source_finder.rb +5 -14
- data/lib/irb/startup_message.rb +82 -0
- data/lib/irb/version.rb +2 -2
- data/lib/irb.rb +16 -7
- metadata +2 -1
data/lib/irb/input-method.rb
CHANGED
|
@@ -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
|
|
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
|
-
|
|
332
|
-
|
|
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?
|
|
354
|
+
return nil if result.nil? || pointer.nil? || pointer < 0
|
|
344
355
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
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
|
-
|
|
360
|
+
x, width = input_method.dialog_doc_position(cursor_pos_to_render, autocomplete_dialog, screen_width)
|
|
361
|
+
return nil unless x
|
|
350
362
|
|
|
351
|
-
|
|
363
|
+
dialog.trap_key = ALT_D_SEQUENCES
|
|
352
364
|
|
|
353
365
|
if key.match?(dialog.name)
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
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
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
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
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
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
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
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
|
-
|
|
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
|
-
|
|
470
|
+
elsif right_width.positive? && left_width <= 0
|
|
471
|
+
width = right_width
|
|
423
472
|
x = right_x
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
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
|
-
|
|
436
|
-
contents = [message] + doc.accept(formatter).split("\n")
|
|
477
|
+
return nil
|
|
437
478
|
end
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
}
|
|
479
|
+
else
|
|
480
|
+
x = right_x
|
|
481
|
+
end
|
|
482
|
+
[x, width]
|
|
443
483
|
end
|
|
444
484
|
|
|
445
485
|
def display_document(matched)
|
|
446
|
-
|
|
447
|
-
return unless
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
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
|
-
|
|
455
|
-
|
|
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
|
-
|
|
458
|
-
|
|
459
|
-
|
|
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.
|
|
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
|
|
data/lib/irb/nesting_parser.rb
CHANGED
|
@@ -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
|