diff options
author | bfredl <bjorn.linse@gmail.com> | 2023-08-30 13:14:29 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-30 13:14:29 +0200 |
commit | bec13cfcafe73d33ed707b690fd924b1f52241f8 (patch) | |
tree | 4b98ad20532fb172374f995ee066e7b95005aec7 | |
parent | f85aa2e67f3e11c3a0f1e373d321398681e84990 (diff) | |
parent | b051b131f5ce99ffe1b1bc22a2032ebc886e4e35 (diff) | |
download | rneovim-bec13cfcafe73d33ed707b690fd924b1f52241f8.tar.gz rneovim-bec13cfcafe73d33ed707b690fd924b1f52241f8.tar.bz2 rneovim-bec13cfcafe73d33ed707b690fd924b1f52241f8.zip |
Merge pull request #24944 from bfredl/memline0
fix(api): nvim_buf_get_offset in a new buffer with zero or one lines
-rw-r--r-- | src/nvim/memline.c | 5 | ||||
-rw-r--r-- | test/functional/api/buffer_spec.lua | 12 |
2 files changed, 17 insertions, 0 deletions
diff --git a/src/nvim/memline.c b/src/nvim/memline.c index 5839061aac..123e9d0a0b 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -3934,6 +3934,11 @@ long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp, bool no_ff) if (buf->b_ml.ml_usedchunks == -1 || buf->b_ml.ml_chunksize == NULL || lnum < 0) { + // memline is currently empty. Although if it is loaded, + // it behaves like there is one empty line. + if (!ffdos && buf->b_ml.ml_mfp && (lnum == 1 || lnum == 2)) { + return lnum - 1; + } return -1; } diff --git a/test/functional/api/buffer_spec.lua b/test/functional/api/buffer_spec.lua index aea1f359e6..292e5a2d56 100644 --- a/test/functional/api/buffer_spec.lua +++ b/test/functional/api/buffer_spec.lua @@ -1151,6 +1151,18 @@ describe('api/buf', function() eq(6, bufmeths.get_offset(1,1)) command("bunload! 1") eq(-1, bufmeths.get_offset(1,1)) + eq(-1, bufmeths.get_offset(1,0)) + end) + + it('works in empty buffer', function() + eq(0, get_offset(0)) + eq(1, get_offset(1)) + end) + + it('works in buffer with one line inserted', function() + feed('itext') + eq(0, get_offset(0)) + eq(5, get_offset(1)) end) end) |