Skip to content

Commit

Permalink
fix: Improve edge case where file lines is shorter than BUFFER_SIZE
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed May 1, 2024
1 parent 1c2fe2b commit 65cdbb3
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lua/jsonfly/insert.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
{","}
)
Expand Down

0 comments on commit 65cdbb3

Please sign in to comment.