Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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

Expand All @@ -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 = {
{
Expand All @@ -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:
Expand All @@ -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
Expand Down
61 changes: 61 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env just --justfile

set dotenv-load := true

_default:
just --list -u

lint:
stylua lua

8 changes: 8 additions & 0 deletions lua/jsonfly/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
48 changes: 47 additions & 1 deletion lua/telescope/_extensions/jsonfly.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
},
},
}

Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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()
Expand Down
Loading