diff options
author | tomoya ishida <tomoyapenguin@gmail.com> | 2024-02-13 03:38:27 +0900 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2024-02-12 18:38:30 +0000 |
commit | 7af97dc71fd6790a3f4ffe47dcc5720b675f6b6b (patch) | |
tree | 746868021ac678da2ab27261d88794a1daf9e2d2 /lib/irb/cmd | |
parent | e878bbd641f255b5d8f9e99b84ff8082a5b7fdaf (diff) |
[ruby/irb] Powerup show_source by enabling RubyVM.keep_script_lines
(https://github.com/ruby/irb/pull/862)
* Powerup show_source by enabling RubyVM.keep_script_lines
* Add file_content field to avoid reading file twice while show_source
* Change path passed to eval, don't change irb_path.
* Encapsulate source coloring logic and binary file check insode class Source
* Add edit command testcase when irb_path does not exist
* Memoize irb_path existence to reduce file existence check calculating eval_path
https://github.com/ruby/irb/commit/239683a937
Diffstat (limited to 'lib/irb/cmd')
-rw-r--r-- | lib/irb/cmd/edit.rb | 18 | ||||
-rw-r--r-- | lib/irb/cmd/show_source.rb | 15 |
2 files changed, 18 insertions, 15 deletions
diff --git a/lib/irb/cmd/edit.rb b/lib/irb/cmd/edit.rb index 69606beea0..2f89f83ecc 100644 --- a/lib/irb/cmd/edit.rb +++ b/lib/irb/cmd/edit.rb @@ -24,11 +24,9 @@ module IRB def execute(*args) path = args.first - if path.nil? && (irb_path = @irb_context.irb_path) - path = irb_path - end - - if !File.exist?(path) + if path.nil? + path = @irb_context.irb_path + elsif !File.exist?(path) source = begin SourceFinder.new(@irb_context).find_source(path) @@ -37,14 +35,16 @@ module IRB # in this case, we should just ignore the error end - if source + if source&.file_exist? && !source.binary_file? path = source.file - else - puts "Can not find file: #{path}" - return end end + unless File.exist?(path) + puts "Can not find file: #{path}" + return + end + if editor = (ENV['VISUAL'] || ENV['EDITOR']) puts "command: '#{editor}'" puts " path: #{path}" diff --git a/lib/irb/cmd/show_source.rb b/lib/irb/cmd/show_source.rb index 826cb11ed2..cd07de3e90 100644 --- a/lib/irb/cmd/show_source.rb +++ b/lib/irb/cmd/show_source.rb @@ -45,15 +45,18 @@ module IRB private def show_source(source) - file_content = IRB::Color.colorize_code(File.read(source.file)) - code = file_content.lines[(source.first_line - 1)...source.last_line].join - content = <<~CONTENT + if source.binary_file? + content = "\n#{bold('Defined in binary file')}: #{source.file}\n\n" + else + code = source.colorized_content || 'Source not available' + content = <<~CONTENT - #{bold("From")}: #{source.file}:#{source.first_line} + #{bold("From")}: #{source.file}:#{source.line} - #{code} - CONTENT + #{code.chomp} + CONTENT + end Pager.page_content(content) end |