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/diff).
Instance Attribute Summary (collapse)
-
- (Object) blocks
readonly
Returns the value of attribute blocks.
-
- (Object) end_new
readonly
Returns the value of attribute end_new.
-
- (Object) end_old
readonly
Returns the value of attribute end_old.
-
- (Object) file_length_difference
readonly
Returns the value of attribute file_length_difference.
-
- (Object) flag_context
Change the "start" and "end" fields to note that context should be added to this hunk.
-
- (Object) start_new
readonly
Returns the value of attribute start_new.
-
- (Object) start_old
readonly
Returns the value of attribute start_old.
Instance Method Summary (collapse)
- - (Object) diff(format)
- - (Object) each_old(block)
-
- (Hunk) initialize(data_old, data_new, piece, context, file_length_difference)
constructor
Create a hunk using references to both the old and new data, as well as the piece of data.
-
- (Boolean) overlaps?(hunk = nil)
Is there an overlap between hunk arg0 and old hunk arg1? Note: if end of old hunk is one less than beginning of second, they overlap.
- - (Object) unshift(hunk)
Constructor Details
- (Hunk) initialize(data_old, data_new, piece, context, file_length_difference)
Create a hunk using references to both the old and new data, as well as the piece of data
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 |
# File 'lib/diff/lcs/hunk.rb', line 18 def initialize(data_old, data_new, piece, context, file_length_difference) # At first, a hunk will have just one Block in it @blocks = [ Diff::LCS::Block.new(piece) ] @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 # 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 = context end |
Instance Attribute Details
- (Object) blocks (readonly)
Returns the value of attribute blocks
54 55 56 |
# File 'lib/diff/lcs/hunk.rb', line 54 def blocks @blocks end |
- (Object) end_new (readonly)
Returns the value of attribute end_new
56 57 58 |
# File 'lib/diff/lcs/hunk.rb', line 56 def end_new @end_new end |
- (Object) end_old (readonly)
Returns the value of attribute end_old
56 57 58 |
# File 'lib/diff/lcs/hunk.rb', line 56 def end_old @end_old end |
- (Object) file_length_difference (readonly)
Returns the value of attribute file_length_difference
57 58 59 |
# File 'lib/diff/lcs/hunk.rb', line 57 def file_length_difference @file_length_difference end |
- (Object) flag_context
Change the "start" and "end" fields to note that context should be added to this hunk
61 62 63 |
# File 'lib/diff/lcs/hunk.rb', line 61 def flag_context @flag_context end |
- (Object) start_new (readonly)
Returns the value of attribute start_new
55 56 57 |
# File 'lib/diff/lcs/hunk.rb', line 55 def start_new @start_new end |
- (Object) start_old (readonly)
Returns the value of attribute start_old
55 56 57 |
# File 'lib/diff/lcs/hunk.rb', line 55 def start_old @start_old end |
Instance Method Details
- (Object) diff(format)
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/diff/lcs/hunk.rb', line 94 def diff(format) case format when :old old_diff when :unified unified_diff when :context context_diff when :ed self when :reverse_ed, :ed_finish ed_diff(format) else raise "Unknown diff format #{format}." end end |
- (Object) each_old(block)
111 112 113 |
# File 'lib/diff/lcs/hunk.rb', line 111 def each_old(block) @data_old[@start_old .. @end_old].each { |e| yield e } end |
- (Boolean) overlaps?(hunk = nil)
Is there an overlap between hunk arg0 and old hunk arg1? Note: if end of old hunk is one less than beginning of second, they overlap
86 87 88 89 90 91 92 |
# File 'lib/diff/lcs/hunk.rb', line 86 def overlaps?(hunk = nil) return nil if hunk.nil? a = (@start_old - hunk.end_old) <= 1 b = (@start_new - hunk.end_new) <= 1 return (a or b) end |
- (Object) unshift(hunk)
78 79 80 81 82 |
# File 'lib/diff/lcs/hunk.rb', line 78 def unshift(hunk) @start_old = hunk.start_old @start_new = hunk.start_new blocks.unshift(*hunk.blocks) end |