From 65cdbb3283e829f7322aad83882efab2759d82fd Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Wed, 1 May 2024 22:35:26 +0200 Subject: [PATCH] fix: Improve edge case where file lines is shorter than BUFFER_SIZE --- lua/jsonfly/insert.lua | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lua/jsonfly/insert.lua b/lua/jsonfly/insert.lua index 5f700c9..11e8648 100644 --- a/lua/jsonfly/insert.lua +++ b/lua/jsonfly/insert.lua @@ -132,7 +132,14 @@ local function add_comma(buffer, insertion_line) -- Find next non-empty character in reverse for ii=insertion_line, 0, -BUFFER_SIZE do - local previous_lines = vim.api.nvim_buf_get_lines(buffer, ii - BUFFER_SIZE, ii, false) + local previous_lines = vim.api.nvim_buf_get_lines( + buffer, + math.max(0, ii - BUFFER_SIZE), + ii, + false + ) + + print("previous lins: " .. vim.inspect(previous_lines)) if #previous_lines == 0 then return @@ -150,12 +157,12 @@ local function add_comma(buffer, insertion_line) end -- Insert comma at position - local line_number = ii - (BUFFER_SIZE - jj) + local line_number = math.max(0, ii - BUFFER_SIZE) + jj - 1 vim.api.nvim_buf_set_text( buffer, - line_number - 1, + line_number, char_index, - line_number - 1, + line_number, char_index, {","} )