Merge pull request #18 from Myzel394/add-jsonpath-support

This commit is contained in:
Myzel394 2025-06-07 22:54:52 +02:00 committed by GitHub
commit db4394d856
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 203 additions and 11 deletions

View File

@ -1,3 +1,5 @@
**NOTICE**: I'm slowly migrating my repositories to my own Git server. Please visit this repository at [https://git.myzel394.app/Myzel394/jsonfly.nvim](https://git.myzel394.app/Myzel394/jsonfly.nvim) for the latest updates.
# jsonfly.nvim
Fly through your JSON, XML and YAML files with ease.
@ -17,6 +19,7 @@ It's completely customizable and even supports highlighting of the values.
* 🗑 Values automatically cached for faster navigation
* 🫣 Automatic concealment based on your configuration
* 📐 Everything completely customizable!
* 📝 [JSONPath](https://github.com/phelipetls/jsonpath.nvim) support - copy JSON paths to your clipboard
## Installation
@ -26,22 +29,30 @@ Install with your favorite plugin manager, for example with [lazy.nvim](https://
{
"nvim-telescope/telescope.nvim",
dependencies = {
-- "Myzel394/easytables.nvim",
-- "Myzel394/telescope-last-positions",
-- "https://git.myzel394.app/Myzel394/easytables.nvim",
-- "https://git.myzel394.app/Myzel394/telescope-last-positions",
-- Other dependencies
-- ..
"Myzel394/jsonfly.nvim",
"https://git.myzel394.app/Myzel394/jsonfly.nvim",
}
}
```
Load the extension with:
```lua
require("telescope").load_extension("jsonfly")
```
### Example
Here's how I load it with lazy.nvim with lazy-loading and `<leader>j` as the keymap :)
```lua
{
"nvim-telescope/telescope.nvim",
dependencies = {
"Myzel394/jsonfly.nvim",
"https://git.myzel394.app/Myzel394/jsonfly.nvim",
},
keys = {
{
@ -55,12 +66,6 @@ Here's how I load it with lazy.nvim with lazy-loading and `<leader>j` as the key
}
```
Load the extension with:
```lua
require("telescope").load_extension("jsonfly")
```
## Usage
Go to a JSON file and run:
@ -87,6 +92,24 @@ The following schemas are valid:
Please note: JSON(fly) is intended to be used with **human-readable** JSON files. Inserting keys won't work with minified JSON files.
## Integrating jsonpath.nvim
json(fly) has native support for [jsonpath.nvim](https://github.com/phelipetls/jsonpath.nvim).
Just make sure jsonpath is loaded when you call json(fly), and you can copy the currently selected path by pressing `<C-j>` (you can also change this keybinding of course).
Example:
```lua
{
url = "https://git.myzel394.app/Myzel394/jsonfly.nvim",
dependencies = {
"phelipetls/jsonpath.nvim"
},
},
```
## See also
* [jsonpath.nvim](https://github.com/phelipetls/jsonpath.nvim) - Copy JSON paths to your clipboard

61
flake.lock generated Normal file
View File

@ -0,0 +1,61 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1748929857,
"narHash": "sha256-lcZQ8RhsmhsK8u7LIFsJhsLh/pzR9yZ8yqpTzyGdj+Q=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "c2a03962b8e24e669fb37b7df10e7c79531ff1a4",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs",
"utils": "utils"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

44
flake.nix Normal file
View File

@ -0,0 +1,44 @@
{
description = "jsonfly";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
utils.url = "github:numtide/flake-utils";
};
outputs = { nixpkgs, utils, ... } @ inputs:
utils.lib.eachDefaultSystem(system:
let
pkgs = nixpkgs.legacyPackages.${system};
logo = pkgs.writeText "logo.txt" ''
'';
in
{
devShells.default = pkgs.mkShell {
packages = with pkgs; [
just
stylua
# If this ever fails, just remove it. It's just for the logo.
lolcat
];
shellHook = ''
cat ${logo} | lolcat
echo "";
echo "Welcome to the jsonfly.nvim development environment!";
echo "";
'';
};
}
);
}

10
justfile Normal file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env just --justfile
set dotenv-load := true
_default:
just --list -u
lint:
stylua lua

View File

@ -205,4 +205,12 @@ function M:extract_key_description(text)
return keys
end
---@param name string
---@return boolean
function M:is_module_available(name)
return pcall(function()
require(name)
end) == true
end
return M

View File

@ -17,6 +17,7 @@
--
---@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 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
---@field number string - Highlight group for numbers, Default: "@number.json"
@ -61,6 +62,20 @@ local DEFAULT_CONFIG = {
use_cache = 500,
commands = {
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)
vim.notify(
'copied JSONPath "' .. utils:truncate_overflow(path, 35, "") .. '" to clipboard',
vim.log.levels.INFO
)
end,
},
},
}
@ -102,6 +117,34 @@ local function show_picker(entries, buffer, xopts)
insert:insert_new_key(entries, key_descriptor, buffer)
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.col,
},
}),
buffer
)
if path then
global_config.commands.copy_jsonpath[3](path, prompt_bufnr)
end
end
)
end
return true
end,
finder = finders.new_table({
@ -196,7 +239,10 @@ return require("telescope").register_extension({
end
if global_config.backend == "lsp" then
local params = vim.lsp.util.make_position_params(xopts.winnr)
local params = vim.lsp.util.make_position_params(
xopts.winnr,
"utf-8"
)
-- Check i
local clients = vim.lsp.get_clients()