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) ⇒ Highlight

Returns a new instance of Highlight


8
9
10
11
12
13
14
15
16
# File 'lib/gitlab/diff/highlight.rb', line 8

def initialize(diff_lines)
  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


4
5
6
# File 'lib/gitlab/diff/highlight.rb', line 4

def diff_file
  @diff_file
end

#diff_linesObject (readonly)

Returns the value of attribute diff_lines


4
5
6
# File 'lib/gitlab/diff/highlight.rb', line 4

def diff_lines
  @diff_lines
end

#raw_linesObject (readonly)

Returns the value of attribute raw_lines


4
5
6
# File 'lib/gitlab/diff/highlight.rb', line 4

def raw_lines
  @raw_lines
end

Instance Method Details

#highlightObject


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gitlab/diff/highlight.rb', line 18

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.type == 'match' || diff_line.type == 'nonewline'

    rich_line = highlight_line(diff_line) || diff_line.text

    if line_inline_diffs = inline_diffs[i]
      rich_line = InlineDiffMarker.new(diff_line.text, rich_line).mark(line_inline_diffs)
    end

    diff_line.text = rich_line

    diff_line
  end
end