Class: RBS::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, content:) ⇒ Buffer

Returns a new instance of Buffer.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rbs/buffer.rb', line 8

def initialize(name:, content:)
  @name = name
  @content = content

  @lines = content.lines

  @ranges = []
  offset = 0
  lines.each do |line|
    size = line.size
    range = offset...(offset+size)
    ranges << range
    offset += size
  end
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



4
5
6
# File 'lib/rbs/buffer.rb', line 4

def content
  @content
end

#linesObject (readonly)

Returns the value of attribute lines.



5
6
7
# File 'lib/rbs/buffer.rb', line 5

def lines
  @lines
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/rbs/buffer.rb', line 3

def name
  @name
end

#rangesObject (readonly)

Returns the value of attribute ranges.



6
7
8
# File 'lib/rbs/buffer.rb', line 6

def ranges
  @ranges
end

Instance Method Details

#last_positionObject



46
47
48
# File 'lib/rbs/buffer.rb', line 46

def last_position
  content.size
end

#loc_to_pos(loc) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/rbs/buffer.rb', line 36

def loc_to_pos(loc)
  line, column = loc

  if range = ranges[line - 1]
    range.begin + column
  else
    last_position
  end
end

#pos_to_loc(pos) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rbs/buffer.rb', line 24

def pos_to_loc(pos)
  index = ranges.bsearch_index do |range|
    pos < range.end ? true : false
  end

  if index
    [index + 1, pos - ranges[index].begin]
  else
    [ranges.size + 1, 0]
  end
end