lib/devloop/diff_parser.rb in devloop-0.1.2 vs lib/devloop/diff_parser.rb in devloop-0.1.3
- old
+ new
@@ -2,33 +2,38 @@
class DiffParser
def self.call(diff)
new.call(diff)
end
+ Diff = Struct.new(:filename, :line_number)
+
def call(diff)
- _, results = diff.split("\n").reduce(["", []]) do |(file, results), line|
+ _, diffs_data = diff.split("\n").reduce(["", []]) do |(file, diffs_data), line|
if line.start_with?("+++ b/")
- [line[6..-1], results]
+ [line[6..-1], diffs_data]
elsif line.start_with?("@@ -")
line_number = line.match(/@@ -(\d+)/)[1]
- if line_number == "1"
- [file, results << "#{relative_path(file)}"]
- else
- [file, results << "#{relative_path(file)}:#{line_number}"]
- end
+ [file, diffs_data << Diff.new(relative_path(file), line_number)]
else
- [file, results]
+ [file, diffs_data]
end
end.uniq
- # Remove filenames with line number if filename without line number is present
- res = results.reject { |result| results.include?(result.split(":").first) && result.include?(":") }.map do |el|
- if el.split(":").last == "0"
- el.split(":").first
+ results = diffs_data.group_by do |el|
+ el.filename
+ end.map do |key, value|
+ line_numbers = value.map(&:line_number).map(&:to_i)
+ if line_numbers.include?(0) || line_numbers.include?(1)
+ key
else
- el
+ lines_range = (line_numbers.min..line_numbers.max).to_a.join(":")
+
+ "#{key}:#{lines_range}"
end
end
+
+ # Remove filenames with line number if filename without line number is present
+ res = results.reject { |result| results.include?(result.split(":").first) && result.include?(":") }
end
private
def relative_path(path)