summaryrefslogtreecommitdiff
path: root/app/presenters/blob_presenter.rb
diff options
context:
space:
mode:
authorPatrick Bajao <ebajao@gitlab.com>2019-09-02 11:19:32 +0800
committerPatrick Bajao <ebajao@gitlab.com>2019-09-02 11:44:45 +0800
commitbf230da05d8a3a6feeeb80d81fa0370cc295478e (patch)
treecaefbfda883d3360cb2aea07a5ea925447e39a5b /app/presenters/blob_presenter.rb
parent60adc14473911fd9bd33feef2fbfd62a9824a11c (diff)
downloadgitlab-ce-bf230da05d8a3a6feeeb80d81fa0370cc295478e.tar.gz
Support selective highlighting of lines65152-unfolded-lines-perf-improvement
Instead of highlighting all lines when not all of them are needed, only highlight from the beginning up to the specified limit. The `BlobPresenter#highlight` method has been updated to support `to` param. This param will be used to limit the content to be highlighted.
Diffstat (limited to 'app/presenters/blob_presenter.rb')
-rw-r--r--app/presenters/blob_presenter.rb18
1 files changed, 16 insertions, 2 deletions
diff --git a/app/presenters/blob_presenter.rb b/app/presenters/blob_presenter.rb
index 2cf3278d240..3a71d2b87f3 100644
--- a/app/presenters/blob_presenter.rb
+++ b/app/presenters/blob_presenter.rb
@@ -3,12 +3,12 @@
class BlobPresenter < Gitlab::View::Presenter::Delegated
presents :blob
- def highlight(plain: nil)
+ def highlight(to: nil, plain: nil)
load_all_blob_data
Gitlab::Highlight.highlight(
blob.path,
- blob.data,
+ limited_blob_data(to: to),
language: blob.language_from_gitattributes,
plain: plain
)
@@ -23,4 +23,18 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated
def load_all_blob_data
blob.load_all_data! if blob.respond_to?(:load_all_data!)
end
+
+ def limited_blob_data(to: nil)
+ return blob.data if to.blank?
+
+ # Even though we don't need all the lines at the start of the file (e.g
+ # viewing the middle part of a file), they still need to be highlighted
+ # to ensure that the succeeding lines can be formatted correctly (e.g.
+ # multi-line comments).
+ all_lines[0..to - 1].join
+ end
+
+ def all_lines
+ @all_lines ||= blob.data.lines
+ end
end