irb 1.7.1 → 1.13.2

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.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/.document +1 -1
  3. data/Gemfile +10 -1
  4. data/README.md +265 -20
  5. data/Rakefile +13 -10
  6. data/doc/irb/irb.rd.ja +1 -3
  7. data/irb.gemspec +2 -1
  8. data/lib/irb/cmd/nop.rb +3 -52
  9. data/lib/irb/color.rb +4 -2
  10. data/lib/irb/command/backtrace.rb +17 -0
  11. data/lib/irb/command/base.rb +62 -0
  12. data/lib/irb/command/break.rb +17 -0
  13. data/lib/irb/command/catch.rb +17 -0
  14. data/lib/irb/command/chws.rb +40 -0
  15. data/lib/irb/command/context.rb +16 -0
  16. data/lib/irb/{cmd → command}/continue.rb +3 -3
  17. data/lib/irb/command/debug.rb +71 -0
  18. data/lib/irb/{cmd → command}/delete.rb +3 -3
  19. data/lib/irb/command/disable_irb.rb +19 -0
  20. data/lib/irb/command/edit.rb +63 -0
  21. data/lib/irb/command/exit.rb +18 -0
  22. data/lib/irb/{cmd → command}/finish.rb +3 -3
  23. data/lib/irb/command/force_exit.rb +18 -0
  24. data/lib/irb/command/help.rb +83 -0
  25. data/lib/irb/command/history.rb +45 -0
  26. data/lib/irb/command/info.rb +17 -0
  27. data/lib/irb/command/internal_helpers.rb +27 -0
  28. data/lib/irb/{cmd → command}/irb_info.rb +7 -7
  29. data/lib/irb/{cmd → command}/load.rb +23 -8
  30. data/lib/irb/{cmd → command}/ls.rb +42 -19
  31. data/lib/irb/{cmd → command}/measure.rb +18 -17
  32. data/lib/irb/{cmd → command}/next.rb +3 -3
  33. data/lib/irb/command/pushws.rb +65 -0
  34. data/lib/irb/command/show_doc.rb +51 -0
  35. data/lib/irb/command/show_source.rb +74 -0
  36. data/lib/irb/{cmd → command}/step.rb +3 -3
  37. data/lib/irb/command/subirb.rb +123 -0
  38. data/lib/irb/{cmd → command}/whereami.rb +3 -5
  39. data/lib/irb/command.rb +23 -0
  40. data/lib/irb/completion.rb +133 -102
  41. data/lib/irb/context.rb +182 -66
  42. data/lib/irb/debug/ui.rb +103 -0
  43. data/lib/irb/{cmd/debug.rb → debug.rb} +53 -59
  44. data/lib/irb/default_commands.rb +265 -0
  45. data/lib/irb/easter-egg.rb +16 -6
  46. data/lib/irb/ext/change-ws.rb +6 -8
  47. data/lib/irb/ext/{history.rb → eval_history.rb} +7 -7
  48. data/lib/irb/ext/loader.rb +4 -4
  49. data/lib/irb/ext/multi-irb.rb +5 -5
  50. data/lib/irb/ext/tracer.rb +12 -51
  51. data/lib/irb/ext/use-loader.rb +6 -8
  52. data/lib/irb/ext/workspaces.rb +10 -34
  53. data/lib/irb/frame.rb +1 -1
  54. data/lib/irb/help.rb +3 -3
  55. data/lib/irb/helper_method/base.rb +16 -0
  56. data/lib/irb/helper_method/conf.rb +11 -0
  57. data/lib/irb/helper_method.rb +29 -0
  58. data/lib/irb/{ext/save-history.rb → history.rb} +20 -58
  59. data/lib/irb/init.rb +154 -58
  60. data/lib/irb/input-method.rb +238 -203
  61. data/lib/irb/inspector.rb +3 -3
  62. data/lib/irb/lc/error.rb +1 -11
  63. data/lib/irb/lc/help-message +4 -0
  64. data/lib/irb/lc/ja/error.rb +1 -11
  65. data/lib/irb/lc/ja/help-message +13 -0
  66. data/lib/irb/locale.rb +2 -2
  67. data/lib/irb/nesting_parser.rb +13 -3
  68. data/lib/irb/notifier.rb +1 -1
  69. data/lib/irb/output-method.rb +2 -8
  70. data/lib/irb/pager.rb +91 -0
  71. data/lib/irb/ruby-lex.rb +391 -471
  72. data/lib/irb/ruby_logo.aa +43 -0
  73. data/lib/irb/source_finder.rb +139 -0
  74. data/lib/irb/statement.rb +80 -0
  75. data/lib/irb/version.rb +3 -3
  76. data/lib/irb/workspace.rb +24 -4
  77. data/lib/irb/ws-for-case-2.rb +1 -1
  78. data/lib/irb/xmp.rb +3 -3
  79. data/lib/irb.rb +1232 -604
  80. data/man/irb.1 +7 -0
  81. metadata +60 -32
  82. data/lib/irb/cmd/backtrace.rb +0 -21
  83. data/lib/irb/cmd/break.rb +0 -21
  84. data/lib/irb/cmd/catch.rb +0 -21
  85. data/lib/irb/cmd/chws.rb +0 -36
  86. data/lib/irb/cmd/edit.rb +0 -61
  87. data/lib/irb/cmd/help.rb +0 -23
  88. data/lib/irb/cmd/info.rb +0 -21
  89. data/lib/irb/cmd/pushws.rb +0 -45
  90. data/lib/irb/cmd/show_cmds.rb +0 -39
  91. data/lib/irb/cmd/show_doc.rb +0 -48
  92. data/lib/irb/cmd/show_source.rb +0 -113
  93. data/lib/irb/cmd/subirb.rb +0 -66
  94. data/lib/irb/extend-command.rb +0 -356
  95. data/lib/irb/src_encoding.rb +0 -7
@@ -1,10 +1,10 @@
1
- require_relative "nop"
2
-
3
1
  module IRB
4
2
  # :stopdoc:
5
3
 
6
- module ExtendCommand
7
- class Measure < Nop
4
+ module Command
5
+ class Measure < Base
6
+ include RubyArgsExtractor
7
+
8
8
  category "Misc"
9
9
  description "`measure` enables the mode to measure processing time. `measure :off` disables it."
10
10
 
@@ -12,32 +12,33 @@ module IRB
12
12
  super(*args)
13
13
  end
14
14
 
15
- def execute(type = nil, arg = nil, &block)
15
+ def execute(arg)
16
+ if arg&.match?(/^do$|^do[^\w]|^\{/)
17
+ warn 'Configure IRB.conf[:MEASURE_PROC] to add custom measure methods.'
18
+ return
19
+ end
20
+ args, kwargs = ruby_args(arg)
21
+ execute_internal(*args, **kwargs)
22
+ end
23
+
24
+ def execute_internal(type = nil, arg = nil)
16
25
  # Please check IRB.init_config in lib/irb/init.rb that sets
17
26
  # IRB.conf[:MEASURE_PROC] to register default "measure" methods,
18
27
  # "measure :time" (abbreviated as "measure") and "measure :stackprof".
28
+
19
29
  case type
20
30
  when :off
21
- IRB.conf[:MEASURE] = nil
22
31
  IRB.unset_measure_callback(arg)
23
32
  when :list
24
33
  IRB.conf[:MEASURE_CALLBACKS].each do |type_name, _, arg_val|
25
34
  puts "- #{type_name}" + (arg_val ? "(#{arg_val.inspect})" : '')
26
35
  end
27
36
  when :on
28
- IRB.conf[:MEASURE] = true
29
- added = IRB.set_measure_callback(type, arg)
37
+ added = IRB.set_measure_callback(arg)
30
38
  puts "#{added[0]} is added." if added
31
39
  else
32
- if block_given?
33
- IRB.conf[:MEASURE] = true
34
- added = IRB.set_measure_callback(&block)
35
- puts "#{added[0]} is added." if added
36
- else
37
- IRB.conf[:MEASURE] = true
38
- added = IRB.set_measure_callback(type, arg)
39
- puts "#{added[0]} is added." if added
40
- end
40
+ added = IRB.set_measure_callback(type, arg)
41
+ puts "#{added[0]} is added." if added
41
42
  end
42
43
  nil
43
44
  end
@@ -5,10 +5,10 @@ require_relative "debug"
5
5
  module IRB
6
6
  # :stopdoc:
7
7
 
8
- module ExtendCommand
8
+ module Command
9
9
  class Next < DebugCommand
10
- def execute(*args)
11
- super(do_cmds: ["next", *args].join(" "))
10
+ def execute(arg)
11
+ execute_debug_command(do_cmds: "next #{arg}")
12
12
  end
13
13
  end
14
14
  end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # change-ws.rb -
4
+ # by Keiju ISHITSUKA(keiju@ruby-lang.org)
5
+ #
6
+
7
+ require_relative "../ext/workspaces"
8
+
9
+ module IRB
10
+ # :stopdoc:
11
+
12
+ module Command
13
+ class Workspaces < Base
14
+ category "Workspace"
15
+ description "Show workspaces."
16
+
17
+ def execute(_arg)
18
+ inspection_resuls = irb_context.instance_variable_get(:@workspace_stack).map do |ws|
19
+ truncated_inspect(ws.main)
20
+ end
21
+
22
+ puts "[" + inspection_resuls.join(", ") + "]"
23
+ end
24
+
25
+ private
26
+
27
+ def truncated_inspect(obj)
28
+ obj_inspection = obj.inspect
29
+
30
+ if obj_inspection.size > 20
31
+ obj_inspection = obj_inspection[0, 19] + "...>"
32
+ end
33
+
34
+ obj_inspection
35
+ end
36
+ end
37
+
38
+ class PushWorkspace < Workspaces
39
+ category "Workspace"
40
+ description "Push an object to the workspace stack."
41
+
42
+ def execute(arg)
43
+ if arg.empty?
44
+ irb_context.push_workspace
45
+ else
46
+ obj = eval(arg, irb_context.workspace.binding)
47
+ irb_context.push_workspace(obj)
48
+ end
49
+ super
50
+ end
51
+ end
52
+
53
+ class PopWorkspace < Workspaces
54
+ category "Workspace"
55
+ description "Pop a workspace from the workspace stack."
56
+
57
+ def execute(_arg)
58
+ irb_context.pop_workspace
59
+ super
60
+ end
61
+ end
62
+ end
63
+
64
+ # :startdoc:
65
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IRB
4
+ module Command
5
+ class ShowDoc < Base
6
+ include RubyArgsExtractor
7
+
8
+ category "Context"
9
+ description "Look up documentation with RI."
10
+
11
+ help_message <<~HELP_MESSAGE
12
+ Usage: show_doc [name]
13
+
14
+ When name is provided, IRB will look up the documentation for the given name.
15
+ When no name is provided, a RI session will be started.
16
+
17
+ Examples:
18
+
19
+ show_doc
20
+ show_doc Array
21
+ show_doc Array#each
22
+
23
+ HELP_MESSAGE
24
+
25
+ def execute(arg)
26
+ # Accept string literal for backward compatibility
27
+ name = unwrap_string_literal(arg)
28
+ require 'rdoc/ri/driver'
29
+
30
+ unless ShowDoc.const_defined?(:Ri)
31
+ opts = RDoc::RI::Driver.process_args([])
32
+ ShowDoc.const_set(:Ri, RDoc::RI::Driver.new(opts))
33
+ end
34
+
35
+ if name.nil?
36
+ Ri.interactive
37
+ else
38
+ begin
39
+ Ri.display_name(name)
40
+ rescue RDoc::RI::Error
41
+ puts $!.message
42
+ end
43
+ end
44
+
45
+ nil
46
+ rescue LoadError, SystemExit
47
+ warn "Can't display document because `rdoc` is not installed."
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../source_finder"
4
+ require_relative "../pager"
5
+ require_relative "../color"
6
+
7
+ module IRB
8
+ module Command
9
+ class ShowSource < Base
10
+ include RubyArgsExtractor
11
+
12
+ category "Context"
13
+ description "Show the source code of a given method, class/module, or constant."
14
+
15
+ help_message <<~HELP_MESSAGE
16
+ Usage: show_source [target] [-s]
17
+
18
+ -s Show the super method. You can stack it like `-ss` to show the super of the super, etc.
19
+
20
+ Examples:
21
+
22
+ show_source Foo
23
+ show_source Foo#bar
24
+ show_source Foo#bar -s
25
+ show_source Foo.baz
26
+ show_source Foo::BAR
27
+ HELP_MESSAGE
28
+
29
+ def execute(arg)
30
+ # Accept string literal for backward compatibility
31
+ str = unwrap_string_literal(arg)
32
+ unless str.is_a?(String)
33
+ puts "Error: Expected a string but got #{str.inspect}"
34
+ return
35
+ end
36
+
37
+ str, esses = str.split(" -")
38
+ super_level = esses ? esses.count("s") : 0
39
+ source = SourceFinder.new(@irb_context).find_source(str, super_level)
40
+
41
+ if source
42
+ show_source(source)
43
+ elsif super_level > 0
44
+ puts "Error: Couldn't locate a super definition for #{str}"
45
+ else
46
+ puts "Error: Couldn't locate a definition for #{str}"
47
+ end
48
+ nil
49
+ end
50
+
51
+ private
52
+
53
+ def show_source(source)
54
+ if source.binary_file?
55
+ content = "\n#{bold('Defined in binary file')}: #{source.file}\n\n"
56
+ else
57
+ code = source.colorized_content || 'Source not available'
58
+ content = <<~CONTENT
59
+
60
+ #{bold("From")}: #{source.file}:#{source.line}
61
+
62
+ #{code.chomp}
63
+
64
+ CONTENT
65
+ end
66
+ Pager.page_content(content)
67
+ end
68
+
69
+ def bold(str)
70
+ Color.colorize(str, [:BOLD])
71
+ end
72
+ end
73
+ end
74
+ end
@@ -5,10 +5,10 @@ require_relative "debug"
5
5
  module IRB
6
6
  # :stopdoc:
7
7
 
8
- module ExtendCommand
8
+ module Command
9
9
  class Step < DebugCommand
10
- def execute(*args)
11
- super(do_cmds: ["step", *args].join(" "))
10
+ def execute(arg)
11
+ execute_debug_command(do_cmds: "step #{arg}")
12
12
  end
13
13
  end
14
14
  end
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # multi.rb -
4
+ # by Keiju ISHITSUKA(keiju@ruby-lang.org)
5
+ #
6
+
7
+ module IRB
8
+ # :stopdoc:
9
+
10
+ module Command
11
+ class MultiIRBCommand < Base
12
+ include RubyArgsExtractor
13
+
14
+ private
15
+
16
+ def print_deprecated_warning
17
+ warn <<~MSG
18
+ Multi-irb commands are deprecated and will be removed in IRB 2.0.0. Please use workspace commands instead.
19
+ If you have any use case for multi-irb, please leave a comment at https://github.com/ruby/irb/issues/653
20
+ MSG
21
+ end
22
+
23
+ def extend_irb_context
24
+ # this extension patches IRB context like IRB.CurrentContext
25
+ require_relative "../ext/multi-irb"
26
+ end
27
+
28
+ def print_debugger_warning
29
+ warn "Multi-IRB commands are not available when the debugger is enabled."
30
+ end
31
+ end
32
+
33
+ class IrbCommand < MultiIRBCommand
34
+ category "Multi-irb (DEPRECATED)"
35
+ description "Start a child IRB."
36
+
37
+ def execute(arg)
38
+ args, kwargs = ruby_args(arg)
39
+ execute_internal(*args, **kwargs)
40
+ end
41
+
42
+ def execute_internal(*obj)
43
+ print_deprecated_warning
44
+
45
+ if irb_context.with_debugger
46
+ print_debugger_warning
47
+ return
48
+ end
49
+
50
+ extend_irb_context
51
+ IRB.irb(nil, *obj)
52
+ puts IRB.JobManager.inspect
53
+ end
54
+ end
55
+
56
+ class Jobs < MultiIRBCommand
57
+ category "Multi-irb (DEPRECATED)"
58
+ description "List of current sessions."
59
+
60
+ def execute(_arg)
61
+ print_deprecated_warning
62
+
63
+ if irb_context.with_debugger
64
+ print_debugger_warning
65
+ return
66
+ end
67
+
68
+ extend_irb_context
69
+ puts IRB.JobManager.inspect
70
+ end
71
+ end
72
+
73
+ class Foreground < MultiIRBCommand
74
+ category "Multi-irb (DEPRECATED)"
75
+ description "Switches to the session of the given number."
76
+
77
+ def execute(arg)
78
+ args, kwargs = ruby_args(arg)
79
+ execute_internal(*args, **kwargs)
80
+ end
81
+
82
+ def execute_internal(key = nil)
83
+ print_deprecated_warning
84
+
85
+ if irb_context.with_debugger
86
+ print_debugger_warning
87
+ return
88
+ end
89
+
90
+ extend_irb_context
91
+
92
+ raise CommandArgumentError.new("Please specify the id of target IRB job (listed in the `jobs` command).") unless key
93
+ IRB.JobManager.switch(key)
94
+ puts IRB.JobManager.inspect
95
+ end
96
+ end
97
+
98
+ class Kill < MultiIRBCommand
99
+ category "Multi-irb (DEPRECATED)"
100
+ description "Kills the session with the given number."
101
+
102
+ def execute(arg)
103
+ args, kwargs = ruby_args(arg)
104
+ execute_internal(*args, **kwargs)
105
+ end
106
+
107
+ def execute_internal(*keys)
108
+ print_deprecated_warning
109
+
110
+ if irb_context.with_debugger
111
+ print_debugger_warning
112
+ return
113
+ end
114
+
115
+ extend_irb_context
116
+ IRB.JobManager.kill(*keys)
117
+ puts IRB.JobManager.inspect
118
+ end
119
+ end
120
+ end
121
+
122
+ # :startdoc:
123
+ end
@@ -1,16 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "nop"
4
-
5
3
  module IRB
6
4
  # :stopdoc:
7
5
 
8
- module ExtendCommand
9
- class Whereami < Nop
6
+ module Command
7
+ class Whereami < Base
10
8
  category "Context"
11
9
  description "Show the source code around binding.irb again."
12
10
 
13
- def execute(*)
11
+ def execute(_arg)
14
12
  code = irb_context.workspace.code_around_binding
15
13
  if code
16
14
  puts code
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # irb/command.rb - irb command
4
+ # by Keiju ISHITSUKA(keiju@ruby-lang.org)
5
+ #
6
+
7
+ require_relative "command/base"
8
+
9
+ module IRB # :nodoc:
10
+ module Command
11
+ @commands = {}
12
+
13
+ class << self
14
+ attr_reader :commands
15
+
16
+ # Registers a command with the given name.
17
+ # Aliasing is intentionally not supported at the moment.
18
+ def register(name, command_class)
19
+ @commands[name.to_sym] = [command_class, []]
20
+ end
21
+ end
22
+ end
23
+ end