diff options
author | Blaž Hrastnik <blaz@mxxn.io> | 2020-05-04 10:30:59 +0900 |
---|---|---|
committer | Björn Linse <bjorn.linse@gmail.com> | 2021-01-01 19:51:37 +0100 |
commit | 29ad2ebc1688176a1c7acaa81103dac289de0ad1 (patch) | |
tree | 442468e1a5b5134f3bed3cda61eeaa16a2963dfc /test/functional/api/buffer_spec.lua | |
parent | 9be19b770d7802e62ab1a084996ac2192b71fd70 (diff) | |
download | rneovim-29ad2ebc1688176a1c7acaa81103dac289de0ad1.tar.gz rneovim-29ad2ebc1688176a1c7acaa81103dac289de0ad1.tar.bz2 rneovim-29ad2ebc1688176a1c7acaa81103dac289de0ad1.zip |
api: set_text: fix validation and some issues
fix double free because intermediary lines weren't xmemdup'd.
NL-for-NUL dance.
Normalize row indices and perform more validation.
Adjust the cursor position if it's on the right side of the replacement.
Tests and documentation.
Diffstat (limited to 'test/functional/api/buffer_spec.lua')
-rw-r--r-- | test/functional/api/buffer_spec.lua | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/test/functional/api/buffer_spec.lua b/test/functional/api/buffer_spec.lua index 8ed642b43e..60ff246436 100644 --- a/test/functional/api/buffer_spec.lua +++ b/test/functional/api/buffer_spec.lua @@ -392,6 +392,89 @@ 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 + local line_count = curbufmeths.line_count + + 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 replace with multiple lines + local err = 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('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, 0, 1, {}) + local id2 = curbufmeths.set_extmark(ns, 0, 0, 7, {}) + local id3 = curbufmeths.set_extmark(ns, 0, 1, 1, {}) + set_text(0, 4, 0, 7, {"q"}) + + -- TODO: if we set text at 0,3, what happens to the mark at 0,3 + + eq({'foo q', 'baz'}, get_lines(0, 2, true)) + -- mark before replacement point is unaffected + rv = curbufmeths.get_extmark_by_id(ns, id1) + eq({0, 1}, rv) + -- mark gets shifted back because the replacement was shorter + rv = curbufmeths.get_extmark_by_id(ns, id2) + eq({0, 5}, rv) + -- mark on the next line is unaffected + rv = curbufmeths.get_extmark_by_id(ns, id3) + eq({1, 1}, rv) + + -- replacing the text spanning two lines will adjust the mark on the next line + set_text(0, 3, 1, 3, {"qux"}) + rv = curbufmeths.get_extmark_by_id(ns, id3) + eq({'fooqux', ''}, get_lines(0, 2, true)) + eq({0, 6}, rv) + -- but mark before replacement point is still unaffected + rv = curbufmeths.get_extmark_by_id(ns, id1) + eq({0, 1}, rv) + -- and the mark in the middle was shifted to the end of the insertion + rv = curbufmeths.get_extmark_by_id(ns, id2) + eq({0, 6}, rv) + end) + end) + describe('nvim_buf_get_offset', function() local get_offset = curbufmeths.get_offset it('works', function() |