Welcome to mirror list, hosted at ThFree Co, Russian Federation.

chunk_collection.rb « word_diff « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d5c3e59d405149dc98a9d8bb91e8741fc0e43f9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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

      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
    end
  end
end