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
26 changes: 26 additions & 0 deletions .github/workflows/unit_tests.yaml
Original file line number Diff line number Diff line change
@@ -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

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
vendor/plenary.nvim
temp.json
tests/**.actual
tests/**.expected
20 changes: 16 additions & 4 deletions lua/json-nvim/escaper.lua → lua/json_nvim/escaper.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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

Expand All @@ -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()
Expand Down
18 changes: 10 additions & 8 deletions lua/json-nvim/formatter.lua → lua/json_nvim/formatter.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/json-nvim/init.lua → lua/json_nvim/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local jn = require("json-nvim.json-nvim")
local jn = require("json_nvim.json_nvim")

local M = {}

Expand Down
50 changes: 28 additions & 22 deletions lua/json-nvim/jq.lua → lua/json_nvim/jq.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -27,13 +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
write_to_temp(input)
cmd = "jq . -e " .. temp_file_path
cmd = "jq . -e " .. tmp_file
result = vim.fn.system(cmd)
end

Expand All @@ -46,14 +55,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 -c . " .. temp_file_path
cmd = "jq -c . " .. tmp_file
result = vim.fn.system(cmd)
result = result:gsub("\r?\n", "")
end
Expand All @@ -67,14 +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
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
Expand All @@ -88,18 +95,17 @@ 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

result = vim.fn.substitute(result, [[\n]], "", "g")

return exit_status == 0
else
write_to_temp(input)
cmd = "jq . -e " .. temp_file_path .. "</dev/null"
cmd = "jq . -e " .. tmp_file .. "</dev/null"
result = vim.fn.system(cmd)
local exit_status = vim.v.shell_error

Expand Down
12 changes: 6 additions & 6 deletions lua/json-nvim/json-nvim.lua → lua/json_nvim/json_nvim.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local escaper = require("json-nvim.escaper")
local formatter = require("json-nvim.formatter")
local jq = require("json-nvim.jq")
local minifier = require("json-nvim.minifier")
local utils = require("json-nvim.utils")
local escaper = require("json_nvim.escaper")
local formatter = require("json_nvim.formatter")
local jq = require("json_nvim.jq")
local minifier = require("json_nvim.minifier")
local utils = require("json_nvim.utils")

---sometimes buffer with valid json content is not
---set to `json` filetype, for example `log.log`
Expand All @@ -26,7 +26,7 @@ local function validate_and_set_buffer_filetype()
end

if choice ~= "y" then
print("json-nvim operation cancelled")
print("json_nvim operation cancelled")
else
vim.bo.filetype = "json"
res = true
Expand Down
24 changes: 10 additions & 14 deletions lua/json-nvim/minifier.lua → lua/json_nvim/minifier.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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 M = {}

function M.minify_string(json_string)
local result = jq.get_collapsed(json_string)
assert(result and result ~= "", "minified result was nil or empty")
return result
end

function M.minify_file()
local content = utils.get_buffer_content_as_string()
local minified = jq.get_collapsed(content)
if minified == nil or minified == "" then
error("result was nil or empty")
return
end

local minified = M.minify_string(content)
local root = utils.get_treesitter_root()
utils.replace_tsnode_text(root, minified)
end
Expand All @@ -19,12 +20,7 @@ end
---@param input_json string
---@param target_node TSNode
function M.minify_and_put(input_json, target_node)
local minified = jq.get_collapsed(input_json)
if minified == nil or minified == "" then
error("result was nil or empty")
return
end

local minified = M.minify_string(input_json)
utils.replace_tsnode_text(target_node, minified)
end

Expand Down
33 changes: 24 additions & 9 deletions lua/json-nvim/utils.lua → lua/json_nvim/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ local M = {}

-- some of the functions below
-- are just in case

function M.get_plugin_root()
local file_separator = ""
if vim.fn.has("win32") == 1 then
file_separator = "\\"
else
file_separator = "/"
end

---@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
Expand All @@ -39,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
Expand Down
6 changes: 3 additions & 3 deletions plugin/json-nvim.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local json_nvim = require("json-nvim")
local json_nvim = require("json_nvim")

local function command(name, callback, options)
local final_opts = vim.tbl_deep_extend("force", options or {}, { bang = true })
Expand All @@ -21,11 +21,11 @@ command("JsonMinifySelection", function()
json_nvim.MinifySelection()
end, { range = true })

command("JsonFormatToken", function()
command("JsonFormatNode", function()
json_nvim.FormatToken()
end)

command("JsonMinifyToken", function()
command("JsonMinifyNode", function()
json_nvim.MinifyToken()
end)

Expand Down
1 change: 1 addition & 0 deletions tests/golden/escape_file.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"[{\"_id\":\"67b3739c332882c1456e076b\",\"index\":0,\"guid\":\"9a249e2b-173b-4ab9-ac77-9257d780eedb\",\"isActive\":true,\"balance\":\"$3,456.75\",\"picture\":\"http://placehold.it/32x32\",\"age\":28,\"eyeColor\":\"green\",\"name\":\"Olga Hess\",\"gender\":\"female\",\"company\":\"REPETWIRE\",\"email\":\"olgahess@repetwire.com\",\"phone\":\"+1 (920) 506-2554\",\"address\":\"918 Fillmore Avenue, Chapin, Indiana, 8526\",\"about\":\"Proident ea enim sit commodo ullamco minim sint non. Cupidatat reprehenderit pariatur sit in in pariatur reprehenderit consectetur occaecat sit. Consectetur excepteur nisi adipisicing pariatur ullamco labore voluptate elit in do.\\r\\n\",\"registered\":\"2016-07-23T05:21:41 -04:00\",\"latitude\":-38.870975,\"longitude\":139.528881,\"tags\":[\"nisi\",\"aliquip\",\"exercitation\",\"id\",\"velit\",\"eiusmod\",\"proident\"],\"friends\":[{\"id\":0,\"name\":\"Constance Mcmillan\"},{\"id\":1,\"name\":\"Alexandria Lamb\"},{\"id\":2,\"name\":\"Amparo Barber\"}],\"greeting\":\"Hello, Olga Hess! You have 8 unread messages.\",\"favoriteFruit\":\"strawberry\"}]"
Loading