From 5b11ed040450b471e4f77261438f93c216aa61cc Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Thu, 11 Jul 2024 15:40:47 +0200 Subject: [PATCH 1/7] feat: Add support for jsonpath --- lua/jsonfly/utils.lua | 6 +++++ lua/telescope/_extensions/jsonfly.lua | 37 ++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lua/jsonfly/utils.lua b/lua/jsonfly/utils.lua index 5256498..e121fbb 100644 --- a/lua/jsonfly/utils.lua +++ b/lua/jsonfly/utils.lua @@ -205,4 +205,10 @@ 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 diff --git a/lua/telescope/_extensions/jsonfly.lua b/lua/telescope/_extensions/jsonfly.lua index 089484b..56c8e82 100644 --- a/lua/telescope/_extensions/jsonfly.lua +++ b/lua/telescope/_extensions/jsonfly.lua @@ -17,6 +17,7 @@ -- ---@class Commands ---@field add_key string[] - Add the currently entered key to the JSON. Must be of type [string, string] ; Example: {"n", "a"} -> When in normal mode, press "a" to add the key; Example: {"i", ""} -> When in insert mode, press to add the key; Default: {"i", ""} +---@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] void> - Example: {"n", "", function(path) vim.fn.setreg("+", path) end} -> When in normal mode, press to copy the JSONPath to the clipboard; Default: {"i", "", a function that will copy the JSONPath to the clipboard} --- ---@class Highlights ---@field number string - Highlight group for numbers, Default: "@number.json" @@ -60,7 +61,16 @@ local DEFAULT_CONFIG = { backend = "lsp", use_cache = 500, commands = { - add_key = {"i", ""} + add_key = {"i", ""}, + copy_jsonpath = { + "i", + "", + ---@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 ) + 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 end, finder = finders.new_table { From 0e5158a8d4660eb560a5a855b3b5529757f6f23f Mon Sep 17 00:00:00 2001 From: Myzel394 Date: Fri, 6 Jun 2025 12:54:16 +0200 Subject: [PATCH 2/7] chore: apply stylua --- lua/jsonfly/utils.lua | 4 +- lua/telescope/_extensions/jsonfly.lua | 105 +++++++++++++------------- 2 files changed, 57 insertions(+), 52 deletions(-) diff --git a/lua/jsonfly/utils.lua b/lua/jsonfly/utils.lua index 1ee547a..17d83ca 100644 --- a/lua/jsonfly/utils.lua +++ b/lua/jsonfly/utils.lua @@ -208,7 +208,9 @@ end ---@param name string ---@return boolean function M:is_module_available(name) - return pcall(function() require(name) end) == true + return pcall(function() + require(name) + end) == true end return M diff --git a/lua/telescope/_extensions/jsonfly.lua b/lua/telescope/_extensions/jsonfly.lua index 82e0639..415e892 100644 --- a/lua/telescope/_extensions/jsonfly.lua +++ b/lua/telescope/_extensions/jsonfly.lua @@ -42,36 +42,36 @@ local action_state = require("telescope.actions.state") ---@type Options local DEFAULT_CONFIG = { - key_max_length = 50, - key_exact_length = false, - max_length = 9999, - overflow_marker = "…", - conceal = "auto", - prompt_title = "JSON(fly)", - highlights = { - string = "@string.json", - number = "@number.json", - boolean = "@boolean.json", - null = "@constant.builtin.json", - other = "@label.json", - }, - jump_behavior = "key_start", - subkeys_display = "normal", - show_nested_child_preview = true, - backend = "lsp", - use_cache = 500, - commands = { - add_key = {"i", ""}, - copy_jsonpath = { - "i", - "", - ---@param path string - ---@param prompt_bufnr number - function(path, prompt_bufnr) - vim.fn.setreg("+", path) - end - } - } + key_max_length = 50, + key_exact_length = false, + max_length = 9999, + overflow_marker = "…", + conceal = "auto", + prompt_title = "JSON(fly)", + highlights = { + string = "@string.json", + number = "@number.json", + boolean = "@boolean.json", + null = "@constant.builtin.json", + other = "@label.json", + }, + jump_behavior = "key_start", + subkeys_display = "normal", + show_nested_child_preview = true, + backend = "lsp", + use_cache = 500, + commands = { + add_key = { "i", "" }, + copy_jsonpath = { + "i", + "", + ---@param path string + ---@param prompt_bufnr number + function(path, prompt_bufnr) + vim.fn.setreg("+", path) + end, + }, + }, } local global_config = {} @@ -112,30 +112,33 @@ 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") + 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 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) + 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 + if path then + global_config.commands.copy_jsonpath[3](path, prompt_bufnr) + end + end + ) + end return true end, From 2346a75cae02dc770801ac5a4625bd4bc7ddd548 Mon Sep 17 00:00:00 2001 From: Myzel394 Date: Fri, 6 Jun 2025 12:55:07 +0200 Subject: [PATCH 3/7] feat(dev): Add flake.nix --- flake.lock | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 43 ++++++++++++++++++++++++++++++++++++++ justfile | 10 +++++++++ 3 files changed, 114 insertions(+) create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 justfile diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..bdd7c5f --- /dev/null +++ b/flake.lock @@ -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 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..2afbe8d --- /dev/null +++ b/flake.nix @@ -0,0 +1,43 @@ +{ + 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; [ + 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 ""; + ''; + }; + } + ); +} diff --git a/justfile b/justfile new file mode 100644 index 0000000..80e0b86 --- /dev/null +++ b/justfile @@ -0,0 +1,10 @@ +#!/usr/bin/env just --justfile + +set dotenv-load := true + +_default: + just --list -u + +lint: + stylua lua + From 7a5fb0ddf323a154ab3804803f7c21a7fd2e3daa Mon Sep 17 00:00:00 2001 From: Myzel394 Date: Sat, 7 Jun 2025 22:32:16 +0200 Subject: [PATCH 4/7] fix: Fix jsonpath node selection --- lua/telescope/_extensions/jsonfly.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lua/telescope/_extensions/jsonfly.lua b/lua/telescope/_extensions/jsonfly.lua index 415e892..7617005 100644 --- a/lua/telescope/_extensions/jsonfly.lua +++ b/lua/telescope/_extensions/jsonfly.lua @@ -69,6 +69,11 @@ local DEFAULT_CONFIG = { ---@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, }, }, @@ -127,7 +132,7 @@ local function show_picker(entries, buffer, xopts) bufnr = buffer, pos = { selection.lnum - 1, - selection.index, + selection.col, }, }), buffer From d0791d5f939c17518eba5b247b2bc1153651ad48 Mon Sep 17 00:00:00 2001 From: Myzel394 Date: Sat, 7 Jun 2025 22:32:29 +0200 Subject: [PATCH 5/7] chore(flake.nix) Add just --- flake.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/flake.nix b/flake.nix index 2afbe8d..585be17 100644 --- a/flake.nix +++ b/flake.nix @@ -25,6 +25,7 @@ { devShells.default = pkgs.mkShell { packages = with pkgs; [ + just stylua # If this ever fails, just remove it. It's just for the logo. From a1c07adc00f6f52b2a00de101859b571b0e69171 Mon Sep 17 00:00:00 2001 From: Myzel394 Date: Sat, 7 Jun 2025 22:43:20 +0200 Subject: [PATCH 6/7] fix: Fix jsonpath node selection --- lua/telescope/_extensions/jsonfly.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lua/telescope/_extensions/jsonfly.lua b/lua/telescope/_extensions/jsonfly.lua index 7617005..21a40fa 100644 --- a/lua/telescope/_extensions/jsonfly.lua +++ b/lua/telescope/_extensions/jsonfly.lua @@ -239,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() From fdbf1dd63a4d706aaf5c5d83d21581693ddbf777 Mon Sep 17 00:00:00 2001 From: Myzel394 Date: Sat, 7 Jun 2025 22:43:38 +0200 Subject: [PATCH 7/7] chore: Update README.md for new jsonpath integration --- README.md | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2529b49..3abfc70 100644 --- a/README.md +++ b/README.md @@ -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 `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 `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 `` (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