Class: Pry::Code::LOC Private

Inherits:
Object show all
Defined in:
lib/pry/code/loc.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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.

Examples:

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"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line, lineno) ⇒ LOC

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of LOC.

Parameters:

  • line (String)

    The line of code.

  • lineno (Integer)

    The position of the +line+.


29
30
31
# File 'lib/pry/code/loc.rb', line 29

def initialize(line, lineno)
  @tuple = [line.chomp, lineno.to_i]
end

Instance Attribute Details

#tupleArray<String, Integer> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Array<String, Integer>)

25
26
27
# File 'lib/pry/code/loc.rb', line 25

def tuple
  @tuple
end

Instance Method Details

#==(other) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

34
35
36
# File 'lib/pry/code/loc.rb', line 34

def ==(other)
  other.tuple == tuple
end

#add_line_number(max_width = 0, color = false)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Prepends the line number lineno to the line.

Parameters:

  • max_width (Integer) (defaults to: 0)

64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pry/code/loc.rb', line 64

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

#add_marker(marker_lineno)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Prepends a marker "=>" or an empty marker to the +line+.

Parameters:

  • marker_lineno (Integer)

    If it is equal to the lineno, then prepend a hashrocket. Otherwise, an empty marker.


81
82
83
84
85
86
87
88
# File 'lib/pry/code/loc.rb', line 81

def add_marker(marker_lineno)
  tuple[0] =
    if lineno == marker_lineno
      " => #{line}"
    else
      "    #{line}"
    end
end

#colorize(code_type)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Paints the line of code.

Parameters:

  • code_type (Symbol)

56
57
58
# File 'lib/pry/code/loc.rb', line 56

def colorize(code_type)
  tuple[0] = SyntaxHighlighter.highlight(line, code_type)
end

#dupObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


38
39
40
# File 'lib/pry/code/loc.rb', line 38

def dup
  self.class.new(line, lineno)
end

#handle_multiline_entries_from_edit_command(line, max_width) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


98
99
100
101
102
# File 'lib/pry/code/loc.rb', line 98

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

#indent(distance)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Indents the line with +distance+ spaces.

Parameters:

  • distance (Integer)

94
95
96
# File 'lib/pry/code/loc.rb', line 94

def indent(distance)
  tuple[0] = "#{' ' * distance}#{line}"
end

#lineString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)

43
44
45
# File 'lib/pry/code/loc.rb', line 43

def line
  tuple.first
end

#linenoInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Integer)

48
49
50
# File 'lib/pry/code/loc.rb', line 48

def lineno
  tuple.last
end