From 46c5e800ceb0ee532871590f9489c1f8055d6fa6 Mon Sep 17 00:00:00 2001 From: Shannon Skipper Date: Mon, 11 Nov 2013 22:54:31 -0800 Subject: [PATCH 01/16] Support %cat checking $LOAD_PATH directories. --- lib/pry/code.rb | 3 +++ spec/code_spec.rb | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/lib/pry/code.rb b/lib/pry/code.rb index 968e9c26..f91ef223 100644 --- a/lib/pry/code.rb +++ b/lib/pry/code.rb @@ -124,6 +124,9 @@ class Pry abs_path = [File.expand_path(filename, Dir.pwd), File.expand_path(filename, Pry::INITIAL_PWD) ].detect { |path| File.readable?(path) } + abs_path ||= $LOAD_PATH.map do |path| + File.expand_path(File.basename(filename), path) + end.detect { |path| File.readable?(path) if path } abs_path or raise MethodSource::SourceNotFoundError, "Cannot open #{filename.inspect} for reading." end diff --git a/spec/code_spec.rb b/spec/code_spec.rb index 520df34d..c81a4ff1 100644 --- a/spec/code_spec.rb +++ b/spec/code_spec.rb @@ -46,6 +46,11 @@ describe Pry::Code do Pry::Code.from_file(File.basename(__FILE__)).code_type.should == :ruby end end + + should 'find files that are relative to a directory in the $LOAD_PATH' do + $LOAD_PATH << 'spec' + Pry::Code.from_file(File.basename(__FILE__)).code_type.should == :ruby + end end describe '.from_method' do From abfda989be88cd015a489c10738fd674273be6fa Mon Sep 17 00:00:00 2001 From: Shannon Skipper Date: Wed, 13 Nov 2013 20:25:11 -0800 Subject: [PATCH 02/16] Also files relative to load path dirs. Oops, fix typo in comments. Switch to backticks instead of plusses. Set and unset in a context. Remove unnecessary #basename. Remove early File#basename so relative paths actually work. --- lib/pry/code.rb | 28 ++++++++++++++++++-------- lib/pry/commands/cat/file_formatter.rb | 2 +- spec/code_spec.rb | 21 +++++++++++++++---- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/lib/pry/code.rb b/lib/pry/code.rb index f91ef223..5fab8091 100644 --- a/lib/pry/code.rb +++ b/lib/pry/code.rb @@ -117,19 +117,31 @@ class Pry end # @param [String] filename - # @raise [MethodSource::SourceNotFoundError] if the +filename+ is not + # @raise [MethodSource::SourceNotFoundError] if the `filename` is not # readable for some reason. - # @return [String] absolute path for the given +filename+. + # @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 ||= $LOAD_PATH.map do |path| - File.expand_path(File.basename(filename), path) - end.detect { |path| File.readable?(path) if path } + abs_path = find_path_in_pwd(filename) || + find_path_in_load_path(filename) abs_path or raise MethodSource::SourceNotFoundError, "Cannot open #{filename.inspect} for reading." end + + # @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), + File.expand_path(filename, Pry::INITIAL_PWD) + ].detect { |path| File.readable?(path) if 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| + File.expand_path(filename, path) + end.detect { |path| File.readable?(path) if path } + end end # @return [Symbol] The type of code stored in this wrapper. diff --git a/lib/pry/commands/cat/file_formatter.rb b/lib/pry/commands/cat/file_formatter.rb index c60c55e8..ad72e456 100644 --- a/lib/pry/commands/cat/file_formatter.rb +++ b/lib/pry/commands/cat/file_formatter.rb @@ -23,7 +23,7 @@ class Pry def file_and_line 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 def file_name diff --git a/spec/code_spec.rb b/spec/code_spec.rb index c81a4ff1..090d55df 100644 --- a/spec/code_spec.rb +++ b/spec/code_spec.rb @@ -46,10 +46,23 @@ describe Pry::Code do Pry::Code.from_file(File.basename(__FILE__)).code_type.should == :ruby end end - - should 'find files that are relative to a directory in the $LOAD_PATH' do - $LOAD_PATH << 'spec' - Pry::Code.from_file(File.basename(__FILE__)).code_type.should == :ruby + + describe 'find files that are relative to the $LOAD_PATH' do + before do + $LOAD_PATH << 'spec/commands' + end + + after do + $LOAD_PATH.delete 'spec/commands' + end + + should 'find files that are in a directory in the $LOAD_PATH' do + Pry::Code.from_file('ls_spec.rb').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 end end From ea1faa535d38a1f493314362b6912526a993bce3 Mon Sep 17 00:00:00 2001 From: Shannon Skipper Date: Wed, 13 Nov 2013 22:47:56 -0800 Subject: [PATCH 03/16] 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. --- lib/pry/code.rb | 32 ++++++++++++++++++------- lib/pry/commands/cat/file_formatter.rb | 33 +++++++------------------- spec/code_spec.rb | 24 ++++++++++++------- 3 files changed, 48 insertions(+), 41 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 diff --git a/spec/code_spec.rb b/spec/code_spec.rb index 090d55df..0b44b867 100644 --- a/spec/code_spec.rb +++ b/spec/code_spec.rb @@ -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 From 9bea1b77897f7f6ca4ccb4de3d53feddb3192918 Mon Sep 17 00:00:00 2001 From: Shannon Skipper Date: Sat, 23 Nov 2013 07:40:58 -0800 Subject: [PATCH 04/16] Removed unncessary 'if' check that @kyrylo pointed out. --- lib/pry/code.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/pry/code.rb b/lib/pry/code.rb index 28cf23e4..97dca51f 100644 --- a/lib/pry/code.rb +++ b/lib/pry/code.rb @@ -136,10 +136,8 @@ class Pry abs_path = [File.expand_path(filename, Dir.pwd), File.expand_path(filename, Pry::INITIAL_PWD) ].detect do |path| - if path - File.readable?(path) || - File.readable?(path << '.rb') && ommitted_rb_ext = true - end + File.readable?(path) || + File.readable?(path << '.rb') && ommitted_rb_ext = true end omitted_rb_ext ? abs_path << '.rb' : abs_path end @@ -151,10 +149,8 @@ class Pry abs_path = $LOAD_PATH.map do |path| File.expand_path(filename, path) end.detect do |path| - if path - File.readable?(path) || - File.readable?(path << '.rb') && ommitted_rb_ext = true - end + File.readable?(path) || + File.readable?(path << '.rb') && ommitted_rb_ext = true end omitted_rb_ext ? abs_path << '.rb' : abs_path end From dcfefd8d6382194709d2befc258827ed686eb214 Mon Sep 17 00:00:00 2001 From: Shannon Skipper Date: Sat, 23 Nov 2013 08:16:43 -0800 Subject: [PATCH 05/16] Fix bug when 'name/' and 'name.rb' both exist. --- lib/pry/code.rb | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/pry/code.rb b/lib/pry/code.rb index 97dca51f..325b51e9 100644 --- a/lib/pry/code.rb +++ b/lib/pry/code.rb @@ -29,6 +29,7 @@ class Pry # arbitrary chaining of formatting methods without mutating the original # object. class Code + DEFAULT_EXT = '.rb' # List of all supported languages. # @return [Hash] @@ -136,10 +137,14 @@ class Pry abs_path = [File.expand_path(filename, Dir.pwd), File.expand_path(filename, Pry::INITIAL_PWD) ].detect do |path| - File.readable?(path) || - File.readable?(path << '.rb') && ommitted_rb_ext = true + if File.directory?(path) + File.readable?(path << DEFAULT_EXT) && ommitted_rb_ext = true + else + File.readable?(path) || + File.readable?(path << DEFAULT_EXT) && ommitted_rb_ext = true + end end - omitted_rb_ext ? abs_path << '.rb' : abs_path + omitted_rb_ext ? abs_path << DEFAULT_EXT : abs_path end # @param [String] filename @@ -149,10 +154,14 @@ class Pry abs_path = $LOAD_PATH.map do |path| File.expand_path(filename, path) end.detect do |path| - File.readable?(path) || - File.readable?(path << '.rb') && ommitted_rb_ext = true + if File.directory?(path) + File.readable?(path << DEFAULT_EXT) && ommitted_rb_ext = true + else + File.readable?(path) || + File.readable?(path << DEFAULT_EXT) && ommitted_rb_ext = true + end end - omitted_rb_ext ? abs_path << '.rb' : abs_path + omitted_rb_ext ? abs_path << DEFAULT_EXT : abs_path end end From 1a24db838c4cede0535027c01fdc3549d5d6cc1a Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sat, 23 Nov 2013 21:37:36 +0200 Subject: [PATCH 06/16] lib/pry/code.rb: refactor $LOAD_PATH path finding --- lib/pry/code.rb | 56 ++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/lib/pry/code.rb b/lib/pry/code.rb index 325b51e9..a89ad421 100644 --- a/lib/pry/code.rb +++ b/lib/pry/code.rb @@ -124,44 +124,38 @@ class Pry # readable for some reason. # @return [String] absolute path for the given `filename`. def abs_path(filename) - abs_path = find_path_in_pwd(filename) || - find_path_in_load_path(filename) - abs_path or raise MethodSource::SourceNotFoundError, - "Cannot open #{filename.inspect} for reading." + find_abs_path(filename) or raise MethodSource::SourceNotFoundError, + "Cannot open #{filename.inspect} for reading." end - # @param [String] filename - # @return [String] absolute path for the given `filename` or nil. - def find_path_in_pwd(filename) - omitted_rb_ext = nil - abs_path = [File.expand_path(filename, Dir.pwd), - File.expand_path(filename, Pry::INITIAL_PWD) - ].detect do |path| - if File.directory?(path) - File.readable?(path << DEFAULT_EXT) && ommitted_rb_ext = true - else - File.readable?(path) || - File.readable?(path << DEFAULT_EXT) && ommitted_rb_ext = true + def find_abs_path(filename) + code_path(filename).detect { |path| readable_source?(path) }.tap do |path| + if path && !File.exist?(path) + path.sub!(/(.+)(? Date: Sat, 23 Nov 2013 22:08:53 +0200 Subject: [PATCH 07/16] spec/code_spec.rb: refactor tests - Use fixtures as a test files - Write more concise spec titles --- spec/code_spec.rb | 18 +++++++++--------- spec/fixtures/slinky.rb | 0 spec/fixtures/slinky/stinky.rb | 0 3 files changed, 9 insertions(+), 9 deletions(-) create mode 100644 spec/fixtures/slinky.rb create mode 100644 spec/fixtures/slinky/stinky.rb diff --git a/spec/code_spec.rb b/spec/code_spec.rb index 0b44b867..3f06e808 100644 --- a/spec/code_spec.rb +++ b/spec/code_spec.rb @@ -47,28 +47,28 @@ describe Pry::Code do end end - describe 'find files that are relative to the $LOAD_PATH' do + describe 'find Ruby files relative to $LOAD_PATH' do before do - $LOAD_PATH << 'spec/commands' + $LOAD_PATH << 'spec/fixtures' end after do - $LOAD_PATH.delete 'spec/commands' + $LOAD_PATH.delete 'spec/fixtures' end - should 'find files that are in a directory in the $LOAD_PATH' do - Pry::Code.from_file('ls_spec.rb').code_type.should == :ruby + it 'finds files with `.rb` extension' do + Pry::Code.from_file('slinky.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 + it 'finds files with `.rb` omitted' do + Pry::Code.from_file('slinky').code_type.should == :ruby end - should 'find files that are relative to a directory in the $LOAD_PATH' do + it 'finds files in a relative directory with `.rb` extension' do Pry::Code.from_file('../helper.rb').code_type.should == :ruby end - should 'find Ruby files relative to $LOAD_PATH with `.rb` omitted' do + it 'finds files in a relative directory with `.rb` omitted' do Pry::Code.from_file('../helper').code_type.should == :ruby end end diff --git a/spec/fixtures/slinky.rb b/spec/fixtures/slinky.rb new file mode 100644 index 00000000..e69de29b diff --git a/spec/fixtures/slinky/stinky.rb b/spec/fixtures/slinky/stinky.rb new file mode 100644 index 00000000..e69de29b From 62ebc08d52ca844c1f7d0db302d6fafed355e346 Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sat, 23 Nov 2013 22:13:54 +0200 Subject: [PATCH 08/16] spec/code_spec.rb: add more tests for $LOAD_PATH behaviour --- spec/code_spec.rb | 8 ++++++++ spec/fixtures/cat_load_path | 0 spec/fixtures/cat_load_path.rb | 0 3 files changed, 8 insertions(+) create mode 100644 spec/fixtures/cat_load_path create mode 100644 spec/fixtures/cat_load_path.rb diff --git a/spec/code_spec.rb b/spec/code_spec.rb index 3f06e808..51b626f6 100644 --- a/spec/code_spec.rb +++ b/spec/code_spec.rb @@ -71,6 +71,14 @@ describe Pry::Code do 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 diff --git a/spec/fixtures/cat_load_path b/spec/fixtures/cat_load_path new file mode 100644 index 00000000..e69de29b diff --git a/spec/fixtures/cat_load_path.rb b/spec/fixtures/cat_load_path.rb new file mode 100644 index 00000000..e69de29b From e324eda394a3c53328df65d6e6ddf2c3a187c9a2 Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sat, 23 Nov 2013 22:21:52 +0200 Subject: [PATCH 09/16] lib/pry/code.rb: remove #known_extensions cruft --- lib/pry/code.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/pry/code.rb b/lib/pry/code.rb index a89ad421..f2c052ec 100644 --- a/lib/pry/code.rb +++ b/lib/pry/code.rb @@ -130,9 +130,7 @@ class Pry def find_abs_path(filename) code_path(filename).detect { |path| readable_source?(path) }.tap do |path| - if path && !File.exist?(path) - path.sub!(/(.+)(? Date: Sat, 23 Nov 2013 23:01:40 +0200 Subject: [PATCH 10/16] lib/pry/code.rb: factor out code related to filename handling Factor out it to `lib/pry/code/code_file.rb`. --- lib/pry/code.rb | 88 ++--------------------------------- lib/pry/code/code_file.rb | 97 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 84 deletions(-) create mode 100644 lib/pry/code/code_file.rb diff --git a/lib/pry/code.rb b/lib/pry/code.rb index f2c052ec..35c3be2c 100644 --- a/lib/pry/code.rb +++ b/lib/pry/code.rb @@ -1,5 +1,6 @@ require 'pry/code/loc' require 'pry/code/code_range' +require 'pry/code/code_file' class Pry class << self @@ -29,27 +30,6 @@ class Pry # arbitrary chaining of formatting methods without mutating the original # object. class Code - 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, - } - class << self include MethodSource::CodeHelpers @@ -59,19 +39,9 @@ class Pry # @param [String] filename The name of a file, or "(pry)". # @param [Symbol] code_type The type of code the file contains. # @return [Code] - def from_file(filename, code_type = type_from_filename(filename)) - 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 - new(code, 1, code_type) + def from_file(filename, code_type = nil) + code_file = CodeFile.new(filename, code_type) + new(code_file.code, 1, code_file.code_type) end # Instantiate a `Code` object containing code extracted from a @@ -101,56 +71,6 @@ class Pry start_line ||= candidate.line new(candidate.source, start_line, :ruby) 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 = :unknown) - _, code_type = Pry::Code::EXTENSIONS.find do |k, _| - k.any? { |ext| ext == File.extname(filename) } - end - - code_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) - 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_source?(path) }.tap do |path| - path << DEFAULT_EXT if path && !File.exist?(path) - end - end - - def readable_source?(path) - File.readable?(path) || File.readable?(path + DEFAULT_EXT) - end - - def code_path(filename) - normalized_load_path = $LOAD_PATH.map { |path| - File.expand_path(filename, path).tap do |p| - if File.directory?(p) - p << DEFAULT_EXT - end - end - } - - [ File.expand_path(filename, Dir.pwd), - File.expand_path(filename, Pry::INITIAL_PWD), - *normalized_load_path ] - end end # @return [Symbol] The type of code stored in this wrapper. diff --git a/lib/pry/code/code_file.rb b/lib/pry/code/code_file.rb new file mode 100644 index 00000000..c7d1181c --- /dev/null +++ b/lib/pry/code/code_file.rb @@ -0,0 +1,97 @@ +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, + } + + attr_reader :code_type + + def initialize(filename, code_type = type_from_filename(filename)) + @filename = filename + @code_type = code_type + end + + 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_source?(path) }.tap do |path| + path << DEFAULT_EXT if path && !File.exist?(path) + end + end + + def readable_source?(path) + File.readable?(path) || File.readable?(path + DEFAULT_EXT) + end + + def code_path(filename) + normalized_load_path = $LOAD_PATH.map { |path| + File.expand_path(filename, path).tap do |p| + if File.directory?(p) + p << DEFAULT_EXT + end + end + } + + [ File.expand_path(filename, Dir.pwd), + File.expand_path(filename, Pry::INITIAL_PWD), + *normalized_load_path ] + end + + + # Guess the CodeRay type of a file from its extension, or nil if + # unknown. + # + # @param [String] filename + # @param [Symbol] default (:unknown) the file type to assume if none could be + # detected. + # @return [Symbol, nil] + 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 From e26725978c741008fa6396b07216f1a65562003b Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sat, 23 Nov 2013 22:59:14 +0200 Subject: [PATCH 11/16] lib/pry/code.rb,lib/pry/code/code_file.rb: add docs --- lib/pry/code.rb | 1 + lib/pry/code/code_file.rb | 25 +++++++++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/pry/code.rb b/lib/pry/code.rb index 35c3be2c..5ba79235 100644 --- a/lib/pry/code.rb +++ b/lib/pry/code.rb @@ -41,6 +41,7 @@ class Pry # @return [Code] def from_file(filename, code_type = nil) code_file = CodeFile.new(filename, code_type) + # binding.pry new(code_file.code, 1, code_file.code_type) end diff --git a/lib/pry/code/code_file.rb b/lib/pry/code/code_file.rb index c7d1181c..17f8f616 100644 --- a/lib/pry/code/code_file.rb +++ b/lib/pry/code/code_file.rb @@ -21,13 +21,17 @@ class Pry %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) @@ -49,8 +53,8 @@ class Pry # 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." + find_abs_path(filename) or + raise MethodSource::SourceNotFoundError, "Cannot open #{filename.inspect} for reading." end def find_abs_path(filename) @@ -63,28 +67,25 @@ class Pry File.readable?(path) || 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) - normalized_load_path = $LOAD_PATH.map { |path| + normalized_load_path = $LOAD_PATH.map do |path| File.expand_path(filename, path).tap do |p| - if File.directory?(p) - p << DEFAULT_EXT - end + p << DEFAULT_EXT if File.directory?(p) end - } + end [ File.expand_path(filename, Dir.pwd), File.expand_path(filename, Pry::INITIAL_PWD), *normalized_load_path ] end - - # Guess the CodeRay type of a file from its extension, or nil if - # unknown. - # # @param [String] filename # @param [Symbol] default (:unknown) the file type to assume if none could be # detected. - # @return [Symbol, nil] + # @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) } From 3a1dad7c3fe3b9b3328e26e657ac432bc31a35fe Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sat, 23 Nov 2013 23:04:53 +0200 Subject: [PATCH 12/16] lib/pry/code/code_file.rb: fix typo in #code --- lib/pry/code/code_file.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/code/code_file.rb b/lib/pry/code/code_file.rb index 17f8f616..a7437e1d 100644 --- a/lib/pry/code/code_file.rb +++ b/lib/pry/code/code_file.rb @@ -38,7 +38,7 @@ class Pry 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)) + File.read(RbxPath.convert_path_to_full(@filename)) else abs_path = abs_path(@filename) @code_type = type_from_filename(abs_path) From 95110424c630c2436668ddd73a70aa2ba4289759 Mon Sep 17 00:00:00 2001 From: Shannon Skipper Date: Sat, 23 Nov 2013 14:46:56 -0800 Subject: [PATCH 13/16] Refactored. --- lib/pry/code/code_file.rb | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/lib/pry/code/code_file.rb b/lib/pry/code/code_file.rb index a7437e1d..618a66a2 100644 --- a/lib/pry/code/code_file.rb +++ b/lib/pry/code/code_file.rb @@ -54,31 +54,27 @@ class Pry # @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." + raise MethodSource::SourceNotFoundError, "Cannot open #{filename.inspect} for reading." end def find_abs_path(filename) - code_path(filename).detect { |path| readable_source?(path) }.tap do |path| - path << DEFAULT_EXT if path && !File.exist?(path) - end + code_path(filename).detect { |path| readable_source?(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_source?(path) - File.readable?(path) || File.readable?(path + DEFAULT_EXT) + 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) - normalized_load_path = $LOAD_PATH.map do |path| - File.expand_path(filename, path).tap do |p| - p << DEFAULT_EXT if File.directory?(p) - end - end - - [ File.expand_path(filename, Dir.pwd), - File.expand_path(filename, Pry::INITIAL_PWD), - *normalized_load_path ] + [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 From 173bbd02dac2bfd3b8744800bf0e74297fcfc827 Mon Sep 17 00:00:00 2001 From: Shannon Skipper Date: Sat, 23 Nov 2013 14:52:21 -0800 Subject: [PATCH 14/16] Shorten #readable_source? to #readable?. --- lib/pry/code/code_file.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pry/code/code_file.rb b/lib/pry/code/code_file.rb index 618a66a2..c97cb67a 100644 --- a/lib/pry/code/code_file.rb +++ b/lib/pry/code/code_file.rb @@ -58,13 +58,13 @@ class Pry end def find_abs_path(filename) - code_path(filename).detect { |path| readable_source?(path) } + 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_source?(path) + def readable?(path) File.readable?(path) && !File.directory?(path) or File.readable?(path << DEFAULT_EXT) end From 10a6739cb353fa54d24e9f8c8e5dd44a1007eeae Mon Sep 17 00:00:00 2001 From: Shannon Skipper Date: Sun, 24 Nov 2013 13:19:12 -0800 Subject: [PATCH 15/16] Add load path autocompletion for %cat command. --- lib/pry/commands/cat.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/pry/commands/cat.rb b/lib/pry/commands/cat.rb index a6f96267..f3b45514 100644 --- a/lib/pry/commands/cat.rb +++ b/lib/pry/commands/cat.rb @@ -45,7 +45,14 @@ class Pry end 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 From 957b2b246be96a9a739a8ba15e118695454c4a27 Mon Sep 17 00:00:00 2001 From: Shannon Skipper Date: Sun, 24 Nov 2013 15:04:16 -0800 Subject: [PATCH 16/16] Adjust indentions to be uniform. --- lib/pry/code/code_file.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/pry/code/code_file.rb b/lib/pry/code/code_file.rb index c97cb67a..f40dcd50 100644 --- a/lib/pry/code/code_file.rb +++ b/lib/pry/code/code_file.rb @@ -54,7 +54,8 @@ class Pry # @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." + raise MethodSource::SourceNotFoundError, + "Cannot open #{filename.inspect} for reading." end def find_abs_path(filename) @@ -63,10 +64,10 @@ class Pry # @param [String] path # @return [Boolean] if the path, with or without the default ext, - # is a readable file then `true`, otherwise `false`. + # is a readable file then `true`, otherwise `false`. def readable?(path) File.readable?(path) && !File.directory?(path) or - File.readable?(path << DEFAULT_EXT) + File.readable?(path << DEFAULT_EXT) end # @return [Array] All the paths that contain code that Pry can use for its