Class: Gitlab::Diff::InlineDiff

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(old_line, new_line, offset: 0) ⇒ InlineDiff

Returns a new instance of InlineDiff.


8
9
10
11
12
# File 'lib/gitlab/diff/inline_diff.rb', line 8

def initialize(old_line, new_line, offset: 0)
  @old_line = old_line[offset..]
  @new_line = new_line[offset..]
  @offset = offset
end

Instance Attribute Details

#new_lineObject

Returns the value of attribute new_line.


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

def new_line
  @new_line
end

#offsetObject

Returns the value of attribute offset.


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

def offset
  @offset
end

#old_lineObject

Returns the value of attribute old_line.


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

def old_line
  @old_line
end

Class Method Details

.for_lines(lines) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gitlab/diff/inline_diff.rb', line 23

def for_lines(lines)
  pair_selector = Gitlab::Diff::PairSelector.new(lines)

  inline_diffs = []

  pair_selector.each do |old_index, new_index|
    old_line = lines[old_index]
    new_line = lines[new_index]

    old_diffs, new_diffs = new(old_line, new_line, offset: 1).inline_diffs

    inline_diffs[old_index] = old_diffs
    inline_diffs[new_index] = new_diffs
  end

  inline_diffs
end

Instance Method Details

#inline_diffsObject


14
15
16
17
18
19
# File 'lib/gitlab/diff/inline_diff.rb', line 14

def inline_diffs
  # Skip inline diff if empty line was replaced with content
  return if old_line == ""

  CharDiff.new(old_line, new_line).changed_ranges(offset: offset)
end