Commit 48e33a5e authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'diff-file-view-optimisations' into 'master'

Diff file view optimisations

See merge request gitlab-org/gitlab!47091
parents 6ecb4aa8 02bd56ff
...@@ -64,15 +64,17 @@ module DiffHelper ...@@ -64,15 +64,17 @@ module DiffHelper
else else
# `sub` and substring-ing would destroy HTML-safeness of `line` # `sub` and substring-ing would destroy HTML-safeness of `line`
if line.start_with?('+', '-', ' ') if line.start_with?('+', '-', ' ')
line.dup.tap do |line| line[1, line.length]
line[0] = ''
end
else else
line line
end end
end end
end end
def diff_link_number(line_type, match, text)
line_type == match ? " " : text
end
def parallel_diff_discussions(left, right, diff_file) def parallel_diff_discussions(left, right, diff_file)
return unless @grouped_diff_discussions return unless @grouped_diff_discussions
......
- diff_files = diffs.diff_files = render partial: 'projects/diffs/file', collection: diffs.diff_files, as: :diff_file, locals: { project: diffs.project, environment: environment, diff_page_context: 'is-commit' }
= render partial: 'projects/diffs/file', collection: diff_files, as: :diff_file, locals: { project: diffs.project, environment: environment, diff_page_context: 'is-commit' }
...@@ -14,16 +14,15 @@ ...@@ -14,16 +14,15 @@
= submodule_diff_compare_link(diff_file) = submodule_diff_compare_link(diff_file)
- unless diff_file.submodule? - unless diff_file.submodule?
- blob = diff_file.blob
.file-actions.d-none.d-sm-block .file-actions.d-none.d-sm-block
- if blob&.readable_text? - if diff_file.blob&.readable_text?
= link_to '#', class: 'js-toggle-diff-comments gl-button btn active has-tooltip', title: _("Toggle comments for this file"), disabled: @diff_notes_disabled do = link_to '#', class: 'js-toggle-diff-comments gl-button btn active has-tooltip', title: _("Toggle comments for this file"), disabled: @diff_notes_disabled do
= sprite_icon('comment') = sprite_icon('comment')
\ \
- if editable_diff?(diff_file) - if editable_diff?(diff_file)
- link_opts = @merge_request.persisted? ? { from_merge_request_iid: @merge_request.iid } : {} - link_opts = @merge_request.persisted? ? { from_merge_request_iid: @merge_request.iid } : {}
= edit_blob_button(@merge_request.source_project, @merge_request.source_branch, diff_file.new_path, = edit_blob_button(@merge_request.source_project, @merge_request.source_branch, diff_file.new_path,
blob: blob, link_opts: link_opts) blob: diff_file.blob, link_opts: link_opts)
- if image_diff && image_replaced - if image_diff && image_replaced
= view_file_button(diff_file.old_content_sha, diff_file.old_path, project, replaced: true) = view_file_button(diff_file.old_content_sha, diff_file.old_path, project, replaced: true)
......
- show_toggle = local_assigns.fetch(:show_toggle, true) - if local_assigns.fetch(:show_toggle, true)
- if show_toggle
%i.fa.diff-toggle-caret.fa-fw %i.fa.diff-toggle-caret.fa-fw
- if diff_file.submodule? - if diff_file.submodule?
- blob = diff_file.blob
%span %span
= sprite_icon('archive') = sprite_icon('archive')
%strong.file-title-name %strong.file-title-name
= submodule_link(blob, diff_file.content_sha, diff_file.repository) = submodule_link(diff_file.blob, diff_file.content_sha, diff_file.repository)
= copy_file_path_button(blob.path) = copy_file_path_button(diff_file.blob.path)
- else - else
= conditional_link_to url.present?, url do = conditional_link_to url.present?, url do
= blob_icon diff_file.b_mode, diff_file.file_path = blob_icon diff_file.b_mode, diff_file.file_path
......
- email = local_assigns.fetch(:email, false)
- plain = local_assigns.fetch(:plain, false) - plain = local_assigns.fetch(:plain, false)
- discussions = local_assigns.fetch(:discussions, nil) - discussions = local_assigns.fetch(:discussions, nil)
- type = line.type
- line_code = diff_file.line_code(line) - line_code = diff_file.line_code(line)
- if discussions && line.discussable? - if discussions && line.discussable?
- line_discussions = discussions[line_code] - line_discussions = discussions[line_code]
%tr.line_holder{ class: type, id: (line_code unless plain) }
- case type %tr.line_holder{ class: line.type, id: (line_code unless plain) }
- case line.type
- when 'match' - when 'match'
= diff_match_line line.old_pos, line.new_pos, text: line.text = diff_match_line line.old_pos, line.new_pos, text: line.text
- when 'old-nonewline', 'new-nonewline' - when 'old-nonewline', 'new-nonewline'
...@@ -14,21 +13,21 @@ ...@@ -14,21 +13,21 @@
%td.new_line.diff-line-num %td.new_line.diff-line-num
%td.line_content.match= line.text %td.line_content.match= line.text
- else - else
%td.old_line.diff-line-num{ class: [type, ("js-avatar-container" if !plain)], data: { linenumber: line.old_pos } } %td.old_line.diff-line-num{ class: [line.type, ("js-avatar-container" if !plain)], data: { linenumber: line.old_pos } }
- link_text = type == "new" ? " " : line.old_pos
- if plain - if plain
= link_text = diff_link_number(line.type, "new", line.old_pos)
- else - else
= add_diff_note_button(line_code, diff_file.position(line), type) = add_diff_note_button(line_code, diff_file.position(line), line.type)
%a{ href: "##{line_code}", data: { linenumber: link_text } } %a{ href: "##{line_code}", data: { linenumber: diff_link_number(line.type, "new", line.old_pos) } }
%td.new_line.diff-line-num{ class: type, data: { linenumber: line.new_pos } }
- link_text = type == "old" ? " " : line.new_pos %td.new_line.diff-line-num{ class: line.type, data: { linenumber: line.new_pos } }
- if plain - if plain
= link_text = diff_link_number(line.type, "old", line.new_pos)
- else - else
%a{ href: "##{line_code}", data: { linenumber: link_text } } %a{ href: "##{line_code}", data: { linenumber: diff_link_number(line.type, "old", line.new_pos) } }
%td.line_content{ class: type }<
- if email %td.line_content{ class: line.type }<
- if local_assigns.fetch(:email, false)
%pre= line.rich_text %pre= line.rich_text
- else - else
= diff_line_content(line.rich_text) = diff_line_content(line.rich_text)
......
...@@ -130,6 +130,38 @@ RSpec.describe DiffHelper do ...@@ -130,6 +130,38 @@ RSpec.describe DiffHelper do
end end
end end
describe "#diff_link_number" do
using RSpec::Parameterized::TableSyntax
let(:line) do
double(:line, type: line_type)
end
# This helper is used to generate the line numbers on the
# diff lines. It essentially just returns a blank string
# on the old/new lines. The following table tests all the
# possible permutations for clarity.
where(:line_type, :match, :line_number, :expected_return_value) do
"new" | "new" | 1 | " "
"new" | "old" | 2 | 2
"old" | "new" | 3 | 3
"old" | "old" | 4 | " "
"new-nonewline" | "new" | 5 | 5
"new-nonewline" | "old" | 6 | 6
"old-nonewline" | "new" | 7 | 7
"old-nonewline" | "old" | 8 | 8
"match" | "new" | 9 | 9
"match" | "old" | 10 | 10
end
with_them do
it "returns the expected value" do
expect(helper.diff_link_number(line.type, match, line_number)).to eq(expected_return_value)
end
end
end
describe "#mark_inline_diffs" do describe "#mark_inline_diffs" do
let(:old_line) { %{abc 'def'} } let(:old_line) { %{abc 'def'} }
let(:new_line) { %{abc "def"} } let(:new_line) { %{abc "def"} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment