diff options
Diffstat (limited to 'test/functional/api/buffer_spec.lua')
-rw-r--r-- | test/functional/api/buffer_spec.lua | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/test/functional/api/buffer_spec.lua b/test/functional/api/buffer_spec.lua index 8ed642b43e..fb8ed6a9d7 100644 --- a/test/functional/api/buffer_spec.lua +++ b/test/functional/api/buffer_spec.lua @@ -392,6 +392,129 @@ describe('api/buf', function() end) end) + describe('nvim_buf_get_lines, nvim_buf_set_text', function() + local get_lines, set_text = curbufmeths.get_lines, curbufmeths.set_text + + it('works', function() + insert([[ + hello foo! + text + ]]) + + eq({'hello foo!'}, get_lines(0, 1, true)) + + + -- can replace a single word + set_text(0, 6, 0, 9, {'world'}) + eq({'hello world!', 'text'}, get_lines(0, 2, true)) + + -- can insert text + set_text(0, 0, 0, 0, {'well '}) + eq({'well hello world!', 'text'}, get_lines(0, 2, true)) + + -- can delete text + set_text(0, 0, 0, 5, {''}) + eq({'hello world!', 'text'}, get_lines(0, 2, true)) + + -- can replace with multiple lines + set_text(0, 6, 0, 11, {'foo', 'wo', 'more'}) + eq({'hello foo', 'wo', 'more!', 'text'}, get_lines(0, 4, true)) + + -- will join multiple lines if needed + set_text(0, 6, 3, 4, {'bar'}) + eq({'hello bar'}, get_lines(0, 1, true)) + end) + + it('works with undo', function() + insert([[ + hello world! + foo bar + ]]) + + -- setting text + set_text(0, 0, 0, 0, {'well '}) + feed('u') + eq({'hello world!'}, get_lines(0, 1, true)) + + -- deleting text + set_text(0, 0, 0, 6, {''}) + feed('u') + eq({'hello world!'}, get_lines(0, 1, true)) + + -- inserting newlines + set_text(0, 0, 0, 0, {'hello', 'mr '}) + feed('u') + eq({'hello world!'}, get_lines(0, 1, true)) + + -- deleting newlines + set_text(0, 0, 1, 4, {'hello'}) + feed('u') + eq({'hello world!'}, get_lines(0, 1, true)) + end) + + it('updates the cursor position', function() + insert([[ + hello world! + ]]) + + -- position the cursor on `!` + curwin('set_cursor', {1, 11}) + -- replace 'world' with 'foo' + set_text(0, 6, 0, 11, {'foo'}) + eq('hello foo!', curbuf_depr('get_line', 0)) + -- cursor should be moved left by two columns (replacement is shorter by 2 chars) + eq({1, 9}, curwin('get_cursor')) + end) + + it('can handle NULs', function() + set_text(0, 0, 0, 0, {'ab\0cd'}) + eq('ab\0cd', curbuf_depr('get_line', 0)) + end) + + it('adjusts extmarks', function() + local ns = request('nvim_create_namespace', "my-fancy-plugin") + insert([[ + foo bar + baz + ]]) + local id1 = curbufmeths.set_extmark(ns, 0, 1, {}) + local id2 = curbufmeths.set_extmark(ns, 0, 7, {}) + local id3 = curbufmeths.set_extmark(ns, 1, 1, {}) + set_text(0, 4, 0, 7, {"q"}) + + eq({'foo q', 'baz'}, get_lines(0, 2, true)) + -- mark before replacement point is unaffected + eq({0, 1}, curbufmeths.get_extmark_by_id(ns, id1, {})) + -- mark gets shifted back because the replacement was shorter + eq({0, 5}, curbufmeths.get_extmark_by_id(ns, id2, {})) + -- mark on the next line is unaffected + eq({1, 1}, curbufmeths.get_extmark_by_id(ns, id3, {})) + + -- replacing the text spanning two lines will adjust the mark on the next line + set_text(0, 3, 1, 3, {"qux"}) + eq({'fooqux', ''}, get_lines(0, 2, true)) + eq({0, 6}, curbufmeths.get_extmark_by_id(ns, id3, {})) + -- but mark before replacement point is still unaffected + eq({0, 1}, curbufmeths.get_extmark_by_id(ns, id1, {})) + -- and the mark in the middle was shifted to the end of the insertion + eq({0, 6}, curbufmeths.get_extmark_by_id(ns, id2, {})) + + -- marks should be put back into the same place after undoing + set_text(0, 0, 0, 2, {''}) + feed('u') + eq({0, 1}, curbufmeths.get_extmark_by_id(ns, id1, {})) + eq({0, 6}, curbufmeths.get_extmark_by_id(ns, id2, {})) + eq({0, 6}, curbufmeths.get_extmark_by_id(ns, id3, {})) + + -- marks should be shifted over by the correct number of bytes for multibyte + -- chars + set_text(0, 0, 0, 0, {'Ø'}) + eq({0, 3}, curbufmeths.get_extmark_by_id(ns, id1, {})) + eq({0, 8}, curbufmeths.get_extmark_by_id(ns, id2, {})) + eq({0, 8}, curbufmeths.get_extmark_by_id(ns, id3, {})) + end) + end) + describe('nvim_buf_get_offset', function() local get_offset = curbufmeths.get_offset it('works', function() |