diff options
Diffstat (limited to 'lib/irb/cmd')
-rw-r--r-- | lib/irb/cmd/backtrace.rb | 21 | ||||
-rw-r--r-- | lib/irb/cmd/break.rb | 21 | ||||
-rw-r--r-- | lib/irb/cmd/catch.rb | 21 | ||||
-rw-r--r-- | lib/irb/cmd/chws.rb | 36 | ||||
-rw-r--r-- | lib/irb/cmd/continue.rb | 17 | ||||
-rw-r--r-- | lib/irb/cmd/debug.rb | 136 | ||||
-rw-r--r-- | lib/irb/cmd/delete.rb | 17 | ||||
-rw-r--r-- | lib/irb/cmd/edit.rb | 61 | ||||
-rw-r--r-- | lib/irb/cmd/finish.rb | 17 | ||||
-rw-r--r-- | lib/irb/cmd/fork.rb | 34 | ||||
-rw-r--r-- | lib/irb/cmd/help.rb | 23 | ||||
-rw-r--r-- | lib/irb/cmd/info.rb | 21 | ||||
-rw-r--r-- | lib/irb/cmd/irb_info.rb | 37 | ||||
-rw-r--r-- | lib/irb/cmd/load.rb | 76 | ||||
-rw-r--r-- | lib/irb/cmd/ls.rb | 132 | ||||
-rw-r--r-- | lib/irb/cmd/measure.rb | 48 | ||||
-rw-r--r-- | lib/irb/cmd/next.rb | 17 | ||||
-rw-r--r-- | lib/irb/cmd/nop.rb | 59 | ||||
-rw-r--r-- | lib/irb/cmd/pushws.rb | 45 | ||||
-rw-r--r-- | lib/irb/cmd/show_cmds.rb | 39 | ||||
-rw-r--r-- | lib/irb/cmd/show_doc.rb | 48 | ||||
-rw-r--r-- | lib/irb/cmd/show_source.rb | 113 | ||||
-rw-r--r-- | lib/irb/cmd/step.rb | 17 | ||||
-rw-r--r-- | lib/irb/cmd/subirb.rb | 66 | ||||
-rw-r--r-- | lib/irb/cmd/whereami.rb | 25 |
25 files changed, 3 insertions, 1144 deletions
diff --git a/lib/irb/cmd/backtrace.rb b/lib/irb/cmd/backtrace.rb deleted file mode 100644 index f632894618..0000000000 --- a/lib/irb/cmd/backtrace.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -require_relative "debug" - -module IRB - # :stopdoc: - - module ExtendCommand - class Backtrace < DebugCommand - def self.transform_args(args) - args&.dump - end - - def execute(*args) - super(pre_cmds: ["backtrace", *args].join(" ")) - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/break.rb b/lib/irb/cmd/break.rb deleted file mode 100644 index df259a90ca..0000000000 --- a/lib/irb/cmd/break.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -require_relative "debug" - -module IRB - # :stopdoc: - - module ExtendCommand - class Break < DebugCommand - def self.transform_args(args) - args&.dump - end - - def execute(args = nil) - super(pre_cmds: "break #{args}") - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/catch.rb b/lib/irb/cmd/catch.rb deleted file mode 100644 index 40b62c7533..0000000000 --- a/lib/irb/cmd/catch.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -require_relative "debug" - -module IRB - # :stopdoc: - - module ExtendCommand - class Catch < DebugCommand - def self.transform_args(args) - args&.dump - end - - def execute(*args) - super(pre_cmds: ["catch", *args].join(" ")) - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/chws.rb b/lib/irb/cmd/chws.rb deleted file mode 100644 index 44712fa445..0000000000 --- a/lib/irb/cmd/chws.rb +++ /dev/null @@ -1,36 +0,0 @@ -# frozen_string_literal: false -# -# change-ws.rb - -# by Keiju ISHITSUKA(keiju@ruby-lang.org) -# - -require_relative "nop" -require_relative "../ext/change-ws" - -module IRB - # :stopdoc: - - module ExtendCommand - - class CurrentWorkingWorkspace < Nop - category "IRB" - description "Show the current workspace." - - def execute(*obj) - irb_context.main - end - end - - class ChangeWorkspace < Nop - category "IRB" - description "Change the current workspace to an object." - - def execute(*obj) - irb_context.change_workspace(*obj) - irb_context.main - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/continue.rb b/lib/irb/cmd/continue.rb deleted file mode 100644 index 9136177eef..0000000000 --- a/lib/irb/cmd/continue.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -require_relative "debug" - -module IRB - # :stopdoc: - - module ExtendCommand - class Continue < DebugCommand - def execute(*args) - super(do_cmds: ["continue", *args].join(" ")) - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/debug.rb b/lib/irb/cmd/debug.rb deleted file mode 100644 index 7d39b9fa27..0000000000 --- a/lib/irb/cmd/debug.rb +++ /dev/null @@ -1,136 +0,0 @@ -require_relative "nop" - -module IRB - # :stopdoc: - - module ExtendCommand - class Debug < Nop - category "Debugging" - description "Start the debugger of debug.gem." - - BINDING_IRB_FRAME_REGEXPS = [ - '<internal:prelude>', - binding.method(:irb).source_location.first, - ].map { |file| /\A#{Regexp.escape(file)}:\d+:in `irb'\z/ } - IRB_DIR = File.expand_path('..', __dir__) - - def execute(pre_cmds: nil, do_cmds: nil) - unless binding_irb? - puts "`debug` command is only available when IRB is started with binding.irb" - return - end - - unless setup_debugger - puts <<~MSG - You need to install the debug gem before using this command. - If you use `bundle exec`, please add `gem "debug"` into your Gemfile. - MSG - return - end - - options = { oneshot: true, hook_call: false } - if pre_cmds || do_cmds - options[:command] = ['irb', pre_cmds, do_cmds] - end - if DEBUGGER__::LineBreakpoint.instance_method(:initialize).parameters.include?([:key, :skip_src]) - options[:skip_src] = true - end - - # To make debugger commands like `next` or `continue` work without asking - # the user to quit IRB after that, we need to exit IRB first and then hit - # a TracePoint on #debug_break. - file, lineno = IRB::Irb.instance_method(:debug_break).source_location - DEBUGGER__::SESSION.add_line_breakpoint(file, lineno + 1, **options) - # exit current Irb#run call - throw :IRB_EXIT - end - - private - - def binding_irb? - caller.any? do |frame| - BINDING_IRB_FRAME_REGEXPS.any? do |regexp| - frame.match?(regexp) - end - end - end - - module SkipPathHelperForIRB - def skip_internal_path?(path) - # The latter can be removed once https://github.com/ruby/debug/issues/866 is resolved - super || path.match?(IRB_DIR) || path.match?('<internal:prelude>') - end - end - - def setup_debugger - unless defined?(DEBUGGER__::SESSION) - begin - require "debug/session" - rescue LoadError # debug.gem is not written in Gemfile - return false unless load_bundled_debug_gem - end - DEBUGGER__.start(nonstop: true) - end - - unless DEBUGGER__.respond_to?(:capture_frames_without_irb) - DEBUGGER__.singleton_class.send(:alias_method, :capture_frames_without_irb, :capture_frames) - - def DEBUGGER__.capture_frames(*args) - frames = capture_frames_without_irb(*args) - frames.reject! do |frame| - frame.realpath&.start_with?(IRB_DIR) || frame.path == "<internal:prelude>" - end - frames - end - - DEBUGGER__::ThreadClient.prepend(SkipPathHelperForIRB) - end - - true - end - - # This is used when debug.gem is not written in Gemfile. Even if it's not - # installed by `bundle install`, debug.gem is installed by default because - # it's a bundled gem. This method tries to activate and load that. - def load_bundled_debug_gem - # Discover latest debug.gem under GEM_PATH - debug_gem = Gem.paths.path.flat_map { |path| Dir.glob("#{path}/gems/debug-*") }.select do |path| - File.basename(path).match?(/\Adebug-\d+\.\d+\.\d+(\w+)?\z/) - end.sort_by do |path| - Gem::Version.new(File.basename(path).delete_prefix('debug-')) - end.last - return false unless debug_gem - - # Discover debug/debug.so under extensions for Ruby 3.2+ - ext_name = "/debug/debug.#{RbConfig::CONFIG['DLEXT']}" - ext_path = Gem.paths.path.flat_map do |path| - Dir.glob("#{path}/extensions/**/#{File.basename(debug_gem)}#{ext_name}") - end.first - - # Attempt to forcibly load the bundled gem - if ext_path - $LOAD_PATH << ext_path.delete_suffix(ext_name) - end - $LOAD_PATH << "#{debug_gem}/lib" - begin - require "debug/session" - puts "Loaded #{File.basename(debug_gem)}" - true - rescue LoadError - false - end - end - end - - class DebugCommand < Debug - def self.category - "Debugging" - end - - def self.description - command_name = self.name.split("::").last.downcase - "Start the debugger of debug.gem and run its `#{command_name}` command." - end - end - end -end diff --git a/lib/irb/cmd/delete.rb b/lib/irb/cmd/delete.rb deleted file mode 100644 index aeb26d2572..0000000000 --- a/lib/irb/cmd/delete.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -require_relative "debug" - -module IRB - # :stopdoc: - - module ExtendCommand - class Delete < DebugCommand - def execute(*args) - super(pre_cmds: ["delete", *args].join(" ")) - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/edit.rb b/lib/irb/cmd/edit.rb deleted file mode 100644 index 0103891cf4..0000000000 --- a/lib/irb/cmd/edit.rb +++ /dev/null @@ -1,61 +0,0 @@ -require 'shellwords' -require_relative "nop" - -module IRB - # :stopdoc: - - module ExtendCommand - class Edit < Nop - category "Misc" - description 'Open a file with the editor command defined with `ENV["EDITOR"]`.' - - class << self - def transform_args(args) - # Return a string literal as is for backward compatibility - if args.nil? || args.empty? || string_literal?(args) - args - else # Otherwise, consider the input as a String for convenience - args.strip.dump - end - end - end - - def execute(*args) - path = args.first - - if path.nil? && (irb_path = @irb_context.irb_path) - path = irb_path - end - - if !File.exist?(path) - require_relative "show_source" - - source = - begin - ShowSource.find_source(path, @irb_context) - rescue NameError - # if user enters a path that doesn't exist, it'll cause NameError when passed here because find_source would try to evaluate it as well - # in this case, we should just ignore the error - end - - if source && File.exist?(source.file) - path = source.file - else - puts "Can not find file: #{path}" - return - end - end - - if editor = ENV['EDITOR'] - puts "command: '#{editor}'" - puts " path: #{path}" - system(*Shellwords.split(editor), path) - else - puts "Can not find editor setting: ENV['EDITOR']" - end - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/finish.rb b/lib/irb/cmd/finish.rb deleted file mode 100644 index 29f100feb5..0000000000 --- a/lib/irb/cmd/finish.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -require_relative "debug" - -module IRB - # :stopdoc: - - module ExtendCommand - class Finish < DebugCommand - def execute(*args) - super(do_cmds: ["finish", *args].join(" ")) - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/fork.rb b/lib/irb/cmd/fork.rb deleted file mode 100644 index 1cd235997f..0000000000 --- a/lib/irb/cmd/fork.rb +++ /dev/null @@ -1,34 +0,0 @@ -# frozen_string_literal: false -# -# fork.rb - -# by Keiju ISHITSUKA(keiju@ruby-lang.org) -# - -require_relative "nop" - -module IRB - # :stopdoc: - - module ExtendCommand - class Fork < Nop - def execute - pid = __send__ ExtendCommand.irb_original_method_name("fork") - unless pid - class << self - alias_method :exit, ExtendCommand.irb_original_method_name('exit') - end - if block_given? - begin - yield - ensure - exit - end - end - end - pid - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/help.rb b/lib/irb/cmd/help.rb deleted file mode 100644 index 64b885c383..0000000000 --- a/lib/irb/cmd/help.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -require_relative "show_doc" - -module IRB - module ExtendCommand - class Help < ShowDoc - category "Context" - description "[DEPRECATED] Enter the mode to look up RI documents." - - DEPRECATION_MESSAGE = <<~MSG - [Deprecation] The `help` command will be repurposed to display command help in the future. - For RI document lookup, please use the `show_doc` command instead. - For command help, please use `show_cmds` for now. - MSG - - def execute(*names) - warn DEPRECATION_MESSAGE - super - end - end - end -end diff --git a/lib/irb/cmd/info.rb b/lib/irb/cmd/info.rb deleted file mode 100644 index 2c0a32b34f..0000000000 --- a/lib/irb/cmd/info.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -require_relative "debug" - -module IRB - # :stopdoc: - - module ExtendCommand - class Info < DebugCommand - def self.transform_args(args) - args&.dump - end - - def execute(*args) - super(pre_cmds: ["info", *args].join(" ")) - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/irb_info.rb b/lib/irb/cmd/irb_info.rb deleted file mode 100644 index da11e8d40b..0000000000 --- a/lib/irb/cmd/irb_info.rb +++ /dev/null @@ -1,37 +0,0 @@ -# frozen_string_literal: false - -require_relative "nop" - -module IRB - # :stopdoc: - - module ExtendCommand - class IrbInfo < Nop - category "IRB" - description "Show information about IRB." - - def execute - Class.new { - def inspect - str = "Ruby version: #{RUBY_VERSION}\n" - str += "IRB version: #{IRB.version}\n" - str += "InputMethod: #{IRB.CurrentContext.io.inspect}\n" - str += ".irbrc path: #{IRB.rc_file}\n" if File.exist?(IRB.rc_file) - str += "RUBY_PLATFORM: #{RUBY_PLATFORM}\n" - str += "LANG env: #{ENV["LANG"]}\n" if ENV["LANG"] && !ENV["LANG"].empty? - str += "LC_ALL env: #{ENV["LC_ALL"]}\n" if ENV["LC_ALL"] && !ENV["LC_ALL"].empty? - str += "East Asian Ambiguous Width: #{Reline.ambiguous_width.inspect}\n" - if RbConfig::CONFIG['host_os'] =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/ - codepage = `chcp`.b.sub(/.*: (\d+)\n/, '\1') - str += "Code page: #{codepage}\n" - end - str - end - alias_method :to_s, :inspect - }.new - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/load.rb b/lib/irb/cmd/load.rb deleted file mode 100644 index a3e797a7e0..0000000000 --- a/lib/irb/cmd/load.rb +++ /dev/null @@ -1,76 +0,0 @@ -# frozen_string_literal: false -# -# load.rb - -# by Keiju ISHITSUKA(keiju@ruby-lang.org) -# - -require_relative "nop" -require_relative "../ext/loader" - -module IRB - # :stopdoc: - - module ExtendCommand - class LoaderCommand < Nop - include IrbLoader - - def raise_cmd_argument_error - raise CommandArgumentError.new("Please specify the file name.") - end - end - - class Load < LoaderCommand - category "IRB" - description "Load a Ruby file." - - def execute(file_name = nil, priv = nil) - raise_cmd_argument_error unless file_name - irb_load(file_name, priv) - end - end - - class Require < LoaderCommand - category "IRB" - description "Require a Ruby file." - def execute(file_name = nil) - raise_cmd_argument_error unless file_name - - rex = Regexp.new("#{Regexp.quote(file_name)}(\.o|\.rb)?") - return false if $".find{|f| f =~ rex} - - case file_name - when /\.rb$/ - begin - if irb_load(file_name) - $".push file_name - return true - end - rescue LoadError - end - when /\.(so|o|sl)$/ - return ruby_require(file_name) - end - - begin - irb_load(f = file_name + ".rb") - $".push f - return true - rescue LoadError - return ruby_require(file_name) - end - end - end - - class Source < LoaderCommand - category "IRB" - description "Loads a given file in the current session." - - def execute(file_name = nil) - raise_cmd_argument_error unless file_name - - source_file(file_name) - end - end - end - # :startdoc: -end diff --git a/lib/irb/cmd/ls.rb b/lib/irb/cmd/ls.rb deleted file mode 100644 index 063a88d33f..0000000000 --- a/lib/irb/cmd/ls.rb +++ /dev/null @@ -1,132 +0,0 @@ -# frozen_string_literal: true - -require "reline" -require_relative "nop" -require_relative "../color" - -module IRB - # :stopdoc: - - module ExtendCommand - class Ls < Nop - category "Context" - description "Show methods, constants, and variables. `-g [query]` or `-G [query]` allows you to filter out the output." - - def self.transform_args(args) - if match = args&.match(/\A(?<args>.+\s|)(-g|-G)\s+(?<grep>[^\s]+)\s*\n\z/) - args = match[:args] - "#{args}#{',' unless args.chomp.empty?} grep: /#{match[:grep]}/" - else - args - end - end - - def execute(*arg, grep: nil) - o = Output.new(grep: grep) - - obj = arg.empty? ? irb_context.workspace.main : arg.first - locals = arg.empty? ? irb_context.workspace.binding.local_variables : [] - klass = (obj.class == Class || obj.class == Module ? obj : obj.class) - - o.dump("constants", obj.constants) if obj.respond_to?(:constants) - dump_methods(o, klass, obj) - o.dump("instance variables", obj.instance_variables) - o.dump("class variables", klass.class_variables) - o.dump("locals", locals) - nil - end - - def dump_methods(o, klass, obj) - singleton_class = begin obj.singleton_class; rescue TypeError; nil end - dumped_mods = Array.new - ancestors = klass.ancestors - ancestors = ancestors.reject { |c| c >= Object } if klass < Object - singleton_ancestors = (singleton_class&.ancestors || []).reject { |c| c >= Class } - - # singleton_class' ancestors should be at the front - maps = class_method_map(singleton_ancestors, dumped_mods) + class_method_map(ancestors, dumped_mods) - maps.each do |mod, methods| - name = mod == singleton_class ? "#{klass}.methods" : "#{mod}#methods" - o.dump(name, methods) - end - end - - def class_method_map(classes, dumped_mods) - dumped_methods = Array.new - classes.map do |mod| - next if dumped_mods.include? mod - - dumped_mods << mod - - methods = mod.public_instance_methods(false).select do |method| - if dumped_methods.include? method - false - else - dumped_methods << method - true - end - end - - [mod, methods] - end.compact - end - - class Output - MARGIN = " " - - def initialize(grep: nil) - @grep = grep - @line_width = screen_width - MARGIN.length # right padding - end - - def dump(name, strs) - strs = strs.grep(@grep) if @grep - strs = strs.sort - return if strs.empty? - - # Attempt a single line - print "#{Color.colorize(name, [:BOLD, :BLUE])}: " - if fits_on_line?(strs, cols: strs.size, offset: "#{name}: ".length) - puts strs.join(MARGIN) - return - end - puts - - # Dump with the largest # of columns that fits on a line - cols = strs.size - until fits_on_line?(strs, cols: cols, offset: MARGIN.length) || cols == 1 - cols -= 1 - end - widths = col_widths(strs, cols: cols) - strs.each_slice(cols) do |ss| - puts ss.map.with_index { |s, i| "#{MARGIN}%-#{widths[i]}s" % s }.join - end - end - - private - - def fits_on_line?(strs, cols:, offset: 0) - width = col_widths(strs, cols: cols).sum + MARGIN.length * (cols - 1) - width <= @line_width - offset - end - - def col_widths(strs, cols:) - cols.times.map do |col| - (col...strs.size).step(cols).map do |i| - strs[i].length - end.max - end - end - - def screen_width - Reline.get_screen_size.last - rescue Errno::EINVAL # in `winsize': Invalid argument - <STDIN> - 80 - end - end - private_constant :Output - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/measure.rb b/lib/irb/cmd/measure.rb deleted file mode 100644 index 9122e2dac9..0000000000 --- a/lib/irb/cmd/measure.rb +++ /dev/null @@ -1,48 +0,0 @@ -require_relative "nop" - -module IRB - # :stopdoc: - - module ExtendCommand - class Measure < Nop - category "Misc" - description "`measure` enables the mode to measure processing time. `measure :off` disables it." - - def initialize(*args) - super(*args) - end - - def execute(type = nil, arg = nil, &block) - # Please check IRB.init_config in lib/irb/init.rb that sets - # IRB.conf[:MEASURE_PROC] to register default "measure" methods, - # "measure :time" (abbreviated as "measure") and "measure :stackprof". - case type - when :off - IRB.conf[:MEASURE] = nil - IRB.unset_measure_callback(arg) - when :list - IRB.conf[:MEASURE_CALLBACKS].each do |type_name, _, arg_val| - puts "- #{type_name}" + (arg_val ? "(#{arg_val.inspect})" : '') - end - when :on - IRB.conf[:MEASURE] = true - added = IRB.set_measure_callback(type, arg) - puts "#{added[0]} is added." if added - else - if block_given? - IRB.conf[:MEASURE] = true - added = IRB.set_measure_callback(&block) - puts "#{added[0]} is added." if added - else - IRB.conf[:MEASURE] = true - added = IRB.set_measure_callback(type, arg) - puts "#{added[0]} is added." if added - end - end - nil - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/next.rb b/lib/irb/cmd/next.rb deleted file mode 100644 index d29c82e7fc..0000000000 --- a/lib/irb/cmd/next.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -require_relative "debug" - -module IRB - # :stopdoc: - - module ExtendCommand - class Next < DebugCommand - def execute(*args) - super(do_cmds: ["next", *args].join(" ")) - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/nop.rb b/lib/irb/cmd/nop.rb index fc6231f0a7..9d2e3c4d47 100644 --- a/lib/irb/cmd/nop.rb +++ b/lib/irb/cmd/nop.rb @@ -1,57 +1,4 @@ -# frozen_string_literal: false -# -# nop.rb - -# by Keiju ISHITSUKA(keiju@ruby-lang.org) -# +# frozen_string_literal: true -module IRB - # :stopdoc: - - module ExtendCommand - class CommandArgumentError < StandardError; end - - class Nop - class << self - def category(category = nil) - @category = category if category - @category - end - - def description(description = nil) - @description = description if description - @description - end - - private - - def string_literal?(args) - sexp = Ripper.sexp(args) - sexp && sexp.size == 2 && sexp.last&.first&.first == :string_literal - end - end - - def self.execute(conf, *opts, **kwargs, &block) - command = new(conf) - command.execute(*opts, **kwargs, &block) - rescue CommandArgumentError => e - puts e.message - end - - def initialize(conf) - @irb_context = conf - end - - attr_reader :irb_context - - def irb - @irb_context.irb - end - - def execute(*opts) - #nop - end - end - end - - # :startdoc: -end +# This file is just a placeholder for backward-compatibility. +# Please require 'irb' and inherit your command from `IRB::Command::Base` instead. diff --git a/lib/irb/cmd/pushws.rb b/lib/irb/cmd/pushws.rb deleted file mode 100644 index d64d822182..0000000000 --- a/lib/irb/cmd/pushws.rb +++ /dev/null @@ -1,45 +0,0 @@ -# frozen_string_literal: false -# -# change-ws.rb - -# by Keiju ISHITSUKA(keiju@ruby-lang.org) -# - -require_relative "nop" -require_relative "../ext/workspaces" - -module IRB - # :stopdoc: - - module ExtendCommand - class Workspaces < Nop - category "IRB" - description "Show workspaces." - - def execute(*obj) - irb_context.workspaces.collect{|ws| ws.main} - end - end - - class PushWorkspace < Workspaces - category "IRB" - description "Push an object to the workspace stack." - - def execute(*obj) - irb_context.push_workspace(*obj) - super - end - end - - class PopWorkspace < Workspaces - category "IRB" - description "Pop a workspace from the workspace stack." - - def execute(*obj) - irb_context.pop_workspace(*obj) - super - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/show_cmds.rb b/lib/irb/cmd/show_cmds.rb deleted file mode 100644 index acced27d48..0000000000 --- a/lib/irb/cmd/show_cmds.rb +++ /dev/null @@ -1,39 +0,0 @@ -# frozen_string_literal: true - -require "stringio" -require_relative "nop" - -module IRB - # :stopdoc: - - module ExtendCommand - class ShowCmds < Nop - category "IRB" - description "List all available commands and their description." - - def execute(*args) - commands_info = IRB::ExtendCommandBundle.all_commands_info - commands_grouped_by_categories = commands_info.group_by { |cmd| cmd[:category] } - longest_cmd_name_length = commands_info.map { |c| c[:display_name] }.max { |a, b| a.length <=> b.length }.length - - output = StringIO.new - - commands_grouped_by_categories.each do |category, cmds| - output.puts Color.colorize(category, [:BOLD]) - - cmds.each do |cmd| - output.puts " #{cmd[:display_name].to_s.ljust(longest_cmd_name_length)} #{cmd[:description]}" - end - - output.puts - end - - puts output.string - - nil - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/show_doc.rb b/lib/irb/cmd/show_doc.rb deleted file mode 100644 index 99dd9ab95a..0000000000 --- a/lib/irb/cmd/show_doc.rb +++ /dev/null @@ -1,48 +0,0 @@ -# frozen_string_literal: true - -require_relative "nop" - -module IRB - module ExtendCommand - class ShowDoc < Nop - class << self - def transform_args(args) - # Return a string literal as is for backward compatibility - if args.empty? || string_literal?(args) - args - else # Otherwise, consider the input as a String for convenience - args.strip.dump - end - end - end - - category "Context" - description "Enter the mode to look up RI documents." - - def execute(*names) - require 'rdoc/ri/driver' - - unless ShowDoc.const_defined?(:Ri) - opts = RDoc::RI::Driver.process_args([]) - ShowDoc.const_set(:Ri, RDoc::RI::Driver.new(opts)) - end - - if names.empty? - Ri.interactive - else - names.each do |name| - begin - Ri.display_name(name.to_s) - rescue RDoc::RI::Error - puts $!.message - end - end - end - - nil - rescue LoadError, SystemExit - warn "Can't display document because `rdoc` is not installed." - end - end - end -end diff --git a/lib/irb/cmd/show_source.rb b/lib/irb/cmd/show_source.rb deleted file mode 100644 index a74895b2dc..0000000000 --- a/lib/irb/cmd/show_source.rb +++ /dev/null @@ -1,113 +0,0 @@ -# frozen_string_literal: true - -require_relative "nop" -require_relative "../color" -require_relative "../ruby-lex" - -module IRB - # :stopdoc: - - module ExtendCommand - class ShowSource < Nop - category "Context" - description "Show the source code of a given method or constant." - - class << self - def transform_args(args) - # Return a string literal as is for backward compatibility - if args.empty? || string_literal?(args) - args - else # Otherwise, consider the input as a String for convenience - args.strip.dump - end - end - - def find_source(str, irb_context) - case str - when /\A[A-Z]\w*(::[A-Z]\w*)*\z/ # Const::Name - eval(str, irb_context.workspace.binding) # trigger autoload - base = irb_context.workspace.binding.receiver.yield_self { |r| r.is_a?(Module) ? r : Object } - file, line = base.const_source_location(str) - when /\A(?<owner>[A-Z]\w*(::[A-Z]\w*)*)#(?<method>[^ :.]+)\z/ # Class#method - owner = eval(Regexp.last_match[:owner], irb_context.workspace.binding) - method = Regexp.last_match[:method] - if owner.respond_to?(:instance_method) - methods = owner.instance_methods + owner.private_instance_methods - file, line = owner.instance_method(method).source_location if methods.include?(method.to_sym) - end - when /\A((?<receiver>.+)(\.|::))?(?<method>[^ :.]+)\z/ # method, receiver.method, receiver::method - receiver = eval(Regexp.last_match[:receiver] || 'self', irb_context.workspace.binding) - method = Regexp.last_match[:method] - file, line = receiver.method(method).source_location if receiver.respond_to?(method, true) - end - if file && line - Source.new(file: file, first_line: line, last_line: find_end(file, line, irb_context)) - end - end - - private - - def find_end(file, first_line, irb_context) - return first_line unless File.exist?(file) - lex = RubyLex.new(irb_context) - lines = File.read(file).lines[(first_line - 1)..-1] - tokens = RubyLex.ripper_lex_without_warning(lines.join) - prev_tokens = [] - - # chunk with line number - tokens.chunk { |tok| tok.pos[0] }.each do |lnum, chunk| - code = lines[0..lnum].join - prev_tokens.concat chunk - continue = lex.process_continue(prev_tokens) - code_block_open = lex.check_code_block(code, prev_tokens) - if !continue && !code_block_open - return first_line + lnum - end - end - first_line - end - end - - def execute(str = nil) - unless str.is_a?(String) - puts "Error: Expected a string but got #{str.inspect}" - return - end - - source = self.class.find_source(str, @irb_context) - if source && File.exist?(source.file) - show_source(source) - else - puts "Error: Couldn't locate a definition for #{str}" - end - nil - end - - private - - # @param [IRB::ExtendCommand::ShowSource::Source] source - def show_source(source) - puts - puts "#{bold("From")}: #{source.file}:#{source.first_line}" - puts - code = IRB::Color.colorize_code(File.read(source.file)) - puts code.lines[(source.first_line - 1)...source.last_line].join - puts - end - - def bold(str) - Color.colorize(str, [:BOLD]) - end - - Source = Struct.new( - :file, # @param [String] - file name - :first_line, # @param [String] - first line - :last_line, # @param [String] - last line - keyword_init: true, - ) - private_constant :Source - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/step.rb b/lib/irb/cmd/step.rb deleted file mode 100644 index 2bc74a9d79..0000000000 --- a/lib/irb/cmd/step.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -require_relative "debug" - -module IRB - # :stopdoc: - - module ExtendCommand - class Step < DebugCommand - def execute(*args) - super(do_cmds: ["step", *args].join(" ")) - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/subirb.rb b/lib/irb/cmd/subirb.rb deleted file mode 100644 index 5f3e02c988..0000000000 --- a/lib/irb/cmd/subirb.rb +++ /dev/null @@ -1,66 +0,0 @@ -# frozen_string_literal: false -# -# multi.rb - -# by Keiju ISHITSUKA(keiju@ruby-lang.org) -# - -require_relative "nop" - -module IRB - # :stopdoc: - - module ExtendCommand - class MultiIRBCommand < Nop - def initialize(conf) - super - extend_irb_context - end - - private - - def extend_irb_context - # this extension patches IRB context like IRB.CurrentContext - require_relative "../ext/multi-irb" - end - end - - class IrbCommand < MultiIRBCommand - category "IRB" - description "Start a child IRB." - - def execute(*obj) - IRB.irb(nil, *obj) - end - end - - class Jobs < MultiIRBCommand - category "IRB" - description "List of current sessions." - - def execute - IRB.JobManager - end - end - - class Foreground < MultiIRBCommand - category "IRB" - description "Switches to the session of the given number." - - def execute(key = nil) - raise CommandArgumentError.new("Please specify the id of target IRB job (listed in the `jobs` command).") unless key - IRB.JobManager.switch(key) - end - end - - class Kill < MultiIRBCommand - category "IRB" - description "Kills the session with the given number." - - def execute(*keys) - IRB.JobManager.kill(*keys) - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/whereami.rb b/lib/irb/cmd/whereami.rb deleted file mode 100644 index 8f56ba073d..0000000000 --- a/lib/irb/cmd/whereami.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -require_relative "nop" - -module IRB - # :stopdoc: - - module ExtendCommand - class Whereami < Nop - category "Context" - description "Show the source code around binding.irb again." - - def execute(*) - code = irb_context.workspace.code_around_binding - if code - puts code - else - puts "The current context doesn't have code." - end - end - end - end - - # :startdoc: -end |