all repos — dotfiles @ e781918e358e8342fd5f63abeedadc9b0cfe117d

linux dotfiles

nvim: add jq.lua
Prithu Goswami pg@prithu.dev
Tue, 12 Mar 2024 08:35:32 +0530
commit

e781918e358e8342fd5f63abeedadc9b0cfe117d

parent

b8b12c23c7cb1a7cc40a137d4b55ed40ba2790e8

2 files changed, 90 insertions(+), 0 deletions(-)

jump to
M config/nvim/init.luaconfig/nvim/init.lua

@@ -5,6 +5,7 @@ require 'completion'

require 'lsp' require 'highlights' require 'snippets' +require 'jq' -- TODO move each plugin setup to own file local function nvim_tree_on_attach(bufnr)
A config/nvim/lua/jq.lua

@@ -0,0 +1,89 @@

+local kmap = vim.keymap.set +local ucmd = vim.api.nvim_create_user_command + +function buf_text(bufnr) + if bufnr == nil then + bufnr = vim.api.nvim_win_get_buf(0) + end + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, vim.api.nvim_buf_line_count(bufnr), true) + local text = '' + for i, line in ipairs(lines) do + text = text .. line .. '\n' + end + return text +end + +function set_buf_text(text, bufnr) + if bufnr == nil then + bufnr = vim.fn.bufnr('%') + end + + if type(text) == 'string' then + text = vim.fn.split(text, '\n') + end + + vim.api.nvim_buf_set_lines( + bufnr, + 0, + -1, + false, + text + ) +end + +function jq_filter(json_bufnr, jq_filter) + -- spawn jq and pipe in json, returning the output text + local modified = vim.bo[json_bufnr].modified + local fname = vim.fn.bufname(json_bufnr) + + if (not modified) and fname ~= '' then + -- the following should be faster as it lets jq read the file contents + return vim.fn.system({ 'jq', jq_filter, fname }) + else + local json = buf_text(json_bufnr) + return vim.fn.system({ 'jq', jq_filter }, json) + end +end + +function Jq_command(horizontal) + local splitcmd = 'vnew' + if horizontal == true then + splitcmd = 'new' + end + + local json_bufnr = vim.fn.bufnr() + + vim.cmd(splitcmd) + vim.cmd'set filetype=conf' + set_buf_text('# JQ filter: press <CR> to execute it\n\n.') + vim.cmd'normal!G' + local jq_bufnr = vim.fn.bufnr() + local jq_winnr = vim.fn.bufwinid(jq_bufnr) + + vim.cmd(splitcmd) + vim.cmd'set filetype=json' + local result_bufnr = vim.fn.bufnr() + + + vim.fn.win_gotoid(jq_winnr) + + -- setup keybinding autocmd in the filter buffer: + kmap( + 'n', + '<CR>', + function() + local filter = buf_text(jq_bufnr) + set_buf_text(jq_filter(json_bufnr, filter), result_bufnr) + end, + { buffer = jq_bufnr } + ) +end + +ucmd('Jq', function() + Jq_command(false) +end, {}) + +ucmd('Jqhorizontal', function() + Jq_command(true) +end, {}) +