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
|
# frozen_string_literal: true
module Blobs
class UnfoldPresenter < BlobPresenter
include ActiveModel::Model
include Gitlab::Utils::StrongMemoize
attr_accessor :full, :since, :to, :bottom, :unfold, :offset, :indent
def initialize(blob, params)
super(blob, params)
@all_lines = highlight.lines
if full?
self.attributes = { since: 1, to: @all_lines.size, bottom: false, unfold: false, offset: 0, indent: 0 }
end
end
# Converts a String array to Gitlab::Diff::Line array, with match line added
def diff_lines
diff_lines = lines.map do |line|
Gitlab::Diff::Line.new(line, nil, nil, nil, nil, rich_text: line)
end
add_match_line(diff_lines)
diff_lines
end
def lines
strong_memoize(:lines) do
lines = @all_lines
lines = lines[since - 1..to - 1] unless full?
lines.map(&:html_safe)
end
end
def match_line_text
return '' if bottom?
lines_length = lines.length - 1
line = [since, lines_length].join(',')
"@@ -#{line}+#{line} @@"
end
private
def add_match_line(diff_lines)
return unless unfold?
if bottom? && to < @all_lines.size
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
end
end
|