2021-03-02 16:11:07 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module WordDiff
|
|
|
|
class ChunkCollection
|
|
|
|
def initialize
|
|
|
|
@chunks = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def add(chunk)
|
|
|
|
@chunks << chunk
|
|
|
|
end
|
|
|
|
|
|
|
|
def content
|
|
|
|
@chunks.join('')
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset
|
|
|
|
@chunks = []
|
|
|
|
end
|
2021-03-22 23:09:04 -04:00
|
|
|
|
|
|
|
def marker_ranges
|
|
|
|
start = 0
|
|
|
|
|
|
|
|
@chunks.each_with_object([]) do |element, ranges|
|
|
|
|
mode = mode_for_element(element)
|
|
|
|
|
|
|
|
ranges << Gitlab::MarkerRange.new(start, start + element.length - 1, mode: mode) if mode
|
|
|
|
|
|
|
|
start += element.length
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def mode_for_element(element)
|
|
|
|
return Gitlab::MarkerRange::DELETION if element.removed?
|
|
|
|
return Gitlab::MarkerRange::ADDITION if element.added?
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
2021-03-02 16:11:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|