Class: DebuggerCode

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-debugger/debugger_code.rb,
lib/puppet-debugger/code/loc.rb,
lib/puppet-debugger/code/code_range.rb

Overview

‘Pry::Code` is a class that encapsulates lines of source code and their line numbers and formats them for terminal output. It can read from a file or method definition or be instantiated with a `String` or an `Array`.

In general, the formatting methods in ‘Code` return a new `Code` object which will format the text as specified when `#to_s` is called. This allows arbitrary chaining of formatting methods without mutating the original object.

Defined Under Namespace

Classes: CodeRange, LOC

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines = [], start_line = 1, code_type = :ruby, filename = nil) ⇒ DebuggerCode

Instantiate a ‘Code` object containing code from the given `Array`, `String`, or `IO`. The first line will be line 1 unless specified otherwise. If you need non-contiguous line numbers, you can create an empty `Code` object and then use `#push` to insert the lines.

Parameters:

  • lines (Array<String>, String, IO) (defaults to: [])
  • start_line (Integer?) (defaults to: 1)
  • code_type (Symbol?) (defaults to: :ruby)


52
53
54
55
56
57
58
59
60
61
# File 'lib/puppet-debugger/debugger_code.rb', line 52

def initialize(lines = [], start_line = 1, code_type = :ruby, filename = nil)
  lines = lines.lines if lines.is_a? String
  @lines = lines.each_with_index.map do |line, lineno|
    LOC.new(line, lineno + start_line.to_i)
  end
  @code_type = code_type
  @filename = filename
  @with_file_reference = nil
  @with_marker = @with_indentation = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Forward any missing methods to the output of ‘#to_s`.



323
324
325
# File 'lib/puppet-debugger/debugger_code.rb', line 323

def method_missing(name, *args, &block)
  to_s.send(name, *args, &block)
end

Instance Attribute Details

#code_typeSymbol

Returns The type of code stored in this wrapper.

Returns:

  • (Symbol)

    The type of code stored in this wrapper.



42
43
44
# File 'lib/puppet-debugger/debugger_code.rb', line 42

def code_type
  @code_type
end

#filenameSymbol

Returns The type of code stored in this wrapper.

Returns:

  • (Symbol)

    The type of code stored in this wrapper.



42
43
44
# File 'lib/puppet-debugger/debugger_code.rb', line 42

def filename
  @filename
end

Class Method Details

.expression_at(raw, _line_number, _consume = 0) ⇒ String

Get the multiline expression that starts on the given line number.

Parameters:

  • line_number (Integer)

    (1-based)

Returns:



280
281
282
283
# File 'lib/puppet-debugger/debugger_code.rb', line 280

def self.expression_at(raw, _line_number, _consume = 0)
  # self.class.expression_at(raw, line_number, :consume => consume)
  raw
end

.from_file(filename, code_type = nil) ⇒ Code

Instantiate a ‘Code` object containing code loaded from a file or Pry’s line buffer.

Parameters:

  • filename (String)

    The name of a file, or “(pry)”.

  • code_type (Symbol) (defaults to: nil)

    The type of code the file contains.

Returns:

  • (Code)


25
26
27
28
# File 'lib/puppet-debugger/debugger_code.rb', line 25

def from_file(filename, code_type = nil)
  code_file = CodeFile.new(filename, code_type)
  new(code_file.code, 1, code_file.code_type, filename)
end

.from_string(code, code_type = nil) ⇒ Code

Instantiate a ‘Code` object containing code loaded from a file or Pry’s line buffer.

Parameters:

  • source (String)

    code“.

  • code_type (Symbol) (defaults to: nil)

    The type of code the file contains.

Returns:

  • (Code)


36
37
38
# File 'lib/puppet-debugger/debugger_code.rb', line 36

def from_string(code, code_type = nil)
  new(code, 1, code_type)
end

Instance Method Details

#==(other) ⇒ Boolean

Two ‘Code` objects are equal if they contain the same lines with the same numbers. Otherwise, call `to_s` and `chomp` and compare as Strings.

Parameters:

  • other (Code, Object)

Returns:

  • (Boolean)


313
314
315
316
317
318
319
320
# File 'lib/puppet-debugger/debugger_code.rb', line 313

def ==(other)
  if other.is_a?(Code)
    other_lines = other.instance_variable_get(:@lines)
    @lines.each_with_index.all? { |loc, i| loc == other_lines[i] }
  else
    to_s.chomp == other.to_s.chomp
  end
end

#add_file_referenceObject



239
240
241
242
# File 'lib/puppet-debugger/debugger_code.rb', line 239

def add_file_reference
  return "From inline code: \n" unless filename
  "From file: #{File.basename(filename)}\n"
end

#after(lineno, lines = 1) ⇒ Code

Remove all lines except for the lines after and excluding lineno.

Parameters:

  • lineno (Integer)
  • lines (Integer) (defaults to: 1)

Returns:

  • (Code)


153
154
155
156
157
158
159
# File 'lib/puppet-debugger/debugger_code.rb', line 153

def after(lineno, lines = 1)
  return self unless lineno

  select do |loc|
    loc.lineno > lineno && loc.lineno <= lineno + lines
  end
end

#around(lineno, lines = 1) ⇒ Code

Remove all lines except for the lines on either side of and including lineno.

Parameters:

  • lineno (Integer)
  • lines (Integer) (defaults to: 1)

Returns:

  • (Code)


140
141
142
143
144
145
146
# File 'lib/puppet-debugger/debugger_code.rb', line 140

def around(lineno, lines = 1)
  return self unless lineno

  select do |loc|
    loc.lineno >= lineno - lines && loc.lineno <= lineno + lines
  end
end

#before(lineno, lines = 1) ⇒ Code

Remove all lines except for the lines up to and excluding lineno.

Parameters:

  • lineno (Integer)
  • lines (Integer) (defaults to: 1)

Returns:

  • (Code)


126
127
128
129
130
131
132
# File 'lib/puppet-debugger/debugger_code.rb', line 126

def before(lineno, lines = 1)
  return self unless lineno

  select do |loc|
    loc.lineno >= lineno - lines && loc.lineno < lineno
  end
end

#between(start_line, end_line = nil) ⇒ Code

Remove all lines that aren’t in the given range, expressed either as a ‘Range` object or a first and last line number (inclusive). Negative indices count from the end of the array of lines.

Parameters:

  • start_line (Range, Integer)
  • end_line (Integer?) (defaults to: nil)

Returns:

  • (Code)


93
94
95
96
97
98
99
100
101
# File 'lib/puppet-debugger/debugger_code.rb', line 93

def between(start_line, end_line = nil)
  return self unless start_line

  code_range = CodeRange.new(start_line, end_line)

  alter do
    @lines = @lines[code_range.indices_range(@lines)] || []
  end
end

#comment_describing(line_number) ⇒ String

Get the comment that describes the expression on the given line number.

Parameters:

  • line_number (Integer)

    (1-based)

Returns:



264
265
266
# File 'lib/puppet-debugger/debugger_code.rb', line 264

def comment_describing(line_number)
  self.class.comment_describing(raw, line_number)
end

#expression_at(line_number, consume = 0) ⇒ String

Get the multiline expression that starts on the given line number.

Parameters:

  • line_number (Integer)

    (1-based)

Returns:



272
273
274
# File 'lib/puppet-debugger/debugger_code.rb', line 272

def expression_at(line_number, consume = 0)
  self.class.expression_at(raw, line_number, consume: consume)
end

#grep(pattern) ⇒ Code

Remove all lines that don’t match the given ‘pattern`.

Parameters:

  • pattern (Regexp)

Returns:

  • (Code)


165
166
167
168
169
170
171
172
# File 'lib/puppet-debugger/debugger_code.rb', line 165

def grep(pattern)
  return self unless pattern
  pattern = Regexp.new(pattern)

  select do |loc|
    loc.line =~ pattern
  end
end

#highlightedString

Returns a (possibly highlighted) copy of the source code.

Returns:

  • (String)

    a (possibly highlighted) copy of the source code.



235
236
237
# File 'lib/puppet-debugger/debugger_code.rb', line 235

def highlighted
  print_to_output('', true)
end

#inspectString

Returns:



219
220
221
# File 'lib/puppet-debugger/debugger_code.rb', line 219

def inspect
  Object.instance_method(:to_s).bind(self).call
end

#lengthInteger

Return the number of lines stored.

Returns:

  • (Integer)


304
305
306
# File 'lib/puppet-debugger/debugger_code.rb', line 304

def length
  @lines ? @lines.length : 0
end

#max_lineno_widthInteger

Returns the number of digits in the last line.

Returns:

  • (Integer)

    the number of digits in the last line.



224
225
226
# File 'lib/puppet-debugger/debugger_code.rb', line 224

def max_lineno_width
  !@lines.empty? ? @lines.last.lineno.to_s.length : 0
end

#nesting_at(line_number, _top_module = Object) ⇒ Array<Module>

Get the (approximate) Module.nesting at the give line number.

Parameters:

  • line_number (Integer)

    line number starting from 1

  • top_module (Module)

    the module in which this code exists

Returns:

  • (Array<Module>)

    a list of open modules.



290
291
292
# File 'lib/puppet-debugger/debugger_code.rb', line 290

def nesting_at(line_number, _top_module = Object)
  Indent.nesting_at(raw, line_number)
end

Writes a formatted representation (based on the configuration of the object) to the given output, which must respond to ‘#<<`.



246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/puppet-debugger/debugger_code.rb', line 246

def print_to_output(output, _color = false)
  print_output = output.dup
  print_output << add_file_reference if @with_file_reference
  @lines.each do |loc|
    loc = loc.dup
    loc.add_line_number(max_lineno_width) if @with_line_numbers
    loc.add_marker(@marker_lineno)        if @with_marker
    loc.indent(@indentation_num)          if @with_indentation
    print_output << loc.line
    print_output << "\n"
  end
  print_output
end

#push(line, lineno = nil) ⇒ String Also known as: <<

Append the given line. lineno is one more than the last existing line, unless specified otherwise.

Parameters:

  • line (String)
  • lineno (Integer?) (defaults to: nil)

Returns:

  • (String)

    The inserted line.



69
70
71
72
73
# File 'lib/puppet-debugger/debugger_code.rb', line 69

def push(line, lineno = nil)
  lineno = @lines.last.lineno + 1 if lineno.nil?
  @lines.push(LOC.new(line, lineno))
  line
end

#rawString

Return an unformatted String of the code.

Returns:



297
298
299
# File 'lib/puppet-debugger/debugger_code.rb', line 297

def raw
  @lines.map(&:line).join("\n") << "\n"
end

#select {|LOC| ... } ⇒ Code

Filter the lines using the given block.

Yields:

Returns:

  • (Code)


80
81
82
83
84
# File 'lib/puppet-debugger/debugger_code.rb', line 80

def select(&block)
  alter do
    @lines = @lines.select(&block)
  end
end

#take_lines(start_line, num_lines) ⇒ Code

Take ‘num_lines` from `start_line`, forward or backwards.

Parameters:

  • start_line (Integer)
  • num_lines (Integer)

Returns:

  • (Code)


108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/puppet-debugger/debugger_code.rb', line 108

def take_lines(start_line, num_lines)
  start_idx =
    if start_line >= 0
      @lines.index { |loc| loc.lineno >= start_line } || @lines.length
    else
      [@lines.length + start_line, 0].max
    end

  alter do
    @lines = @lines.slice(start_idx, num_lines)
  end
end

#to_sString

Returns a formatted representation (based on the configuration of the object).

Returns:

  • (String)

    a formatted representation (based on the configuration of the object).



230
231
232
# File 'lib/puppet-debugger/debugger_code.rb', line 230

def to_s
  print_to_output('', false)
end

#with_file_reference(y_n = true) ⇒ Code

Format output with line numbers next to it, unless ‘y_n` is falsy.

Parameters:

  • y_n (Boolean?) (defaults to: true)

Returns:

  • (Code)


188
189
190
191
192
# File 'lib/puppet-debugger/debugger_code.rb', line 188

def with_file_reference(y_n = true)
  alter do
    @with_file_reference = y_n
  end
end

#with_indentation(spaces = 0) ⇒ Code

Format output with the specified number of spaces in front of every line, unless ‘spaces` is falsy.

Parameters:

  • spaces (Integer?) (defaults to: 0)

Returns:

  • (Code)


211
212
213
214
215
216
# File 'lib/puppet-debugger/debugger_code.rb', line 211

def with_indentation(spaces = 0)
  alter do
    @with_indentation = !!spaces
    @indentation_num  = spaces
  end
end

#with_line_numbers(y_n = true) ⇒ Code

Format output with line numbers next to it, unless ‘y_n` is falsy.

Parameters:

  • y_n (Boolean?) (defaults to: true)

Returns:

  • (Code)


178
179
180
181
182
# File 'lib/puppet-debugger/debugger_code.rb', line 178

def with_line_numbers(y_n = true)
  alter do
    @with_line_numbers = y_n
  end
end

#with_marker(lineno = 1) ⇒ Code

Format output with a marker next to the given lineno, unless lineno is falsy.

Parameters:

  • lineno (Integer?) (defaults to: 1)

Returns:

  • (Code)


199
200
201
202
203
204
# File 'lib/puppet-debugger/debugger_code.rb', line 199

def with_marker(lineno = 1)
  alter do
    @with_marker   = !!lineno
    @marker_lineno = lineno
  end
end