Skip to content

Commit 9460ede

Browse files
Merge pull request #28 from gennaro-tedesco/fix_escaping
added parse_key function to strip vim special characters
2 parents 4c4082c + ccaf4b6 commit 9460ede

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

lua/nvim-jqx/init.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local jqx = require("nvim-jqx.jqx")
55
local config = require("nvim-jqx.config")
66
local fw = require("nvim-jqx.floating")
77

8+
---@returns boolean
89
local function is_valid_ft(ft)
910
if not (ft == "json" or ft == "yaml") then
1011
print("only json or yaml files")
@@ -28,6 +29,7 @@ local function is_valid_ft(ft)
2829
return true
2930
end
3031

32+
---set keymaps in the qf to query entry on keypress
3133
local function set_qf_maps(ft)
3234
vim.api.nvim_exec(
3335
[[autocmd FileType qf nnoremap <buffer> ]]
@@ -39,6 +41,9 @@ local function set_qf_maps(ft)
3941
)
4042
end
4143

44+
---main function interface that populates the quickfix list on invocation
45+
---invoke set_qf_maps and jqx.populate_qf
46+
---@param type string variable type to fetch if given
4247
local function jqx_open(type)
4348
local ft = vim.bo.filetype
4449
if not is_valid_ft(ft) then
@@ -52,6 +57,8 @@ local function jqx_open(type)
5257
jqx.populate_qf(ft, type, config.sort)
5358
end
5459

60+
---execute jq query from user input
61+
---@param q string user query
5562
local function query_jq(q)
5663
local ft = vim.bo.filetype
5764
if not is_valid_ft(ft) then

lua/nvim-jqx/jqx.lua

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,37 @@ local function has_value(tab, val)
1010
return false
1111
end
1212

13+
---remove vim special characters from json keys
14+
---@param key string
15+
---@return string "parsed key"
16+
local function parse_key(key)
17+
return (key:gsub("/", "\\/"))
18+
end
19+
20+
---return location of key in buffer
21+
---@param key string
22+
---@param ft string
23+
---@return table
1324
local function get_key_location(key, ft)
1425
if ft == "json" then
1526
return {
16-
row = vim.api.nvim_exec([[g/^\s*"]] .. key .. [["/echo line('.')]], true),
17-
col = vim.api.nvim_exec([[g/^\s*"]] .. key .. [["/execute "normal! ^" | echo col('.')-1]], true),
27+
row = vim.api.nvim_exec([[g/^\s*"]] .. parse_key(key) .. [["/echo line('.')]], true),
28+
col = vim.api.nvim_exec([[g/^\s*"]] .. parse_key(key) .. [["/execute "normal! ^" | echo col('.')-1]], true),
1829
}
1930
elseif ft == "yaml" then
2031
return {
21-
row = vim.api.nvim_exec([[g/^]] .. key .. [[/echo line('.')]], true),
22-
col = vim.api.nvim_exec([[g/^]] .. key .. [[/execute "normal! ^" | echo col('.')]], true),
32+
row = vim.api.nvim_exec([[g/^]] .. parse_key(key) .. [[/echo line('.')]], true),
33+
col = vim.api.nvim_exec([[g/^]] .. parse_key(key) .. [[/execute "normal! ^" | echo col('.')]], true),
2334
}
35+
else
36+
return {}
2437
end
2538
end
2639

40+
---fetch json keys for cur buf and populate qf list
41+
---@param ft string
42+
---@param type string variable type to fetch if given
43+
---@param sort boolean sort list alphabetically
2744
local function populate_qf(ft, type, sort)
2845
local cmd_lines = {}
2946
local cur_file = vim.fn.getreg("%")

0 commit comments

Comments
 (0)