Module: Black::Diff::OutputParser

Defined in:
lib/black/diff.rb

Class Method Summary collapse

Class Method Details

.diff_optionsObject

‘diff` command arguments to format the output



54
55
56
# File 'lib/black/diff.rb', line 54

def self.diff_options
  %w(-u)
end

.identify_type(line) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/black/diff.rb', line 91

def self.identify_type(line)
  if line.start_with?('---', '+++')
    "file-header"
  elsif line[0] == "+"
    "addition"
  elsif line[0] == "-"
    "deletion"
  elsif line.match(/^@@ -/)
    "metadata"
  else
    "unchanged"
  end
end

.parse(diff_enumerator) ⇒ Object

Parse the diff output



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
# File 'lib/black/diff.rb', line 59

def self.parse(diff_enumerator)
  Enumerator.new do |y|
    index = 0
    old_line_number = 1
    new_line_number = 1

    diff_enumerator.each do |line|
      type = identify_type(line)
      next if type == 'file-header'

      if type == "metadata"
        old_line_number = line.match(/\-[0-9]*/)[0].to_i.abs rescue 0
        new_line_number = line.match(/\+[0-9]*/)[0].to_i.abs rescue 0
      end

      d = {
        "type" => type,
        "index" => index,
        "old_line_number" => old_line_number,
        "new_line_number" => new_line_number,
        "content" => line
      }

      y.yield Line.new(d)

      index += 1
      new_line_number += 1 if %w(addition unchanged).include?(type)
      old_line_number += 1 if %w(deletion unchanged).include?(type)
    end
  end
end