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.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #absolute_position(position) ⇒ Object
- #detach ⇒ Object
-
#initialize(name: nil, content:, parent: nil) ⇒ Buffer
constructor
A new instance of Buffer.
- #inspect ⇒ Object
- #last_position ⇒ Object
- #line_count ⇒ Object
- #lines ⇒ Object
- #loc_to_pos(loc) ⇒ Object
- #parent_buffer ⇒ Object
- #parent_position(position) ⇒ Object
- #pos_to_loc(pos) ⇒ Object
- #ranges ⇒ Object
- #rbs_location(location, loc2 = nil) ⇒ Object
- #sub_buffer(lines:) ⇒ Object
- #top_buffer ⇒ Object
Constructor Details
#initialize(name: nil, content:, parent: nil) ⇒ Buffer
Returns a new instance of Buffer.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/rbs/buffer.rb', line 9 def initialize(name: nil, content:, parent: nil) case when name && content @name = name @content = content @parent = nil when parent && content @name = parent[0].name @content = content @parent = parent end end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
6 7 8 |
# File 'lib/rbs/buffer.rb', line 6 def content @content end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/rbs/buffer.rb', line 5 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
7 8 9 |
# File 'lib/rbs/buffer.rb', line 7 def parent @parent end |
Instance Method Details
#absolute_position(position) ⇒ Object
131 132 133 134 135 136 137 138 |
# 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 ⇒ Object
148 149 150 |
# File 'lib/rbs/buffer.rb', line 148 def detach Buffer.new(name: name, content: content) end |
#inspect ⇒ Object
86 87 88 |
# File 'lib/rbs/buffer.rb', line 86 def inspect "#<RBS::Buffer:#{__id__} @name=#{name}, @content=#{content.bytesize} bytes, @lines=#{ranges.size} lines,>" end |
#last_position ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/rbs/buffer.rb', line 78 def last_position if ranges.empty? 0 else ranges[-1].end end end |
#line_count ⇒ Object
26 27 28 |
# File 'lib/rbs/buffer.rb', line 26 def line_count ranges.size end |
#lines ⇒ Object
22 23 24 |
# File 'lib/rbs/buffer.rb', line 22 def lines ranges.map { self.content[_1] || raise } #$ String end |
#loc_to_pos(loc) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# 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 ⇒ Object
116 117 118 119 120 |
# File 'lib/rbs/buffer.rb', line 116 def parent_buffer if parent parent[0] end end |
#parent_position(position) ⇒ Object
122 123 124 125 126 127 128 129 |
# 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) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rbs/buffer.rb', line 56 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
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# 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) ⇒ Object
90 91 92 93 94 95 96 |
# 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:) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# 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 ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/rbs/buffer.rb', line 140 def top_buffer if parent_buffer parent_buffer.top_buffer else self end end |