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 | 112 | ||||
| -rw-r--r-- | lib/irb/cmd/delete.rb | 17 | ||||
| -rw-r--r-- | lib/irb/cmd/edit.rb | 65 | ||||
| -rw-r--r-- | lib/irb/cmd/finish.rb | 17 | ||||
| -rw-r--r-- | lib/irb/cmd/fork.rb | 40 | ||||
| -rw-r--r-- | lib/irb/cmd/help.rb | 49 | ||||
| -rw-r--r-- | lib/irb/cmd/info.rb | 21 | ||||
| -rw-r--r-- | lib/irb/cmd/irb_info.rb | 34 | ||||
| -rw-r--r-- | lib/irb/cmd/load.rb | 68 | ||||
| -rw-r--r-- | lib/irb/cmd/ls.rb | 113 | ||||
| -rw-r--r-- | lib/irb/cmd/measure.rb | 45 | ||||
| -rw-r--r-- | lib/irb/cmd/next.rb | 17 | ||||
| -rw-r--r-- | lib/irb/cmd/nop.rb | 47 | ||||
| -rw-r--r-- | lib/irb/cmd/pushws.rb | 42 | ||||
| -rw-r--r-- | lib/irb/cmd/show_source.rb | 114 | ||||
| -rw-r--r-- | lib/irb/cmd/step.rb | 18 | ||||
| -rw-r--r-- | lib/irb/cmd/subirb.rb | 45 | ||||
| -rw-r--r-- | lib/irb/cmd/whereami.rb | 22 |
23 files changed, 0 insertions, 1002 deletions
diff --git a/lib/irb/cmd/backtrace.rb b/lib/irb/cmd/backtrace.rb deleted file mode 100644 index ac4f0e0e7e..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 < Debug - 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 2c82413f6a..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 < Debug - 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 8c9e086a9c..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 < Debug - 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 b28c090686..0000000000 --- a/lib/irb/cmd/chws.rb +++ /dev/null @@ -1,36 +0,0 @@ -# frozen_string_literal: false -# -# change-ws.rb - -# $Release Version: 0.9.6$ -# $Revision$ -# by Keiju ISHITSUKA(keiju@ruby-lang.org) -# -# -- -# -# -# - -require_relative "nop" -require_relative "../ext/change-ws" - -module IRB - # :stopdoc: - - module ExtendCommand - - class CurrentWorkingWorkspace < Nop - def execute(*obj) - irb_context.main - end - end - - class ChangeWorkspace < Nop - 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 94696e4b6c..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 < Debug - 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 2c09c99cf0..0000000000 --- a/lib/irb/cmd/debug.rb +++ /dev/null @@ -1,112 +0,0 @@ -require_relative "nop" - -module IRB - # :stopdoc: - - module ExtendCommand - class Debug < Nop - 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 - - 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 - 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+ - debug_so = Gem.paths.path.flat_map do |path| - Dir.glob("#{path}/extensions/**/#{File.basename(debug_gem)}/debug/debug.so") - end.first - - # Attempt to forcibly load the bundled gem - if debug_so - $LOAD_PATH << debug_so.delete_suffix('/debug/debug.so') - end - $LOAD_PATH << "#{debug_gem}/lib" - begin - require "debug/session" - puts "Loaded #{File.basename(debug_gem)}" - true - rescue LoadError - false - end - end - end - end -end diff --git a/lib/irb/cmd/delete.rb b/lib/irb/cmd/delete.rb deleted file mode 100644 index 3810ae414e..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 < Debug - 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 8d3fab3273..0000000000 --- a/lib/irb/cmd/edit.rb +++ /dev/null @@ -1,65 +0,0 @@ -require 'shellwords' -require_relative "nop" - -module IRB - # :stopdoc: - - module ExtendCommand - class Edit < Nop - 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 - - private - - def string_literal?(args) - sexp = Ripper.sexp(args) - sexp && sexp.size == 2 && sexp.last&.first&.first == :string_literal - 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 de4b4f12cf..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 < Debug - 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 255a670dce..0000000000 --- a/lib/irb/cmd/fork.rb +++ /dev/null @@ -1,40 +0,0 @@ -# frozen_string_literal: false -# -# fork.rb - -# $Release Version: 0.9.6 $ -# $Revision$ -# 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 0497c57457..0000000000 --- a/lib/irb/cmd/help.rb +++ /dev/null @@ -1,49 +0,0 @@ -# frozen_string_literal: false -# -# help.rb - helper using ri -# $Release Version: 0.9.6$ -# $Revision$ -# -# -- -# -# -# - -require_relative "nop" - -module IRB - # :stopdoc: - - module ExtendCommand - class Help < Nop - def execute(*names) - require 'rdoc/ri/driver' - opts = RDoc::RI::Driver.process_args([]) - IRB::ExtendCommand::Help.const_set(:Ri, RDoc::RI::Driver.new(opts)) - rescue LoadError, SystemExit - IRB::ExtendCommand::Help.remove_method(:execute) - # raise NoMethodError in ensure - else - def execute(*names) - if names.empty? - Ri.interactive - return - end - names.each do |name| - begin - Ri.display_name(name.to_s) - rescue RDoc::RI::Error - puts $!.message - end - end - nil - end - nil - ensure - execute(*names) - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/info.rb b/lib/irb/cmd/info.rb deleted file mode 100644 index 413c286429..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 < Debug - 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 8a4e1bd603..0000000000 --- a/lib/irb/cmd/irb_info.rb +++ /dev/null @@ -1,34 +0,0 @@ -# frozen_string_literal: false - -require_relative "nop" - -module IRB - # :stopdoc: - - module ExtendCommand - class IrbInfo < Nop - 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 2c5c01e89c..0000000000 --- a/lib/irb/cmd/load.rb +++ /dev/null @@ -1,68 +0,0 @@ -# frozen_string_literal: false -# -# load.rb - -# $Release Version: 0.9.6$ -# $Revision$ -# by Keiju ISHITSUKA(keiju@ruby-lang.org) -# -# -- -# -# -# - -require_relative "nop" -require_relative "../ext/loader" - -module IRB - # :stopdoc: - - module ExtendCommand - class Load < Nop - include IrbLoader - - def execute(file_name, priv = nil) - return irb_load(file_name, priv) - end - end - - class Require < Nop - include IrbLoader - - def execute(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 < Nop - include IrbLoader - def execute(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 735bf17241..0000000000 --- a/lib/irb/cmd/ls.rb +++ /dev/null @@ -1,113 +0,0 @@ -# frozen_string_literal: true - -require "reline" -require_relative "nop" -require_relative "../color" - -module IRB - # :stopdoc: - - module ExtendCommand - class Ls < Nop - 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 - maps = class_method_map((singleton_class || klass).ancestors) - 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 = Array.new - classes.reject { |mod| mod >= Object }.map do |mod| - methods = mod.public_instance_methods(false).select do |m| - dumped.push(m) unless dumped.include?(m) - end - [mod, methods] - end.reverse - 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 a97baee9f1..0000000000 --- a/lib/irb/cmd/measure.rb +++ /dev/null @@ -1,45 +0,0 @@ -require_relative "nop" - -module IRB - # :stopdoc: - - module ExtendCommand - class Measure < Nop - 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 2943a753fb..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 < Debug - 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 deleted file mode 100644 index 881a736722..0000000000 --- a/lib/irb/cmd/nop.rb +++ /dev/null @@ -1,47 +0,0 @@ -# frozen_string_literal: false -# -# nop.rb - -# $Release Version: 0.9.6$ -# $Revision$ -# by Keiju ISHITSUKA(keiju@ruby-lang.org) -# -# -- -# -# -# -module IRB - # :stopdoc: - - module ExtendCommand - class Nop - - if RUBY_ENGINE == "ruby" && RUBY_VERSION >= "2.7.0" - def self.execute(conf, *opts, **kwargs, &block) - command = new(conf) - command.execute(*opts, **kwargs, &block) - end - else - def self.execute(conf, *opts, &block) - command = new(conf) - command.execute(*opts, &block) - end - 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 diff --git a/lib/irb/cmd/pushws.rb b/lib/irb/cmd/pushws.rb deleted file mode 100644 index 791d8f8dbb..0000000000 --- a/lib/irb/cmd/pushws.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: false -# -# change-ws.rb - -# $Release Version: 0.9.6$ -# $Revision$ -# by Keiju ISHITSUKA(keiju@ruby-lang.org) -# -# -- -# -# -# - -require_relative "nop" -require_relative "../ext/workspaces" - -module IRB - # :stopdoc: - - module ExtendCommand - class Workspaces < Nop - def execute(*obj) - irb_context.workspaces.collect{|ws| ws.main} - end - end - - class PushWorkspace < Workspaces - def execute(*obj) - irb_context.push_workspace(*obj) - super - end - end - - class PopWorkspace < Workspaces - def execute(*obj) - irb_context.pop_workspace(*obj) - super - end - end - end - - # :startdoc: -end diff --git a/lib/irb/cmd/show_source.rb b/lib/irb/cmd/show_source.rb deleted file mode 100644 index 03c21b78c7..0000000000 --- a/lib/irb/cmd/show_source.rb +++ /dev/null @@ -1,114 +0,0 @@ -# frozen_string_literal: true - -require_relative "nop" -require_relative "../color" -require_relative "../ruby-lex" - -module IRB - # :stopdoc: - - module ExtendCommand - class ShowSource < 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 - - 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) if base.respond_to?(:const_source_location) # Ruby 2.7+ - 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) && owner.instance_methods.include?(method.to_sym) - file, line = owner.instance_method(method).source_location - 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) - end - if file && line - Source.new(file: file, first_line: line, last_line: find_end(file, line)) - end - end - - private - - def find_end(file, first_line) - return first_line unless File.exist?(file) - lex = RubyLex.new - 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 - - def string_literal?(args) - sexp = Ripper.sexp(args) - sexp && sexp.size == 2 && sexp.last&.first&.first == :string_literal - 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 dbd59806f8..0000000000 --- a/lib/irb/cmd/step.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -require_relative "debug" - -module IRB - # :stopdoc: - - module ExtendCommand - class Step < Debug - def execute(*args) - # Run `next` first to move out of binding.irb - super(pre_cmds: "next", 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 b322aadc53..0000000000 --- a/lib/irb/cmd/subirb.rb +++ /dev/null @@ -1,45 +0,0 @@ -# frozen_string_literal: false -# multi.rb - -# $Release Version: 0.9.6$ -# $Revision$ -# by Keiju ISHITSUKA(keiju@ruby-lang.org) -# -# -- -# -# -# - -require_relative "nop" -require_relative "../ext/multi-irb" - -module IRB - # :stopdoc: - - module ExtendCommand - class IrbCommand < Nop - def execute(*obj) - IRB.irb(nil, *obj) - end - end - - class Jobs < Nop - def execute - IRB.JobManager - end - end - - class Foreground < Nop - def execute(key) - IRB.JobManager.switch(key) - end - end - - class Kill < Nop - 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 b8c7e047fa..0000000000 --- a/lib/irb/cmd/whereami.rb +++ /dev/null @@ -1,22 +0,0 @@ -# frozen_string_literal: true - -require_relative "nop" - -module IRB - # :stopdoc: - - module ExtendCommand - class Whereami < Nop - 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 |
