diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pry/code.rb | 1 | ||||
-rw-r--r-- | 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) } |