Class: Diff::Hunk

Inherits:
Struct
  • Object
show all
Defined in:
lib/diff/hunk.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#a_startsObject

Returns the value of attribute a_starts

Returns:

  • (Object)

    the current value of a_starts



5
6
7
# File 'lib/diff/hunk.rb', line 5

def a_starts
  @a_starts
end

#b_startObject

Returns the value of attribute b_start

Returns:

  • (Object)

    the current value of b_start



5
6
7
# File 'lib/diff/hunk.rb', line 5

def b_start
  @b_start
end

#editsObject

Returns the value of attribute edits

Returns:

  • (Object)

    the current value of edits



5
6
7
# File 'lib/diff/hunk.rb', line 5

def edits
  @edits
end

Class Method Details

.build(hunk, edits, offset) ⇒ Object



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

def self.build(hunk, edits, offset)
  counter = -1

  until counter == 0
    hunk.edits.push(edits[offset]) if offset >= 0 and counter > 0

    offset += 1
    break if offset >= edits.size

    case edits[offset + HUNK_CONTEXT]&.type
    when :ins, :del
      counter = 2 * HUNK_CONTEXT + 1
    else
      counter -= 1
    end
  end

  offset
end

.filter(edits) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/diff/hunk.rb', line 6

def self.filter(edits)
  hunks  = []
  offset = 0

  loop do
    offset += 1 while edits[offset]&.type == :eql
    return hunks if offset >= edits.size

    offset -= HUNK_CONTEXT + 1

    a_starts = (offset < 0) ? []  : edits[offset].a_lines.map(&:number)
    b_start  = (offset < 0) ? nil : edits[offset].b_line.number

    hunks.push(Hunk.new(a_starts, b_start, []))
    offset = Hunk.build(hunks.last, edits, offset)
  end
end

Instance Method Details

#headerObject



44
45
46
47
48
49
50
51
52
# File 'lib/diff/hunk.rb', line 44

def header
  a_lines = edits.map(&:a_lines).transpose
  offsets = a_lines.map.with_index { |lines, i| format("-", lines, a_starts[i]) }

  offsets.push(format("+", edits.map(&:b_line), b_start))
  sep = "@" * offsets.size

  [sep, *offsets, sep].join(" ")
end