Skip to content

Commit 36cc0b4

Browse files
committed
Revert "Introduce sub buffer"
This reverts commit f2259d9.
1 parent 8214d75 commit 36cc0b4

4 files changed

Lines changed: 32 additions & 199 deletions

File tree

lib/rbs/buffer.rb

Lines changed: 24 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,39 @@ module RBS
44
class Buffer
55
attr_reader :name
66
attr_reader :content
7-
attr_reader :parent
87

9-
def initialize(name: nil, content:, parent: nil)
10-
case
11-
when name && content
12-
@name = name
13-
@content = content
14-
@parent = nil
15-
when parent && content
16-
@name = parent[0].name
17-
@content = content
18-
@parent = parent
19-
end
8+
def initialize(name:, content:)
9+
@name = name
10+
@content = content
2011
end
2112

2213
def lines
23-
ranges.map { self.content[_1] || raise } #$ String
24-
end
25-
26-
def line_count
27-
ranges.size
14+
@lines ||= content.lines
2815
end
2916

3017
def ranges
31-
@ranges ||= begin
32-
lines = content.lines
33-
lines << "" if content.end_with?("\n")
34-
35-
ranges = [] #: Array[Range[Integer]]
36-
offset = 0
37-
38-
lines.each do |line|
39-
size0 = line.size
40-
line = line.chomp
41-
range = offset...(offset+line.size)
42-
ranges << range
43-
44-
offset += size0
18+
@ranges ||=
19+
begin
20+
@ranges = []
21+
offset = 0
22+
lines.each do |line|
23+
size = line.size
24+
range = offset...(offset+size)
25+
@ranges << range
26+
offset += size
27+
end
28+
29+
if !content.end_with?("\n") && content.size > 0
30+
@ranges[-1] = @ranges[-1].begin...(@ranges[-1].end+1)
31+
end
32+
33+
@ranges
4534
end
46-
47-
ranges
48-
end
4935
end
5036

5137
def pos_to_loc(pos)
5238
index = ranges.bsearch_index do |range|
53-
pos <= range.end ? true : false
39+
pos < range.end ? true : false
5440
end
5541

5642
if index
@@ -63,85 +49,19 @@ def pos_to_loc(pos)
6349
def loc_to_pos(loc)
6450
line, column = loc
6551

66-
if range = ranges.fetch(line - 1, nil)
52+
if range = ranges[line - 1]
6753
range.begin + column
6854
else
6955
last_position
7056
end
7157
end
7258

7359
def last_position
74-
if ranges.empty?
75-
0
76-
else
77-
ranges[-1].end
78-
end
60+
content.size
7961
end
8062

8163
def inspect
82-
"#<RBS::Buffer:#{__id__} @name=#{name}, @content=#{content.bytesize} bytes, @lines=#{ranges.size} lines,>"
83-
end
84-
85-
def rbs_location(location, loc2=nil)
86-
if loc2
87-
Location.new(self, location.start_character_offset, loc2.end_character_offset)
88-
else
89-
Location.new(self, location.start_character_offset, location.end_character_offset)
90-
end
91-
end
92-
93-
def sub_buffer(lines:)
94-
buf = +""
95-
lines.each_with_index do |range, index|
96-
start_pos = range.begin
97-
end_pos = range.end
98-
slice = content[start_pos...end_pos] or raise
99-
if slice.include?("\n")
100-
raise "Line #{index + 1} cannot contain newline character."
101-
end
102-
buf << slice
103-
buf << "\n"
104-
end
105-
106-
buf.chomp!
107-
108-
Buffer.new(content: buf, parent: [self, lines])
109-
end
110-
111-
def parent_buffer
112-
if parent
113-
parent[0]
114-
end
115-
end
116-
117-
def parent_position(position)
118-
parent or raise "#parent_position is unavailable with buffer without parent"
119-
return nil unless position <= last_position
120-
121-
line, column = pos_to_loc(position)
122-
parent_range = parent[1][line - 1]
123-
parent_range.begin + column
124-
end
125-
126-
def absolute_position(position)
127-
if parent_buffer
128-
pos = parent_position(position) or return
129-
parent_buffer.absolute_position(pos)
130-
else
131-
position
132-
end
133-
end
134-
135-
def top_buffer
136-
if parent_buffer
137-
parent_buffer.top_buffer
138-
else
139-
self
140-
end
141-
end
142-
143-
def detach
144-
Buffer.new(name: name, content: content)
64+
"#<RBS::Buffer:#{__id__} @name=#{name}, @content=#{content.bytesize} bytes, @lines=#{lines.size} lines,>"
14565
end
14666
end
14767
end

sig/buffer.rbs

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,80 +11,22 @@ module RBS
1111
# The content of the buffer.
1212
attr_reader content: String
1313

14-
attr_reader parent: [Buffer, Array[Range[Integer]]]?
14+
@lines: Array[String]
15+
16+
@ranges: Array[Range[Integer]]
1517

1618
def initialize: (name: Pathname name, content: String content) -> void
17-
| (content: String content, parent: [Buffer, Array[Range[Integer]]] parent) -> void
1819

19-
# Array of lines of the content, without the EOL
20-
#
21-
# ```rb
22-
# buffer = Buffer.new(name: name, content: "123\nabc")
23-
# buffer.lines # => ["123", "abc"]
24-
# ```
25-
#
26-
# If the input has EOL at the end of the file, the `lines` has an empty string at the end.
27-
#
28-
# ```rb
29-
# buffer = Buffer.new(name: name, content: "123\nabc\n")
30-
# buffer.lines # => ["123", "abc", ""]
31-
# ```
32-
#
3320
def lines: () -> Array[String]
3421

35-
@ranges: Array[Range[Integer]]?
36-
# Array of ranges that stores the ranges of the each line, without the EOL
37-
#
38-
# ```rb
39-
# buffer = Buffer.new(name: name, content: "123\nabc\n")
40-
# buffer.ranges # => [0...3, 4...7, 8...8]
41-
# ```
42-
#
4322
def ranges: () -> Array[Range[Integer]]
4423

45-
# Returns the number of the lines
46-
def line_count: () -> Integer
47-
4824
# Translate position to location.
4925
def pos_to_loc: (Integer pos) -> loc
5026

5127
# Translate location to position.
5228
def loc_to_pos: (loc loc) -> Integer
5329

5430
def last_position: () -> Integer
55-
56-
# Translate `Prism::Location` to `RBS::Location` attached to this buffer
57-
#
58-
# It assumes the `Prism::Location` has a source which is equivalent to `self`.
59-
#
60-
def rbs_location: (Prism::Location) -> Location
61-
| (Prism::Location, Prism::Location) -> Location
62-
63-
# Construct a buffer from substrings of this buffer.
64-
#
65-
# The returned buffer contains lines from given ranges.
66-
#
67-
# ```rb
68-
# buffer = Buffer.new(name: name, content: <<TEXT)
69-
# 12345
70-
# abcde
71-
# ABCDE
72-
# TEXT
73-
#
74-
# buffer.sub_buffer(lines: [0...1, 2...3]) # => Buffer with content = 1\n34
75-
# buffer.sub_buffer(lines: [5..7]) # => Raises an error because the range contains newline
76-
# ```
77-
#
78-
%a{pure} def sub_buffer: (lines: Array[Range[Integer]]) -> Buffer
79-
80-
%a{pure} def parent_buffer: () -> Buffer?
81-
82-
%a{pure} def parent_position: (Integer) -> Integer?
83-
84-
%a{pure} def absolute_position: (Integer) -> Integer?
85-
86-
%a{pure} def top_buffer: () -> Buffer
87-
88-
%a{pure} def detach: () -> Buffer
8931
end
9032
end

test/rbs/buffer_test.rb

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def test_buffer
99
abc
1010
CONTENT
1111

12-
assert_equal ["123", "abc", ""], buffer.lines
13-
assert_equal [0...3, 4...7, 8...8], buffer.ranges
12+
assert_equal ["123\n", "abc\n"], buffer.lines
13+
assert_equal [0...4, 4...8], buffer.ranges
1414

1515
assert_equal [1, 0], buffer.pos_to_loc(0)
1616
assert_equal [1, 1], buffer.pos_to_loc(1)
@@ -41,8 +41,8 @@ def test_buffer
4141
def test_buffer_with_no_eol
4242
buffer = Buffer.new(name: Pathname("foo.rbs"), content: "123\nabc")
4343

44-
assert_equal ["123", "abc"], buffer.lines
45-
assert_equal [0...3, 4...7], buffer.ranges
44+
assert_equal ["123\n", "abc"], buffer.lines
45+
assert_equal [0...4, 4...8], buffer.ranges
4646

4747
assert_equal [1, 0], buffer.pos_to_loc(0)
4848
assert_equal [1, 1], buffer.pos_to_loc(1)
@@ -67,33 +67,4 @@ def test_buffer_with_no_eol
6767

6868
assert_equal 7, buffer.last_position
6969
end
70-
71-
def test_sub_buffer
72-
buffer = Buffer.new(name: Pathname("foo.rbs"), content: <<~CONTENT)
73-
123
74-
abc
75-
CONTENT
76-
77-
buffer.sub_buffer(lines: [1...3, 5...7]).tap do |sub_buffer|
78-
assert_equal <<~CONTENT.chomp, sub_buffer.content
79-
23
80-
bc
81-
CONTENT
82-
83-
assert_equal 1, sub_buffer.parent_position(0)
84-
assert_equal 2, sub_buffer.parent_position(1)
85-
assert_equal 3, sub_buffer.parent_position(2)
86-
assert_equal 5, sub_buffer.parent_position(3)
87-
assert_equal 6, sub_buffer.parent_position(4)
88-
assert_equal 7, sub_buffer.parent_position(5)
89-
90-
assert_equal [1, 0], sub_buffer.pos_to_loc(0)
91-
assert_equal [1, 1], sub_buffer.pos_to_loc(1)
92-
assert_equal [1, 2], sub_buffer.pos_to_loc(2)
93-
assert_equal [2, 0], sub_buffer.pos_to_loc(3)
94-
assert_equal [2, 1], sub_buffer.pos_to_loc(4)
95-
assert_equal [2, 2], sub_buffer.pos_to_loc(5)
96-
assert_equal [3, 0], sub_buffer.pos_to_loc(6)
97-
end
98-
end
9970
end

test/rbs/parser_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_interface_mixin
6262
end
6363

6464
def test_type_error_for_content
65-
buffer = RBS::Buffer.new(content: 1, name: Pathname("a.rbs"))
65+
buffer = RBS::Buffer.new(content: 1, name: nil)
6666
assert_raises TypeError do
6767
RBS::Parser.parse_signature(buffer)
6868
end

0 commit comments

Comments
 (0)