summaryrefslogtreecommitdiff
path: root/lib/gitlab/diff/suggestion_diff.rb
blob: ee153c226b70b7a7ea671b64ba7bfe67f6847254 (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
# frozen_string_literal: true

module Gitlab
  module Diff
    class SuggestionDiff
      include Gitlab::Utils::StrongMemoize

      delegate :from_content, :to_content, :from_line, to: :@suggestible

      def initialize(suggestible)
        @suggestible = suggestible
      end

      def diff_lines
        Gitlab::Diff::Parser.new.parse(raw_diff.each_line).to_a
      end

      private

      def raw_diff
        "#{diff_header}\n#{from_content_as_diff}#{to_content_as_diff}"
      end

      def diff_header
        "@@ -#{from_line} +#{from_line}"
      end

      def from_content_as_diff
        from_content.lines.map { |line| line.prepend('-') }.join
      end

      def to_content_as_diff
        to_content.lines.map { |line| line.prepend('+') }.join
      end
    end
  end
end