ruby-changes:62984
From: aycabta <ko1@a...>
Date: Wed, 16 Sep 2020 21:24:14 +0900 (JST)
Subject: [ruby-changes:62984] 2159798f4c (ruby_2_7): Merge IRB 1.2.6
https://git.ruby-lang.org/ruby.git/commit/?id=2159798f4c From 2159798f4c0f71007db733cddd82b99186c8e424 Mon Sep 17 00:00:00 2001 From: aycabta <aycabta@g...> Date: Wed, 16 Sep 2020 11:19:17 +0900 Subject: Merge IRB 1.2.6 diff --git a/lib/irb.rb b/lib/irb.rb index ee6979c..d8e1209 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -10,18 +10,19 @@ https://github.com/ruby/ruby/blob/trunk/lib/irb.rb#L10 # # require "ripper" +require "reline" -require "irb/init" -require "irb/context" -require "irb/extend-command" +require_relative "irb/init" +require_relative "irb/context" +require_relative "irb/extend-command" -require "irb/ruby-lex" -require "irb/input-method" -require "irb/locale" -require "irb/color" +require_relative "irb/ruby-lex" +require_relative "irb/input-method" +require_relative "irb/locale" +require_relative "irb/color" -require "irb/version" -require "irb/easter-egg" +require_relative "irb/version" +require_relative "irb/easter-egg" # IRB stands for "interactive Ruby" and is a tool to interactively execute Ruby # expressions read from the standard input. @@ -271,7 +272,7 @@ require "irb/easter-egg" https://github.com/ruby/ruby/blob/trunk/lib/irb.rb#L272 # On the other hand, each conf in IRB@Command+line+options is used to # individually configure IRB.irb. # -# If a proc is set for IRB.conf[:IRB_RC], its will be invoked after execution +# If a proc is set for <code>IRB.conf[:IRB_RC]</code>, its will be invoked after execution # of that proc with the context of the current session as its argument. Each # session can be configured using this mechanism. # @@ -399,7 +400,7 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb.rb#L400 irb.run(@CONF) end - # Calls each event hook of IRB.conf[:AT_EXIT] when the current session quits. + # Calls each event hook of <code>IRB.conf[:TA_EXIT]</code> when the current session quits. def IRB.irb_at_exit @CONF[:AT_EXIT].each{|hook| hook.call} end @@ -538,7 +539,15 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb.rb#L539 begin line.untaint if RUBY_VERSION < '2.7' @context.evaluate(line, line_no, exception: exc) - output_value if @context.echo? && (@context.echo_on_assignment? || !assignment_expression?(line)) + if @context.echo? + if assignment_expression?(line) + if @context.echo_on_assignment? + output_value(@context.omit_on_assignment?) + end + else + output_value + end + end rescue Interrupt => exc rescue SystemExit, SignalException raise @@ -737,9 +746,32 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb.rb#L746 p end - def output_value # :nodoc: + def output_value(omit = false) # :nodoc: str = @context.inspect_last_value multiline_p = str.include?("\n") + if omit + winwidth = @context.io.winsize.last + if multiline_p + first_line = str.split("\n").first + result = @context.newline_before_multiline_output? ? (@context.return_format % first_line) : first_line + output_width = Reline::Unicode.calculate_width(result, true) + diff_size = output_width - Reline::Unicode.calculate_width(first_line, true) + if diff_size.positive? and output_width > winwidth + lines, _ = Reline::Unicode.split_by_width(first_line, winwidth - diff_size - 3) + str = "%s...\e[0m" % lines.first + multiline_p = false + else + str.gsub!(/(\A.*?\n).*/m, "\\1...") + end + else + output_width = Reline::Unicode.calculate_width(@context.return_format % str, true) + diff_size = output_width - Reline::Unicode.calculate_width(str, true) + if diff_size.positive? and output_width > winwidth + lines, _ = Reline::Unicode.split_by_width(str, winwidth - diff_size - 3) + str = "%s...\e[0m" % lines.first + end + end + end if multiline_p && @context.newline_before_multiline_output? printf @context.return_format, "\n#{str}" else diff --git a/lib/irb/cmd/fork.rb b/lib/irb/cmd/fork.rb index 31d53dc..19c78fc 100644 --- a/lib/irb/cmd/fork.rb +++ b/lib/irb/cmd/fork.rb @@ -35,5 +35,3 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/cmd/fork.rb#L35 end end # :startdoc: - - diff --git a/lib/irb/cmd/info.rb b/lib/irb/cmd/info.rb new file mode 100644 index 0000000..53ec71d --- /dev/null +++ b/lib/irb/cmd/info.rb @@ -0,0 +1,24 @@ https://github.com/ruby/ruby/blob/trunk/lib/irb/cmd/info.rb#L1 +# frozen_string_literal: false + +require_relative "nop" + +# :stopdoc: +module IRB + module ExtendCommand + class Info < 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 + end + alias_method :to_s, :inspect + }.new + end + end + end +end +# :startdoc: diff --git a/lib/irb/cmd/pushws.rb b/lib/irb/cmd/pushws.rb index 187b276..612157d 100644 --- a/lib/irb/cmd/pushws.rb +++ b/lib/irb/cmd/pushws.rb @@ -38,4 +38,3 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/cmd/pushws.rb#L38 end end # :startdoc: - diff --git a/lib/irb/color.rb b/lib/irb/color.rb index d2b9674..0f49291 100644 --- a/lib/irb/color.rb +++ b/lib/irb/color.rb @@ -1,6 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L1 # frozen_string_literal: true require 'reline' require 'ripper' +require 'irb/ruby-lex' module IRB # :nodoc: module Color @@ -145,37 +146,38 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L146 seen.delete(obj) end - # Ripper::Lexer::Elem#state is supported on Ruby 2.5+ def supported? return @supported if defined?(@supported) - @supported = Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.5.0') + @supported = Ripper::Lexer::Elem.method_defined?(:state) end def scan(code, allow_last_error:) pos = [1, 0] verbose, $VERBOSE = $VERBOSE, nil - lexer = Ripper::Lexer.new(code) - if lexer.respond_to?(:scan) # Ruby 2.7+ - lexer.scan.each do |elem| - str = elem.tok - next if allow_last_error and /meets end of file|unexpected end-of-input/ =~ elem.message - next if ([elem.pos[0], elem.pos[1] + str.bytesize] <=> pos) <= 0 - - str.each_line do |line| - if line.end_with?("\n") - pos[0] += 1 - pos[1] = 0 - else - pos[1] += line.bytesize + RubyLex.compile_with_errors_suppressed(code) do |inner_code, line_no| + lexer = Ripper::Lexer.new(inner_code, '(ripper)', line_no) + if lexer.respond_to?(:scan) # Ruby 2.7+ + lexer.scan.each do |elem| + str = elem.tok + next if allow_last_error and /meets end of file|unexpected end-of-input/ =~ elem.message + next if ([elem.pos[0], elem.pos[1] + str.bytesize] <=> pos) <= 0 + + str.each_line do |line| + if line.end_with?("\n") + pos[0] += 1 + pos[1] = 0 + else + pos[1] += line.bytesize + end end - end - yield(elem.event, str, elem.state) - end - else - lexer.parse.each do |elem| - yield(elem.event, elem.tok, elem.state) + yield(elem.event, str, elem.state) + end + else + lexer.parse.each do |elem| + yield(elem.event, elem.tok, elem.state) + end end end $VERBOSE = verbose diff --git a/lib/irb/completion.rb b/lib/irb/completion.rb index c44aa90..c9328e5 100644 --- a/lib/irb/completion.rb +++ b/lib/irb/completion.rb @@ -7,7 +7,6 @@ https://github.com/ruby/ruby/blob/trunk/lib/irb/completion.rb#L7 # From Original Idea of shugo@r... # -require "readline" autoload :RDoc, "rdoc" module IRB @@ -97,17 +96,13 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/completion.rb#L96 when /^(:[^:.]*)$/ # Symbol return nil if doc_namespace - if Symbol.respond_to?(:all_symbols) - sym = $1 - candidates = Symbol.all_symbols.collect do |s| - ":" + s.id2name.encode(Encoding.default_external) - rescue Encoding::UndefinedConversionError - # ignore - end - candidates.grep(/^#{Regexp.quote(sym)}/) - else - [] + sym = $1 + candidates = Symbol.all_symbols.collect do |s| + ":" + s.id2name.encode(Encoding.default_external) + rescue Encoding::UndefinedConversionError + # ignore end + candidates.grep(/^#{Regexp.quote(sym)}/) when /^::([A-Z][^:\.\(]*)$/ # Absolute Constant or class methods diff --git a/lib/irb/context.rb b/lib/irb/context.rb index 218f7c6..4f00172 100644 --- a/lib/irb/context.rb +++ b/lib/irb/context.rb @@ -131,7 +131,12 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/context.rb#L131 @echo_on_assignment = IRB.conf[:ECHO_ON_ASSIGNMENT] if @echo_on_assignment.nil? - @echo_on_assignment = false + @echo_on_assignment = true + end + + @omit_on_assignment = IRB.conf[:OMIT_ON_ASSIGNMENT] + if @omit_on_assignment.nil? + @omit_on_assignment = true end @newline_before_multiline_output = IRB.conf[:NEWLINE_BEFORE_MULTILINE_OUTPUT] @@ -240,7 +245,7 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/ (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/