Skip to content

Commit

Permalink
fix: Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed May 3, 2024
1 parent 65cdbb3 commit 47775bb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 47 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ It's completely customizable and even supports highlighting of the values.
## Features

* 🔍 Search for deeply nested keys - `expo.android.imageAsset.0.uri`
* Insert keys quickly into your buffer
* 👀 Insert nested keys quickly into your buffer
* 🎨 See values with their correct syntax highlighting (numbers, strings, booleans, null; configurable)
* 💻 Use your LSP or the built-in JSON parser
* 🗑 Values automatically cached for faster navigation
Expand Down Expand Up @@ -73,6 +73,8 @@ Now you can search for keys, subkeys, part of keys etc.

### Inserting Keys

JSON(fly) supports inserting your current search prompt into your buffer.

If you search for a key that doesn't exist you can add it to your buffer by pressing `<C-a>` (CTRL + a).

You can enter nested keys, arrays, indices, subkeys etc. JSON(fly) will automatically manage everything for you.
Expand Down
116 changes: 70 additions & 46 deletions lua/jsonfly/insert.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ local function add_comma(buffer, insertion_line)
false
)

print("previous lins: " .. vim.inspect(previous_lines))

if #previous_lines == 0 then
return
end
Expand Down Expand Up @@ -178,14 +176,17 @@ 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, false) then
local position_closing_bracket = string.find(line, "[%}%]]")
local remaining_line = string.sub(line, position_closing_bracket + 1)

vim.api.nvim_buf_set_lines(
buffer,
line_number,
line_number + 1,
false,
{
"{",
"},"
"}" .. remaining_line
}
)

Expand All @@ -195,25 +196,6 @@ local function expand_empty_object(buffer, line_number)
return line_number
end

---@param buffer number
function M:jump_to_cursor_helper(buffer)
vim.fn.search(CURSOR_SEARCH_HELPER)

-- Remove cursor helper
local position = vim.api.nvim_win_get_cursor(0)
vim.api.nvim_buf_set_text(
buffer,
position[1] - 1,
position[2],
position[1] - 1,
position[2] + #CURSOR_SEARCH_HELPER,
{""}
)

-- -- Go into insert mode
vim.cmd [[execute "normal a"]]
end

---@param keys KeyDescription[]
---@param input_key_depth number
local function get_key_descriptor_index(keys, input_key_depth)
Expand Down Expand Up @@ -259,7 +241,7 @@ end

---@param keys KeyDescription[]
---@return string[]
local function flat_key_description(keys)
local function flatten_key_description(keys)
local flat_keys = {}

for ii=1, #keys do
Expand All @@ -280,7 +262,7 @@ end
---@param starting_keys KeyDescription[]
---@param key KeyDescription - Th key to be inserted; must be of type `array_index`; will be modified in-place
local function normalize_array_indexes(entries, starting_keys, key)
local starting_keys_flat = flat_key_description(starting_keys)
local starting_keys_flat = flatten_key_description(starting_keys)
local starting_key_index = get_entry_by_keys(entries, starting_keys_flat)
local entry = entries[starting_key_index]

Expand All @@ -299,34 +281,46 @@ local function count_array_children(entries)
return #entries
end

---Jump to the cursor helper and remove it
---@param buffer number
function M:jump_to_cursor_helper(buffer)
vim.fn.search(CURSOR_SEARCH_HELPER)

-- Remove cursor helper
local position = vim.api.nvim_win_get_cursor(0)
vim.api.nvim_buf_set_text(
buffer,
position[1] - 1,
position[2],
position[1] - 1,
position[2] + #CURSOR_SEARCH_HELPER,
{""}
)

-- -- Go into insert mode
vim.cmd [[execute "normal a"]]
end

-- TODO: Handle top level empty arrays
---@param entries Entry[]
---@param keys KeyDescription[]
---@param buffer number
function M:insert_new_key(entries, keys, buffer)
-- Close current buffer
vim.cmd [[quit!]]

local input_key = flat_key_description(keys)
local entry_index = find_best_fitting_entry(entries, input_key) or 0
---@type Entry
local entry = entries[entry_index]
local input_key = flatten_key_description(keys)
---@type boolean
local should_add_comma = true

---@type KeyDescription[]
local remaining_keys
---@type integer
local existing_keys_index
---@type Entry
local entry

if entry == nil then
-- Insert as root
existing_keys_index = 0
if #entries == 0 then
remaining_keys = table.slice(keys, 2, #keys)

-- Top level array
if entries[1].key == "0" then
-- Normalize array indexes
remaining_keys[1].key = remaining_keys[1].key - count_array_children(entries)
end

entry = {
key = "",
position = {
Expand All @@ -335,14 +329,42 @@ function M:insert_new_key(entries, keys, buffer)
value_start = 1
}
}
should_add_comma = false
else
local existing_input_keys_depth = #utils:split_by_char(entry.key, ".") + 1
existing_keys_index = get_key_descriptor_index(keys, existing_input_keys_depth)
remaining_keys = table.slice(keys, existing_keys_index, #keys)
local entry_index = find_best_fitting_entry(entries, input_key) or 0
entry = entries[entry_index]

if remaining_keys[1].type == "array_index" then
local starting_keys = table.slice(keys, 1, existing_keys_index - 1)
normalize_array_indexes(entries, starting_keys, remaining_keys[1])
---@type integer
local existing_keys_index

if entry == nil then
-- Insert as root
existing_keys_index = 0
remaining_keys = table.slice(keys, 2, #keys)

-- Top level array
if entries[1].key == "0" then
-- Normalize array indexes
remaining_keys[1].key = remaining_keys[1].key - count_array_children(entries)
end

entry = {
key = "",
position = {
key_start = 1,
line_number = 1,
value_start = 1
}
}
else
local existing_input_keys_depth = #utils:split_by_char(entry.key, ".") + 1
existing_keys_index = get_key_descriptor_index(keys, existing_input_keys_depth)
remaining_keys = table.slice(keys, existing_keys_index, #keys)

if remaining_keys[1].type == "array_index" then
local starting_keys = table.slice(keys, 1, existing_keys_index - 1)
normalize_array_indexes(entries, starting_keys, remaining_keys[1])
end
end
end

Expand All @@ -367,7 +389,9 @@ function M:insert_new_key(entries, keys, buffer)
local start_line = vim.api.nvim_win_get_cursor(0)[1] - 1

-- Add comma to previous JSON entry
add_comma(buffer, start_line)
if should_add_comma then
add_comma(buffer, start_line)
end
local new_start_line = expand_empty_object(buffer, start_line)

if new_start_line ~= start_line then
Expand Down

0 comments on commit 47775bb

Please sign in to comment.