Module: DeepCover::Tools::FormatGeneratedCode

Defined in:
lib/deep_cover/tools/format_generated_code.rb

Instance Method Summary collapse

Instance Method Details

#format_generated_code(covered_code) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/deep_cover/tools/format_generated_code.rb', line 5

def format_generated_code(covered_code)
  inserts = []
  generated_code = covered_code.instrument_source do |inserted, _node, expr_limit|
    inserts << [expr_limit, inserted.size]
    Term::ANSIColor.yellow(inserted)
  end

  inserts = inserts.sort_by { |exp, _| [exp.line, exp.column] }.reverse
  generated_lines = generated_code.split("\n")

  inserts.each do |exp_limit, size|
    # Line index starts at 1, so array index returns the next line
    comment_line = generated_lines[exp_limit.line]
    next if Tools.blank?(comment_line)
    next unless comment_line.start_with?('#>')
    next if comment_line.start_with?('#>X')
    next unless comment_line.size >= exp_limit.column
    comment_line.insert(exp_limit.column, ' ' * size)
  end
  generated_lines.join("\n")
end