mirror of
https://github.com/Myzel394/jsonfly.nvim.git
synced 2025-06-18 04:05:26 +02:00
feat: Add support for jsonpath
This commit is contained in:
parent
4d22028b13
commit
5b11ed0404
@ -205,4 +205,10 @@ function M:extract_key_description(text)
|
|||||||
return keys
|
return keys
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param name string
|
||||||
|
---@return boolean
|
||||||
|
function M:is_module_available(name)
|
||||||
|
return pcall(function() require(name) end) == true
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
--
|
--
|
||||||
---@class Commands
|
---@class Commands
|
||||||
---@field add_key string[] - Add the currently entered key to the JSON. Must be of type [string, string] <mode, key>; Example: {"n", "a"} -> When in normal mode, press "a" to add the key; Example: {"i", "<C-a>"} -> When in insert mode, press <C-a> to add the key; Default: {"i", "<C-a>"}
|
---@field add_key string[] - Add the currently entered key to the JSON. Must be of type [string, string] <mode, key>; Example: {"n", "a"} -> When in normal mode, press "a" to add the key; Example: {"i", "<C-a>"} -> When in insert mode, press <C-a> to add the key; Default: {"i", "<C-a>"}
|
||||||
|
---@field copy_jsonpath [string, string, function] - Copy the JSONPath of the currently selected key. jsonpath must be installed. Must be of type [string, string, function] <mode, key, (path: string, bufnr: number) -> void> - Example: {"n", "<C-j>", function(path) vim.fn.setreg("+", path) end} -> When in normal mode, press <C-j> to copy the JSONPath to the clipboard; Default: {"i", "<C-j>", a function that will copy the JSONPath to the clipboard}
|
||||||
---
|
---
|
||||||
---@class Highlights
|
---@class Highlights
|
||||||
---@field number string - Highlight group for numbers, Default: "@number.json"
|
---@field number string - Highlight group for numbers, Default: "@number.json"
|
||||||
@ -60,7 +61,16 @@ local DEFAULT_CONFIG = {
|
|||||||
backend = "lsp",
|
backend = "lsp",
|
||||||
use_cache = 500,
|
use_cache = 500,
|
||||||
commands = {
|
commands = {
|
||||||
add_key = {"i", "<C-a>"}
|
add_key = {"i", "<C-a>"},
|
||||||
|
copy_jsonpath = {
|
||||||
|
"i",
|
||||||
|
"<C-j>",
|
||||||
|
---@param path string
|
||||||
|
---@param prompt_bufnr number
|
||||||
|
function(path, prompt_bufnr)
|
||||||
|
vim.fn.setreg("+", path)
|
||||||
|
end
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,6 +115,31 @@ local function show_picker(entries, buffer, xopts)
|
|||||||
end
|
end
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if global_config.commands.copy_jsonpath and utils:is_module_available("jsonpath") then
|
||||||
|
map(
|
||||||
|
global_config.commands.copy_jsonpath[1],
|
||||||
|
global_config.commands.copy_jsonpath[2],
|
||||||
|
function(prompt_bufnr)
|
||||||
|
local jsonpath = require("jsonpath")
|
||||||
|
|
||||||
|
local current_picker = action_state.get_current_picker(prompt_bufnr)
|
||||||
|
local selection = current_picker:get_selection()
|
||||||
|
|
||||||
|
local path = jsonpath.get(vim.treesitter.get_node({
|
||||||
|
bufnr = buffer,
|
||||||
|
pos = {
|
||||||
|
selection.lnum - 1,
|
||||||
|
selection.index,
|
||||||
|
}
|
||||||
|
}), buffer)
|
||||||
|
|
||||||
|
if path then
|
||||||
|
global_config.commands.copy_jsonpath[3](path, prompt_bufnr)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end,
|
end,
|
||||||
finder = finders.new_table {
|
finder = finders.new_table {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user