diff options
author | aycabta <aycabta@gmail.com> | 2021-08-27 21:35:42 +0900 |
---|---|---|
committer | aycabta <aycabta@gmail.com> | 2021-08-29 20:30:33 +0900 |
commit | 7e8a6270525f4ea944e8b3b3c3014a48bdb5a734 (patch) | |
tree | 4dab7eb73a7109c4e2e85e8d7be356a538f02244 | |
parent | 4cd344c958a0886fc02238f7f397188dbfca27bd (diff) |
[ruby/reline] Implement doc display dialog in conjunction with autocomplete
https://github.com/ruby/reline/commit/e97bbc4ccf
-rw-r--r-- | lib/reline.rb | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/lib/reline.rb b/lib/reline.rb index 842f6ba61d..ddc4a06af9 100644 --- a/lib/reline.rb +++ b/lib/reline.rb @@ -209,14 +209,39 @@ module Reline y = 0 end cursor_pos_to_render = Reline::CursorPos.new(x, y) + context.clear + context.push(cursor_pos_to_render, result, pointer) [cursor_pos_to_render, result, pointer] } + require 'rdoc' + SHOW_DOC_DIALOG = ->() { + if just_cursor_moving and completion_journey_data.nil? + return nil + end + cursor_pos_to_render, result, pointer = context.pop(3) + return nil if result.nil? or pointer.nil? + name = result[pointer] + driver = RDoc::RI::Driver.new + doc = RDoc::Markup::Document.new + begin + driver.add_method(doc, name) + rescue RDoc::RI::Driver::NotFoundError => e + return nil + end + formatter = RDoc::Markup::ToBs.new + formatter.width = 40 + str = doc.accept(formatter) + [Reline::CursorPos.new(cursor_pos_to_render.x + 40, cursor_pos_to_render.y + pointer), str.split("\n"), nil] + } + def readmultiline(prompt = '', add_hist = false, &confirm_multiline_termination) unless confirm_multiline_termination raise ArgumentError.new('#readmultiline needs block to confirm multiline termination') end - add_dialog_proc(:autocomplete, DEFAULT_DIALOG_PROC_AUTOCOMPLETE) + context = [] + add_dialog_proc(:autocomplete, DEFAULT_DIALOG_PROC_AUTOCOMPLETE, context) + add_dialog_proc(:show_doc, SHOW_DOC_DIALOG, context) inner_readline(prompt, add_hist, true, &confirm_multiline_termination) whole_buffer = line_editor.whole_buffer.dup |