Skip to content

Commit b40fcf5

Browse files
committed
Fix buffer for empty string
1 parent d570fb4 commit b40fcf5

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

lib/rbs/buffer.rb

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,24 @@ def line_count
2929

3030
def ranges
3131
@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
32+
if content.empty?
33+
ranges = [0...0] #: Array[Range[Integer]]
34+
lines = [""]
35+
else
36+
lines = content.lines
37+
lines << "" if content.end_with?("\n")
38+
39+
ranges = [] #: Array[Range[Integer]]
40+
offset = 0
41+
42+
lines.each do |line|
43+
size0 = line.size
44+
line = line.chomp
45+
range = offset...(offset+line.size)
46+
ranges << range
47+
48+
offset += size0
49+
end
4550
end
4651

4752
ranges

test/rbs/ast/ruby/comment_block_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,21 @@ def test_each_paragraph
144144
end
145145
end
146146

147+
def test_each_paragraph__empty
148+
buffer, comments = parse_comments(<<~RUBY)
149+
#
150+
RUBY
151+
152+
block = CommentBlock.new(buffer, comments)
153+
154+
paragraphs = block.each_paragraph([]).to_a
155+
156+
paragraphs[0].tap do |paragraph|
157+
assert_instance_of RBS::Location, paragraph
158+
assert_equal "", paragraph.local_source
159+
end
160+
end
161+
147162
def test_each_paragraph_colon
148163
buffer, comments = parse_comments(<<~RUBY)
149164
# : Foo

test/rbs/buffer_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,23 @@ def test_sub_buffer
9696
assert_equal [3, 0], sub_buffer.pos_to_loc(6)
9797
end
9898
end
99+
100+
def test_empty_buffer
101+
buffer = Buffer.new(name: Pathname("foo.rbs"), content: "")
102+
103+
assert_equal "", buffer.content
104+
assert_equal [""], buffer.lines
105+
assert_equal [0...0], buffer.ranges
106+
assert_equal "", buffer.content
107+
end
108+
109+
def test_empty_sub_buffer
110+
buffer = Buffer.new(name: Pathname("foo.rbs"), content: "")
111+
112+
sub_buffer = buffer.sub_buffer(lines: [0...0])
113+
assert_equal "", sub_buffer.content
114+
assert_equal [""], sub_buffer.lines
115+
assert_equal [0...0], sub_buffer.ranges
116+
assert_equal "", sub_buffer.content
117+
end
99118
end

0 commit comments

Comments
 (0)