summaryrefslogtreecommitdiff
path: root/lib/gitlab/suggestions/file_suggestion.rb
blob: 7805b27902d6c0383144aa0214836b42a5eeda72 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# frozen_string_literal: true

module Gitlab
  module Suggestions
    class FileSuggestion
      include Gitlab::Utils::StrongMemoize

      SuggestionForDifferentFileError = Class.new(StandardError)

      attr_reader :file_path
      attr_reader :blob
      attr_reader :suggestions

      def initialize(file_path, suggestions)
        @file_path = file_path
        @suggestions = suggestions.sort_by(&:from_line_index)
        @blob = suggestions.first&.diff_file&.new_blob
      end

      def line_conflict?
        strong_memoize(:line_conflict) do
          _line_conflict?
        end
      end

      def new_content
        @new_content ||= _new_content
      end

      private

      def blob_data_lines
        blob.load_all_data!
        blob.data.lines
      end

      def current_content
        @current_content ||= blob.nil? ? [''] : blob_data_lines
      end

      def _new_content
        current_content.tap do |content|
          # NOTE: We need to cater for line number changes when the range is more than one line.
          offset = 0

          suggestions.each do |suggestion|
            range = line_range(suggestion, offset)
            content[range] = suggestion.to_content
            offset += range.count - 1
          end
        end.join
      end

      def line_range(suggestion, offset = 0)
        (suggestion.from_line_index - offset)..(suggestion.to_line_index - offset)
      end

      def _line_conflict?
        has_conflict = false

        suggestions.each_with_object([]) do |suggestion, ranges|
          range_in_test = line_range(suggestion)

          if has_range_conflict?(range_in_test, ranges)
            has_conflict = true
            break
          end

          ranges << range_in_test
        end

        has_conflict
      end

      def has_range_conflict?(range_in_test, ranges)
        ranges.any? do |range|
          range.overlaps?(range_in_test)
        end
      end
    end
  end
end