diff options
author | Shannon Skipper <shannonskipper@gmail.com> | 2013-11-13 22:47:56 -0800 |
---|---|---|
committer | Shannon Skipper <shannonskipper@gmail.com> | 2013-11-18 15:15:28 -0800 |
commit | ea1faa535d38a1f493314362b6912526a993bce3 (patch) | |
tree | dc26990ecf8437061830445398b35d906ceebf1b /lib | |
parent | abfda989be88cd015a489c10738fd674273be6fa (diff) | |
download | pry-ea1faa535d38a1f493314362b6912526a993bce3.tar.gz |
Add cat support for Ruby files with .rb ext omitted.
Add support for %cat with .rb ext omitted.
Allow highlighting ommitted '.rb' ext, DRY up ext checking.
Only call Pry::Code#from_file once on instantiation.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pry/code.rb | 32 | ||||
-rw-r--r-- | lib/pry/commands/cat/file_formatter.rb | 33 |
2 files changed, 32 insertions, 33 deletions
diff --git a/lib/pry/code.rb b/lib/pry/code.rb index 5fab8091..28cf23e4 100644 --- a/lib/pry/code.rb +++ b/lib/pry/code.rb @@ -66,7 +66,9 @@ class Pry elsif RbxPath.is_core_path?(filename) File.read RbxPath.convert_path_to_full(filename) else - File.read(abs_path(filename)) + abs_path = abs_path(filename) + code_type = type_from_filename(abs_path) + File.read(abs_path) end new(code, 1, code_type) end @@ -108,12 +110,12 @@ class Pry # @param [Symbol] default (:ruby) the file type to assume if none could be # detected. # @return [Symbol, nil] - def type_from_filename(filename, default = :ruby) - _, type = Pry::Code::EXTENSIONS.find do |k, _| + def type_from_filename(filename, default = :unknown) + _, code_type = Pry::Code::EXTENSIONS.find do |k, _| k.any? { |ext| ext == File.extname(filename) } end - type || default + code_type || default end # @param [String] filename @@ -130,17 +132,31 @@ class Pry # @param [String] filename # @return [String] absolute path for the given `filename` or nil. def find_path_in_pwd(filename) - [File.expand_path(filename, Dir.pwd), + omitted_rb_ext = nil + abs_path = [File.expand_path(filename, Dir.pwd), File.expand_path(filename, Pry::INITIAL_PWD) - ].detect { |path| File.readable?(path) if path } + ].detect do |path| + if path + File.readable?(path) || + File.readable?(path << '.rb') && ommitted_rb_ext = true + end + end + omitted_rb_ext ? abs_path << '.rb' : abs_path end # @param [String] filename # @return [String] absolute path for the given `filename` or nil. def find_path_in_load_path(filename) - $LOAD_PATH.map do |path| + omitted_rb_ext = nil + abs_path = $LOAD_PATH.map do |path| File.expand_path(filename, path) - end.detect { |path| File.readable?(path) if path } + end.detect do |path| + if path + File.readable?(path) || + File.readable?(path << '.rb') && ommitted_rb_ext = true + end + end + omitted_rb_ext ? abs_path << '.rb' : abs_path end end diff --git a/lib/pry/commands/cat/file_formatter.rb b/lib/pry/commands/cat/file_formatter.rb index ad72e456..03226dfb 100644 --- a/lib/pry/commands/cat/file_formatter.rb +++ b/lib/pry/commands/cat/file_formatter.rb @@ -9,13 +9,14 @@ class Pry @file_with_embedded_line = file_with_embedded_line @opts = opts @_pry_ = _pry_ + @code_from_file = Pry::Code.from_file(file_name) end def format raise CommandError, "Must provide a filename, --in, or --ex." if !file_with_embedded_line set_file_and_dir_locals(file_name, _pry_, _pry_.current_context) - decorate(Pry::Code.from_file(file_name)) + decorate(@code_from_file) end private @@ -47,36 +48,18 @@ class Pry end def detect_code_type_from_file(file_name) - name, ext = File.basename(file_name).split('.', 2) - - if ext - case ext - when "py" - :python - when "rb", "gemspec", "rakefile", "ru", "pryrc", "irbrc" - :ruby - when "js" - return :javascript - when "yml", "prytheme" - :yaml - when "groovy" - :groovy - when "c" - :c - when "cpp" - :cpp - when "java" - :java - else - :text - end - else + code_type = @code_from_file.code_type + + if code_type == :unknown + name, ext = File.basename(file_name).split('.', 2) case name when "Rakefile", "Gemfile" :ruby else :text end + else + code_type end end end |