Class: Gitlab::Diff::InlineDiffMarker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_line, rich_line = raw_line) ⇒ InlineDiffMarker

Returns a new instance of InlineDiffMarker


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

def initialize(raw_line, rich_line = raw_line)
  @raw_line = raw_line
  @rich_line = ERB::Util.html_escape(rich_line)
end

Instance Attribute Details

#raw_lineObject

Returns the value of attribute raw_line


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

def raw_line
  @raw_line
end

#rich_lineObject

Returns the value of attribute rich_line


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

def rich_line
  @rich_line
end

Instance Method Details

#mark(line_inline_diffs) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gitlab/diff/inline_diff_marker.rb', line 11

def mark(line_inline_diffs)
  return rich_line unless line_inline_diffs

  marker_ranges = []
  line_inline_diffs.each do |inline_diff_range|
    # Map the inline-diff range based on the raw line to character positions in the rich line
    inline_diff_positions = position_mapping[inline_diff_range].flatten
    # Turn the array of character positions into ranges
    marker_ranges.concat(collapse_ranges(inline_diff_positions))
  end

  offset = 0
  # Mark each range
  marker_ranges.each_with_index do |range, i|
    class_names = ["idiff"]
    class_names << "left"   if i == 0
    class_names << "right"  if i == marker_ranges.length - 1

    offset = insert_around_range(rich_line, range, "<span class='#{class_names.join(" ")}'>", "</span>", offset)
  end

  rich_line.html_safe
end