Class: Diff::Hunk
- Inherits:
-
Struct
- Object
- Struct
- Diff::Hunk
- Defined in:
- lib/diff/hunk.rb
Instance Attribute Summary collapse
-
#a_starts ⇒ Object
Returns the value of attribute a_starts.
-
#b_start ⇒ Object
Returns the value of attribute b_start.
-
#edits ⇒ Object
Returns the value of attribute edits.
Class Method Summary collapse
Instance Method Summary collapse
Instance Attribute Details
#a_starts ⇒ Object
Returns the value of attribute a_starts
5 6 7 |
# File 'lib/diff/hunk.rb', line 5 def a_starts @a_starts end |
#b_start ⇒ Object
Returns the value of attribute b_start
5 6 7 |
# File 'lib/diff/hunk.rb', line 5 def b_start @b_start end |
#edits ⇒ Object
Returns the value of attribute 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
#header ⇒ Object
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 |