Skip to content

Commit

Permalink
Fix line number on non-newline terminated string
Browse files Browse the repository at this point in the history
  • Loading branch information
pocke committed Sep 6, 2024
1 parent fec8df5 commit fc3230c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/rbs/buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def ranges
@ranges << range
offset += size
end

if !content.end_with?("\n") && content.size > 0
@ranges[-1] = @ranges[-1].begin...(@ranges[-1].end+1)
end

@ranges
end
end
Expand Down
30 changes: 30 additions & 0 deletions test/rbs/buffer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,34 @@ def test_buffer

assert_equal 8, buffer.last_position
end

def test_buffer_with_no_eol
buffer = Buffer.new(name: Pathname("foo.rbs"), content: "123\nabc")

assert_equal ["123\n", "abc"], buffer.lines
assert_equal [0...4, 4...8], buffer.ranges

assert_equal [1, 0], buffer.pos_to_loc(0)
assert_equal [1, 1], buffer.pos_to_loc(1)
assert_equal [1, 2], buffer.pos_to_loc(2)
assert_equal [1, 3], buffer.pos_to_loc(3)
assert_equal [2, 0], buffer.pos_to_loc(4)
assert_equal [2, 1], buffer.pos_to_loc(5)
assert_equal [2, 2], buffer.pos_to_loc(6)
assert_equal [2, 3], buffer.pos_to_loc(7)

assert_equal 0, buffer.loc_to_pos([1, 0])
assert_equal 1, buffer.loc_to_pos([1, 1])
assert_equal 2, buffer.loc_to_pos([1, 2])
assert_equal 3, buffer.loc_to_pos([1, 3])
assert_equal 4, buffer.loc_to_pos([2, 0])
assert_equal 5, buffer.loc_to_pos([2, 1])
assert_equal 6, buffer.loc_to_pos([2, 2])
assert_equal 7, buffer.loc_to_pos([2, 3])

assert_equal "123", buffer.content[buffer.loc_to_pos([1,0])...buffer.loc_to_pos([1,3])]
assert_equal "123\n", buffer.content[buffer.loc_to_pos([1,0])...buffer.loc_to_pos([2,0])]

assert_equal 7, buffer.last_position
end
end

0 comments on commit fc3230c

Please sign in to comment.