Class: RBS::Buffer
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | lib/rbs/buffer.rb |
Class Method Summary
- .new(name: nil, content:, parent: nil) ⇒ Buffer constructor
Instance Attribute Summary
Instance Method Summary
Constructor Details
.new(name: nil, content:, parent: nil) ⇒ Buffer
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
#parent (readonly)
[ GitHub ]# File 'lib/rbs/buffer.rb', line 7
attr_reader :parent
Instance Method Details
#absolute_position(position)
[ GitHub ]# File 'lib/rbs/buffer.rb', line 131
def absolute_position(position) if parent_buffer pos = parent_position(position) or return parent_buffer.absolute_position(pos) else position end end
#detach
[ GitHub ]#inspect
[ GitHub ]#last_position
[ GitHub ]#line_count
[ GitHub ]# File 'lib/rbs/buffer.rb', line 26
def line_count ranges.size end
#lines
[ GitHub ]#loc_to_pos(loc)
[ GitHub ]# File 'lib/rbs/buffer.rb', line 68
def loc_to_pos(loc) line, column = loc if range = ranges.fetch(line - 1, nil) range.begin + column else last_position end end
#parent_buffer
[ GitHub ]#parent_position(position)
[ GitHub ]# File 'lib/rbs/buffer.rb', line 122
def parent_position(position) parent or raise "#parent_position is unavailable with buffer without parent" return nil unless position <= last_position line, column = pos_to_loc(position) parent_range = parent[1][line - 1] parent_range.begin + column end
#pos_to_loc(pos)
[ GitHub ]#ranges
[ GitHub ]# File 'lib/rbs/buffer.rb', line 30
def ranges @ranges ||= begin if content.empty? ranges = [0...0] #: Array[Range[Integer]] lines = [""] else lines = content.lines lines << "" if content.end_with?("\n") ranges = [] #: Array[Range[Integer]] offset = 0 lines.each do |line| size0 = line.size line = line.chomp range = offset...(offset+line.size) ranges << range offset += size0 end end ranges end end
#rbs_location(location, loc2 = nil)
[ GitHub ]# File 'lib/rbs/buffer.rb', line 90
def rbs_location(location, loc2=nil) if loc2 Location.new(self.top_buffer, location.start_character_offset, loc2.end_character_offset) else Location.new(self.top_buffer, location.start_character_offset, location.end_character_offset) end end
#sub_buffer(lines:)
[ GitHub ]# File 'lib/rbs/buffer.rb', line 98
def sub_buffer(lines:) buf = +"" lines.each_with_index do |range, index| start_pos = range.begin end_pos = range.end slice = content[start_pos...end_pos] or raise if slice.include?("\n") raise "Line #{index + 1} cannot contain newline character." end buf << slice buf << "\n" end buf.chomp! Buffer.new(content: buf, parent: [self, lines]) end
#top_buffer
[ GitHub ]# File 'lib/rbs/buffer.rb', line 140
def top_buffer if parent_buffer parent_buffer.top_buffer else self end end