diff options
author | Mark Chao <mchao@gitlab.com> | 2019-03-07 12:29:02 +0800 |
---|---|---|
committer | Mark Chao <mchao@gitlab.com> | 2019-03-07 16:12:36 +0800 |
commit | cea59dbe030bfde83247ef27c49ffd5267b194ea (patch) | |
tree | a71514f4a8670415a62997aa30a3bc5edec9c1fc /app/presenters | |
parent | b14de8e1f519b9b874033f783051814129af176c (diff) | |
download | gitlab-ce-cea59dbe030bfde83247ef27c49ffd5267b194ea.tar.gz |
Move diff_line preparation into presenter
Update spec
Diffstat (limited to 'app/presenters')
-rw-r--r-- | app/presenters/blobs/unfold_presenter.rb | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/app/presenters/blobs/unfold_presenter.rb b/app/presenters/blobs/unfold_presenter.rb index 908b2f97f89..7b13db3bb74 100644 --- a/app/presenters/blobs/unfold_presenter.rb +++ b/app/presenters/blobs/unfold_presenter.rb @@ -25,6 +25,17 @@ module Blobs 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 @@ -40,5 +51,25 @@ module Blobs 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 |