Class: GitSpelunk::Offset::Chunk
- Inherits:
-
Object
- Object
- GitSpelunk::Offset::Chunk
- Defined in:
- lib/git_spelunk/offset.rb
Defined Under Namespace
Classes: LineBlock
Instance Attribute Summary collapse
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#minus_length ⇒ Object
readonly
Returns the value of attribute minus_length.
-
#minus_offset ⇒ Object
readonly
Returns the value of attribute minus_offset.
-
#plus_length ⇒ Object
readonly
Returns the value of attribute plus_length.
-
#plus_offset ⇒ Object
readonly
Returns the value of attribute plus_offset.
Instance Method Summary collapse
- #find_parent_line_number(target) ⇒ Object
- #has_line?(line_number) ⇒ Boolean
-
#initialize(hunk) ⇒ Chunk
constructor
A new instance of Chunk.
Constructor Details
#initialize(hunk) ⇒ Chunk
Returns a new instance of Chunk.
52 53 54 55 56 57 58 59 |
# File 'lib/git_spelunk/offset.rb', line 52 def initialize(hunk) @minus_offset = hunk.old_start @minus_length = hunk.old_lines @plus_offset = hunk.new_start @plus_length = hunk.new_lines @lines = hunk.lines end |
Instance Attribute Details
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
50 51 52 |
# File 'lib/git_spelunk/offset.rb', line 50 def lines @lines end |
#minus_length ⇒ Object (readonly)
Returns the value of attribute minus_length.
50 51 52 |
# File 'lib/git_spelunk/offset.rb', line 50 def minus_length @minus_length end |
#minus_offset ⇒ Object (readonly)
Returns the value of attribute minus_offset.
50 51 52 |
# File 'lib/git_spelunk/offset.rb', line 50 def minus_offset @minus_offset end |
#plus_length ⇒ Object (readonly)
Returns the value of attribute plus_length.
50 51 52 |
# File 'lib/git_spelunk/offset.rb', line 50 def plus_length @plus_length end |
#plus_offset ⇒ Object (readonly)
Returns the value of attribute plus_offset.
50 51 52 |
# File 'lib/git_spelunk/offset.rb', line 50 def plus_offset @plus_offset end |
Instance Method Details
#find_parent_line_number(target) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/git_spelunk/offset.rb', line 83 def find_parent_line_number(target) return 1 if target == 1 # separate in blocks of lines with the same prefix old_line_number = minus_offset new_line_number = plus_offset blocks = [] lines.each do |l| last_block = blocks.last if last_block.nil? || last_block.type != l.line_origin blocks << LineBlock.new(old_line_number, l) else last_block << l end if l.line_origin == :context || l.line_origin == :addition if new_line_number == target # important: we don't finish building the structure. break end new_line_number += 1 end if l.line_origin == :deletion || l.line_origin == :context old_line_number += 1 end end addition_block = blocks.pop last_old_block = blocks.last if last_old_block.type == :context # if the previous context existed in both, just go to the end of that. last_old_block.offset + (last_old_block.size - 1) else # offset N lines into the block that was removed to create the target block, but don't go beyond the edge of it. last_old_block.offset + [addition_block.size - 1, last_old_block.size].min end end |
#has_line?(line_number) ⇒ Boolean
61 62 63 |
# File 'lib/git_spelunk/offset.rb', line 61 def has_line?(line_number) plus_offset <= line_number && line_number <= (plus_offset + plus_length) end |