Skip to content

Commit

Permalink
Add some comments for Pry::Code::LOC
Browse files Browse the repository at this point in the history
Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
  • Loading branch information
kyrylo committed Jan 5, 2013
1 parent 2abf232 commit 8b2fb32
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/pry/code.rb
Expand Up @@ -26,9 +26,29 @@ def Code(obj)
# arbitrary chaining of formatting methods without mutating the original # arbitrary chaining of formatting methods without mutating the original
# object. # object.
class Code class Code
# Represents a line of code. 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 class LOC

# @return [Array<String, Integer>]
attr_accessor :tuple attr_accessor :tuple


# @param [String] line The line of code.
# @param [Integer] lineno The position of the +line+.
def initialize(line, lineno) def initialize(line, lineno)
@tuple = [line.chomp, lineno.to_i] @tuple = [line.chomp, lineno.to_i]
end end
Expand All @@ -37,18 +57,28 @@ def dup
self.class.new(line, lineno) self.class.new(line, lineno)
end end


# @return [String]
def line def line
tuple.first tuple.first
end end


# @return [Integer]
def lineno def lineno
tuple.last tuple.last
end end


# Paints the `line` of code.
#
# @param [Symbol] code_type
# @return [void]
def colorize(code_type) def colorize(code_type)
tuple[0] = CodeRay.scan(line, code_type).term tuple[0] = CodeRay.scan(line, code_type).term
end end


# Prepends the line number `lineno` to the `line`.
#
# @param [Integer] max_width
# @return [void]
def add_line_numbers(max_width = 0) def add_line_numbers(max_width = 0)
padded = lineno.to_s.rjust(max_width) padded = lineno.to_s.rjust(max_width)
colorized_lineno = Pry::Helpers::BaseHelpers.colorize_code(padded) colorized_lineno = Pry::Helpers::BaseHelpers.colorize_code(padded)
Expand All @@ -64,6 +94,10 @@ def add_marker(marker_lineno)
end end
end end


# Indents the `line` with +distance+ spaces.
#
# @param [Integer] distance
# @return [void]
def indent(distance) def indent(distance)
tuple[0] = "#{ ' ' * distance }#{ line }" tuple[0] = "#{ ' ' * distance }#{ line }"
end end
Expand Down

0 comments on commit 8b2fb32

Please sign in to comment.