2019-01-23 03:50:19 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Blobs
|
|
|
|
class UnfoldPresenter < BlobPresenter
|
2019-08-20 06:52:21 -04:00
|
|
|
include ActiveModel::Attributes
|
|
|
|
include ActiveModel::AttributeAssignment
|
2019-01-23 03:50:19 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2021-09-21 05:12:21 -04:00
|
|
|
presents ::Blob
|
|
|
|
|
2019-08-20 06:52:21 -04:00
|
|
|
attribute :full, :boolean, default: false
|
|
|
|
attribute :since, :integer, default: 1
|
|
|
|
attribute :to, :integer, default: 1
|
|
|
|
attribute :bottom, :boolean, default: false
|
|
|
|
attribute :unfold, :boolean, default: true
|
|
|
|
attribute :offset, :integer, default: 0
|
|
|
|
attribute :indent, :integer, default: 0
|
|
|
|
|
|
|
|
alias_method :full?, :full
|
|
|
|
alias_method :bottom?, :bottom
|
|
|
|
alias_method :unfold?, :unfold
|
2019-01-23 03:50:19 -05:00
|
|
|
|
|
|
|
def initialize(blob, params)
|
2019-08-20 06:52:21 -04:00
|
|
|
super(blob)
|
|
|
|
self.attributes = params
|
|
|
|
|
2019-07-22 01:25:24 -04:00
|
|
|
# Load all blob data first as we need to ensure they're all loaded first
|
|
|
|
# so we can accurately show the rest of the diff when unfolding.
|
|
|
|
load_all_blob_data
|
|
|
|
|
2019-08-20 06:52:21 -04:00
|
|
|
handle_full_or_end!
|
2019-01-23 03:50:19 -05:00
|
|
|
end
|
|
|
|
|
2019-07-22 01:25:24 -04:00
|
|
|
# Returns an array of Gitlab::Diff::Line with match line added
|
2019-03-06 23:29:02 -05:00
|
|
|
def diff_lines
|
2019-08-08 20:13:09 -04:00
|
|
|
diff_lines = lines.map.with_index do |line, index|
|
|
|
|
full_line = limited_blob_lines[index].delete("\n")
|
2019-07-22 01:25:24 -04:00
|
|
|
|
2019-08-08 20:13:09 -04:00
|
|
|
Gitlab::Diff::Line.new(full_line, nil, nil, nil, nil, rich_text: line)
|
2019-03-06 23:29:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
add_match_line(diff_lines)
|
|
|
|
|
|
|
|
diff_lines
|
|
|
|
end
|
|
|
|
|
2019-01-23 03:50:19 -05:00
|
|
|
def lines
|
2019-08-09 22:36:32 -04:00
|
|
|
strong_memoize(:lines) do
|
2019-09-01 23:19:32 -04:00
|
|
|
limit(highlight(to: to).lines).map(&:html_safe)
|
2019-08-09 22:36:32 -04:00
|
|
|
end
|
2019-01-23 03:50:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def match_line_text
|
|
|
|
return '' if bottom?
|
|
|
|
|
|
|
|
lines_length = lines.length - 1
|
|
|
|
line = [since, lines_length].join(',')
|
|
|
|
"@@ -#{line}+#{line} @@"
|
|
|
|
end
|
2019-03-06 23:29:02 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-08-20 06:52:21 -04:00
|
|
|
def handle_full_or_end!
|
|
|
|
return unless full? || to == -1
|
2019-08-09 22:36:32 -04:00
|
|
|
|
2019-08-20 06:52:21 -04:00
|
|
|
self.since = 1 if full?
|
|
|
|
|
|
|
|
self.attributes = {
|
2019-08-09 22:36:32 -04:00
|
|
|
to: all_lines_size,
|
|
|
|
bottom: false,
|
|
|
|
unfold: false,
|
|
|
|
offset: 0,
|
|
|
|
indent: 0
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_lines_size
|
|
|
|
strong_memoize(:all_lines_size) do
|
2019-09-01 23:19:32 -04:00
|
|
|
all_lines.size
|
2019-08-09 22:36:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-06 23:29:02 -05:00
|
|
|
def add_match_line(diff_lines)
|
|
|
|
return unless unfold?
|
2019-08-09 22:36:32 -04:00
|
|
|
return if bottom? && to >= all_lines_size
|
2019-03-06 23:29:02 -05:00
|
|
|
|
2019-08-09 22:36:32 -04:00
|
|
|
if bottom? && to < all_lines_size
|
2019-03-06 23:29:02 -05:00
|
|
|
old_pos = to - offset
|
|
|
|
new_pos = to
|
|
|
|
elsif since != 1
|
|
|
|
old_pos = new_pos = since
|
|
|
|
end
|
|
|
|
|
|
|
|
# Match line is not needed when it reaches the top limit or bottom limit of the file.
|
|
|
|
return unless new_pos
|
|
|
|
|
|
|
|
match_line = Gitlab::Diff::Line.new(match_line_text, 'match', nil, old_pos, new_pos)
|
|
|
|
|
|
|
|
bottom? ? diff_lines.push(match_line) : diff_lines.unshift(match_line)
|
|
|
|
end
|
2019-08-08 20:13:09 -04:00
|
|
|
|
|
|
|
def limited_blob_lines
|
2019-08-09 22:36:32 -04:00
|
|
|
strong_memoize(:limited_blob_lines) do
|
2019-09-01 23:19:32 -04:00
|
|
|
limit(all_lines)
|
2019-08-09 22:36:32 -04:00
|
|
|
end
|
2019-08-08 20:13:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def limit(lines)
|
|
|
|
return lines if full?
|
|
|
|
|
2022-02-02 01:16:52 -05:00
|
|
|
lines[since - 1..to - 1] || []
|
2019-08-08 20:13:09 -04:00
|
|
|
end
|
2019-01-23 03:50:19 -05:00
|
|
|
end
|
|
|
|
end
|