mirror of
https://github.com/Myzel394/jsonfly.nvim.git
synced 2025-06-18 12:15:25 +02:00
fix: Small improvements
This commit is contained in:
parent
65cdbb3283
commit
47775bb746
@ -11,7 +11,7 @@ It's completely customizable and even supports highlighting of the values.
|
|||||||
## Features
|
## Features
|
||||||
|
|
||||||
* 🔍 Search for deeply nested keys - `expo.android.imageAsset.0.uri`
|
* 🔍 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)
|
* 🎨 See values with their correct syntax highlighting (numbers, strings, booleans, null; configurable)
|
||||||
* 💻 Use your LSP or the built-in JSON parser
|
* 💻 Use your LSP or the built-in JSON parser
|
||||||
* 🗑 Values automatically cached for faster navigation
|
* 🗑 Values automatically cached for faster navigation
|
||||||
@ -73,6 +73,8 @@ Now you can search for keys, subkeys, part of keys etc.
|
|||||||
|
|
||||||
### Inserting Keys
|
### 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).
|
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.
|
You can enter nested keys, arrays, indices, subkeys etc. JSON(fly) will automatically manage everything for you.
|
||||||
|
@ -139,8 +139,6 @@ local function add_comma(buffer, insertion_line)
|
|||||||
false
|
false
|
||||||
)
|
)
|
||||||
|
|
||||||
print("previous lins: " .. vim.inspect(previous_lines))
|
|
||||||
|
|
||||||
if #previous_lines == 0 then
|
if #previous_lines == 0 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -178,6 +176,9 @@ 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 ""
|
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
|
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(
|
vim.api.nvim_buf_set_lines(
|
||||||
buffer,
|
buffer,
|
||||||
line_number,
|
line_number,
|
||||||
@ -185,7 +186,7 @@ local function expand_empty_object(buffer, line_number)
|
|||||||
false,
|
false,
|
||||||
{
|
{
|
||||||
"{",
|
"{",
|
||||||
"},"
|
"}" .. remaining_line
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -195,25 +196,6 @@ local function expand_empty_object(buffer, line_number)
|
|||||||
return line_number
|
return line_number
|
||||||
end
|
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 keys KeyDescription[]
|
||||||
---@param input_key_depth number
|
---@param input_key_depth number
|
||||||
local function get_key_descriptor_index(keys, input_key_depth)
|
local function get_key_descriptor_index(keys, input_key_depth)
|
||||||
@ -259,7 +241,7 @@ end
|
|||||||
|
|
||||||
---@param keys KeyDescription[]
|
---@param keys KeyDescription[]
|
||||||
---@return string[]
|
---@return string[]
|
||||||
local function flat_key_description(keys)
|
local function flatten_key_description(keys)
|
||||||
local flat_keys = {}
|
local flat_keys = {}
|
||||||
|
|
||||||
for ii=1, #keys do
|
for ii=1, #keys do
|
||||||
@ -280,7 +262,7 @@ end
|
|||||||
---@param starting_keys KeyDescription[]
|
---@param starting_keys KeyDescription[]
|
||||||
---@param key KeyDescription - Th key to be inserted; must be of type `array_index`; will be modified in-place
|
---@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 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 starting_key_index = get_entry_by_keys(entries, starting_keys_flat)
|
||||||
local entry = entries[starting_key_index]
|
local entry = entries[starting_key_index]
|
||||||
|
|
||||||
@ -299,6 +281,27 @@ local function count_array_children(entries)
|
|||||||
return #entries
|
return #entries
|
||||||
end
|
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 entries Entry[]
|
||||||
---@param keys KeyDescription[]
|
---@param keys KeyDescription[]
|
||||||
---@param buffer number
|
---@param buffer number
|
||||||
@ -306,13 +309,31 @@ function M:insert_new_key(entries, keys, buffer)
|
|||||||
-- Close current buffer
|
-- Close current buffer
|
||||||
vim.cmd [[quit!]]
|
vim.cmd [[quit!]]
|
||||||
|
|
||||||
local input_key = flat_key_description(keys)
|
local input_key = flatten_key_description(keys)
|
||||||
local entry_index = find_best_fitting_entry(entries, input_key) or 0
|
---@type boolean
|
||||||
---@type Entry
|
local should_add_comma = true
|
||||||
local entry = entries[entry_index]
|
|
||||||
|
|
||||||
---@type KeyDescription[]
|
---@type KeyDescription[]
|
||||||
local remaining_keys
|
local remaining_keys
|
||||||
|
---@type Entry
|
||||||
|
local entry
|
||||||
|
|
||||||
|
if #entries == 0 then
|
||||||
|
remaining_keys = table.slice(keys, 2, #keys)
|
||||||
|
|
||||||
|
entry = {
|
||||||
|
key = "",
|
||||||
|
position = {
|
||||||
|
key_start = 1,
|
||||||
|
line_number = 1,
|
||||||
|
value_start = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
should_add_comma = false
|
||||||
|
else
|
||||||
|
local entry_index = find_best_fitting_entry(entries, input_key) or 0
|
||||||
|
entry = entries[entry_index]
|
||||||
|
|
||||||
---@type integer
|
---@type integer
|
||||||
local existing_keys_index
|
local existing_keys_index
|
||||||
|
|
||||||
@ -345,6 +366,7 @@ function M:insert_new_key(entries, keys, buffer)
|
|||||||
normalize_array_indexes(entries, starting_keys, remaining_keys[1])
|
normalize_array_indexes(entries, starting_keys, remaining_keys[1])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local _writes = {}
|
local _writes = {}
|
||||||
write_keys(remaining_keys, 1, _writes)
|
write_keys(remaining_keys, 1, _writes)
|
||||||
@ -367,7 +389,9 @@ function M:insert_new_key(entries, keys, buffer)
|
|||||||
local start_line = vim.api.nvim_win_get_cursor(0)[1] - 1
|
local start_line = vim.api.nvim_win_get_cursor(0)[1] - 1
|
||||||
|
|
||||||
-- Add comma to previous JSON entry
|
-- Add comma to previous JSON entry
|
||||||
|
if should_add_comma then
|
||||||
add_comma(buffer, start_line)
|
add_comma(buffer, start_line)
|
||||||
|
end
|
||||||
local new_start_line = expand_empty_object(buffer, start_line)
|
local new_start_line = expand_empty_object(buffer, start_line)
|
||||||
|
|
||||||
if new_start_line ~= start_line then
|
if new_start_line ~= start_line then
|
||||||
|
Loading…
x
Reference in New Issue
Block a user