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 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..585be17 --- /dev/null +++ b/flake.nix @@ -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 ""; + ''; + }; + } + ); +} 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 + diff --git a/lua/jsonfly/utils.lua b/lua/jsonfly/utils.lua index 3085a01..17d83ca 100644 --- a/lua/jsonfly/utils.lua +++ b/lua/jsonfly/utils.lua @@ -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 diff --git a/lua/telescope/_extensions/jsonfly.lua b/lua/telescope/_extensions/jsonfly.lua index 2dbf430..21a40fa 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" @@ -61,6 +62,20 @@ local DEFAULT_CONFIG = { 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) + + 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()