summaryrefslogtreecommitdiff
path: root/lib/irb/cmd/show_source.rb
diff options
context:
space:
mode:
authorStan Lo <[email protected]>2023-01-10 20:43:33 +0000
committergit <[email protected]>2023-01-14 09:19:09 +0000
commitcb9b885e78bb87195d483df1afedf58d0bb81e41 (patch)
tree1ba732ebb576213869f099b5bcb1b1d33620de16 /lib/irb/cmd/show_source.rb
parent2082ba7c69c1d38508bfa549df3f2980cf8d066d (diff)
[ruby/irb] Store context in RubyLex
Some background for this refactor: 1. Through a RubyLex instance's lifetime, the context passed to its methods should be the same. Given that `Context` is only initialised in `Irb#initialize`, this should be true. 2. When `RubyLex` is initialised, the context object should be accessible. This is also true in all 3 of `RubyLex.new`'s invocations. With the above observations, we should be able to store the context in `RubyLex` as an instance variable. And doing so will make `RubyLex`'s instance methods easier to use and maintain. https://github.com/ruby/irb/commit/5c8d3df2df
Diffstat (limited to 'lib/irb/cmd/show_source.rb')
-rw-r--r--lib/irb/cmd/show_source.rb6
1 files changed, 3 insertions, 3 deletions
diff --git a/lib/irb/cmd/show_source.rb b/lib/irb/cmd/show_source.rb
index ea700be4bf..eb21533b56 100644
--- a/lib/irb/cmd/show_source.rb
+++ b/lib/irb/cmd/show_source.rb
@@ -40,15 +40,15 @@ module IRB
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))
+ Source.new(file: file, first_line: line, last_line: find_end(file, line, irb_context))
end
end
private
- def find_end(file, first_line)
+ def find_end(file, first_line, irb_context)
return first_line unless File.exist?(file)
- lex = RubyLex.new
+ lex = RubyLex.new(irb_context)
lines = File.read(file).lines[(first_line - 1)..-1]
tokens = RubyLex.ripper_lex_without_warning(lines.join)
prev_tokens = []