Merge branch 'cat' of github.com:havenwood/pry into havenwood-cat
This commit is contained in:
commit
d8fec69398
|
@ -1,5 +1,6 @@
|
||||||
require 'pry/code/loc'
|
require 'pry/code/loc'
|
||||||
require 'pry/code/code_range'
|
require 'pry/code/code_range'
|
||||||
|
require 'pry/code/code_file'
|
||||||
|
|
||||||
class Pry
|
class Pry
|
||||||
class << self
|
class << self
|
||||||
|
@ -29,26 +30,6 @@ class Pry
|
||||||
# arbitrary chaining of formatting methods without mutating the original
|
# arbitrary chaining of formatting methods without mutating the original
|
||||||
# object.
|
# object.
|
||||||
class Code
|
class Code
|
||||||
|
|
||||||
# List of all supported languages.
|
|
||||||
# @return [Hash]
|
|
||||||
EXTENSIONS = {
|
|
||||||
%w(.py) => :python,
|
|
||||||
%w(.js) => :javascript,
|
|
||||||
%w(.css) => :css,
|
|
||||||
%w(.xml) => :xml,
|
|
||||||
%w(.php) => :php,
|
|
||||||
%w(.html) => :html,
|
|
||||||
%w(.diff) => :diff,
|
|
||||||
%w(.java) => :java,
|
|
||||||
%w(.json) => :json,
|
|
||||||
%w(.c .h) => :c,
|
|
||||||
%w(.rhtml) => :rhtml,
|
|
||||||
%w(.yaml .yml) => :yaml,
|
|
||||||
%w(.cpp .hpp .cc .h cxx) => :cpp,
|
|
||||||
%w(.rb .ru .irbrc .gemspec .pryrc) => :ruby,
|
|
||||||
}
|
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
include MethodSource::CodeHelpers
|
include MethodSource::CodeHelpers
|
||||||
|
|
||||||
|
@ -58,17 +39,10 @@ class Pry
|
||||||
# @param [String] filename The name of a file, or "(pry)".
|
# @param [String] filename The name of a file, or "(pry)".
|
||||||
# @param [Symbol] code_type The type of code the file contains.
|
# @param [Symbol] code_type The type of code the file contains.
|
||||||
# @return [Code]
|
# @return [Code]
|
||||||
def from_file(filename, code_type = type_from_filename(filename))
|
def from_file(filename, code_type = nil)
|
||||||
code = if filename == Pry.eval_path
|
code_file = CodeFile.new(filename, code_type)
|
||||||
Pry.line_buffer.drop(1)
|
# binding.pry
|
||||||
elsif Pry::Method::Patcher.code_for(filename)
|
new(code_file.code, 1, code_file.code_type)
|
||||||
Pry::Method::Patcher.code_for(filename)
|
|
||||||
elsif RbxPath.is_core_path?(filename)
|
|
||||||
File.read RbxPath.convert_path_to_full(filename)
|
|
||||||
else
|
|
||||||
File.read(abs_path(filename))
|
|
||||||
end
|
|
||||||
new(code, 1, code_type)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Instantiate a `Code` object containing code extracted from a
|
# Instantiate a `Code` object containing code extracted from a
|
||||||
|
@ -98,35 +72,6 @@ class Pry
|
||||||
start_line ||= candidate.line
|
start_line ||= candidate.line
|
||||||
new(candidate.source, start_line, :ruby)
|
new(candidate.source, start_line, :ruby)
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
|
||||||
|
|
||||||
# Guess the CodeRay type of a file from its extension, or nil if
|
|
||||||
# unknown.
|
|
||||||
#
|
|
||||||
# @param [String] filename
|
|
||||||
# @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, _|
|
|
||||||
k.any? { |ext| ext == File.extname(filename) }
|
|
||||||
end
|
|
||||||
|
|
||||||
type || default
|
|
||||||
end
|
|
||||||
|
|
||||||
# @param [String] filename
|
|
||||||
# @raise [MethodSource::SourceNotFoundError] if the +filename+ is not
|
|
||||||
# readable for some reason.
|
|
||||||
# @return [String] absolute path for the given +filename+.
|
|
||||||
def abs_path(filename)
|
|
||||||
abs_path = [File.expand_path(filename, Dir.pwd),
|
|
||||||
File.expand_path(filename, Pry::INITIAL_PWD)
|
|
||||||
].detect { |path| File.readable?(path) }
|
|
||||||
abs_path or raise MethodSource::SourceNotFoundError,
|
|
||||||
"Cannot open #{filename.inspect} for reading."
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# @return [Symbol] The type of code stored in this wrapper.
|
# @return [Symbol] The type of code stored in this wrapper.
|
||||||
|
|
|
@ -0,0 +1,95 @@
|
||||||
|
class Pry
|
||||||
|
class CodeFile
|
||||||
|
DEFAULT_EXT = '.rb'
|
||||||
|
|
||||||
|
# List of all supported languages.
|
||||||
|
# @return [Hash]
|
||||||
|
EXTENSIONS = {
|
||||||
|
%w(.py) => :python,
|
||||||
|
%w(.js) => :javascript,
|
||||||
|
%w(.css) => :css,
|
||||||
|
%w(.xml) => :xml,
|
||||||
|
%w(.php) => :php,
|
||||||
|
%w(.html) => :html,
|
||||||
|
%w(.diff) => :diff,
|
||||||
|
%w(.java) => :java,
|
||||||
|
%w(.json) => :json,
|
||||||
|
%w(.c .h) => :c,
|
||||||
|
%w(.rhtml) => :rhtml,
|
||||||
|
%w(.yaml .yml) => :yaml,
|
||||||
|
%w(.cpp .hpp .cc .h cxx) => :cpp,
|
||||||
|
%w(.rb .ru .irbrc .gemspec .pryrc) => :ruby,
|
||||||
|
}
|
||||||
|
|
||||||
|
# @return [Symbol] The type of code stored in this wrapper.
|
||||||
|
attr_reader :code_type
|
||||||
|
|
||||||
|
# @param [String] filename The name of a file with code to be detected
|
||||||
|
# @param [Symbol] code_type The type of code the `filename` contains
|
||||||
|
def initialize(filename, code_type = type_from_filename(filename))
|
||||||
|
@filename = filename
|
||||||
|
@code_type = code_type
|
||||||
|
end
|
||||||
|
|
||||||
|
# @return [String] The code contained in the current `@filename`.
|
||||||
|
def code
|
||||||
|
if @filename == Pry.eval_path
|
||||||
|
Pry.line_buffer.drop(1)
|
||||||
|
elsif Pry::Method::Patcher.code_for(@filename)
|
||||||
|
Pry::Method::Patcher.code_for(@filename)
|
||||||
|
elsif RbxPath.is_core_path?(@filename)
|
||||||
|
File.read(RbxPath.convert_path_to_full(@filename))
|
||||||
|
else
|
||||||
|
abs_path = abs_path(@filename)
|
||||||
|
@code_type = type_from_filename(abs_path)
|
||||||
|
File.read(abs_path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
# @param [String] filename
|
||||||
|
# @raise [MethodSource::SourceNotFoundError] if the `filename` is not
|
||||||
|
# readable for some reason.
|
||||||
|
# @return [String] absolute path for the given `filename`.
|
||||||
|
def abs_path(filename)
|
||||||
|
find_abs_path(filename) or
|
||||||
|
raise MethodSource::SourceNotFoundError,
|
||||||
|
"Cannot open #{filename.inspect} for reading."
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_abs_path(filename)
|
||||||
|
code_path(filename).detect { |path| readable?(path) }
|
||||||
|
end
|
||||||
|
|
||||||
|
# @param [String] path
|
||||||
|
# @return [Boolean] if the path, with or without the default ext,
|
||||||
|
# is a readable file then `true`, otherwise `false`.
|
||||||
|
def readable?(path)
|
||||||
|
File.readable?(path) && !File.directory?(path) or
|
||||||
|
File.readable?(path << DEFAULT_EXT)
|
||||||
|
end
|
||||||
|
|
||||||
|
# @return [Array] All the paths that contain code that Pry can use for its
|
||||||
|
# API's. Skips directories.
|
||||||
|
def code_path(filename)
|
||||||
|
[File.expand_path(filename, Dir.pwd),
|
||||||
|
File.expand_path(filename, Pry::INITIAL_PWD),
|
||||||
|
*$LOAD_PATH.map { |path| File.expand_path(filename, path) }]
|
||||||
|
end
|
||||||
|
|
||||||
|
# @param [String] filename
|
||||||
|
# @param [Symbol] default (:unknown) the file type to assume if none could be
|
||||||
|
# detected.
|
||||||
|
# @return [Symbol, nil] The CodeRay type of a file from its extension, or
|
||||||
|
# `nil` if `:unknown`.
|
||||||
|
def type_from_filename(filename, default = :unknown)
|
||||||
|
_, @code_type = EXTENSIONS.find do |k, _|
|
||||||
|
k.any? { |ext| ext == File.extname(filename) }
|
||||||
|
end
|
||||||
|
|
||||||
|
code_type || default
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
|
@ -45,7 +45,14 @@ class Pry
|
||||||
end
|
end
|
||||||
|
|
||||||
def complete(search)
|
def complete(search)
|
||||||
super + Bond::Rc.files(search.split(" ").last || '')
|
search_term = search.split.last || ''
|
||||||
|
super +
|
||||||
|
Bond::Rc.files(search_term) +
|
||||||
|
$LOAD_PATH.flat_map do |path|
|
||||||
|
Bond::Rc.files(File.join(path.dup, search_term)).map do |file_path|
|
||||||
|
File.basename(file_path)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -9,13 +9,14 @@ class Pry
|
||||||
@file_with_embedded_line = file_with_embedded_line
|
@file_with_embedded_line = file_with_embedded_line
|
||||||
@opts = opts
|
@opts = opts
|
||||||
@_pry_ = _pry_
|
@_pry_ = _pry_
|
||||||
|
@code_from_file = Pry::Code.from_file(file_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def format
|
def format
|
||||||
raise CommandError, "Must provide a filename, --in, or --ex." if !file_with_embedded_line
|
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)
|
set_file_and_dir_locals(file_name, _pry_, _pry_.current_context)
|
||||||
decorate(Pry::Code.from_file(file_name))
|
decorate(@code_from_file)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -23,7 +24,7 @@ class Pry
|
||||||
def file_and_line
|
def file_and_line
|
||||||
file_name, line_num = file_with_embedded_line.split(':')
|
file_name, line_num = file_with_embedded_line.split(':')
|
||||||
|
|
||||||
[File.expand_path(file_name), line_num ? line_num.to_i : nil]
|
[file_name, line_num ? line_num.to_i : nil]
|
||||||
end
|
end
|
||||||
|
|
||||||
def file_name
|
def file_name
|
||||||
|
@ -47,36 +48,18 @@ class Pry
|
||||||
end
|
end
|
||||||
|
|
||||||
def detect_code_type_from_file(file_name)
|
def detect_code_type_from_file(file_name)
|
||||||
name, ext = File.basename(file_name).split('.', 2)
|
code_type = @code_from_file.code_type
|
||||||
|
|
||||||
if ext
|
if code_type == :unknown
|
||||||
case ext
|
name, ext = File.basename(file_name).split('.', 2)
|
||||||
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
|
|
||||||
case name
|
case name
|
||||||
when "Rakefile", "Gemfile"
|
when "Rakefile", "Gemfile"
|
||||||
:ruby
|
:ruby
|
||||||
else
|
else
|
||||||
:text
|
:text
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
code_type
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,9 +11,9 @@ describe Pry::Code do
|
||||||
Pry::Code.from_file('(pry)').grep(/:hay_guys/).length.should == 1
|
Pry::Code.from_file('(pry)').grep(/:hay_guys/).length.should == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
should 'default to Ruby' do
|
should 'default to unknown' do
|
||||||
temp_file('') do |f|
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -23,12 +23,6 @@ describe Pry::Code do
|
||||||
end
|
end
|
||||||
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
|
should 'raise an error if the file doesn\'t exist' do
|
||||||
proc do
|
proc do
|
||||||
Pry::Code.from_file('/knalkjsdnalsd/alkjdlkq')
|
Pry::Code.from_file('/knalkjsdnalsd/alkjdlkq')
|
||||||
|
@ -41,11 +35,51 @@ describe Pry::Code do
|
||||||
end
|
end
|
||||||
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
|
should 'find files that are relative to the current working directory' do
|
||||||
Dir.chdir('spec') do |f|
|
Dir.chdir('spec') do |f|
|
||||||
Pry::Code.from_file(File.basename(__FILE__)).code_type.should == :ruby
|
Pry::Code.from_file(File.basename(__FILE__)).code_type.should == :ruby
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'find Ruby files relative to $LOAD_PATH' do
|
||||||
|
before do
|
||||||
|
$LOAD_PATH << 'spec/fixtures'
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
$LOAD_PATH.delete 'spec/fixtures'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'finds files with `.rb` extension' do
|
||||||
|
Pry::Code.from_file('slinky.rb').code_type.should == :ruby
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'finds files with `.rb` omitted' do
|
||||||
|
Pry::Code.from_file('slinky').code_type.should == :ruby
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'finds files in a relative directory with `.rb` extension' do
|
||||||
|
Pry::Code.from_file('../helper.rb').code_type.should == :ruby
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'finds files in a relative directory with `.rb` omitted' do
|
||||||
|
Pry::Code.from_file('../helper').code_type.should == :ruby
|
||||||
|
end
|
||||||
|
|
||||||
|
it "doesn't confuse files with the same name, but without an extension" do
|
||||||
|
Pry::Code.from_file('cat_load_path').code_type.should == :unknown
|
||||||
|
end
|
||||||
|
|
||||||
|
it "doesn't confuse files with the same name, but with an extension" do
|
||||||
|
Pry::Code.from_file('cat_load_path.rb').code_type.should == :ruby
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '.from_method' do
|
describe '.from_method' do
|
||||||
|
|
Loading…
Reference in New Issue