Skip to content

Commit 0267004

Browse files
committed
Add show doc dialog for Reline
1 parent 9861023 commit 0267004

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

lib/irb/input-method.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
require_relative 'completion'
1515
require 'io/console'
1616
require 'reline'
17+
require 'rdoc'
1718

1819
module IRB
1920
STDIN_FILE_NAME = "(line)" # :nodoc:
@@ -308,6 +309,46 @@ def auto_indent(&block)
308309
@auto_indent_proc = block
309310
end
310311

312+
SHOW_DOC_DIALOG = ->() {
313+
begin
314+
require 'rdoc'
315+
rescue LoadError
316+
return nil
317+
end
318+
if just_cursor_moving and completion_journey_data.nil?
319+
return nil
320+
end
321+
cursor_pos_to_render, result, pointer = context.pop(3)
322+
return nil if result.nil? or pointer.nil?
323+
name = result[pointer]
324+
325+
driver = RDoc::RI::Driver.new
326+
name = driver.expand_name(name)
327+
doc = nil
328+
used_for_class = false
329+
if not name =~ /#|\./
330+
found, klasses, includes, extends = driver.classes_and_includes_and_extends_for(name)
331+
if not found.empty?
332+
doc = driver.class_document(name, found, klasses, includes, extends)
333+
used_for_class = true
334+
end
335+
end
336+
unless used_for_class
337+
doc = RDoc::Markup::Document.new
338+
begin
339+
driver.add_method(doc, name)
340+
rescue RDoc::RI::Driver::NotFoundError
341+
doc = nil
342+
end
343+
end
344+
return nil if doc.nil?
345+
formatter = RDoc::Markup::ToAnsi.new
346+
formatter.width = 40
347+
str = doc.accept(formatter)
348+
349+
[Reline::CursorPos.new(cursor_pos_to_render.x + 40, cursor_pos_to_render.y + pointer), str.split("\n"), nil, '49']
350+
}
351+
311352
# Reads the next line from this input method.
312353
#
313354
# See IO#gets for more information.
@@ -316,6 +357,7 @@ def gets
316357
Reline.output = @stdout
317358
Reline.prompt_proc = @prompt_proc
318359
Reline.auto_indent_proc = @auto_indent_proc if @auto_indent_proc
360+
Reline.add_dialog_proc(:show_doc, SHOW_DOC_DIALOG, Reline::DEFAULT_DIALOG_CONTEXT)
319361
if l = readmultiline(@prompt, false, &@check_termination_proc)
320362
HISTORY.push(l) if !l.empty?
321363
@line[@line_no += 1] = l + "\n"

0 commit comments

Comments
 (0)