Skip to content

Commit

Permalink
fix: Remove wrong bracket expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Apr 27, 2024
1 parent ec73267 commit a287e40
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lua/jsonfly/insert.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ function table.slice(tbl, first, last, step)
end

---@param line string
---@param also_match_end_bracket boolean - Whether to also match only a closing bracket
---@return boolean - Whether the line contains an empty JSON object
local function line_contains_empty_json(line)
local function line_contains_empty_json(line, also_match_end_bracket)
-- Starting and ending on same line
return string.match(line, ".*[%{%[]%s*[%]%]]%s*,?*%s*")
-- Opening bracket on line
or string.match(line, ".*[%{%[]%s*")
-- Closing bracket on line
or string.match(line, ".*.*[%]%}]%s*,?%s*")
or (also_match_end_bracket and string.match(line, ".*.*[%]%}]%s*,?%s*"))
end

---@param entry Entry
Expand Down Expand Up @@ -87,9 +88,9 @@ local function write_keys(keys, index, lines)
return
end

if key.type == "object_wrapper" then
if key.type == "object_wrapper" and #lines > 0 then
local previous_line = lines[#lines] or ""
if line_contains_empty_json(previous_line) then
if line_contains_empty_json(previous_line, true) then
lines[#lines + 1] = "{"
else
lines[#lines] = previous_line .. " {"
Expand All @@ -105,7 +106,7 @@ local function write_keys(keys, index, lines)
elseif key.type == "array_wrapper" then
local previous_line = lines[#lines] or ""
-- Starting and ending on same line
if line_contains_empty_json(previous_line) then
if line_contains_empty_json(previous_line, true) then
lines[#lines + 1] = "["
else
lines[#lines] = previous_line .. " ["
Expand Down Expand Up @@ -169,7 +170,7 @@ end
local function expand_empty_object(buffer, line_number)
local line = vim.api.nvim_buf_get_lines(buffer, line_number, line_number + 1, false)[1] or ""

if line_contains_empty_json(line) then
if line_contains_empty_json(line, false) then
vim.api.nvim_buf_set_lines(
buffer,
line_number,
Expand Down

0 comments on commit a287e40

Please sign in to comment.