summaryrefslogtreecommitdiff
path: root/lib/pry/code/loc.rb
blob: 8cb3d8fe161b59a785af253cf39786738e95c124 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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