Class: Fdlint::Parser::PositionInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/fdlint/parser/position_info.rb

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ PositionInfo

Returns a new instance of PositionInfo.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fdlint/parser/position_info.rb', line 16

def initialize(text)
  lines = text.split(/\n/)

  @lines = []
  num = 0
  lines.each do |line|
    num += line.length + 1
    @lines << num
  end

  @len = lines.length
end

Instance Method Details

#locate(pos) ⇒ Object

Public: Turn given number position index into a

position object which contains the 
column and row index info

pos - position index of the source string

Returns the position object



36
37
38
39
40
41
42
43
44
# File 'lib/fdlint/parser/position_info.rb', line 36

def locate(pos)
  if @row && pos >= @lines[@row -1] && pos < @lines[@row]
    row = @row
  else
    row = @row = find(0, @len - 1, pos)
  end
  col = row > 0 ? pos - @lines[row - 1] : pos
  Position.new(pos, row + 1, col + 1)
end