123456789_123456789_123456789_123456789_123456789_

Class: RBS::Buffer

Relationships & Source Files
Inherits: Object
Defined in: lib/rbs/buffer.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(name:, content:) ⇒ Buffer

[ GitHub ]

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

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

Instance Attribute Details

#content (readonly)

[ GitHub ]

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

attr_reader :content

#name (readonly)

[ GitHub ]

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

attr_reader :name

Instance Method Details

#inspect

[ GitHub ]

  
# File 'lib/rbs/buffer.rb', line 58

def inspect
  "#<RBS::Buffer:#{__id__} @name=#{name}, @content=#{content.bytesize} bytes, @lines=#{lines.size} lines,>"
end

#last_position

[ GitHub ]

  
# File 'lib/rbs/buffer.rb', line 54

def last_position
  content.size
end

#lines

[ GitHub ]

  
# File 'lib/rbs/buffer.rb', line 13

def lines
  @lines ||= content.lines
end

#loc_to_pos(loc)

[ GitHub ]

  
# File 'lib/rbs/buffer.rb', line 44

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)

[ GitHub ]

  
# File 'lib/rbs/buffer.rb', line 32

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

[ GitHub ]

  
# File 'lib/rbs/buffer.rb', line 17

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