diff options
| author | Josh Rahm <joshuarahm@gmail.com> | 2024-03-09 15:00:41 -0700 |
|---|---|---|
| committer | Josh Rahm <joshuarahm@gmail.com> | 2024-03-09 15:00:41 -0700 |
| commit | 7a7f497b483cd65e340064f23ed1c73425ecba0a (patch) | |
| tree | d5c99ea22a1e10300d06165f8ac96df6b0dc59e1 /test/functional/api/buffer_spec.lua | |
| parent | 1b7b916b7631ddf73c38e3a0070d64e4636cb2f3 (diff) | |
| parent | ade1b12f49c3b3914c74847d791eb90ea90b56b7 (diff) | |
| download | rneovim-7a7f497b483cd65e340064f23ed1c73425ecba0a.tar.gz rneovim-7a7f497b483cd65e340064f23ed1c73425ecba0a.tar.bz2 rneovim-7a7f497b483cd65e340064f23ed1c73425ecba0a.zip | |
Merge remote-tracking branch 'upstream/master' into aucmd_textputpost
Diffstat (limited to 'test/functional/api/buffer_spec.lua')
| -rw-r--r-- | test/functional/api/buffer_spec.lua | 1598 |
1 files changed, 832 insertions, 766 deletions
diff --git a/test/functional/api/buffer_spec.lua b/test/functional/api/buffer_spec.lua index 6ed9aa574a..78d220ff57 100644 --- a/test/functional/api/buffer_spec.lua +++ b/test/functional/api/buffer_spec.lua @@ -1,19 +1,18 @@ local helpers = require('test.functional.helpers')(after_each) local Screen = require('test.functional.ui.screen') -local clear, nvim, buffer = helpers.clear, helpers.nvim, helpers.buffer -local curbuf, curwin, eq = helpers.curbuf, helpers.curwin, helpers.eq -local curbufmeths, ok = helpers.curbufmeths, helpers.ok +local clear = helpers.clear +local eq = helpers.eq +local ok = helpers.ok local describe_lua_and_rpc = helpers.describe_lua_and_rpc(describe) -local meths = helpers.meths -local funcs = helpers.funcs +local api = helpers.api +local fn = helpers.fn local request = helpers.request local exc_exec = helpers.exc_exec local exec_lua = helpers.exec_lua local feed_command = helpers.feed_command local insert = helpers.insert -local NIL = helpers.NIL +local NIL = vim.NIL local command = helpers.command -local bufmeths = helpers.bufmeths local feed = helpers.feed local pcall_err = helpers.pcall_err local assert_alive = helpers.assert_alive @@ -23,16 +22,15 @@ describe('api/buf', function() -- access deprecated functions local function curbuf_depr(method, ...) - return request('buffer_'..method, 0, ...) + return request('buffer_' .. method, 0, ...) end - describe('nvim_buf_set_lines, nvim_buf_line_count', function() it('deprecated forms', function() eq(1, curbuf_depr('line_count')) - curbuf_depr('insert', -1, {'line'}) + curbuf_depr('insert', -1, { 'line' }) eq(2, curbuf_depr('line_count')) - curbuf_depr('insert', -1, {'line'}) + curbuf_depr('insert', -1, { 'line' }) eq(3, curbuf_depr('line_count')) curbuf_depr('del_line', -1) eq(2, curbuf_depr('line_count')) @@ -43,108 +41,114 @@ describe('api/buf', function() end) it("doesn't crash just after set undolevels=1 #24894", function() - local buf = meths.create_buf(false, true) - meths.buf_set_option(buf, 'undolevels', -1) - meths.buf_set_lines(buf, 0, 1, false, { }) + local buf = api.nvim_create_buf(false, true) + api.nvim_buf_set_option(buf, 'undolevels', -1) + api.nvim_buf_set_lines(buf, 0, 1, false, {}) assert_alive() end) it('cursor position is maintained after lines are inserted #9961', function() -- replace the buffer contents with these three lines. - request('nvim_buf_set_lines', 0, 0, -1, 1, {"line1", "line2", "line3", "line4"}) + api.nvim_buf_set_lines(0, 0, -1, true, { 'line1', 'line2', 'line3', 'line4' }) -- Set the current cursor to {3, 2}. - curwin('set_cursor', {3, 2}) + api.nvim_win_set_cursor(0, { 3, 2 }) -- add 2 lines and delete 1 line above the current cursor position. - request('nvim_buf_set_lines', 0, 1, 2, 1, {"line5", "line6"}) + api.nvim_buf_set_lines(0, 1, 2, true, { 'line5', 'line6' }) -- check the current set of lines in the buffer. - eq({"line1", "line5", "line6", "line3", "line4"}, buffer('get_lines', 0, 0, -1, 1)) + eq({ 'line1', 'line5', 'line6', 'line3', 'line4' }, api.nvim_buf_get_lines(0, 0, -1, true)) -- cursor should be moved below by 1 line. - eq({4, 2}, curwin('get_cursor')) + eq({ 4, 2 }, api.nvim_win_get_cursor(0)) -- add a line after the current cursor position. - request('nvim_buf_set_lines', 0, 5, 5, 1, {"line7"}) + api.nvim_buf_set_lines(0, 5, 5, true, { 'line7' }) -- check the current set of lines in the buffer. - eq({"line1", "line5", "line6", "line3", "line4", "line7"}, buffer('get_lines', 0, 0, -1, 1)) + eq( + { 'line1', 'line5', 'line6', 'line3', 'line4', 'line7' }, + api.nvim_buf_get_lines(0, 0, -1, true) + ) -- cursor position is unchanged. - eq({4, 2}, curwin('get_cursor')) + eq({ 4, 2 }, api.nvim_win_get_cursor(0)) -- overwrite current cursor line. - request('nvim_buf_set_lines', 0, 3, 5, 1, {"line8", "line9"}) + api.nvim_buf_set_lines(0, 3, 5, true, { 'line8', 'line9' }) -- check the current set of lines in the buffer. - eq({"line1", "line5", "line6", "line8", "line9", "line7"}, buffer('get_lines', 0, 0, -1, 1)) + eq( + { 'line1', 'line5', 'line6', 'line8', 'line9', 'line7' }, + api.nvim_buf_get_lines(0, 0, -1, true) + ) -- cursor position is unchanged. - eq({4, 2}, curwin('get_cursor')) + eq({ 4, 2 }, api.nvim_win_get_cursor(0)) -- delete current cursor line. - request('nvim_buf_set_lines', 0, 3, 5, 1, {}) + api.nvim_buf_set_lines(0, 3, 5, true, {}) -- check the current set of lines in the buffer. - eq({"line1", "line5", "line6", "line7"}, buffer('get_lines', 0, 0, -1, 1)) + eq({ 'line1', 'line5', 'line6', 'line7' }, api.nvim_buf_get_lines(0, 0, -1, true)) -- cursor position is unchanged. - eq({4, 2}, curwin('get_cursor')) + eq({ 4, 2 }, api.nvim_win_get_cursor(0)) end) it('cursor position is maintained in non-current window', function() - meths.buf_set_lines(0, 0, -1, 1, {"line1", "line2", "line3", "line4"}) - meths.win_set_cursor(0, {3, 2}) - local win = meths.get_current_win() - local buf = meths.get_current_buf() + api.nvim_buf_set_lines(0, 0, -1, true, { 'line1', 'line2', 'line3', 'line4' }) + api.nvim_win_set_cursor(0, { 3, 2 }) + local win = api.nvim_get_current_win() + local buf = api.nvim_get_current_buf() command('new') - meths.buf_set_lines(buf, 1, 2, 1, {"line5", "line6"}) - eq({"line1", "line5", "line6", "line3", "line4"}, meths.buf_get_lines(buf, 0, -1, true)) - eq({4, 2}, meths.win_get_cursor(win)) + api.nvim_buf_set_lines(buf, 1, 2, true, { 'line5', 'line6' }) + eq({ 'line1', 'line5', 'line6', 'line3', 'line4' }, api.nvim_buf_get_lines(buf, 0, -1, true)) + eq({ 4, 2 }, api.nvim_win_get_cursor(win)) end) it('cursor position is maintained in TWO non-current windows', function() - meths.buf_set_lines(0, 0, -1, 1, {"line1", "line2", "line3", "line4"}) - meths.win_set_cursor(0, {3, 2}) - local win = meths.get_current_win() - local buf = meths.get_current_buf() + api.nvim_buf_set_lines(0, 0, -1, true, { 'line1', 'line2', 'line3', 'line4' }) + api.nvim_win_set_cursor(0, { 3, 2 }) + local win = api.nvim_get_current_win() + local buf = api.nvim_get_current_buf() command('split') - meths.win_set_cursor(0, {4, 2}) - local win2 = meths.get_current_win() + api.nvim_win_set_cursor(0, { 4, 2 }) + local win2 = api.nvim_get_current_win() -- set current window to third one with another buffer - command("new") + command('new') - meths.buf_set_lines(buf, 1, 2, 1, {"line5", "line6"}) - eq({"line1", "line5", "line6", "line3", "line4"}, meths.buf_get_lines(buf, 0, -1, true)) - eq({4, 2}, meths.win_get_cursor(win)) - eq({5, 2}, meths.win_get_cursor(win2)) + api.nvim_buf_set_lines(buf, 1, 2, true, { 'line5', 'line6' }) + eq({ 'line1', 'line5', 'line6', 'line3', 'line4' }, api.nvim_buf_get_lines(buf, 0, -1, true)) + eq({ 4, 2 }, api.nvim_win_get_cursor(win)) + eq({ 5, 2 }, api.nvim_win_get_cursor(win2)) end) it('line_count has defined behaviour for unloaded buffers', function() -- we'll need to know our bufnr for when it gets unloaded - local bufnr = curbuf('get_number') + local bufnr = api.nvim_buf_get_number(0) -- replace the buffer contents with these three lines - request('nvim_buf_set_lines', bufnr, 0, -1, 1, {"line1", "line2", "line3", "line4"}) + api.nvim_buf_set_lines(bufnr, 0, -1, true, { 'line1', 'line2', 'line3', 'line4' }) -- check the line count is correct - eq(4, request('nvim_buf_line_count', bufnr)) + eq(4, api.nvim_buf_line_count(bufnr)) -- force unload the buffer (this will discard changes) command('new') - command('bunload! '..bufnr) + command('bunload! ' .. bufnr) -- line count for an unloaded buffer should always be 0 - eq(0, request('nvim_buf_line_count', bufnr)) + eq(0, api.nvim_buf_line_count(bufnr)) end) it('get_lines has defined behaviour for unloaded buffers', function() -- we'll need to know our bufnr for when it gets unloaded - local bufnr = curbuf('get_number') + local bufnr = api.nvim_buf_get_number(0) -- replace the buffer contents with these three lines - buffer('set_lines', bufnr, 0, -1, 1, {"line1", "line2", "line3", "line4"}) + api.nvim_buf_set_lines(bufnr, 0, -1, true, { 'line1', 'line2', 'line3', 'line4' }) -- confirm that getting lines works - eq({"line2", "line3"}, buffer('get_lines', bufnr, 1, 3, 1)) + eq({ 'line2', 'line3' }, api.nvim_buf_get_lines(bufnr, 1, 3, true)) -- force unload the buffer (this will discard changes) command('new') - command('bunload! '..bufnr) + command('bunload! ' .. bufnr) -- attempting to get lines now always gives empty list - eq({}, buffer('get_lines', bufnr, 1, 3, 1)) + eq({}, api.nvim_buf_get_lines(bufnr, 1, 3, true)) -- it's impossible to get out-of-bounds errors for an unloaded buffer - eq({}, buffer('get_lines', bufnr, 8888, 9999, 1)) + eq({}, api.nvim_buf_get_lines(bufnr, 8888, 9999, true)) end) describe('handles topline', function() @@ -152,28 +156,32 @@ describe('api/buf', function() before_each(function() screen = Screen.new(20, 12) screen:set_default_attr_ids { - [1] = {bold = true, foreground = Screen.colors.Blue1}; - [2] = {reverse = true, bold = true}; - [3] = {reverse = true}; + [1] = { bold = true, foreground = Screen.colors.Blue1 }, + [2] = { reverse = true, bold = true }, + [3] = { reverse = true }, } screen:attach() - meths.buf_set_lines(0, 0, -1, 1, {"aaa", "bbb", "ccc", "ddd", "www", "xxx", "yyy", "zzz"}) - meths.set_option_value('modified', false, {}) + api.nvim_buf_set_lines( + 0, + 0, + -1, + true, + { 'aaa', 'bbb', 'ccc', 'ddd', 'www', 'xxx', 'yyy', 'zzz' } + ) + api.nvim_set_option_value('modified', false, {}) end) it('of current window', function() - local win = meths.get_current_win() - local buf = meths.get_current_buf() + local win = api.nvim_get_current_win() + local buf = api.nvim_get_current_buf() command('new | wincmd w') - meths.win_set_cursor(win, {8,0}) + api.nvim_win_set_cursor(win, { 8, 0 }) - screen:expect{grid=[[ + screen:expect { + grid = [[ | - {1:~ }| - {1:~ }| - {1:~ }| - {1:~ }| + {1:~ }|*4 {3:[No Name] }| www | xxx | @@ -181,15 +189,14 @@ describe('api/buf', function() ^zzz | {2:[No Name] }| | - ]]} + ]], + } - meths.buf_set_lines(buf, 0, 2, true, {"aaabbb"}) - screen:expect{grid=[[ + api.nvim_buf_set_lines(buf, 0, 2, true, { 'aaabbb' }) + screen:expect { + grid = [[ | - {1:~ }| - {1:~ }| - {1:~ }| - {1:~ }| + {1:~ }|*4 {3:[No Name] }| www | xxx | @@ -197,16 +204,15 @@ describe('api/buf', function() ^zzz | {2:[No Name] [+] }| | - ]]} + ]], + } -- replacing topline keeps it the topline - meths.buf_set_lines(buf, 3, 4, true, {"wwweeee"}) - screen:expect{grid=[[ + api.nvim_buf_set_lines(buf, 3, 4, true, { 'wwweeee' }) + screen:expect { + grid = [[ | - {1:~ }| - {1:~ }| - {1:~ }| - {1:~ }| + {1:~ }|*4 {3:[No Name] }| wwweeee | xxx | @@ -214,16 +220,15 @@ describe('api/buf', function() ^zzz | {2:[No Name] [+] }| | - ]]} + ]], + } -- inserting just before topline does not scroll up if cursor would be moved - meths.buf_set_lines(buf, 3, 3, true, {"mmm"}) - screen:expect{grid=[[ + api.nvim_buf_set_lines(buf, 3, 3, true, { 'mmm' }) + screen:expect { + grid = [[ | - {1:~ }| - {1:~ }| - {1:~ }| - {1:~ }| + {1:~ }|*4 {3:[No Name] }| wwweeee | xxx | @@ -231,15 +236,15 @@ describe('api/buf', function() ^zzz | {2:[No Name] [+] }| | - ]], unchanged=true} + ]], + unchanged = true, + } - meths.win_set_cursor(0, {7, 0}) - screen:expect{grid=[[ + api.nvim_win_set_cursor(0, { 7, 0 }) + screen:expect { + grid = [[ | - {1:~ }| - {1:~ }| - {1:~ }| - {1:~ }| + {1:~ }|*4 {3:[No Name] }| wwweeee | xxx | @@ -247,15 +252,14 @@ describe('api/buf', function() zzz | {2:[No Name] [+] }| | - ]]} + ]], + } - meths.buf_set_lines(buf, 4, 4, true, {"mmmeeeee"}) - screen:expect{grid=[[ + api.nvim_buf_set_lines(buf, 4, 4, true, { 'mmmeeeee' }) + screen:expect { + grid = [[ | - {1:~ }| - {1:~ }| - {1:~ }| - {1:~ }| + {1:~ }|*4 {3:[No Name] }| mmmeeeee | wwweeee | @@ -263,22 +267,21 @@ describe('api/buf', function() ^yyy | {2:[No Name] [+] }| | - ]]} + ]], + } end) it('of non-current window', function() - local win = meths.get_current_win() - local buf = meths.get_current_buf() + local win = api.nvim_get_current_win() + local buf = api.nvim_get_current_buf() command('new') - meths.win_set_cursor(win, {8,0}) + api.nvim_win_set_cursor(win, { 8, 0 }) - screen:expect{grid=[[ + screen:expect { + grid = [[ ^ | - {1:~ }| - {1:~ }| - {1:~ }| - {1:~ }| + {1:~ }|*4 {2:[No Name] }| www | xxx | @@ -286,15 +289,14 @@ describe('api/buf', function() zzz | {3:[No Name] }| | - ]]} + ]], + } - meths.buf_set_lines(buf, 0, 2, true, {"aaabbb"}) - screen:expect{grid=[[ + api.nvim_buf_set_lines(buf, 0, 2, true, { 'aaabbb' }) + screen:expect { + grid = [[ ^ | - {1:~ }| - {1:~ }| - {1:~ }| - {1:~ }| + {1:~ }|*4 {2:[No Name] }| www | xxx | @@ -302,16 +304,15 @@ describe('api/buf', function() zzz | {3:[No Name] [+] }| | - ]]} + ]], + } -- replacing topline keeps it the topline - meths.buf_set_lines(buf, 3, 4, true, {"wwweeee"}) - screen:expect{grid=[[ + api.nvim_buf_set_lines(buf, 3, 4, true, { 'wwweeee' }) + screen:expect { + grid = [[ ^ | - {1:~ }| - {1:~ }| - {1:~ }| - {1:~ }| + {1:~ }|*4 {2:[No Name] }| wwweeee | xxx | @@ -319,16 +320,15 @@ describe('api/buf', function() zzz | {3:[No Name] [+] }| | - ]]} + ]], + } -- inserting just before topline scrolls up - meths.buf_set_lines(buf, 3, 3, true, {"mmm"}) - screen:expect{grid=[[ + api.nvim_buf_set_lines(buf, 3, 3, true, { 'mmm' }) + screen:expect { + grid = [[ ^ | - {1:~ }| - {1:~ }| - {1:~ }| - {1:~ }| + {1:~ }|*4 {2:[No Name] }| mmm | wwweeee | @@ -336,18 +336,20 @@ describe('api/buf', function() yyy | {3:[No Name] [+] }| | - ]]} + ]], + } end) it('of split windows with same buffer', function() - local win = meths.get_current_win() - local buf = meths.get_current_buf() + local win = api.nvim_get_current_win() + local buf = api.nvim_get_current_buf() command('split') - meths.win_set_cursor(win, {8,0}) - meths.win_set_cursor(0, {1,0}) + api.nvim_win_set_cursor(win, { 8, 0 }) + api.nvim_win_set_cursor(0, { 1, 0 }) - screen:expect{grid=[[ + screen:expect { + grid = [[ ^aaa | bbb | ccc | @@ -360,10 +362,12 @@ describe('api/buf', function() zzz | {3:[No Name] }| | - ]]} - meths.buf_set_lines(buf, 0, 2, true, {"aaabbb"}) + ]], + } + api.nvim_buf_set_lines(buf, 0, 2, true, { 'aaabbb' }) - screen:expect{grid=[[ + screen:expect { + grid = [[ ^aaabbb | ccc | ddd | @@ -376,11 +380,13 @@ describe('api/buf', function() zzz | {3:[No Name] [+] }| | - ]]} + ]], + } -- replacing topline keeps it the topline - meths.buf_set_lines(buf, 3, 4, true, {"wwweeee"}) - screen:expect{grid=[[ + api.nvim_buf_set_lines(buf, 3, 4, true, { 'wwweeee' }) + screen:expect { + grid = [[ ^aaabbb | ccc | ddd | @@ -393,11 +399,13 @@ describe('api/buf', function() zzz | {3:[No Name] [+] }| | - ]]} + ]], + } -- inserting just before topline scrolls up - meths.buf_set_lines(buf, 3, 3, true, {"mmm"}) - screen:expect{grid=[[ + api.nvim_buf_set_lines(buf, 3, 3, true, { 'mmm' }) + screen:expect { + grid = [[ ^aaabbb | ccc | ddd | @@ -410,20 +418,21 @@ describe('api/buf', function() yyy | {3:[No Name] [+] }| | - ]]} + ]], + } end) end) it('handles clearing out non-current buffer #24911', function() - local buf = meths.get_current_buf() - meths.buf_set_lines(buf, 0, -1, true, {"aaa", "bbb", "ccc"}) - command("new") + local buf = api.nvim_get_current_buf() + api.nvim_buf_set_lines(buf, 0, -1, true, { 'aaa', 'bbb', 'ccc' }) + command('new') - meths.buf_set_lines(0, 0, -1, true, {"xxx", "yyy", "zzz"}) + api.nvim_buf_set_lines(0, 0, -1, true, { 'xxx', 'yyy', 'zzz' }) - meths.buf_set_lines(buf, 0, -1, true, {}) - eq({"xxx", "yyy", "zzz"}, meths.buf_get_lines(0, 0, -1, true)) - eq({''}, meths.buf_get_lines(buf, 0, -1, true)) + api.nvim_buf_set_lines(buf, 0, -1, true, {}) + eq({ 'xxx', 'yyy', 'zzz' }, api.nvim_buf_get_lines(0, 0, -1, true)) + eq({ '' }, api.nvim_buf_get_lines(buf, 0, -1, true)) end) end) @@ -461,8 +470,8 @@ describe('api/buf', function() describe('deprecated: {get,set}_line_slice', function() it('get_line_slice: out-of-bounds returns empty array', function() - curbuf_depr('set_line_slice', 0, 0, true, true, {'a', 'b', 'c'}) - eq({'a', 'b', 'c'}, curbuf_depr('get_line_slice', 0, 2, true, true)) --sanity + curbuf_depr('set_line_slice', 0, 0, true, true, { 'a', 'b', 'c' }) + eq({ 'a', 'b', 'c' }, curbuf_depr('get_line_slice', 0, 2, true, true)) --sanity eq({}, curbuf_depr('get_line_slice', 2, 3, false, true)) eq({}, curbuf_depr('get_line_slice', 3, 9, true, true)) @@ -472,62 +481,73 @@ describe('api/buf', function() end) it('set_line_slice: out-of-bounds extends past end', function() - curbuf_depr('set_line_slice', 0, 0, true, true, {'a', 'b', 'c'}) - eq({'a', 'b', 'c'}, curbuf_depr('get_line_slice', 0, 2, true, true)) --sanity + curbuf_depr('set_line_slice', 0, 0, true, true, { 'a', 'b', 'c' }) + eq({ 'a', 'b', 'c' }, curbuf_depr('get_line_slice', 0, 2, true, true)) --sanity - eq({'c'}, curbuf_depr('get_line_slice', -1, 4, true, true)) - eq({'a', 'b', 'c'}, curbuf_depr('get_line_slice', 0, 5, true, true)) - curbuf_depr('set_line_slice', 4, 5, true, true, {'d'}) - eq({'a', 'b', 'c', 'd'}, curbuf_depr('get_line_slice', 0, 5, true, true)) - curbuf_depr('set_line_slice', -4, -5, true, true, {'e'}) - eq({'e', 'a', 'b', 'c', 'd'}, curbuf_depr('get_line_slice', 0, 5, true, true)) + eq({ 'c' }, curbuf_depr('get_line_slice', -1, 4, true, true)) + eq({ 'a', 'b', 'c' }, curbuf_depr('get_line_slice', 0, 5, true, true)) + curbuf_depr('set_line_slice', 4, 5, true, true, { 'd' }) + eq({ 'a', 'b', 'c', 'd' }, curbuf_depr('get_line_slice', 0, 5, true, true)) + curbuf_depr('set_line_slice', -4, -5, true, true, { 'e' }) + eq({ 'e', 'a', 'b', 'c', 'd' }, curbuf_depr('get_line_slice', 0, 5, true, true)) end) it('works', function() - eq({''}, curbuf_depr('get_line_slice', 0, -1, true, true)) + eq({ '' }, curbuf_depr('get_line_slice', 0, -1, true, true)) -- Replace buffer - curbuf_depr('set_line_slice', 0, -1, true, true, {'a', 'b', 'c'}) - eq({'a', 'b', 'c'}, curbuf_depr('get_line_slice', 0, -1, true, true)) - eq({'b', 'c'}, curbuf_depr('get_line_slice', 1, -1, true, true)) - eq({'b'}, curbuf_depr('get_line_slice', 1, 2, true, false)) + curbuf_depr('set_line_slice', 0, -1, true, true, { 'a', 'b', 'c' }) + eq({ 'a', 'b', 'c' }, curbuf_depr('get_line_slice', 0, -1, true, true)) + eq({ 'b', 'c' }, curbuf_depr('get_line_slice', 1, -1, true, true)) + eq({ 'b' }, curbuf_depr('get_line_slice', 1, 2, true, false)) eq({}, curbuf_depr('get_line_slice', 1, 1, true, false)) - eq({'a', 'b'}, curbuf_depr('get_line_slice', 0, -1, true, false)) - eq({'b'}, curbuf_depr('get_line_slice', 1, -1, true, false)) - eq({'b', 'c'}, curbuf_depr('get_line_slice', -2, -1, true, true)) - curbuf_depr('set_line_slice', 1, 2, true, false, {'a', 'b', 'c'}) - eq({'a', 'a', 'b', 'c', 'c'}, curbuf_depr('get_line_slice', 0, -1, true, true)) - curbuf_depr('set_line_slice', -1, -1, true, true, {'a', 'b', 'c'}) - eq({'a', 'a', 'b', 'c', 'a', 'b', 'c'}, - curbuf_depr('get_line_slice', 0, -1, true, true)) + eq({ 'a', 'b' }, curbuf_depr('get_line_slice', 0, -1, true, false)) + eq({ 'b' }, curbuf_depr('get_line_slice', 1, -1, true, false)) + eq({ 'b', 'c' }, curbuf_depr('get_line_slice', -2, -1, true, true)) + curbuf_depr('set_line_slice', 1, 2, true, false, { 'a', 'b', 'c' }) + eq({ 'a', 'a', 'b', 'c', 'c' }, curbuf_depr('get_line_slice', 0, -1, true, true)) + curbuf_depr('set_line_slice', -1, -1, true, true, { 'a', 'b', 'c' }) + eq({ 'a', 'a', 'b', 'c', 'a', 'b', 'c' }, curbuf_depr('get_line_slice', 0, -1, true, true)) curbuf_depr('set_line_slice', 0, -3, true, false, {}) - eq({'a', 'b', 'c'}, curbuf_depr('get_line_slice', 0, -1, true, true)) + eq({ 'a', 'b', 'c' }, curbuf_depr('get_line_slice', 0, -1, true, true)) curbuf_depr('set_line_slice', 0, -1, true, true, {}) - eq({''}, curbuf_depr('get_line_slice', 0, -1, true, true)) + eq({ '' }, curbuf_depr('get_line_slice', 0, -1, true, true)) end) end) - describe_lua_and_rpc('nvim_buf_get_lines, nvim_buf_set_lines', function(api) - local get_lines = api.curbufmeths.get_lines - local set_lines = api.curbufmeths.set_lines - local line_count = api.curbufmeths.line_count + describe_lua_and_rpc('nvim_buf_get_lines, nvim_buf_set_lines', function(lua_or_rpc) + local function get_lines(...) + return lua_or_rpc.nvim_buf_get_lines(0, ...) + end + + local function set_lines(...) + return lua_or_rpc.nvim_buf_set_lines(0, ...) + end + + local function line_count() + return lua_or_rpc.nvim_buf_line_count(0) + end it('fails correctly when input is not valid', function() - eq(1, api.curbufmeths.get_number()) - eq([['replacement string' item contains newlines]], - pcall_err(bufmeths.set_lines, 1, 1, 2, false, {'b\na'})) + eq(1, lua_or_rpc.nvim_buf_get_number(0)) + eq( + [['replacement string' item contains newlines]], + pcall_err(lua_or_rpc.nvim_buf_set_lines, 1, 1, 2, false, { 'b\na' }) + ) end) it("fails if 'nomodifiable'", function() command('set nomodifiable') - eq([[Buffer is not 'modifiable']], - pcall_err(api.bufmeths.set_lines, 1, 1, 2, false, {'a','b'})) + eq( + [[Buffer is not 'modifiable']], + pcall_err(lua_or_rpc.nvim_buf_set_lines, 1, 1, 2, false, { 'a', 'b' }) + ) end) it('has correct line_count when inserting and deleting', function() eq(1, line_count()) - set_lines(-1, -1, true, {'line'}) + set_lines(-1, -1, true, { 'line' }) eq(2, line_count()) - set_lines(-1, -1, true, {'line'}) + set_lines(-1, -1, true, { 'line' }) eq(3, line_count()) set_lines(-2, -1, true, {}) eq(2, line_count()) @@ -538,81 +558,80 @@ describe('api/buf', function() end) it('can get, set and delete a single line', function() - eq({''}, get_lines(0, 1, true)) - set_lines(0, 1, true, {'line1'}) - eq({'line1'}, get_lines(0, 1, true)) - set_lines(0, 1, true, {'line2'}) - eq({'line2'}, get_lines(0, 1, true)) + eq({ '' }, get_lines(0, 1, true)) + set_lines(0, 1, true, { 'line1' }) + eq({ 'line1' }, get_lines(0, 1, true)) + set_lines(0, 1, true, { 'line2' }) + eq({ 'line2' }, get_lines(0, 1, true)) set_lines(0, 1, true, {}) - eq({''}, get_lines(0, 1, true)) + eq({ '' }, get_lines(0, 1, true)) end) it('can get a single line with strict indexing', function() - set_lines(0, 1, true, {'line1.a'}) + set_lines(0, 1, true, { 'line1.a' }) eq(1, line_count()) -- sanity eq('Index out of bounds', pcall_err(get_lines, 1, 2, true)) eq('Index out of bounds', pcall_err(get_lines, -3, -2, true)) end) it('can get a single line with non-strict indexing', function() - set_lines(0, 1, true, {'line1.a'}) + set_lines(0, 1, true, { 'line1.a' }) eq(1, line_count()) -- sanity eq({}, get_lines(1, 2, false)) eq({}, get_lines(-3, -2, false)) end) it('can set and delete a single line with strict indexing', function() - set_lines(0, 1, true, {'line1.a'}) - eq('Index out of bounds', pcall_err(set_lines, 1, 2, true, {'line1.b'})) - eq('Index out of bounds', pcall_err(set_lines, -3, -2, true, {'line1.c'})) - eq({'line1.a'}, get_lines(0, -1, true)) + set_lines(0, 1, true, { 'line1.a' }) + eq('Index out of bounds', pcall_err(set_lines, 1, 2, true, { 'line1.b' })) + eq('Index out of bounds', pcall_err(set_lines, -3, -2, true, { 'line1.c' })) + eq({ 'line1.a' }, get_lines(0, -1, true)) eq('Index out of bounds', pcall_err(set_lines, 1, 2, true, {})) eq('Index out of bounds', pcall_err(set_lines, -3, -2, true, {})) - eq({'line1.a'}, get_lines(0, -1, true)) + eq({ 'line1.a' }, get_lines(0, -1, true)) end) it('can set and delete a single line with non-strict indexing', function() - set_lines(0, 1, true, {'line1.a'}) - set_lines(1, 2, false, {'line1.b'}) - set_lines(-4, -3, false, {'line1.c'}) - eq({'line1.c', 'line1.a', 'line1.b'}, get_lines(0, -1, true)) + set_lines(0, 1, true, { 'line1.a' }) + set_lines(1, 2, false, { 'line1.b' }) + set_lines(-4, -3, false, { 'line1.c' }) + eq({ 'line1.c', 'line1.a', 'line1.b' }, get_lines(0, -1, true)) set_lines(3, 4, false, {}) set_lines(-5, -4, false, {}) - eq({'line1.c', 'line1.a', 'line1.b'}, get_lines(0, -1, true)) + eq({ 'line1.c', 'line1.a', 'line1.b' }, get_lines(0, -1, true)) end) it('can handle NULs', function() - set_lines(0, 1, true, {'ab\0cd'}) - eq({'ab\0cd'}, get_lines(0, -1, true)) + set_lines(0, 1, true, { 'ab\0cd' }) + eq({ 'ab\0cd' }, get_lines(0, -1, true)) end) it('works with multiple lines', function() - eq({''}, get_lines(0, -1, true)) + eq({ '' }, get_lines(0, -1, true)) -- Replace buffer - for _, mode in pairs({false, true}) do - set_lines(0, -1, mode, {'a', 'b', 'c'}) - eq({'a', 'b', 'c'}, get_lines(0, -1, mode)) - eq({'b', 'c'}, get_lines(1, -1, mode)) - eq({'b'}, get_lines(1, 2, mode)) + for _, mode in pairs({ false, true }) do + set_lines(0, -1, mode, { 'a', 'b', 'c' }) + eq({ 'a', 'b', 'c' }, get_lines(0, -1, mode)) + eq({ 'b', 'c' }, get_lines(1, -1, mode)) + eq({ 'b' }, get_lines(1, 2, mode)) eq({}, get_lines(1, 1, mode)) - eq({'a', 'b'}, get_lines(0, -2, mode)) - eq({'b'}, get_lines(1, -2, mode)) - eq({'b', 'c'}, get_lines(-3, -1, mode)) - set_lines(1, 2, mode, {'a', 'b', 'c'}) - eq({'a', 'a', 'b', 'c', 'c'}, get_lines(0, -1, mode)) - set_lines(-2, -1, mode, {'a', 'b', 'c'}) - eq({'a', 'a', 'b', 'c', 'a', 'b', 'c'}, - get_lines(0, -1, mode)) + eq({ 'a', 'b' }, get_lines(0, -2, mode)) + eq({ 'b' }, get_lines(1, -2, mode)) + eq({ 'b', 'c' }, get_lines(-3, -1, mode)) + set_lines(1, 2, mode, { 'a', 'b', 'c' }) + eq({ 'a', 'a', 'b', 'c', 'c' }, get_lines(0, -1, mode)) + set_lines(-2, -1, mode, { 'a', 'b', 'c' }) + eq({ 'a', 'a', 'b', 'c', 'a', 'b', 'c' }, get_lines(0, -1, mode)) set_lines(0, -4, mode, {}) - eq({'a', 'b', 'c'}, get_lines(0, -1, mode)) + eq({ 'a', 'b', 'c' }, get_lines(0, -1, mode)) set_lines(0, -1, mode, {}) - eq({''}, get_lines(0, -1, mode)) + eq({ '' }, get_lines(0, -1, mode)) end end) it('can get line ranges with non-strict indexing', function() - set_lines(0, -1, true, {'a', 'b', 'c'}) - eq({'a', 'b', 'c'}, get_lines(0, -1, true)) --sanity + set_lines(0, -1, true, { 'a', 'b', 'c' }) + eq({ 'a', 'b', 'c' }, get_lines(0, -1, true)) --sanity eq({}, get_lines(3, 4, false)) eq({}, get_lines(3, 10, false)) @@ -622,8 +641,8 @@ describe('api/buf', function() end) it('can get line ranges with strict indexing', function() - set_lines(0, -1, true, {'a', 'b', 'c'}) - eq({'a', 'b', 'c'}, get_lines(0, -1, true)) --sanity + set_lines(0, -1, true, { 'a', 'b', 'c' }) + eq({ 'a', 'b', 'c' }, get_lines(0, -1, true)) --sanity eq('Index out of bounds', pcall_err(get_lines, 3, 4, true)) eq('Index out of bounds', pcall_err(get_lines, 3, 10, true)) @@ -634,20 +653,20 @@ describe('api/buf', function() end) it('set_lines: out-of-bounds can extend past end', function() - set_lines(0, -1, true, {'a', 'b', 'c'}) - eq({'a', 'b', 'c'}, get_lines(0, -1, true)) --sanity + set_lines(0, -1, true, { 'a', 'b', 'c' }) + eq({ 'a', 'b', 'c' }, get_lines(0, -1, true)) --sanity - eq({'c'}, get_lines(-2, 5, false)) - eq({'a', 'b', 'c'}, get_lines(0, 6, false)) - eq('Index out of bounds', pcall_err(set_lines, 4, 6, true, {'d'})) - set_lines(4, 6, false, {'d'}) - eq({'a', 'b', 'c', 'd'}, get_lines(0, -1, true)) - eq('Index out of bounds', pcall_err(set_lines, -6, -6, true, {'e'})) - set_lines(-6, -6, false, {'e'}) - eq({'e', 'a', 'b', 'c', 'd'}, get_lines(0, -1, true)) + eq({ 'c' }, get_lines(-2, 5, false)) + eq({ 'a', 'b', 'c' }, get_lines(0, 6, false)) + eq('Index out of bounds', pcall_err(set_lines, 4, 6, true, { 'd' })) + set_lines(4, 6, false, { 'd' }) + eq({ 'a', 'b', 'c', 'd' }, get_lines(0, -1, true)) + eq('Index out of bounds', pcall_err(set_lines, -6, -6, true, { 'e' })) + set_lines(-6, -6, false, { 'e' }) + eq({ 'e', 'a', 'b', 'c', 'd' }, get_lines(0, -1, true)) end) - it("set_lines on alternate buffer does not access invalid line (E315)", function() + it('set_lines on alternate buffer does not access invalid line (E315)', function() feed_command('set hidden') insert('Initial file') command('enew') @@ -667,8 +686,8 @@ describe('api/buf', function() it("set_lines of invisible buffer doesn't move cursor in current window", function() local screen = Screen.new(20, 5) screen:set_default_attr_ids({ - [1] = {bold = true, foreground = Screen.colors.Blue1}, - [2] = {bold = true}, + [1] = { bold = true, foreground = Screen.colors.Blue1 }, + [2] = { bold = true }, }) screen:attach() @@ -676,7 +695,7 @@ describe('api/buf', function() Who would win? A real window with proper text]]) - local buf = api.meths.create_buf(false,true) + local buf = lua_or_rpc.nvim_create_buf(false, true) screen:expect([[ Who would win? | A real window | @@ -685,7 +704,7 @@ describe('api/buf', function() | ]]) - api.meths.buf_set_lines(buf, 0, -1, true, {'or some', 'scratchy text'}) + lua_or_rpc.nvim_buf_set_lines(buf, 0, -1, true, { 'or some', 'scratchy text' }) feed('i') -- provoke redraw screen:expect([[ Who would win? | @@ -701,37 +720,42 @@ describe('api/buf', function() visible buffer line 1 line 2 ]]) - local hiddenbuf = api.meths.create_buf(false,true) + local hiddenbuf = lua_or_rpc.nvim_create_buf(false, true) command('vsplit') command('vsplit') feed('<c-w>l<c-w>l<c-w>l') - eq(3, funcs.winnr()) + eq(3, fn.winnr()) feed('<c-w>h') - eq(2, funcs.winnr()) - api.meths.buf_set_lines(hiddenbuf, 0, -1, true, - {'hidden buffer line 1', 'line 2'}) + eq(2, fn.winnr()) + lua_or_rpc.nvim_buf_set_lines(hiddenbuf, 0, -1, true, { 'hidd |