diff options
| author | Stan Lo <stan001212@gmail.com> | 2024-02-13 13:33:33 +0000 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2024-02-13 13:33:36 +0000 |
| commit | ec26786b1a37350c0ff21666b028ec5e2aa6a318 (patch) | |
| tree | 74898ff938d7736be88fc25ded1d1fe7f3bd1896 /lib | |
| parent | b5327647c2a49c36621631a06527837e99b009e5 (diff) | |
[ruby/irb] Refactor eval_path and `SourceFinder::Source`
(https://github.com/ruby/irb/pull/870)
* Assign `@eval_path` through `irb_path=` method
This simplifies the original caching logic for the `eval_path` method
and makes it easier to understand.
* Refactor SourceFinder::Source
https://github.com/ruby/irb/commit/c63e4c4035
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/irb/context.rb | 42 | ||||
| -rw-r--r-- | lib/irb/ext/loader.rb | 6 | ||||
| -rw-r--r-- | lib/irb/source_finder.rb | 13 |
3 files changed, 37 insertions, 24 deletions
diff --git a/lib/irb/context.rb b/lib/irb/context.rb index 814a8bd4ad..e50958978f 100644 --- a/lib/irb/context.rb +++ b/lib/irb/context.rb @@ -77,7 +77,7 @@ module IRB else @irb_name = IRB.conf[:IRB_NAME]+"#"+IRB.JobManager.n_jobs.to_s end - @irb_path = "(" + @irb_name + ")" + self.irb_path = "(" + @irb_name + ")" case input_method when nil @@ -121,11 +121,11 @@ module IRB when '-' @io = FileInputMethod.new($stdin) @irb_name = '-' - @irb_path = '-' + self.irb_path = '-' when String @io = FileInputMethod.new(input_method) @irb_name = File.basename(input_method) - @irb_path = input_method + self.irb_path = input_method else @io = input_method end @@ -246,9 +246,27 @@ module IRB # Can be either name from <code>IRB.conf[:IRB_NAME]</code>, or the number of # the current job set by JobManager, such as <code>irb#2</code> attr_accessor :irb_name - # Can be either the #irb_name surrounded by parenthesis, or the - # +input_method+ passed to Context.new - attr_accessor :irb_path + + # Can be one of the following: + # - the #irb_name surrounded by parenthesis + # - the +input_method+ passed to Context.new + # - the file path of the current IRB context in a binding.irb session + attr_reader :irb_path + + # Sets @irb_path to the given +path+ as well as @eval_path + # @eval_path is used for evaluating code in the context of IRB session + # It's the same as irb_path, but with the IRB name postfix + # This makes sure users can distinguish the methods defined in the IRB session + # from the methods defined in the current file's context, especially with binding.irb + def irb_path=(path) + @irb_path = path + + if File.exist?(path) + @eval_path = "#{path}(#{IRB.conf[:IRB_NAME]})" + else + @eval_path = path + end + end # Whether multiline editor mode is enabled or not. # @@ -557,7 +575,7 @@ module IRB if IRB.conf[:MEASURE] && !IRB.conf[:MEASURE_CALLBACKS].empty? last_proc = proc do - result = @workspace.evaluate(line, eval_path, line_no) + result = @workspace.evaluate(line, @eval_path, line_no) end IRB.conf[:MEASURE_CALLBACKS].inject(last_proc) do |chain, item| _name, callback, arg = item @@ -568,20 +586,12 @@ module IRB end end.call else - result = @workspace.evaluate(line, eval_path, line_no) + result = @workspace.evaluate(line, @eval_path, line_no) end set_last_value(result) end - private def eval_path - # We need to use differente path to distinguish source_location of method defined in the actual file and method defined in irb session. - if !defined?(@irb_path_existence) || @irb_path_existence[0] != irb_path - @irb_path_existence = [irb_path, File.exist?(irb_path)] - end - @irb_path_existence[1] ? "#{irb_path}(#{IRB.conf[:IRB_NAME]})" : irb_path - end - def inspect_last_value # :nodoc: @inspect_method.inspect_value(@last_value) end diff --git a/lib/irb/ext/loader.rb b/lib/irb/ext/loader.rb index d65695df3b..5bd25546fa 100644 --- a/lib/irb/ext/loader.rb +++ b/lib/irb/ext/loader.rb @@ -98,13 +98,13 @@ module IRB # :nodoc: def old # :nodoc: back_io = @io - back_path = @irb_path + back_path = irb_path back_name = @irb_name back_scanner = @irb.scanner begin @io = FileInputMethod.new(path) @irb_name = File.basename(path) - @irb_path = path + self.irb_path = path @irb.signal_status(:IN_LOAD) do if back_io.kind_of?(FileInputMethod) @irb.eval_input @@ -119,7 +119,7 @@ module IRB # :nodoc: ensure @io = back_io @irb_name = back_name - @irb_path = back_path + self.irb_path = back_path @irb.scanner = back_scanner end end diff --git a/lib/irb/source_finder.rb b/lib/irb/source_finder.rb index 26aae7643b..07e864dad5 100644 --- a/lib/irb/source_finder.rb +++ b/lib/irb/source_finder.rb @@ -27,7 +27,7 @@ module IRB def colorized_content if !binary_file? && file_exist? - end_line = Source.find_end(file_content, @line) + end_line = find_end # To correctly colorize, we need to colorize full content and extract the relevant lines. colored = IRB::Color.colorize_code(file_content) colored.lines[@line - 1...end_line].join @@ -36,9 +36,12 @@ module IRB end end - def self.find_end(code, first_line) + private + + def find_end lex = RubyLex.new - lines = code.lines[(first_line - 1)..-1] + code = file_content + lines = code.lines[(@line - 1)..-1] tokens = RubyLex.ripper_lex_without_warning(lines.join) prev_tokens = [] @@ -49,10 +52,10 @@ module IRB continue = lex.should_continue?(prev_tokens) syntax = lex.check_code_syntax(code, local_variables: []) if !continue && syntax == :valid - return first_line + lnum + return @line + lnum end end - first_line + @line end end |
