Skip to content

Commit c62a5a4

Browse files
authored
Fix buffer line index validations (#98)
* Fix buffer line index validations Fixes: #97 This is technically a breaking change for plugins that depend on index 0 returning the last line in the buffer, but IMO that behavior is pretty obviously incorrect and we should just fix it. * fixup! Fix buffer line index validations remove extra comment
1 parent ebd0ef9 commit c62a5a4

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

lib/neovim/buffer.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def length
5454
# @param index [Integer]
5555
# @return [String]
5656
def [](index)
57-
check_index(index)
57+
check_index(index, 1)
5858
@lines[index - 1]
5959
end
6060

@@ -64,7 +64,7 @@ def [](index)
6464
# @param str [String]
6565
# @return [String]
6666
def []=(index, str)
67-
check_index(index)
67+
check_index(index, 1)
6868
@lines[index - 1] = str
6969
end
7070

@@ -73,21 +73,24 @@ def []=(index, str)
7373
# @param index [Integer]
7474
# @return [void]
7575
def delete(index)
76-
check_index(index)
76+
check_index(index, 1)
7777
@lines.delete(index - 1)
7878
nil
7979
end
8080

8181
# Append a line after the given line (1-indexed).
8282
#
83+
# Unlike the other methods, `0` is a valid index argument here, and inserts
84+
# into the first line of the buffer.
85+
#
8386
# To maintain backwards compatibility with +vim+, the cursor is forced back
8487
# to its previous position after inserting the line.
8588
#
8689
# @param index [Integer]
8790
# @param str [String]
8891
# @return [String]
8992
def append(index, str)
90-
check_index(index)
93+
check_index(index, 0)
9194
window = @session.request(:nvim_get_current_win)
9295
cursor = window.cursor
9396

@@ -127,8 +130,8 @@ def active?
127130

128131
private
129132

130-
def check_index(index)
131-
raise ArgumentError, "Index out of bounds" if index < 0
133+
def check_index(index, min)
134+
raise ArgumentError, "Index out of bounds" if index < min
132135
end
133136

134137
public

0 commit comments

Comments
 (0)