Class: Diff::LCS::Hunk
- Inherits:
-
Object
- Object
- Diff::LCS::Hunk
- 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
-
#blocks ⇒ Object
readonly
Returns the value of attribute blocks.
-
#end_new ⇒ Object
readonly
Returns the value of attribute end_new.
-
#end_old ⇒ Object
readonly
Returns the value of attribute end_old.
-
#file_length_difference ⇒ Object
readonly
Returns the value of attribute file_length_difference.
-
#flag_context ⇒ Object
Change the “start” and “end” fields to note that context should be added to this hunk.
-
#start_new ⇒ Object
readonly
Returns the value of attribute start_new.
-
#start_old ⇒ Object
readonly
Returns the value of attribute start_old.
Instance Method Summary collapse
-
#diff(format, last = false) ⇒ Object
Returns a diff string based on a format.
-
#initialize(data_old, data_new, piece, flag_context, file_length_difference) ⇒ Hunk
constructor
Create a hunk using references to both the old and new data, as well as the piece of data.
-
#merge(hunk) ⇒ Object
(also: #unshift)
Merges this hunk and the provided hunk together if they overlap.
-
#overlaps?(hunk) ⇒ Boolean
Determines whether there is an overlap between this hunk and the provided hunk.
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.
14 15 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 |
# File 'lib/diff/lcs/hunk.rb', line 14 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.from_chunk(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 @preferred_data_encoding = data_old.fetch(0) { data_new.fetch(0) { "" } }.encoding @newline = "\n".encode(@preferred_data_encoding) @missing_newline = "\\ No newline at end of file".encode(@preferred_data_encoding) @data_old = data_old @data_new = data_new @old_empty = data_old.empty? || (data_old.size == 1 && data_old[0].empty?) @new_empty = data_new.empty? || (data_new.size == 1 && data_new[0].empty?) 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 and 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
#blocks ⇒ Object (readonly)
Returns the value of attribute blocks.
61 62 63 |
# File 'lib/diff/lcs/hunk.rb', line 61 def blocks @blocks end |
#end_new ⇒ Object (readonly)
Returns the value of attribute end_new.
63 64 65 |
# File 'lib/diff/lcs/hunk.rb', line 63 def end_new @end_new end |
#end_old ⇒ Object (readonly)
Returns the value of attribute end_old.
63 64 65 |
# File 'lib/diff/lcs/hunk.rb', line 63 def end_old @end_old end |
#file_length_difference ⇒ Object (readonly)
Returns the value of attribute file_length_difference.
64 65 66 |
# File 'lib/diff/lcs/hunk.rb', line 64 def file_length_difference @file_length_difference end |
#flag_context ⇒ Object
Change the “start” and “end” fields to note that context should be added to this hunk. :attr_accessor: :flag_context
69 70 71 |
# File 'lib/diff/lcs/hunk.rb', line 69 def flag_context @flag_context end |
#start_new ⇒ Object (readonly)
Returns the value of attribute start_new.
62 63 64 |
# File 'lib/diff/lcs/hunk.rb', line 62 def start_new @start_new end |
#start_old ⇒ Object (readonly)
Returns the value of attribute start_old.
62 63 64 |
# File 'lib/diff/lcs/hunk.rb', line 62 def start_old @start_old end |
Instance Method Details
#diff(format, last = false) ⇒ Object
Returns a diff string based on a format.
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/diff/lcs/hunk.rb', line 112 def diff(format, last = false) case format when :old old_diff(last) when :unified unified_diff(last) when :context context_diff(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.
95 96 97 98 99 100 101 |
# File 'lib/diff/lcs/hunk.rb', line 95 def merge(hunk) return unless overlaps?(hunk) @start_old = hunk.start_old @start_new = hunk.start_new blocks.unshift(*hunk.blocks) 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.
107 108 109 |
# File 'lib/diff/lcs/hunk.rb', line 107 def overlaps?(hunk) hunk && (((@start_old - hunk.end_old) <= 1) || ((@start_new - hunk.end_new) <= 1)) end |