summaryrefslogtreecommitdiff
path: root/app/presenters
diff options
context:
space:
mode:
authorPatrick Bajao <ebajao@gitlab.com>2019-07-22 13:25:24 +0800
committerPatrick Bajao <ebajao@gitlab.com>2019-07-22 13:25:24 +0800
commit866bef8059591db9a17db71d66a1321b0ca25153 (patch)
tree84dcaf3f611c769c5676220040d163374fbcd3f0 /app/presenters
parentbf172b115c206cb6ed6c2b271fad600f1548b43c (diff)
downloadgitlab-ce-866bef8059591db9a17db71d66a1321b0ca25153.tar.gz
Fix suggestion on lines that are not part of an MR57953-fix-unfolded-diff-suggestions
Return the `text` as plain string in the response instead of including HTML tags but keep `rich_text` as is. The fix is to modify `Blob::UnfoldPresenter#diff_files` to map each raw diff line (limited by the range specified) to a corresponding line in an array of highlighted lines to use as `rich_text`.
Diffstat (limited to 'app/presenters')
-rw-r--r--app/presenters/blob_presenter.rb8
-rw-r--r--app/presenters/blobs/unfold_presenter.rb30
2 files changed, 28 insertions, 10 deletions
diff --git a/app/presenters/blob_presenter.rb b/app/presenters/blob_presenter.rb
index 91c9abe750b..2cf3278d240 100644
--- a/app/presenters/blob_presenter.rb
+++ b/app/presenters/blob_presenter.rb
@@ -4,7 +4,7 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated
presents :blob
def highlight(plain: nil)
- blob.load_all_data! if blob.respond_to?(:load_all_data!)
+ load_all_blob_data
Gitlab::Highlight.highlight(
blob.path,
@@ -17,4 +17,10 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated
def web_url
Gitlab::Routing.url_helpers.project_blob_url(blob.repository.project, File.join(blob.commit_id, blob.path))
end
+
+ private
+
+ def load_all_blob_data
+ blob.load_all_data! if blob.respond_to?(:load_all_data!)
+ end
end
diff --git a/app/presenters/blobs/unfold_presenter.rb b/app/presenters/blobs/unfold_presenter.rb
index 7b13db3bb74..21a1e1309e0 100644
--- a/app/presenters/blobs/unfold_presenter.rb
+++ b/app/presenters/blobs/unfold_presenter.rb
@@ -16,8 +16,12 @@ module Blobs
attribute :indent, Integer, default: 0
def initialize(blob, params)
+ # 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
+
@subject = blob
- @all_lines = highlight.lines
+ @all_lines = blob.data.lines
super(params)
if full?
@@ -25,10 +29,12 @@ module Blobs
end
end
- # Converts a String array to Gitlab::Diff::Line array, with match line added
+ # Returns an array of Gitlab::Diff::Line 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)
+ diff_lines = lines.map.with_index do |line, index|
+ full_line = limited_blob_lines[index].delete("\n")
+
+ Gitlab::Diff::Line.new(full_line, nil, nil, nil, nil, rich_text: line)
end
add_match_line(diff_lines)
@@ -37,11 +43,7 @@ module Blobs
end
def lines
- strong_memoize(:lines) do
- lines = @all_lines
- lines = lines[since - 1..to - 1] unless full?
- lines.map(&:html_safe)
- end
+ @lines ||= limit(highlight.lines).map(&:html_safe)
end
def match_line_text
@@ -71,5 +73,15 @@ module Blobs
bottom? ? diff_lines.push(match_line) : diff_lines.unshift(match_line)
end
+
+ def limited_blob_lines
+ @limited_blob_lines ||= limit(@all_lines)
+ end
+
+ def limit(lines)
+ return lines if full?
+
+ lines[since - 1..to - 1]
+ end
end
end