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.
This commit is contained in:
Shannon Skipper 2013-11-13 22:47:56 -08:00
parent abfda989be
commit ea1faa535d
3 changed files with 48 additions and 41 deletions

View File

@ -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

View File

@ -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

View File

@ -11,9 +11,9 @@ describe Pry::Code do
Pry::Code.from_file('(pry)').grep(/:hay_guys/).length.should == 1
end
should 'default to Ruby' do
should 'default to unknown' do
temp_file('') do |f|
Pry::Code.from_file(f.path).code_type.should == :ruby
Pry::Code.from_file(f.path).code_type.should == :unknown
end
end
@ -23,12 +23,6 @@ describe Pry::Code do
end
end
should 'use the provided extension' do
temp_file('.c') do |f|
Pry::Code.from_file(f.path, :ruby).code_type.should == :ruby
end
end
should 'raise an error if the file doesn\'t exist' do
proc do
Pry::Code.from_file('/knalkjsdnalsd/alkjdlkq')
@ -41,6 +35,12 @@ describe Pry::Code do
end
end
should 'check for Ruby files relative to origin pwd with `.rb` omitted' do
Dir.chdir('spec') do |f|
Pry::Code.from_file('spec/' + File.basename(__FILE__, '.*')).code_type.should == :ruby
end
end
should 'find files that are relative to the current working directory' do
Dir.chdir('spec') do |f|
Pry::Code.from_file(File.basename(__FILE__)).code_type.should == :ruby
@ -60,9 +60,17 @@ describe Pry::Code do
Pry::Code.from_file('ls_spec.rb').code_type.should == :ruby
end
should 'find Ruby files in $LOAD_PATH with `.rb` omitted' do
Pry::Code.from_file('ls_spec').code_type.should == :ruby
end
should 'find files that are relative to a directory in the $LOAD_PATH' do
Pry::Code.from_file('../helper.rb').code_type.should == :ruby
end
should 'find Ruby files relative to $LOAD_PATH with `.rb` omitted' do
Pry::Code.from_file('../helper').code_type.should == :ruby
end
end
end