diff --git a/.github/workflows/unit_tests.yaml b/.github/workflows/unit_tests.yaml
new file mode 100644
index 0000000..d7cdae0
--- /dev/null
+++ b/.github/workflows/unit_tests.yaml
@@ -0,0 +1,26 @@
+name: Run Neovim Tests
+
+on:
+ push:
+ branches: [main]
+ pull_request:
+
+jobs:
+ test:
+ runs-on: ubuntu-latest
+
+ steps:
+ - name: Checkout Code
+ uses: actions/checkout@v3
+
+ - 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
+
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
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
diff --git a/lua/json-nvim/escaper.lua b/lua/json_nvim/escaper.lua
similarity index 65%
rename from lua/json-nvim/escaper.lua
rename to lua/json_nvim/escaper.lua
index d01a6f2..0b64273 100644
--- a/lua/json-nvim/escaper.lua
+++ b/lua/json_nvim/escaper.lua
@@ -1,5 +1,5 @@
-local jq = require("json-nvim.jq")
-local utils = require("json-nvim.utils")
+local jq = require("json_nvim.jq")
+local utils = require("json_nvim.utils")
local function get_escaped_input(input)
local compacted = jq.get_collapsed(input)
@@ -21,11 +21,23 @@ end
local M = {}
+function M.escape_string(input)
+ local result = get_escaped_input(input)
+ assert(result and result ~= "", "escaped result was nil or empty")
+ return result
+end
+
+function M.unescape_string(input)
+ local result = get_unescaped_input(input)
+ assert(result and result ~= "", "unescaped result was nil or empty")
+ return result
+end
+
function M.escape_file()
local root = utils.get_treesitter_root()
local content = utils.get_buffer_content_as_string()
- local escaped = get_escaped_input(content)
+ local escaped = M.escape_string(content)
utils.replace_tsnode_text(root, escaped)
end
@@ -38,7 +50,7 @@ function M.unescape_file()
return
end
- local unescaped = get_unescaped_input(content)
+ local unescaped = M.unescape_string(content)
utils.replace_tsnode_text(root, unescaped)
M.unescape_file()
diff --git a/lua/json-nvim/formatter.lua b/lua/json_nvim/formatter.lua
similarity index 82%
rename from lua/json-nvim/formatter.lua
rename to lua/json_nvim/formatter.lua
index f1b426f..bd0261d 100644
--- a/lua/json-nvim/formatter.lua
+++ b/lua/json_nvim/formatter.lua
@@ -1,5 +1,5 @@
-local jq = require("json-nvim.jq")
-local utils = require("json-nvim.utils")
+local jq = require("json_nvim.jq")
+local utils = require("json_nvim.utils")
---comment
---@param target_node TSNode
@@ -19,13 +19,15 @@ end
local M = {}
+function M.format_string(json_string)
+ local result = jq.get_formatted(json_string)
+ assert(result and result ~= "", "minified result was nil or empty")
+ return result
+end
+
function M.format_file()
local content = utils.get_buffer_content_as_string()
- local formatted = jq.get_formatted(content)
- if formatted == nil or formatted == "" then
- error("result was nil or empty")
- return
- end
+ local formatted = M.format_string(content)
local replacement = utils.split(formatted, "\n\r")
local root = utils.get_treesitter_root()
@@ -36,7 +38,7 @@ end
---@param input_json string
---@param target_node TSNode
function M.format_and_put(input_json, target_node)
- local formatted = jq.get_formatted(input_json)
+ local formatted = M.format_string(input_json)
if formatted == nil or formatted == "" then
error("result was nil or empty")
return
diff --git a/lua/json-nvim/init.lua b/lua/json_nvim/init.lua
similarity index 94%
rename from lua/json-nvim/init.lua
rename to lua/json_nvim/init.lua
index 55a24d6..4e807c5 100644
--- a/lua/json-nvim/init.lua
+++ b/lua/json_nvim/init.lua
@@ -1,4 +1,4 @@
-local jn = require("json-nvim.json-nvim")
+local jn = require("json_nvim.json_nvim")
local M = {}
diff --git a/lua/json-nvim/jq.lua b/lua/json_nvim/jq.lua
similarity index 63%
rename from lua/json-nvim/jq.lua
rename to lua/json_nvim/jq.lua
index fcf7d3a..3977ed3 100644
--- a/lua/json-nvim/jq.lua
+++ b/lua/json_nvim/jq.lua
@@ -1,17 +1,27 @@
-local utils = require("json-nvim.utils")
+local utils = require("json_nvim.utils")
-local temp_file_path = utils.get_plugin_root() .. "temp.json"
+local temp_file_path, os_file_sep = utils.get_os_temp_file_path()
-local function write_to_temp(input)
- local f = io.open(temp_file_path, "w")
- f:write(input)
+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 }
+---@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")
+ f:write(arg.input)
f:close()
+ return tmp_file
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)
@@ -27,12 +37,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
- cmd = string.format("echo '%s' | jq .", input)
+ cmd = "jq . -e " .. tmp_file
result = vim.fn.system(cmd)
end
@@ -45,17 +55,16 @@ 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
- cmd = string.format("echo '%s' | jq -c .", input)
+ cmd = "jq -c . " .. tmp_file
result = vim.fn.system(cmd)
- result = result:gsub("[\n\r]", "")
+ result = result:gsub("\r?\n", "")
end
-
return result
end
@@ -66,13 +75,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
- cmd = string.format("echo '%s' | jq -r .", input)
+ cmd = "jq . -r " .. tmp_file
result = vim.fn.system(cmd)
result = result:gsub("[\n\r]", "")
end
@@ -86,9 +95,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
@@ -96,9 +105,8 @@ function M.is_valid(input)
return exit_status == 0
else
- cmd = string.format("echo '%s' | jq -e .", input)
+ cmd = "jq . -e " .. tmp_file .. "