@@ -315,9 +315,17 @@ def auto_indent(&block)
315315 @auto_indent_proc = block
316316 end
317317
318- def retrieve_doc_namespace ( matched )
318+ def retrieve_document_target ( matched )
319319 preposing , _target , postposing , bind = @completion_params
320- @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
321329 end
322330
323331 def rdoc_ri_driver
@@ -345,22 +353,25 @@ def show_doc_dialog_proc
345353 cursor_pos_to_render , result , pointer , autocomplete_dialog = context . pop ( 4 )
346354 return nil if result . nil? || pointer . nil? || pointer < 0
347355
348- name = input_method . retrieve_doc_namespace ( result [ pointer ] )
349- # Use first one because document dialog does not support multiple namespaces.
350- name = name . first if name . is_a? ( Array )
351-
352- show_easter_egg = name &.match? ( /\A RubyVM/ ) && !ENV [ 'RUBY_YES_I_AM_NOT_A_NORMAL_USER' ]
356+ matched_text = result [ pointer ]
357+ show_easter_egg = matched_text &.match? ( /\A RubyVM/ ) && !ENV [ 'RUBY_YES_I_AM_NOT_A_NORMAL_USER' ]
358+ target = show_easter_egg ? nil : input_method . retrieve_document_target ( matched_text )
353359
354360 x , width = input_method . dialog_doc_position ( cursor_pos_to_render , autocomplete_dialog , screen_width )
355361 return nil unless x
356362
357363 dialog . trap_key = ALT_D_SEQUENCES
358364 open_doc = key . match? ( dialog . name )
359365
360- contents = if show_easter_egg
361- input_method . easter_egg_dialog_contents ( open_doc : open_doc )
366+ contents = case target
367+ when CommandDocument
368+ input_method . command_doc_dialog_contents ( target . name , width , open_doc : open_doc )
369+ when MethodDocument
370+ input_method . rdoc_dialog_contents ( target . name , width , open_doc : open_doc )
362371 else
363- input_method . rdoc_dialog_contents ( name , width , open_doc : open_doc )
372+ if show_easter_egg
373+ input_method . easter_egg_dialog_contents ( open_doc : open_doc )
374+ end
364375 end
365376 return nil unless contents
366377
@@ -370,6 +381,23 @@ def show_doc_dialog_proc
370381 }
371382 end
372383
384+ def command_doc_dialog_contents ( command_name , width , open_doc : false )
385+ command_class = IRB ::Command . load_command ( command_name )
386+ return unless command_class
387+
388+ if open_doc
389+ content = command_class . help_message || command_class . description
390+ begin
391+ print "\e [?1049h"
392+ Pager . page_content ( content )
393+ ensure
394+ print "\e [?1049l"
395+ end
396+ end
397+
398+ [ PRESS_ALT_D_TO_READ_FULL_DOC , "" ] + command_class . doc_dialog_content ( command_name , width )
399+ end
400+
373401 def easter_egg_dialog_contents ( open_doc : false )
374402 IRB . __send__ ( :easter_egg ) if open_doc
375403 type = STDOUT . external_encoding == Encoding ::UTF_8 ? :unicode : :ascii
@@ -447,31 +475,40 @@ def dialog_doc_position(cursor_pos_to_render, autocomplete_dialog, screen_width)
447475 end
448476
449477 def display_document ( matched )
450- driver = rdoc_ri_driver
451- return unless driver
452-
453- if matched =~ /\A (?:::)?RubyVM/ && !ENV [ 'RUBY_YES_I_AM_NOT_A_NORMAL_USER' ]
454- IRB . __send__ ( :easter_egg )
455- return
456- end
478+ target = retrieve_document_target ( matched )
479+ return unless target
480+
481+ case target
482+ when CommandDocument
483+ command_class = IRB ::Command . load_command ( target . name )
484+ if command_class
485+ content = command_class . help_message || command_class . description
486+ Pager . page_content ( content )
487+ end
488+ when MethodDocument
489+ driver = rdoc_ri_driver
490+ return unless driver
457491
458- namespace = retrieve_doc_namespace ( matched )
459- return unless namespace
492+ if matched =~ /\A (?:::)?RubyVM/ && !ENV [ 'RUBY_YES_I_AM_NOT_A_NORMAL_USER' ]
493+ IRB . __send__ ( :easter_egg )
494+ return
495+ end
460496
461- if namespace . is_a? ( Array )
462- out = RDoc ::Markup ::Document . new
463- namespace . each do |m |
497+ if target . names . length > 1
498+ out = RDoc ::Markup ::Document . new
499+ target . names . each do |m |
500+ begin
501+ driver . add_method ( out , m )
502+ rescue RDoc ::RI ::Driver ::NotFoundError
503+ end
504+ end
505+ driver . display ( out )
506+ else
464507 begin
465- driver . add_method ( out , m )
508+ driver . display_names ( [ target . name ] )
466509 rescue RDoc ::RI ::Driver ::NotFoundError
467510 end
468511 end
469- driver . display ( out )
470- else
471- begin
472- driver . display_names ( [ namespace ] )
473- rescue RDoc ::RI ::Driver ::NotFoundError
474- end
475512 end
476513 end
477514
0 commit comments