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(data) ⇒ Chunk

Returns a new instance of Chunk.



54
55
56
57
# File 'lib/git_spelunk/offset.rb', line 54

def initialize(data)
  @minus_offset, @minus_length, @plus_offset, @plus_length = *extract_stats(data[0])
  @lines = data[1..-1]
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



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

def lines
  @lines
end

#minus_lengthObject (readonly)

Returns the value of attribute minus_length.



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

def minus_length
  @minus_length
end

#minus_offsetObject (readonly)

Returns the value of attribute minus_offset.



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

def minus_offset
  @minus_offset
end

#plus_lengthObject (readonly)

Returns the value of attribute plus_length.



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

def plus_length
  @plus_length
end

#plus_offsetObject (readonly)

Returns the value of attribute plus_offset.



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

def plus_offset
  @plus_offset
end

Instance Method Details

#find_parent_line_number(target) ⇒ Object



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

def find_parent_line_number(target)
  # separate in blocks of lines with the same prefix

  old_line_number = minus_offset
  new_line_number = plus_offset

  blocks = []
  lines.each do |l|
    next if l =~ /\\ No newline at end of file/
    last_block = blocks.last

    if last_block.nil? || last_block.type != l[0]
      blocks << LineBlock.new(old_line_number, l)
    else
      last_block << l
    end

    if l[0] == "+" || l[0] == " "
      if new_line_number == target
        # important: we don't finish building the structure.
        break
      end

      new_line_number += 1
    end

    if l[0] == "-" || l[0] == " "
      old_line_number += 1
    end
  end

  addition_block = blocks.pop
  last_old_block = blocks.last

  if last_old_block.type == " "
    last_old_block.offset + (last_old_block.size - 1)
  else
    # offset N lines into last 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)


59
60
61
# File 'lib/git_spelunk/offset.rb', line 59

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