2016-01-29 22:53:50 +05:30
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
class ParallelDiff
|
|
|
|
attr_accessor :diff_file
|
|
|
|
|
|
|
|
def initialize(diff_file)
|
|
|
|
@diff_file = diff_file
|
|
|
|
end
|
|
|
|
|
|
|
|
def parallelize
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
i = 0
|
|
|
|
free_right_index = nil
|
|
|
|
|
|
|
|
lines = []
|
2016-01-29 22:53:50 +05:30
|
|
|
highlighted_diff_lines = diff_file.highlighted_diff_lines
|
|
|
|
highlighted_diff_lines.each do |line|
|
2016-08-24 12:49:21 +05:30
|
|
|
line_code = diff_file.line_code(line)
|
|
|
|
position = diff_file.position(line)
|
2016-01-29 22:53:50 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
case line.type
|
2016-01-29 22:53:50 +05:30
|
|
|
when 'match', nil
|
|
|
|
# line in the right panel is the same as in the left one
|
|
|
|
lines << {
|
|
|
|
left: {
|
2016-08-24 12:49:21 +05:30
|
|
|
type: line.type,
|
|
|
|
number: line.old_pos,
|
|
|
|
text: line.text,
|
2016-01-29 22:53:50 +05:30
|
|
|
line_code: line_code,
|
2016-08-24 12:49:21 +05:30
|
|
|
position: position
|
2016-01-29 22:53:50 +05:30
|
|
|
},
|
|
|
|
right: {
|
2016-08-24 12:49:21 +05:30
|
|
|
type: line.type,
|
|
|
|
number: line.new_pos,
|
|
|
|
text: line.text,
|
|
|
|
line_code: line_code,
|
|
|
|
position: position
|
2016-01-29 22:53:50 +05:30
|
|
|
}
|
|
|
|
}
|
2016-08-24 12:49:21 +05:30
|
|
|
|
|
|
|
free_right_index = nil
|
|
|
|
i += 1
|
2016-01-29 22:53:50 +05:30
|
|
|
when 'old'
|
2016-08-24 12:49:21 +05:30
|
|
|
lines << {
|
|
|
|
left: {
|
|
|
|
type: line.type,
|
|
|
|
number: line.old_pos,
|
|
|
|
text: line.text,
|
|
|
|
line_code: line_code,
|
|
|
|
position: position
|
|
|
|
},
|
|
|
|
right: {
|
|
|
|
type: nil,
|
|
|
|
number: nil,
|
|
|
|
text: "",
|
|
|
|
line_code: line_code,
|
|
|
|
position: position
|
2016-01-29 22:53:50 +05:30
|
|
|
}
|
2016-08-24 12:49:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
# Once we come upon a new line it can be put on the right of this old line
|
|
|
|
free_right_index ||= i
|
|
|
|
i += 1
|
2016-01-29 22:53:50 +05:30
|
|
|
when 'new'
|
2016-08-24 12:49:21 +05:30
|
|
|
data = {
|
|
|
|
type: line.type,
|
|
|
|
number: line.new_pos,
|
|
|
|
text: line.text,
|
|
|
|
line_code: line_code,
|
|
|
|
position: position
|
|
|
|
}
|
|
|
|
|
|
|
|
if free_right_index
|
|
|
|
# If an old line came before this without a line on the right, this
|
|
|
|
# line can be put to the right of it.
|
|
|
|
lines[free_right_index][:right] = data
|
|
|
|
|
|
|
|
# If there are any other old lines on the left that don't yet have
|
|
|
|
# a new counterpart on the right, update the free_right_index
|
|
|
|
next_free_right_index = free_right_index + 1
|
|
|
|
free_right_index = next_free_right_index < i ? next_free_right_index : nil
|
2016-01-29 22:53:50 +05:30
|
|
|
else
|
|
|
|
lines << {
|
|
|
|
left: {
|
|
|
|
type: nil,
|
|
|
|
number: nil,
|
|
|
|
text: "",
|
|
|
|
line_code: line_code,
|
2016-08-24 12:49:21 +05:30
|
|
|
position: position
|
2016-01-29 22:53:50 +05:30
|
|
|
},
|
2016-08-24 12:49:21 +05:30
|
|
|
right: data
|
2016-01-29 22:53:50 +05:30
|
|
|
}
|
2016-08-24 12:49:21 +05:30
|
|
|
|
|
|
|
free_right_index = nil
|
|
|
|
i += 1
|
2016-01-29 22:53:50 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
lines
|
2016-01-29 22:53:50 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|