irb 1.3.2 → 1.3.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 133288193d64ac38bec03ec49cafddf944ddd4a8b9e5599c531e901526ba2984
4
- data.tar.gz: 48c150830a57ba009cfbbf5841c0d100a4826c3e96ae077d0fc436753006ce5f
3
+ metadata.gz: cd13d45b58713bde6589b963fc6d2a93c166d340aaba132c9b7981ffe88062cb
4
+ data.tar.gz: 56d4e5c73fb369f36cc34bbda1462492a0db7b7393f9b3e15a4e1a54e2ad1ec7
5
5
  SHA512:
6
- metadata.gz: 8a7ee8228dc3e3a67905895b6dd846abe1161f31e2568d8a5cf7ee257684cb5a72a1d803302f304e22c54f168fc5de7de2fee67c925d26dce136c9744ccd2fb6
7
- data.tar.gz: 1226f533e0d64e3abf06c5d757a4e702f5f2788b6fac489bc72a171f38b8b210390f9bcda010e0ad1a3d2c523af9794682ae0da7c885354ad969b56ff2ec32d3
6
+ metadata.gz: cd6725cf6479552499418c3a93058ef9adb471f44fff49bd30d382e6c534dac1aa43086d002a93b06a856c2bffd5d5decdd2a79c1e9b3d31c344ecccf6cfc60f
7
+ data.tar.gz: 064efe186e582ca440903e98f7b2b5cfab38867c7f3812ffe6f2e8bd1bc3c01b64ca4828c9d38ba26c0b9d6c7ceba76f4e1974484527a9ba6cbea249970cb067
data/irb.gemspec CHANGED
@@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
32
32
  "lib/irb/cmd/chws.rb",
33
33
  "lib/irb/cmd/fork.rb",
34
34
  "lib/irb/cmd/help.rb",
35
+ "lib/irb/cmd/info.rb",
35
36
  "lib/irb/cmd/load.rb",
36
37
  "lib/irb/cmd/measure.rb",
37
38
  "lib/irb/cmd/nop.rb",
data/lib/irb.rb CHANGED
@@ -525,7 +525,7 @@ module IRB
525
525
  printf "Use \"exit\" to leave %s\n", @context.ap_name
526
526
  end
527
527
  else
528
- print "\n"
528
+ print "\n" if @context.prompting?
529
529
  end
530
530
  end
531
531
  l
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: false
2
+
3
+ require_relative "nop"
4
+
5
+ # :stopdoc:
6
+ module IRB
7
+ module ExtendCommand
8
+ class Info < Nop
9
+ def execute
10
+ Class.new {
11
+ def inspect
12
+ str = "Ruby version: #{RUBY_VERSION}\n"
13
+ str += "IRB version: #{IRB.version}\n"
14
+ str += "InputMethod: #{IRB.CurrentContext.io.inspect}\n"
15
+ str += ".irbrc path: #{IRB.rc_file}\n" if File.exist?(IRB.rc_file)
16
+ str
17
+ end
18
+ alias_method :to_s, :inspect
19
+ }.new
20
+ end
21
+ end
22
+ end
23
+ end
24
+ # :startdoc:
@@ -8,7 +8,7 @@ module IRB
8
8
  super(*args)
9
9
  end
10
10
 
11
- def execute(type = nil, arg = nil)
11
+ def execute(type = nil, arg = nil, &block)
12
12
  case type
13
13
  when :off
14
14
  IRB.conf[:MEASURE] = nil
@@ -22,9 +22,15 @@ module IRB
22
22
  added = IRB.set_measure_callback(type, arg)
23
23
  puts "#{added[0]} is added." if added
24
24
  else
25
- IRB.conf[:MEASURE] = true
26
- added = IRB.set_measure_callback(type, arg)
27
- puts "#{added[0]} is added." if added
25
+ if block_given?
26
+ IRB.conf[:MEASURE] = true
27
+ added = IRB.set_measure_callback(&block)
28
+ puts "#{added[0]} is added." if added
29
+ else
30
+ IRB.conf[:MEASURE] = true
31
+ added = IRB.set_measure_callback(type, arg)
32
+ puts "#{added[0]} is added." if added
33
+ end
28
34
  end
29
35
  nil
30
36
  end
data/lib/irb/cmd/nop.rb CHANGED
@@ -15,9 +15,9 @@ module IRB
15
15
  class Nop
16
16
 
17
17
 
18
- def self.execute(conf, *opts)
18
+ def self.execute(conf, *opts, &block)
19
19
  command = new(conf)
20
- command.execute(*opts)
20
+ command.execute(*opts, &block)
21
21
  end
22
22
 
23
23
  def initialize(conf)
@@ -4,11 +4,21 @@ require 'irb/color'
4
4
 
5
5
  module IRB
6
6
  class ColorPrinter < ::PP
7
- def self.pp(obj, out = $>, width = 79)
8
- q = ColorPrinter.new(out, width)
9
- q.guard_inspect_key {q.pp obj}
10
- q.flush
11
- out << "\n"
7
+ class << self
8
+ def pp(obj, out = $>, width = screen_width)
9
+ q = ColorPrinter.new(out, width)
10
+ q.guard_inspect_key {q.pp obj}
11
+ q.flush
12
+ out << "\n"
13
+ end
14
+
15
+ private
16
+
17
+ def screen_width
18
+ Reline.get_screen_size.last
19
+ rescue Errno::EINVAL # in `winsize': Invalid argument - <STDIN>
20
+ 79
21
+ end
12
22
  end
13
23
 
14
24
  def text(str, width = nil)
@@ -31,8 +31,31 @@ module IRB # :nodoc:
31
31
  load_file(path, priv)
32
32
  end
33
33
 
34
+ if File.respond_to?(:absolute_path?)
35
+ def absolute_path?(path)
36
+ File.absolute_path?(path)
37
+ end
38
+ else
39
+ separator =
40
+ if File::ALT_SEPARATOR
41
+ File::SEPARATOR
42
+ else
43
+ "[#{Regexp.quote(File::SEPARATOR + File::ALT_SEPARATOR)}]"
44
+ end
45
+ ABSOLUTE_PATH_PATTERN = # :nodoc:
46
+ case Dir.pwd
47
+ when /\A\w:/, /\A#{separator}{2}/
48
+ /\A(?:\w:|#{separator})#{separator}/
49
+ else
50
+ /\A#{separator}/
51
+ end
52
+ def absolute_path?(path)
53
+ ABSOLUTE_PATH_PATTERN =~ path
54
+ end
55
+ end
56
+
34
57
  def search_file_from_ruby_path(fn) # :nodoc:
35
- if /^#{Regexp.quote(File::Separator)}/ =~ fn
58
+ if absolute_path?(fn)
36
59
  return fn if File.exist?(fn)
37
60
  return nil
38
61
  end
@@ -50,16 +73,18 @@ module IRB # :nodoc:
50
73
  # See Irb#suspend_input_method for more information.
51
74
  def source_file(path)
52
75
  irb.suspend_name(path, File.basename(path)) do
53
- irb.suspend_input_method(FileInputMethod.new(path)) do
54
- |back_io|
55
- irb.signal_status(:IN_LOAD) do
56
- if back_io.kind_of?(FileInputMethod)
57
- irb.eval_input
58
- else
59
- begin
76
+ FileInputMethod.open(path) do |io|
77
+ irb.suspend_input_method(io) do
78
+ |back_io|
79
+ irb.signal_status(:IN_LOAD) do
80
+ if back_io.kind_of?(FileInputMethod)
60
81
  irb.eval_input
61
- rescue LoadAbort
62
- print "load abort!!\n"
82
+ else
83
+ begin
84
+ irb.eval_input
85
+ rescue LoadAbort
86
+ print "load abort!!\n"
87
+ end
63
88
  end
64
89
  end
65
90
  end
@@ -79,16 +104,18 @@ module IRB # :nodoc:
79
104
  ws = WorkSpace.new
80
105
  end
81
106
  irb.suspend_workspace(ws) do
82
- irb.suspend_input_method(FileInputMethod.new(path)) do
83
- |back_io|
84
- irb.signal_status(:IN_LOAD) do
85
- if back_io.kind_of?(FileInputMethod)
86
- irb.eval_input
87
- else
88
- begin
107
+ FileInputMethod.open(path) do |io|
108
+ irb.suspend_input_method(io) do
109
+ |back_io|
110
+ irb.signal_status(:IN_LOAD) do
111
+ if back_io.kind_of?(FileInputMethod)
89
112
  irb.eval_input
90
- rescue LoadAbort
91
- print "load abort!!\n"
113
+ else
114
+ begin
115
+ irb.eval_input
116
+ rescue LoadAbort
117
+ print "load abort!!\n"
118
+ end
92
119
  end
93
120
  end
94
121
  end
data/lib/irb/init.rb CHANGED
@@ -146,7 +146,7 @@ module IRB # :nodoc:
146
146
  @CONF[:AT_EXIT] = []
147
147
  end
148
148
 
149
- def IRB.set_measure_callback(type = nil, arg = nil)
149
+ def IRB.set_measure_callback(type = nil, arg = nil, &block)
150
150
  added = nil
151
151
  if type
152
152
  type_sym = type.upcase.to_sym
@@ -155,6 +155,16 @@ module IRB # :nodoc:
155
155
  end
156
156
  elsif IRB.conf[:MEASURE_PROC][:CUSTOM]
157
157
  added = [:CUSTOM, IRB.conf[:MEASURE_PROC][:CUSTOM], arg]
158
+ elsif block_given?
159
+ added = [:BLOCK, block, arg]
160
+ found = IRB.conf[:MEASURE_CALLBACKS].find{ |m| m[0] == added[0] && m[2] == added[2] }
161
+ if found
162
+ found[1] = block
163
+ return added
164
+ else
165
+ IRB.conf[:MEASURE_CALLBACKS] << added
166
+ return added
167
+ end
158
168
  else
159
169
  added = [:TIME, IRB.conf[:MEASURE_PROC][:TIME], arg]
160
170
  end
@@ -124,10 +124,22 @@ module IRB
124
124
 
125
125
  # Use a File for IO with irb, see InputMethod
126
126
  class FileInputMethod < InputMethod
127
+ class << self
128
+ def open(file, &block)
129
+ begin
130
+ io = new(file)
131
+ block.call(io)
132
+ ensure
133
+ io&.close
134
+ end
135
+ end
136
+ end
137
+
127
138
  # Creates a new input method object
128
139
  def initialize(file)
129
140
  super
130
141
  @io = IRB::MagicFile.open(file)
142
+ @external_encoding = @io.external_encoding
131
143
  end
132
144
  # The file name of this input method, usually given during initialization.
133
145
  attr_reader :file_name
@@ -137,7 +149,7 @@ module IRB
137
149
  #
138
150
  # See IO#eof? for more information.
139
151
  def eof?
140
- @io.eof?
152
+ @io.closed? || @io.eof?
141
153
  end
142
154
 
143
155
  # Reads the next line from this input method.
@@ -150,13 +162,17 @@ module IRB
150
162
 
151
163
  # The external encoding for standard input.
152
164
  def encoding
153
- @io.external_encoding
165
+ @external_encoding
154
166
  end
155
167
 
156
168
  # For debug message
157
169
  def inspect
158
170
  'FileInputMethod'
159
171
  end
172
+
173
+ def close
174
+ @io.close
175
+ end
160
176
  end
161
177
 
162
178
  begin
data/lib/irb/ruby-lex.rb CHANGED
@@ -223,7 +223,10 @@ class RubyLex
223
223
  throw :TERM_INPUT if @line == ''
224
224
  else
225
225
  @line_no += l.count("\n")
226
- next if l == "\n"
226
+ if l == "\n"
227
+ @exp_line_no += 1
228
+ next
229
+ end
227
230
  @line.concat l
228
231
  if @code_block_open or @ltype or @continue or @indent > 0
229
232
  next
@@ -233,7 +236,7 @@ class RubyLex
233
236
  @line.force_encoding(@io.encoding)
234
237
  yield @line, @exp_line_no
235
238
  end
236
- break if @io.eof?
239
+ raise TerminateLineInput if @io.eof?
237
240
  @line = ''
238
241
  @exp_line_no = @line_no
239
242
 
@@ -424,14 +427,30 @@ class RubyLex
424
427
  indent
425
428
  end
426
429
 
430
+ def is_method_calling?(tokens, index)
431
+ tk = tokens[index]
432
+ if tk[3].anybits?(Ripper::EXPR_CMDARG) and tk[1] == :on_ident
433
+ # The target method call to pass the block with "do".
434
+ return true
435
+ elsif tk[3].anybits?(Ripper::EXPR_ARG) and tk[1] == :on_ident
436
+ non_sp_index = tokens[0..(index - 1)].rindex{ |t| t[1] != :on_sp }
437
+ if non_sp_index
438
+ prev_tk = tokens[non_sp_index]
439
+ if prev_tk[3].anybits?(Ripper::EXPR_DOT) and prev_tk[1] == :on_period
440
+ # The target method call with receiver to pass the block with "do".
441
+ return true
442
+ end
443
+ end
444
+ end
445
+ false
446
+ end
447
+
427
448
  def take_corresponding_syntax_to_kw_do(tokens, index)
428
449
  syntax_of_do = nil
429
450
  # Finding a syntax correnponding to "do".
430
451
  index.downto(0) do |i|
431
452
  tk = tokens[i]
432
453
  # In "continue", the token isn't the corresponding syntax to "do".
433
- #is_continue = process_continue(@tokens[0..(i - 1)])
434
- # continue ではなく、直前に (:on_ignored_nl|:on_nl|:on_comment):on_sp* みたいなのがあるかどうかを調べる
435
454
  non_sp_index = tokens[0..(i - 1)].rindex{ |t| t[1] != :on_sp }
436
455
  first_in_fomula = false
437
456
  if non_sp_index.nil?
@@ -439,8 +458,7 @@ class RubyLex
439
458
  elsif [:on_ignored_nl, :on_nl, :on_comment].include?(tokens[non_sp_index][1])
440
459
  first_in_fomula = true
441
460
  end
442
- if tk[3].anybits?(Ripper::EXPR_CMDARG) and tk[1] == :on_ident
443
- # The target method call to pass the block with "do".
461
+ if is_method_calling?(tokens, i)
444
462
  syntax_of_do = :method_calling
445
463
  break if first_in_fomula
446
464
  elsif tk[1] == :on_kw && %w{while until for}.include?(tk[2])
@@ -458,6 +476,34 @@ class RubyLex
458
476
  syntax_of_do
459
477
  end
460
478
 
479
+ def is_the_in_correspond_to_a_for(tokens, index)
480
+ syntax_of_in = nil
481
+ # Finding a syntax correnponding to "do".
482
+ index.downto(0) do |i|
483
+ tk = tokens[i]
484
+ # In "continue", the token isn't the corresponding syntax to "do".
485
+ non_sp_index = tokens[0..(i - 1)].rindex{ |t| t[1] != :on_sp }
486
+ first_in_fomula = false
487
+ if non_sp_index.nil?
488
+ first_in_fomula = true
489
+ elsif [:on_ignored_nl, :on_nl, :on_comment].include?(tokens[non_sp_index][1])
490
+ first_in_fomula = true
491
+ end
492
+ if tk[1] == :on_kw && tk[2] == 'for'
493
+ # A loop syntax in front of "do" found.
494
+ #
495
+ # while cond do # also "until" or "for"
496
+ # end
497
+ #
498
+ # This "do" doesn't increment indent because the loop syntax already
499
+ # incremented.
500
+ syntax_of_in = :for
501
+ end
502
+ break if first_in_fomula
503
+ end
504
+ syntax_of_in
505
+ end
506
+
461
507
  def check_newline_depth_difference
462
508
  depth_difference = 0
463
509
  open_brace_on_line = 0
@@ -513,8 +559,12 @@ class RubyLex
513
559
  unless t[3].allbits?(Ripper::EXPR_LABEL)
514
560
  depth_difference += 1
515
561
  end
516
- when 'else', 'elsif', 'ensure', 'when', 'in'
562
+ when 'else', 'elsif', 'ensure', 'when'
517
563
  depth_difference += 1
564
+ when 'in'
565
+ unless is_the_in_correspond_to_a_for(@tokens, index)
566
+ depth_difference += 1
567
+ end
518
568
  when 'end'
519
569
  depth_difference -= 1
520
570
  end
data/lib/irb/version.rb CHANGED
@@ -11,7 +11,7 @@
11
11
  #
12
12
 
13
13
  module IRB # :nodoc:
14
- VERSION = "1.3.2"
14
+ VERSION = "1.3.3"
15
15
  @RELEASE_VERSION = VERSION
16
- @LAST_UPDATE_DATE = "2021-01-18"
16
+ @LAST_UPDATE_DATE = "2021-02-07"
17
17
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: irb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keiju ISHITSUKA
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-17 00:00:00.000000000 Z
11
+ date: 2021-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: reline
@@ -75,6 +75,7 @@ files:
75
75
  - lib/irb/cmd/chws.rb
76
76
  - lib/irb/cmd/fork.rb
77
77
  - lib/irb/cmd/help.rb
78
+ - lib/irb/cmd/info.rb
78
79
  - lib/irb/cmd/load.rb
79
80
  - lib/irb/cmd/measure.rb
80
81
  - lib/irb/cmd/nop.rb