Class: Diff::LCS::Hunk

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

Overview

A Hunk is a group of Blocks which overlap because of the context surrounding each block. (So if we’re not using context, every hunk will contain one block.) Used in the diff program (bin/ldiff).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_old, data_new, piece, flag_context, file_length_difference) ⇒ Hunk

Create a hunk using references to both the old and new data, as well as the piece of data.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/diff/lcs/hunk.rb', line 16

def initialize(data_old, data_new, piece, flag_context, file_length_difference)
  # At first, a hunk will have just one Block in it
  @blocks = [Diff::LCS::Block.new(piece)]

  if @blocks[0].remove.empty? && @blocks[0].insert.empty?
    fail "Cannot build a hunk from #{piece.inspect}; has no add or remove actions"
  end

  if String.method_defined?(:encoding)
    @preferred_data_encoding = data_old.fetch(0) { data_new.fetch(0, "") }.encoding
  end

  @data_old = data_old
  @data_new = data_new

  before = after = file_length_difference
  after += @blocks[0].diff_size
  @file_length_difference = after # The caller must get this manually
  @max_diff_size = @blocks.map { |e| e.diff_size.abs }.max

  # Save the start & end of each array. If the array doesn't exist (e.g.,
  # we're only adding items in this block), then figure out the line number
  # based on the line number of the other file and the current difference in
  # file lengths.
  if @blocks[0].remove.empty?
    a1 = a2 = nil
  else
    a1 = @blocks[0].remove[0].position
    a2 = @blocks[0].remove[-1].position
  end

  if @blocks[0].insert.empty?
    b1 = b2 = nil
  else
    b1 = @blocks[0].insert[0].position
    b2 = @blocks[0].insert[-1].position
  end

  @start_old = a1 || (b1 - before)
  @start_new = b1 || (a1 + before)
  @end_old = a2 || (b2 - after)
  @end_new = b2 || (a2 + after)

  self.flag_context = flag_context
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



62
63
64
# File 'lib/diff/lcs/hunk.rb', line 62

def blocks
  @blocks
end

#end_newObject (readonly)

Returns the value of attribute end_new.



64
65
66
# File 'lib/diff/lcs/hunk.rb', line 64

def end_new
  @end_new
end

#end_oldObject (readonly)

Returns the value of attribute end_old.



64
65
66
# File 'lib/diff/lcs/hunk.rb', line 64

def end_old
  @end_old
end

#file_length_differenceObject (readonly)

Returns the value of attribute file_length_difference.



65
66
67
# File 'lib/diff/lcs/hunk.rb', line 65

def file_length_difference
  @file_length_difference
end

#flag_contextObject

Change the “start” and “end” fields to note that context should be added to this hunk.



69
70
71
# File 'lib/diff/lcs/hunk.rb', line 69

def flag_context
  @flag_context
end

#start_newObject (readonly)

Returns the value of attribute start_new.



63
64
65
# File 'lib/diff/lcs/hunk.rb', line 63

def start_new
  @start_new
end

#start_oldObject (readonly)

Returns the value of attribute start_old.



63
64
65
# File 'lib/diff/lcs/hunk.rb', line 63

def start_old
  @start_old
end

Instance Method Details

#diff(format, last = false) ⇒ Object

Returns a diff string based on a format.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/diff/lcs/hunk.rb', line 115

def diff(format, last = false)
  case format
  when :old
    old_diff(last)
  when :unified
    unified_diff(last)
  when :context
    context_diff(last)
  when :ed
    self
  when :reverse_ed, :ed_finish
    ed_diff(format, last)
  else
    fail "Unknown diff format #{format}."
  end
end

#merge(hunk) ⇒ Object Also known as: unshift

Merges this hunk and the provided hunk together if they overlap. Returns a truthy value so that if there is no overlap, you can know the merge was skipped.



97
98
99
100
101
102
103
# File 'lib/diff/lcs/hunk.rb', line 97

def merge(hunk)
  return unless overlaps?(hunk)

  @start_old = hunk.start_old
  @start_new = hunk.start_new
  blocks.unshift(*hunk.blocks)
end

#missing_last_newline?(data) ⇒ Boolean

Returns:

  • (Boolean)


331
332
333
334
335
336
337
338
339
340
341
# File 'lib/diff/lcs/hunk.rb', line 331

def missing_last_newline?(data)
  newline = encode("\n")

  if data[-2]
    data[-2].end_with?(newline) && !data[-1].end_with?(newline)
  elsif data[-1]
    !data[-1].end_with?(newline)
  else
    true
  end
end

#overlaps?(hunk) ⇒ Boolean

Determines whether there is an overlap between this hunk and the provided hunk. This will be true if the difference between the two hunks start or end positions is within one position of each other.

Returns:

  • (Boolean)


109
110
111
112
# File 'lib/diff/lcs/hunk.rb', line 109

def overlaps?(hunk)
  hunk and (((@start_old - hunk.end_old) <= 1) or
            ((@start_new - hunk.end_new) <= 1))
end