Class: Gitlab::Diff::Highlight

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/diff/highlight.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(diff_lines, repository: nil) ⇒ Highlight

Returns a new instance of Highlight.


10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/gitlab/diff/highlight.rb', line 10

def initialize(diff_lines, repository: nil)
  @repository = repository

  if diff_lines.is_a?(Gitlab::Diff::File)
    @diff_file = diff_lines
    @diff_lines = @diff_file.diff_lines
  else
    @diff_lines = diff_lines
  end

  @raw_lines = @diff_lines.map(&:text)
end

Instance Attribute Details

#diff_fileObject (readonly)

Returns the value of attribute diff_file


6
7
8
# File 'lib/gitlab/diff/highlight.rb', line 6

def diff_file
  @diff_file
end

#diff_linesObject (readonly)

Returns the value of attribute diff_lines


6
7
8
# File 'lib/gitlab/diff/highlight.rb', line 6

def diff_lines
  @diff_lines
end

#raw_linesObject (readonly)

Returns the value of attribute raw_lines


6
7
8
# File 'lib/gitlab/diff/highlight.rb', line 6

def raw_lines
  @raw_lines
end

#repositoryObject (readonly)

Returns the value of attribute repository


6
7
8
# File 'lib/gitlab/diff/highlight.rb', line 6

def repository
  @repository
end

Instance Method Details

#highlightObject


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gitlab/diff/highlight.rb', line 23

def highlight
  @diff_lines.map.with_index do |diff_line, i|
    diff_line = diff_line.dup
    # ignore highlighting for "match" lines
    next diff_line if diff_line.meta?

    rich_line = highlight_line(diff_line) || ERB::Util.html_escape(diff_line.text)

    if line_inline_diffs = inline_diffs[i]
      begin
        rich_line = InlineDiffMarker.new(diff_line.text, rich_line).mark(line_inline_diffs)
      # This should only happen when the encoding of the diff doesn't
      # match the blob, which is a bug. But we shouldn't fail to render
      # completely in that case, even though we want to report the error.
      rescue RangeError => e
        Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e, issue_url: 'https://gitlab.com/gitlab-org/gitlab-foss/issues/45441')
      end
    end

    diff_line.rich_text = rich_line

    diff_line
  end
end