From 32735077e74bf288c9463e167a07c2be98b96052 Mon Sep 17 00:00:00 2001
From: barrancojared <51723690+jaredbarranco@users.noreply.github.com>
Date: Sat, 15 Feb 2025 13:03:06 -0800
Subject: [PATCH 01/10] Use temp file for non-windows jq actions
use temp_file for non-windows is_valid
debug is_valid
back to original
debug loggers
more debug loggers
fix debug
log exit_status
remove redundant debugs, add validity check debug
bool to string
debugging
debugging
debugging
debugging
In jq.lua, use temp file for jq actions.
remove debug line
---
lua/json-nvim/jq.lua | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)
diff --git a/lua/json-nvim/jq.lua b/lua/json-nvim/jq.lua
index fcf7d3a..f5d1e66 100644
--- a/lua/json-nvim/jq.lua
+++ b/lua/json-nvim/jq.lua
@@ -32,7 +32,8 @@ function M.get_formatted(input)
cmd = "jq . " .. temp_file_path
result = vim.fn.system(cmd)
else
- cmd = string.format("echo '%s' | jq .", input)
+ write_to_temp(input)
+ cmd = "jq . -e " .. temp_file_path
result = vim.fn.system(cmd)
end
@@ -51,11 +52,11 @@ function M.get_collapsed(input)
result = vim.fn.system(cmd)
result = vim.fn.substitute(result, [[\n]], "", "g")
else
- cmd = string.format("echo '%s' | jq -c .", input)
+ write_to_temp(input)
+ cmd = "jq . -e " .. temp_file_path
result = vim.fn.system(cmd)
- result = result:gsub("[\n\r]", "")
+ result = result:gsub("\r?\n", "")
end
-
return result
end
@@ -72,7 +73,8 @@ function M.get_rawed(input)
result = vim.fn.system(cmd)
result = vim.fn.substitute(result, [[\n]], "", "g")
else
- cmd = string.format("echo '%s' | jq -r .", input)
+ write_to_temp(input)
+ cmd = "jq . -r " .. temp_file_path
result = vim.fn.system(cmd)
result = result:gsub("[\n\r]", "")
end
@@ -96,9 +98,9 @@ function M.is_valid(input)
return exit_status == 0
else
- cmd = string.format("echo '%s' | jq -e .", input)
+ write_to_temp(input)
+ cmd = "jq . -e " .. temp_file_path .. "
Date: Sun, 16 Feb 2025 21:37:40 -0800
Subject: [PATCH 02/10] use a temporary directory and epoch/operation for file
operations
---
lua/json-nvim/jq.lua | 45 +++++++++++++++++++++++++-------------------
1 file changed, 26 insertions(+), 19 deletions(-)
diff --git a/lua/json-nvim/jq.lua b/lua/json-nvim/jq.lua
index f5d1e66..9fae352 100644
--- a/lua/json-nvim/jq.lua
+++ b/lua/json-nvim/jq.lua
@@ -1,11 +1,22 @@
local utils = require("json-nvim.utils")
-local temp_file_path = utils.get_plugin_root() .. "temp.json"
+local temp_file_path = utils.get_plugin_root() .. "tmp/"
-local function write_to_temp(input)
- local f = io.open(temp_file_path, "w")
+--write data to a temporary file in plugin root
+---@param arg { input: string, operation: string }
+---@return string
+local function write_to_temp(arg)
+ if type(arg.input) ~= "string" then
+ error("no input to write_to_temp()")
+ end
+ local tmp_file = temp_file_path .. os.time() .. (arg.operation and ("-" .. arg.operation .. ".json") or ".json")
+ local f = io.open(tmp_file, "w")
+ -- if not f then
+ -- error("Failed to open file: " .. tmp_file)
+ -- end
f:write(input)
f:close()
+ return tmp_file
end
local M = {}
@@ -27,13 +38,12 @@ end
function M.get_formatted(input)
local result
local cmd
+ local tmp_file = write_to_temp({ input = input, operation = "get_formatted" })
if vim.fn.has("win32") == 1 then
- write_to_temp(input)
- cmd = "jq . " .. temp_file_path
+ cmd = "jq . " .. tmp_file
result = vim.fn.system(cmd)
else
- write_to_temp(input)
- cmd = "jq . -e " .. temp_file_path
+ cmd = "jq . -e " .. tmp_file
result = vim.fn.system(cmd)
end
@@ -46,14 +56,13 @@ end
function M.get_collapsed(input)
local result
local cmd
+ local tmp_file = write_to_temp({ input = input, operation = "get_collapsed" })
if vim.fn.has("win32") == 1 then
- write_to_temp(input)
- cmd = "jq -c . " .. temp_file_path
+ cmd = "jq -c . " .. tmp_file
result = vim.fn.system(cmd)
result = vim.fn.substitute(result, [[\n]], "", "g")
else
- write_to_temp(input)
- cmd = "jq . -e " .. temp_file_path
+ cmd = "jq . -e " .. tmp_file
result = vim.fn.system(cmd)
result = result:gsub("\r?\n", "")
end
@@ -67,14 +76,13 @@ end
function M.get_rawed(input)
local result
local cmd
+ local tmp_file = write_to_temp({ input = input, operation = "get_rawed" })
if vim.fn.has("win32") == 1 then
- write_to_temp(input)
- cmd = "jq -r . " .. temp_file_path
+ cmd = "jq -r . " .. tmp_file
result = vim.fn.system(cmd)
result = vim.fn.substitute(result, [[\n]], "", "g")
else
- write_to_temp(input)
- cmd = "jq . -r " .. temp_file_path
+ cmd = "jq . -r " .. tmp_file
result = vim.fn.system(cmd)
result = result:gsub("[\n\r]", "")
end
@@ -88,9 +96,9 @@ end
function M.is_valid(input)
local cmd
local result
+ local tmp_file = write_to_temp({ input = input, operation = "is_valid" })
if vim.fn.has("win32") == 1 then
- write_to_temp(input)
- cmd = "jq . -e " .. temp_file_path
+ cmd = "jq . -e " .. tmp_file
result = vim.fn.system(cmd)
local exit_status = vim.v.shell_error
@@ -98,8 +106,7 @@ function M.is_valid(input)
return exit_status == 0
else
- write_to_temp(input)
- cmd = "jq . -e " .. temp_file_path .. "
Date: Sun, 16 Feb 2025 21:53:21 -0800
Subject: [PATCH 03/10] remove apparent non-required code
The init_path logic below seems to account for OS path separators
---
lua/json-nvim/utils.lua | 6 ------
1 file changed, 6 deletions(-)
diff --git a/lua/json-nvim/utils.lua b/lua/json-nvim/utils.lua
index 62df204..aba82b6 100644
--- a/lua/json-nvim/utils.lua
+++ b/lua/json-nvim/utils.lua
@@ -5,12 +5,6 @@ local M = {}
function M.get_plugin_root()
local file_separator = ""
- if vim.fn.has("win32") == 1 then
- file_separator = "\\"
- else
- file_separator = "/"
- end
-
local init_path = debug.getinfo(1).source
if not init_path:find("\\") then
From 1eae66b75f6ad07ef3436021ad20f06e388bab65 Mon Sep 17 00:00:00 2001
From: barrancojared <51723690+jaredbarranco@users.noreply.github.com>
Date: Sun, 16 Feb 2025 22:29:07 -0800
Subject: [PATCH 04/10] use OS specific temp directory
For OS management of temp files
---
lua/json-nvim/jq.lua | 4 +++-
lua/json-nvim/utils.lua | 27 ++++++++++++++++++++++++---
2 files changed, 27 insertions(+), 4 deletions(-)
diff --git a/lua/json-nvim/jq.lua b/lua/json-nvim/jq.lua
index 9fae352..4abfc7f 100644
--- a/lua/json-nvim/jq.lua
+++ b/lua/json-nvim/jq.lua
@@ -1,6 +1,8 @@
local utils = require("json-nvim.utils")
-local temp_file_path = utils.get_plugin_root() .. "tmp/"
+local temp_file_path, os_file_sep = utils.get_os_temp_file_path()
+
+temp_file_path = temp_file_path .. os_file_sep .. "json-nvim_"
--write data to a temporary file in plugin root
---@param arg { input: string, operation: string }
diff --git a/lua/json-nvim/utils.lua b/lua/json-nvim/utils.lua
index aba82b6..bb112b7 100644
--- a/lua/json-nvim/utils.lua
+++ b/lua/json-nvim/utils.lua
@@ -2,15 +2,19 @@ local M = {}
-- some of the functions below
-- are just in case
-
-function M.get_plugin_root()
- local file_separator = ""
+---@return string
+local function get_os_file_separator()
+ local file_separator
local init_path = debug.getinfo(1).source
if not init_path:find("\\") then
file_separator = "/"
end
+ return file_separator
+end
+function M.get_plugin_root()
+ local file_separator = get_os_file_separator()
local target_index = 0
local separator_encountered = 0
for i = #init_path, 1, -1 do
@@ -33,6 +37,23 @@ function M.get_jq_modules_directory()
return result
end
+-- Returns the OS TMP DIR and path_separator
+---@return string?
+---@return string
+function M.get_os_temp_file_path()
+ local tmp_dir
+ if vim.fn.has("win32") == 1 then
+ tmp_dir = os.getenv("TEMP")
+ else
+ tmp_dir = os.getenv("TMPDIR")
+ end
+ if not tmp_dir then
+ error("json-nvim uses temp files. Either $TEMP (Windows) or $TMPDIR (Unix) must be defined")
+ end
+
+ return tmp_dir, get_os_file_separator()
+end
+
---takes json string as keys and returns which casing is used
---@param input string
---@return string
From 2c96d7ea21048954acd18c4ffd541a5860b5f4c4 Mon Sep 17 00:00:00 2001
From: barrancojared <51723690+jaredbarranco@users.noreply.github.com>
Date: Sun, 16 Feb 2025 22:29:31 -0800
Subject: [PATCH 05/10] use correct arg.input
---
lua/json-nvim/jq.lua | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/lua/json-nvim/jq.lua b/lua/json-nvim/jq.lua
index 4abfc7f..c2946d5 100644
--- a/lua/json-nvim/jq.lua
+++ b/lua/json-nvim/jq.lua
@@ -13,10 +13,7 @@ local function write_to_temp(arg)
end
local tmp_file = temp_file_path .. os.time() .. (arg.operation and ("-" .. arg.operation .. ".json") or ".json")
local f = io.open(tmp_file, "w")
- -- if not f then
- -- error("Failed to open file: " .. tmp_file)
- -- end
- f:write(input)
+ f:write(arg.input)
f:close()
return tmp_file
end
From a583ac49793f6ea3ce2a8eaa33a26dd007e55111 Mon Sep 17 00:00:00 2001
From: barrancojared <51723690+jaredbarranco@users.noreply.github.com>
Date: Tue, 18 Feb 2025 19:51:56 -0800
Subject: [PATCH 06/10] fix: use jq -c on get_collapsed
---
lua/json-nvim/jq.lua | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lua/json-nvim/jq.lua b/lua/json-nvim/jq.lua
index f5d1e66..b299c5d 100644
--- a/lua/json-nvim/jq.lua
+++ b/lua/json-nvim/jq.lua
@@ -53,7 +53,7 @@ function M.get_collapsed(input)
result = vim.fn.substitute(result, [[\n]], "", "g")
else
write_to_temp(input)
- cmd = "jq . -e " .. temp_file_path
+ cmd = "jq -c . " .. temp_file_path
result = vim.fn.system(cmd)
result = result:gsub("\r?\n", "")
end
From 4a8c59837e4cccda023149c01f9815e672ebc2b8 Mon Sep 17 00:00:00 2001
From: barrancojared <51723690+jaredbarranco@users.noreply.github.com>
Date: Fri, 16 May 2025 08:35:46 -0700
Subject: [PATCH 07/10] typo fix in jq.lua comments
---
lua/json_nvim/jq.lua | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lua/json_nvim/jq.lua b/lua/json_nvim/jq.lua
index 5545091..3977ed3 100644
--- a/lua/json_nvim/jq.lua
+++ b/lua/json_nvim/jq.lua
@@ -21,7 +21,7 @@ end
local M = {}
---get all keys from json text
----this function is used for scase_switching feature
+---this function is used for case_switching feature
---@param json string
---@return string[]
function M.get_keys(json)
From e72eb1a7051aae85134a251e4418f3ae1fb554cd Mon Sep 17 00:00:00 2001
From: barrancojared <51723690+jaredbarranco@users.noreply.github.com>
Date: Fri, 16 May 2025 08:42:13 -0700
Subject: [PATCH 08/10] update workflow
echo runner temp
cache apt, env. namespace incorrect
fix tmpdir declaration
set temp for windows
---
.github/workflows/unit_tests.yaml | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/.github/workflows/unit_tests.yaml b/.github/workflows/unit_tests.yaml
index 6502963..d7cdae0 100644
--- a/.github/workflows/unit_tests.yaml
+++ b/.github/workflows/unit_tests.yaml
@@ -13,11 +13,14 @@ jobs:
- name: Checkout Code
uses: actions/checkout@v3
- - name: Install Neovim
- run: |
- sudo apt update
- sudo apt install -y neovim
+ - uses: awalsh128/cache-apt-pkgs-action@latest
+ with:
+ packages: neovim
+ version: 1.0
- name: Run Lua Tests
+ env:
+ TMPDIR: ${{ runner.temp }}
+ TEMP: ${{ runner.temp }}
run: nvim --headless -l tests/run_tests.lua
From 2e2bb1a7c8fc58bbc93280bb885801db128a5f6f Mon Sep 17 00:00:00 2001
From: barrancojared <51723690+jaredbarranco@users.noreply.github.com>
Date: Fri, 16 May 2025 09:23:47 -0700
Subject: [PATCH 09/10] ignore .actual and .expected in test dir
ignore .expected
---
.gitignore | 2 ++
1 file changed, 2 insertions(+)
diff --git a/.gitignore b/.gitignore
index fdd8d68..42e6967 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,4 @@
vendor/plenary.nvim
temp.json
+tests/**.actual
+tests/**.expected
From 277fc4153f03eeb5b5d38a16952d7e42860afd60 Mon Sep 17 00:00:00 2001
From: barrancojared <51723690+jaredbarranco@users.noreply.github.com>
Date: Fri, 16 May 2025 09:34:30 -0700
Subject: [PATCH 10/10] update readme for node name update
---
README.md | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/README.md b/README.md
index fed3e09..d9c6ecd 100644
--- a/README.md
+++ b/README.md
@@ -29,7 +29,7 @@
Using [lazy.nvim](https://github.com/folke/lazy.nvim):
```lua
{
- "VPavliashvili/json-nvim",
+ "jaredbarranco/json-nvim",
ft = "json", -- only load for json filetype
}
``````
@@ -66,13 +66,13 @@ Commands involving visual mode always should be used with valid json selection
see example

-* `:JsonFormatToken`
+* `:JsonFormatNode`
see example
- 
+ 
-* `:JsonMinifyToken`
+* `:JsonMinifyNode`
see example
- 
+ 
* `:JsonEscapeFile`
see example