From fc3230cc0c92a1e9bbabff860f12d03c9d02a074 Mon Sep 17 00:00:00 2001 From: Masataka Pocke Kuwabara Date: Thu, 5 Sep 2024 15:58:40 +0900 Subject: [PATCH] Fix line number on non-newline terminated string --- lib/rbs/buffer.rb | 5 +++++ test/rbs/buffer_test.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/rbs/buffer.rb b/lib/rbs/buffer.rb index 10f0273df..c5f48dcfe 100644 --- a/lib/rbs/buffer.rb +++ b/lib/rbs/buffer.rb @@ -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 diff --git a/test/rbs/buffer_test.rb b/test/rbs/buffer_test.rb index 58a36b5f7..a3fbc01c6 100644 --- a/test/rbs/buffer_test.rb +++ b/test/rbs/buffer_test.rb @@ -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