Class: RBS::Buffer
- Inherits:
-
Object
- Object
- RBS::Buffer
- Defined in:
- lib/rbs/buffer.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(name:, content:) ⇒ Buffer
constructor
A new instance of Buffer.
- #inspect ⇒ Object
- #last_position ⇒ Object
- #lines ⇒ Object
- #loc_to_pos(loc) ⇒ Object
- #pos_to_loc(pos) ⇒ Object
- #ranges ⇒ Object
Constructor Details
#initialize(name:, content:) ⇒ Buffer
Returns a new instance of Buffer.
6 7 8 9 |
# File 'lib/rbs/buffer.rb', line 6 def initialize(name:, content:) @name = name @content = content end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
4 5 6 |
# File 'lib/rbs/buffer.rb', line 4 def content @content end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/rbs/buffer.rb', line 3 def name @name end |
Instance Method Details
#inspect ⇒ Object
56 57 58 |
# File 'lib/rbs/buffer.rb', line 56 def inspect "#<RBS::Buffer:#{__id__} @name=#{name}, @content=#{content.bytesize} bytes, @lines=#{lines.size} lines,>" end |
#last_position ⇒ Object
52 53 54 |
# File 'lib/rbs/buffer.rb', line 52 def last_position content.size end |
#lines ⇒ Object
11 12 13 |
# File 'lib/rbs/buffer.rb', line 11 def lines @lines ||= content.lines end |
#loc_to_pos(loc) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/rbs/buffer.rb', line 42 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
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rbs/buffer.rb', line 30 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 |
#ranges ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rbs/buffer.rb', line 15 def ranges @ranges ||= begin @ranges = [] offset = 0 lines.each do |line| size = line.size range = offset...(offset+size) @ranges << range offset += size end @ranges end end |