This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'method_source'
|
||||
|
||||
class Pry
|
||||
class CodeFile
|
||||
DEFAULT_EXT = '.rb'.freeze
|
||||
|
||||
# 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 .rake] => :ruby
|
||||
}.freeze
|
||||
|
||||
FILES = {
|
||||
%w[Gemfile Rakefile Guardfile Capfile] => :ruby
|
||||
}.freeze
|
||||
|
||||
# Store the current working directory. This allows show-source etc. to work if
|
||||
# your process has changed directory since boot. [Issue #675]
|
||||
INITIAL_PWD = Dir.pwd
|
||||
|
||||
# @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)
|
||||
else
|
||||
path = abs_path
|
||||
@code_type = type_from_filename(path)
|
||||
File.read(path)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# @raise [MethodSource::SourceNotFoundError] if the `filename` is not
|
||||
# readable for some reason.
|
||||
# @return [String] absolute path for the given `filename`.
|
||||
def abs_path
|
||||
code_path.detect { |path| readable?(path) } ||
|
||||
raise(MethodSource::SourceNotFoundError,
|
||||
"Cannot open #{@filename.inspect} for reading.")
|
||||
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) ||
|
||||
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
|
||||
[from_pwd, from_pry_init_pwd, *from_load_path]
|
||||
end
|
||||
|
||||
# @param [String] filename
|
||||
# @param [Symbol] default (:unknown) the file type to assume if none could be
|
||||
# detected.
|
||||
# @return [Symbol, nil] The SyntaxHighlighter 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 || FILES.find do |k, _|
|
||||
k.any? { |file_name| file_name == File.basename(filename) }
|
||||
end
|
||||
|
||||
code_type || default
|
||||
end
|
||||
|
||||
# @return [String]
|
||||
def from_pwd
|
||||
File.expand_path(@filename, Dir.pwd)
|
||||
end
|
||||
|
||||
# @return [String]
|
||||
def from_pry_init_pwd
|
||||
File.expand_path(@filename, INITIAL_PWD)
|
||||
end
|
||||
|
||||
# @return [String]
|
||||
def from_load_path
|
||||
$LOAD_PATH.map { |path| File.expand_path(@filename, path) }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,73 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Pry
|
||||
class Code
|
||||
# Represents a range of lines in a code listing.
|
||||
#
|
||||
# @api private
|
||||
class CodeRange
|
||||
# @param [Integer] start_line
|
||||
# @param [Integer?] end_line
|
||||
def initialize(start_line, end_line = nil)
|
||||
@start_line = start_line
|
||||
@end_line = end_line
|
||||
force_set_end_line
|
||||
end
|
||||
|
||||
# @param [Array<LOC>] lines
|
||||
# @return [Range]
|
||||
def indices_range(lines)
|
||||
Range.new(*indices(lines))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :start_line
|
||||
|
||||
attr_reader :end_line
|
||||
|
||||
# If `end_line` is equal to `nil`, then calculate it from the first
|
||||
# parameter, `start_line`. Otherwise, leave it as it is.
|
||||
# @return [void]
|
||||
def force_set_end_line
|
||||
if start_line.is_a?(Range)
|
||||
set_end_line_from_range
|
||||
else
|
||||
@end_line ||= start_line
|
||||
end
|
||||
end
|
||||
|
||||
# Finds indices of `start_line` and `end_line` in the given Array of
|
||||
# +lines+.
|
||||
#
|
||||
# @param [Array<LOC>] lines
|
||||
# @return [Array<Integer>]
|
||||
def indices(lines)
|
||||
[find_start_index(lines), find_end_index(lines)]
|
||||
end
|
||||
|
||||
# @return [Integer]
|
||||
def find_start_index(lines)
|
||||
return start_line if start_line < 0
|
||||
|
||||
lines.index { |loc| loc.lineno >= start_line } || lines.length
|
||||
end
|
||||
|
||||
# @return [Integer]
|
||||
def find_end_index(lines)
|
||||
return end_line if end_line < 0
|
||||
|
||||
(lines.index { |loc| loc.lineno > end_line } || 0) - 1
|
||||
end
|
||||
|
||||
# For example, if the range is 4..10, then `start_line` would be equal to
|
||||
# 4 and `end_line` to 10.
|
||||
# @return [void]
|
||||
def set_end_line_from_range
|
||||
@end_line = start_line.last
|
||||
@end_line -= 1 if start_line.exclude_end?
|
||||
@start_line = start_line.first
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,105 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Pry
|
||||
class Code
|
||||
# Represents a line of code (which may, in fact, contain multiple lines if
|
||||
# the entirety was eval'd as a single unit following the `edit` command).
|
||||
#
|
||||
# A line of code is a tuple, which consists of a line and a line number. A
|
||||
# `LOC` object's state (namely, the line parameter) can be changed via
|
||||
# instance methods. `Pry::Code` heavily uses this class.
|
||||
#
|
||||
# @api private
|
||||
# @example
|
||||
# loc = LOC.new("def example\n :example\nend", 1)
|
||||
# puts loc.line
|
||||
# def example
|
||||
# :example
|
||||
# end
|
||||
# #=> nil
|
||||
#
|
||||
# loc.indent(3)
|
||||
# loc.line #=> " def example\n :example\nend"
|
||||
class LOC
|
||||
# @return [Array<String, Integer>]
|
||||
attr_reader :tuple
|
||||
|
||||
# @param [String] line The line of code.
|
||||
# @param [Integer] lineno The position of the +line+.
|
||||
def initialize(line, lineno)
|
||||
@tuple = [line.chomp, lineno.to_i]
|
||||
end
|
||||
|
||||
# @return [Boolean]
|
||||
def ==(other)
|
||||
other.tuple == tuple
|
||||
end
|
||||
|
||||
def dup
|
||||
self.class.new(line, lineno)
|
||||
end
|
||||
|
||||
# @return [String]
|
||||
def line
|
||||
tuple.first
|
||||
end
|
||||
|
||||
# @return [Integer]
|
||||
def lineno
|
||||
tuple.last
|
||||
end
|
||||
|
||||
# Paints the `line` of code.
|
||||
#
|
||||
# @param [Symbol] code_type
|
||||
# @return [void]
|
||||
def colorize(code_type)
|
||||
tuple[0] = SyntaxHighlighter.highlight(line, code_type)
|
||||
end
|
||||
|
||||
# Prepends the line number `lineno` to the `line`.
|
||||
#
|
||||
# @param [Integer] max_width
|
||||
# @return [void]
|
||||
def add_line_number(max_width = 0, color = false)
|
||||
padded = lineno.to_s.rjust(max_width)
|
||||
colorized_lineno =
|
||||
if color
|
||||
Pry::Helpers::BaseHelpers.colorize_code(padded)
|
||||
else
|
||||
padded
|
||||
end
|
||||
properly_padded_line = handle_multiline_entries_from_edit_command(line, max_width)
|
||||
tuple[0] = "#{colorized_lineno}: #{properly_padded_line}"
|
||||
end
|
||||
|
||||
# Prepends a marker "=>" or an empty marker to the +line+.
|
||||
#
|
||||
# @param [Integer] marker_lineno If it is equal to the `lineno`, then
|
||||
# prepend a hashrocket. Otherwise, an empty marker.
|
||||
# @return [void]
|
||||
def add_marker(marker_lineno)
|
||||
tuple[0] =
|
||||
if lineno == marker_lineno
|
||||
" => #{line}"
|
||||
else
|
||||
" #{line}"
|
||||
end
|
||||
end
|
||||
|
||||
# Indents the `line` with +distance+ spaces.
|
||||
#
|
||||
# @param [Integer] distance
|
||||
# @return [void]
|
||||
def indent(distance)
|
||||
tuple[0] = "#{' ' * distance}#{line}"
|
||||
end
|
||||
|
||||
def handle_multiline_entries_from_edit_command(line, max_width)
|
||||
line.split("\n").map.with_index do |inner_line, i|
|
||||
i.zero? ? inner_line : "#{' ' * (max_width + 2)}#{inner_line}"
|
||||
end.join("\n")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user