irb 1.3.6 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,6 +14,7 @@ require_relative 'magic-file'
14
14
  require_relative 'completion'
15
15
  require 'io/console'
16
16
  require 'reline'
17
+ require 'rdoc'
17
18
 
18
19
  module IRB
19
20
  STDIN_FILE_NAME = "(line)" # :nodoc:
@@ -264,7 +265,8 @@ module IRB
264
265
 
265
266
  class ReidlineInputMethod < InputMethod
266
267
  include Reline
267
- # Creates a new input method object using Readline
268
+
269
+ # Creates a new input method object using Reline
268
270
  def initialize
269
271
  IRB.__send__(:set_encoding, Reline.encoding_system_needs.name, override: false)
270
272
  super
@@ -294,6 +296,10 @@ module IRB
294
296
  end
295
297
  end
296
298
  Reline.dig_perfect_match_proc = IRB::InputCompletor::PerfectMatchedProc
299
+ Reline.autocompletion = IRB.conf[:USE_AUTOCOMPLETE]
300
+ if IRB.conf[:USE_AUTOCOMPLETE]
301
+ Reline.add_dialog_proc(:show_doc, SHOW_DOC_DIALOG, Reline::DEFAULT_DIALOG_CONTEXT)
302
+ end
297
303
  end
298
304
 
299
305
  def check_termination(&block)
@@ -308,6 +314,99 @@ module IRB
308
314
  @auto_indent_proc = block
309
315
  end
310
316
 
317
+ SHOW_DOC_DIALOG = ->() {
318
+ dialog.trap_key = nil
319
+ alt_d = [
320
+ [Reline::Key.new(nil, 0xE4, true)], # Normal Alt+d.
321
+ [27, 100], # Normal Alt+d when convert-meta isn't used.
322
+ [195, 164], # The "ä" that appears when Alt+d is pressed on xterm.
323
+ [226, 136, 130] # The "∂" that appears when Alt+d in pressed on iTerm2.
324
+ ]
325
+
326
+ if just_cursor_moving and completion_journey_data.nil?
327
+ return nil
328
+ end
329
+ cursor_pos_to_render, result, pointer, autocomplete_dialog = context.pop(4)
330
+ return nil if result.nil? or pointer.nil? or pointer < 0
331
+ name = result[pointer]
332
+ name = IRB::InputCompletor.retrieve_completion_data(name, doc_namespace: true)
333
+
334
+ options = {}
335
+ options[:extra_doc_dirs] = IRB.conf[:EXTRA_DOC_DIRS] unless IRB.conf[:EXTRA_DOC_DIRS].empty?
336
+ driver = RDoc::RI::Driver.new(options)
337
+
338
+ if key.match?(dialog.name)
339
+ begin
340
+ driver.display_names([name])
341
+ rescue RDoc::RI::Driver::NotFoundError
342
+ end
343
+ end
344
+
345
+ begin
346
+ name = driver.expand_name(name)
347
+ rescue RDoc::RI::Driver::NotFoundError
348
+ return nil
349
+ rescue
350
+ return nil # unknown error
351
+ end
352
+ doc = nil
353
+ used_for_class = false
354
+ if not name =~ /#|\./
355
+ found, klasses, includes, extends = driver.classes_and_includes_and_extends_for(name)
356
+ if not found.empty?
357
+ doc = driver.class_document(name, found, klasses, includes, extends)
358
+ used_for_class = true
359
+ end
360
+ end
361
+ unless used_for_class
362
+ doc = RDoc::Markup::Document.new
363
+ begin
364
+ driver.add_method(doc, name)
365
+ rescue RDoc::RI::Driver::NotFoundError
366
+ doc = nil
367
+ rescue
368
+ return nil # unknown error
369
+ end
370
+ end
371
+ return nil if doc.nil?
372
+ width = 40
373
+
374
+ right_x = cursor_pos_to_render.x + autocomplete_dialog.width
375
+ if right_x + width > screen_width
376
+ right_width = screen_width - (right_x + 1)
377
+ left_x = autocomplete_dialog.column - width
378
+ left_x = 0 if left_x < 0
379
+ left_width = width > autocomplete_dialog.column ? autocomplete_dialog.column : width
380
+ if right_width.positive? and left_width.positive?
381
+ if right_width >= left_width
382
+ width = right_width
383
+ x = right_x
384
+ else
385
+ width = left_width
386
+ x = left_x
387
+ end
388
+ elsif right_width.positive? and left_width <= 0
389
+ width = right_width
390
+ x = right_x
391
+ elsif right_width <= 0 and left_width.positive?
392
+ width = left_width
393
+ x = left_x
394
+ else # Both are negative width.
395
+ return nil
396
+ end
397
+ else
398
+ x = right_x
399
+ end
400
+ formatter = RDoc::Markup::ToAnsi.new
401
+ formatter.width = width
402
+ dialog.trap_key = alt_d
403
+ message = 'Press Alt+d to read the full document'
404
+ contents = [message] + doc.accept(formatter).split("\n")
405
+
406
+ y = cursor_pos_to_render.y
407
+ DialogRenderInfo.new(pos: Reline::CursorPos.new(x, y), contents: contents, width: width, bg_color: '49')
408
+ }
409
+
311
410
  # Reads the next line from this input method.
312
411
  #
313
412
  # See IO#gets for more information.
@@ -20,8 +20,15 @@ Usage: irb.rb [options] [programfile] [arguments]
20
20
  -W[level=2] Same as `ruby -W`
21
21
  --context-mode n Set n[0-4] to method to create Binding Object,
22
22
  when new workspace was created
23
- --echo Show result(default)
23
+ --extra-doc-dir Add an extra doc dir for the doc dialog
24
+ --echo Show result (default)
24
25
  --noecho Don't show result
26
+ --echo-on-assignment
27
+ Show result on assignment
28
+ --noecho-on-assignment
29
+ Don't show result on assignment
30
+ --truncate-echo-on-assignment
31
+ Show truncated result on assignment (default)
25
32
  --inspect Use `inspect' for output
26
33
  --noinspect Don't use inspect for output
27
34
  --multiline Use multiline editor module
@@ -30,6 +37,8 @@ Usage: irb.rb [options] [programfile] [arguments]
30
37
  --nosingleline Don't use singleline editor module
31
38
  --colorize Use colorization
32
39
  --nocolorize Don't use colorization
40
+ --autocomplete Use autocompletion
41
+ --noautocomplete Don't use autocompletion
33
42
  --prompt prompt-mode/--prompt-mode prompt-mode
34
43
  Switch prompt mode. Pre-defined prompt modes are
35
44
  `default', `simple', `xmp' and `inf-ruby'
@@ -29,6 +29,8 @@ Usage: irb.rb [options] [programfile] [arguments]
29
29
  --nosingleline シングルラインエディタを利用しない.
30
30
  --colorize 色付けを利用する.
31
31
  --nocolorize 色付けを利用しない.
32
+ --autocomplete オートコンプリートを利用する.
33
+ --noautocomplete オートコンプリートを利用しない.
32
34
  --prompt prompt-mode/--prompt-mode prompt-mode
33
35
  プロンプトモードを切替えます. 現在定義されているプ
34
36
  ロンプトモードは, default, simple, xmp, inf-rubyが