api/buffer: introduce buffer_[gs]et_lines with new indexing convention.

-1 is index past the end, and -2 is the index of the last element.
This eliminates the need for include_start/include_end.

Allow the handling of out-of-bounds to be configurable.
This commit is contained in:
Björn Linse
2016-01-23 20:30:26 +01:00
parent b8643f69c1
commit 51c7818d42
2 changed files with 108 additions and 29 deletions

View File

@@ -35,10 +35,11 @@ describe('buffer_* functions', function()
eq('', curbuf('get_line', 0))
end)
it('get_line: out-of-bounds returns empty string', function()
it('get_line: out-of-bounds is an error', function()
curbuf('set_line', 0, 'line1.a')
eq('', curbuf('get_line', 1))
eq('', curbuf('get_line', -2))
eq(1, curbuf('line_count')) -- sanity
eq(false, pcall(curbuf, 'get_line', 1))
eq(false, pcall(curbuf, 'get_line', -2))
end)
it('set_line, del_line: out-of-bounds is an error', function()
@@ -68,14 +69,16 @@ describe('buffer_* functions', function()
eq({}, curbuf('get_line_slice', -4, -5, true, true))
end)
it('set_line_slice: out-of-bounds is an error', function()
it('set_line_slice: out-of-bounds extends past end', function()
curbuf('set_line_slice', 0, 0, true, true, {'a', 'b', 'c'})
eq({'a', 'b', 'c'}, curbuf('get_line_slice', 0, 2, true, true)) --sanity
eq({'c'}, curbuf('get_line_slice', -1, 4, true, true))
eq({'a', 'b', 'c'}, curbuf('get_line_slice', 0, 5, true, true))
eq(false, pcall(curbuf, 'set_line_slice', 4, 5, true, true, {'d'}))
eq(false, pcall(curbuf, 'set_line_slice', -4, -5, true, true, {'d'}))
curbuf('set_line_slice', 4, 5, true, true, {'d'})
eq({'a', 'b', 'c', 'd'}, curbuf('get_line_slice', 0, 5, true, true))
curbuf('set_line_slice', -4, -5, true, true, {'e'})
eq({'e', 'a', 'b', 'c', 'd'}, curbuf('get_line_slice', 0, 5, true, true))
end)
it('works', function()