Class: Gitlab::Diff::Highlight

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

Constant Summary collapse

PREFIX_REGEXP =
/\A(.)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(diff_lines, repository: nil) ⇒ Highlight

Returns a new instance of Highlight.



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

def initialize(diff_lines, repository: nil)
  @repository = repository
  @project = repository&.project

  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.



8
9
10
# File 'lib/gitlab/diff/highlight.rb', line 8

def diff_file
  @diff_file
end

#diff_linesObject (readonly)

Returns the value of attribute diff_lines.



8
9
10
# File 'lib/gitlab/diff/highlight.rb', line 8

def diff_lines
  @diff_lines
end

#projectObject (readonly)

Returns the value of attribute project.



8
9
10
# File 'lib/gitlab/diff/highlight.rb', line 8

def project
  @project
end

#repositoryObject (readonly)

Returns the value of attribute repository.



8
9
10
# File 'lib/gitlab/diff/highlight.rb', line 8

def repository
  @repository
end

Instance Method Details

#highlightObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gitlab/diff/highlight.rb', line 26

def highlight
  populate_marker_ranges

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

    rich_line = apply_syntax_highlight(diff_line)
    rich_line = apply_marker_ranges_highlight(diff_line, rich_line)

    diff_line.rich_text = rich_line

    diff_line
  end
end