2018-08-03 17:22:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-04 22:27:23 +00:00
|
|
|
# Contains functionality shared between `DiffDiscussion` and `LegacyDiffDiscussion`.
|
2017-03-31 00:38:21 +00:00
|
|
|
module DiscussionOnDiff
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2017-04-12 17:59:35 +00:00
|
|
|
NUMBER_OF_TRUNCATED_DIFF_LINES = 16
|
2017-03-31 00:38:21 +00:00
|
|
|
|
2017-04-12 17:59:35 +00:00
|
|
|
included do
|
2017-03-31 00:38:21 +00:00
|
|
|
delegate :line_code,
|
|
|
|
:original_line_code,
|
2018-12-16 16:00:43 +00:00
|
|
|
:note_diff_file,
|
2017-03-31 00:38:21 +00:00
|
|
|
:diff_line,
|
|
|
|
:active?,
|
2017-04-30 20:32:09 +00:00
|
|
|
:created_at_diff?,
|
2017-03-31 00:38:21 +00:00
|
|
|
|
|
|
|
to: :first_note
|
|
|
|
|
|
|
|
delegate :file_path,
|
|
|
|
:blob,
|
|
|
|
:highlighted_diff_lines,
|
|
|
|
:diff_lines,
|
|
|
|
|
|
|
|
to: :diff_file,
|
|
|
|
allow_nil: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def diff_discussion?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-10-07 04:25:17 +00:00
|
|
|
def file_new_path
|
|
|
|
first_note.position.new_path
|
|
|
|
end
|
|
|
|
|
2017-06-29 21:19:09 +00:00
|
|
|
def on_merge_request_commit?
|
|
|
|
for_merge_request? && commit_id.present?
|
|
|
|
end
|
|
|
|
|
2017-03-31 00:38:21 +00:00
|
|
|
# Returns an array of at most 16 highlighted lines above a diff note
|
2018-12-07 14:24:37 +00:00
|
|
|
def truncated_diff_lines(highlight: true, diff_limit: nil)
|
2018-12-20 14:24:25 +00:00
|
|
|
return [] unless on_text?
|
2020-01-31 12:08:33 +00:00
|
|
|
return [] if diff_line.nil?
|
2018-01-30 15:17:44 +00:00
|
|
|
|
2018-12-07 14:24:37 +00:00
|
|
|
diff_limit = [diff_limit, NUMBER_OF_TRUNCATED_DIFF_LINES].compact.min
|
2017-03-31 00:38:21 +00:00
|
|
|
lines = highlight ? highlighted_diff_lines : diff_lines
|
2017-12-22 11:49:56 +00:00
|
|
|
|
2018-12-07 14:24:37 +00:00
|
|
|
initial_line_index = [diff_line.index - diff_limit + 1, 0].max
|
2017-12-22 11:49:56 +00:00
|
|
|
|
2017-03-31 00:38:21 +00:00
|
|
|
prev_lines = []
|
|
|
|
|
2017-12-22 11:49:56 +00:00
|
|
|
lines[initial_line_index..diff_line.index].each do |line|
|
2017-03-31 00:38:21 +00:00
|
|
|
if line.meta?
|
|
|
|
prev_lines.clear
|
|
|
|
else
|
|
|
|
prev_lines << line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
prev_lines
|
|
|
|
end
|
2017-05-24 15:10:10 +00:00
|
|
|
|
2018-12-16 16:00:43 +00:00
|
|
|
def diff_file
|
|
|
|
strong_memoize(:diff_file) do
|
|
|
|
# Falling back here is important as `note_diff_files` are created async.
|
|
|
|
fetch_preloaded_diff_file || first_note.diff_file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-24 15:10:10 +00:00
|
|
|
def line_code_in_diffs(diff_refs)
|
|
|
|
if active?(diff_refs)
|
|
|
|
line_code
|
|
|
|
elsif diff_refs && created_at_diff?(diff_refs)
|
|
|
|
original_line_code
|
|
|
|
end
|
|
|
|
end
|
2018-12-16 16:00:43 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_preloaded_diff_file
|
|
|
|
fetch_preloaded_diff =
|
|
|
|
context_noteable &&
|
|
|
|
context_noteable.preloads_discussion_diff_highlighting? &&
|
|
|
|
note_diff_file
|
|
|
|
|
|
|
|
context_noteable.discussions_diffs.find_by_id(note_diff_file.id) if fetch_preloaded_diff
|
|
|
|
end
|
2017-03-31 00:38:21 +00:00
|
|
|
end
|