Class: GitSpelunk::Offset::Chunk

Inherits:
Object
  • Object
show all
Defined in:
lib/git_spelunk/offset.rb

Defined Under Namespace

Classes: LineBlock

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hunk) ⇒ Chunk

Returns a new instance of Chunk.



53
54
55
56
57
58
59
60
# File 'lib/git_spelunk/offset.rb', line 53

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

#linesObject (readonly)

Returns the value of attribute lines.



51
52
53
# File 'lib/git_spelunk/offset.rb', line 51

def lines
  @lines
end

#minus_lengthObject (readonly)

Returns the value of attribute minus_length.



51
52
53
# File 'lib/git_spelunk/offset.rb', line 51

def minus_length
  @minus_length
end

#minus_offsetObject (readonly)

Returns the value of attribute minus_offset.



51
52
53
# File 'lib/git_spelunk/offset.rb', line 51

def minus_offset
  @minus_offset
end

#plus_lengthObject (readonly)

Returns the value of attribute plus_length.



51
52
53
# File 'lib/git_spelunk/offset.rb', line 51

def plus_length
  @plus_length
end

#plus_offsetObject (readonly)

Returns the value of attribute plus_offset.



51
52
53
# File 'lib/git_spelunk/offset.rb', line 51

def plus_offset
  @plus_offset
end

Instance Method Details

#find_parent_line_number(target) ⇒ Object



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
125
# File 'lib/git_spelunk/offset.rb', line 84

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

Returns:

  • (Boolean)


62
63
64
# File 'lib/git_spelunk/offset.rb', line 62

def has_line?(line_number)
  plus_offset <= line_number && line_number <= (plus_offset + plus_length)
end