commit 863643a100c4db4d10bbfbf76bb9d89a0c56daa2
parent 16e452fd85a823156841b1086afd55d711dcee1a
Author: Tomas Nemec <nemi@skaut.cz>
Date: Mon, 26 Sep 2022 10:53:11 +0200
update
Diffstat:
9 files changed, 316 insertions(+), 4 deletions(-)
diff --git a/after/plugin/ccc.lua b/after/plugin/ccc.lua
@@ -0,0 +1,10 @@
+if not pcall(require, 'ccc') then
+ return
+end
+
+local ccc = require('ccc')
+local mapping = ccc.mapping
+
+ccc.setup({ highlighter = { auto_enable = false } })
+
+vim.keymap.set('n', '<C-c>', '<cmd>CccPick<cr>')
diff --git a/after/plugin/difftastic.lua b/after/plugin/difftastic.lua
@@ -0,0 +1,211 @@
+local json = [[
+{
+ "language": "JSON",
+ "path": "test/after.json",
+ "chunks": [
+ [
+ {
+ "lhs": {
+ "number": 2,
+ "changes": [
+ {
+ "start": 2,
+ "end": 7,
+ "content": "\"bar\"",
+ "kind": "keyword"
+ }
+ ]
+ },
+ "rhs": {
+ "number": 2,
+ "changes": [
+ {
+ "start": 2,
+ "end": 7,
+ "content": "\"zab\"",
+ "kind": "keyword"
+ },
+ {
+ "start": 18,
+ "end": 19,
+ "content": ",",
+ "kind": "normal"
+ }
+ ]
+ }
+ },
+ {
+ "rhs": {
+ "number": 3,
+ "changes": [
+ {
+ "start": 2,
+ "end": 7,
+ "content": "\"woo\"",
+ "kind": "keyword"
+ },
+ {
+ "start": 7,
+ "end": 8,
+ "content": ":",
+ "kind": "normal"
+ },
+ {
+ "start": 9,
+ "end": 10,
+ "content": "[",
+ "kind": "delimiter"
+ },
+ {
+ "start": 10,
+ "end": 18,
+ "content": "\"foobar\"",
+ "kind": "string"
+ },
+ {
+ "start": 18,
+ "end": 19,
+ "content": "]",
+ "kind": "delimiter"
+ }
+ ]
+ }
+ },
+ {
+ "lhs": {
+ "number": 1,
+ "changes": [
+ {
+ "start": 10,
+ "end": 11,
+ "content": "1",
+ "kind": "normal"
+ },
+ {
+ "start": 11,
+ "end": 12,
+ "content": ",",
+ "kind": "normal"
+ }
+ ]
+ },
+ "rhs": {
+ "number": 1,
+ "changes": [
+ {
+ "start": 17,
+ "end": 18,
+ "content": ",",
+ "kind": "normal"
+ },
+ {
+ "start": 19,
+ "end": 20,
+ "content": "5",
+ "kind": "normal"
+ }
+ ]
+ }
+ }
+ ]
+ ],
+ "status": "changed"
+}
+]]
+
+local function get_changes()
+ return vim.fn.json_decode(json)
+end
+
+local function show_diff(lang, lhs_file, rhs_file, chunks)
+ local width = vim.api.nvim_win_get_width(0)
+
+ local lhs_buf = vim.api.nvim_create_buf(true, true)
+ local rhs_buf = vim.api.nvim_create_buf(true, true)
+
+ vim.api.nvim_buf_call(lhs_buf, function()
+ vim.api.nvim_command(':0r ' .. lhs_file)
+ end)
+ vim.api.nvim_buf_call(rhs_buf, function()
+ vim.api.nvim_command(':0r ' .. rhs_file)
+ end)
+
+ vim.api.nvim_buf_set_name(lhs_buf, lhs_file)
+ vim.api.nvim_buf_set_name(rhs_buf, rhs_file)
+
+ vim.api.nvim_buf_set_option(lhs_buf, 'filetype', lang)
+ vim.api.nvim_buf_set_option(rhs_buf, 'filetype', lang)
+
+ -- print(vim.api.nvim_buf_is_valid(buf), vim.api.nvim_buf_is_loaded(buf), vim.api.nvim_buf_line_count(buf))
+ -- do
+ -- return
+ -- end
+
+ -- vim.api.nvim_buf_set_lines(buf, 0, 0, true, { 'Test text' })
+ -- local hl_ns = vim.api.nvim_buf_add_highlight(buf, 0, 'Error', 1, 0, 4)
+ -- vim.api.nvim_buf_add_highlight(buf, hl_ns, 'Identifier', 0, 5, -1)
+
+ local row = 15
+
+ local lhs_height = vim.api.nvim_buf_line_count(lhs_buf)
+ local lhs_win = vim.api.nvim_open_win(lhs_buf, true, {
+ relative = 'win',
+ width = (width / 2) - 2,
+ height = lhs_height,
+ col = 0,
+ row = row,
+ border = 'single',
+ style = 'minimal',
+ })
+
+ local rhs_height = vim.api.nvim_buf_line_count(rhs_buf)
+ local rhs_win = vim.api.nvim_open_win(rhs_buf, true, {
+ relative = 'win',
+ width = (width / 2) - 2,
+ height = rhs_height,
+ col = width / 2,
+ row = row,
+ border = 'single',
+ style = 'minimal',
+ })
+ vim.keymap.set('n', 'q', function()
+ vim.api.nvim_buf_delete(lsh_buf, { force = true })
+ vim.api.nvim_win_close(lhs_win, true)
+ vim.api.nvim_win_close(rhs_win, true)
+ end, { buffer = rhs_buf })
+ vim.keymap.set('n', 'q', function()
+ vim.api.nvim_win_close(lhs_win, true)
+ vim.api.nvim_win_close(rhs_win, true)
+ end, { buffer = lhs_buf })
+
+ -- local augroup = vim.api.nvim_create_augroup('diffstastic', {})
+ -- vim.api.nvim_create_autocmd('BufLeave', {
+ -- buffer = lhs_buf,
+ -- group = augroup,
+ -- callback = function()
+ -- vim.api.nvim_win_close(lhs_win, true)
+ -- vim.api.nvim_win_close(rhs_win, true)
+ -- end,
+ -- })
+ -- vim.api.nvim_create_autocmd('BufLeave', {
+ -- buffer = rhs_buf,
+ -- group = augroup,
+ -- callback = function()
+ -- vim.api.nvim_win_close(lhs_win, true)
+ -- vim.api.nvim_win_close(rhs_win, true)
+ -- end,
+ -- })
+
+end
+
+vim.api.nvim_create_user_command('Diff', function()
+
+ local lhs_file = vim.fn.getcwd() .. '/test/before.json'
+ local rhs_file = vim.fn.getcwd() .. '/test/after.json'
+
+ local changes = get_changes()
+ if not changes then
+ return
+ end
+ show_diff('json', lhs_file, rhs_file, changes.chunks)
+end, {})
diff --git a/ftplugin/json.lua b/ftplugin/json.lua
@@ -1 +1,3 @@
vim.cmd('match Comment +\\/\\/.\\+$+')
+
+vim.keymap.set('n', 'gO', '<cmd>Json<cr>')
diff --git a/lua/plugins.lua b/lua/plugins.lua
@@ -36,7 +36,7 @@ return packer.startup({
use 'mbbill/undotree'
use 'camspiers/animate.vim'
use 'nvchad/nvim-colorizer.lua'
- use 'ziontee113/color-picker.nvim'
+ use 'uga-rosa/ccc.nvim'
use 'rainbowhxch/beacon.nvim'
-- use 'Yggdroot/hiPairs'
use 'jandamm/cryoline.nvim'
diff --git a/lua/tms/autocmd.lua b/lua/tms/autocmd.lua
@@ -1,6 +1,25 @@
vim.api.nvim_create_autocmd('BufEnter', { pattern = 'vifm:*', command = 'startinsert' })
vim.api.nvim_create_autocmd('WinEnter', { command = 'setlocal cursorline' })
vim.api.nvim_create_autocmd('WinLeave', { command = 'setlocal nocursorline' })
-vim.api.nvim_create_autocmd('TextYankPost',
- { pattern = '*', callback = function() require('vim.highlight').on_yank() end })
-vim.api.nvim_create_autocmd('ColorScheme', { callback = function() require('tms.u.reload').colors() end })
+vim.api.nvim_create_autocmd('TextYankPost', {
+ pattern = '*',
+ callback = function()
+ require('vim.highlight').on_yank()
+ end,
+})
+vim.api.nvim_create_autocmd('ColorScheme', {
+ callback = function()
+ require('tms.u.reload').colors()
+ end,
+})
+vim.api.nvim_create_autocmd('BufEnter', {
+ pattern = '*',
+ callback = function(opt)
+ local win = vim.fn.bufwinid(opt.buf)
+ local preview = vim.api.nvim_win_get_option(win, 'previewwindow')
+ if preview then
+ vim.opt.scrolloff = 0
+ vim.opt.sidescrolloff = 0
+ end
+ end,
+})
diff --git a/plugin/json_qf.lua b/plugin/json_qf.lua
@@ -0,0 +1,56 @@
+local function has_value(tab, val)
+ for _, value in ipairs(tab) do
+ if value == val then
+ return true
+ end
+ end
+ return false
+end
+
+local function get_key_location(key)
+ return {
+ row = vim.api.nvim_exec([[g/^\s*"]] .. key .. [["/echo line('.')]], true),
+ col = vim.api.nvim_exec([[g/^\s*"]] .. key .. [["/execute "normal! ^" | echo col('.')-1]], true),
+ }
+end
+
+local function populate_qf(type, sort, use_quickfix)
+ local cmd_lines = {}
+ local cur_file = vim.fn.getreg('%')
+
+ local json_types = { 'string', 'number', 'boolean', 'array', 'object', 'null' }
+ if has_value(json_types, type) then
+ for s in vim.fn.system('jq -c \'to_entries[] | if (.value|type == "' .. type .. '") then .key else empty end\' ' ..
+ cur_file):gmatch('[^\r\n]+') do
+ local key = s:gsub('%"', ''):gsub('^%s*(.-)%s*$', '%1'):gsub(',', '')
+ table.insert(cmd_lines, key)
+ end
+ else
+ local get_keys = sort and 'jq \'keys[]\' ' .. cur_file or 'jq \'keys_unsorted[]\' ' .. cur_file
+ for s in vim.fn.system(get_keys):gmatch('[^\r\n]+') do
+ local key = s:gsub('%"', ''):gsub('^%s*(.-)%s*$', '%1'):gsub(',', '')
+ table.insert(cmd_lines, key)
+ end
+ end
+
+ local qf_list = {}
+ for _, v in pairs(cmd_lines) do
+ table.insert(qf_list,
+ { filename = cur_file, lnum = get_key_location(v).row, col = get_key_location(v).col, text = v })
+ end
+
+ if use_quickfix then
+ vim.fn.setqflist(qf_list, ' ')
+ vim.cmd('copen')
+ else
+ vim.fn.setloclist(0, qf_list, ' ')
+ vim.cmd('lopen')
+ end
+end
+
+vim.api.nvim_create_user_command('Json', function()
+ local ft = vim.api.nvim_buf_get_option(0, 'ft')
+ if ft == 'json' then
+ populate_qf(nil, true, false)
+ end
+end, {})
diff --git a/test/after.json b/test/after.json
@@ -0,0 +1,5 @@
+{
+ "foo": [2, 3, 4, 5],
+ "zab": "testing",
+ "woo": ["foobar"]
+}
diff --git a/test/before.json b/test/before.json
@@ -0,0 +1,4 @@
+{
+ "foo": [1, 2, 3, 4],
+ "bar": "testing"
+}
diff --git a/test/test.json b/test/test.json
@@ -0,0 +1,5 @@
+{
+ "foo": [2, 3, 4, 5],
+ "zab": "testing",
+ "woo": ["foobar"]
+}