2020-06-23 00:09:42 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Suggestions
|
|
|
|
class FileSuggestion
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
SuggestionForDifferentFileError = Class.new(StandardError)
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
attr_reader :file_path
|
|
|
|
attr_reader :blob
|
|
|
|
attr_reader :suggestions
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def initialize(file_path, suggestions)
|
|
|
|
@file_path = file_path
|
|
|
|
@suggestions = suggestions.sort_by(&:from_line_index)
|
|
|
|
@blob = suggestions.first&.diff_file&.new_blob
|
2020-06-23 00:09:42 +05:30
|
|
|
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|
|
2020-07-28 23:09:34 +05:30
|
|
|
# NOTE: We need to cater for line number changes when the range is more than one line.
|
|
|
|
offset = 0
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
suggestions.each do |suggestion|
|
2020-07-28 23:09:34 +05:30
|
|
|
range = line_range(suggestion, offset)
|
2020-06-23 00:09:42 +05:30
|
|
|
content[range] = suggestion.to_content
|
2020-07-28 23:09:34 +05:30
|
|
|
offset += range.count - 1
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
end.join
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def line_range(suggestion, offset = 0)
|
|
|
|
(suggestion.from_line_index - offset)..(suggestion.to_line_index - offset)
|
2020-06-23 00:09:42 +05:30
|
|
|
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
|