Skip to content

Instantly share code, notes, and snippets.

@thraizz
Created July 5, 2022 08:27
Show Gist options
  • Save thraizz/e845e3e40addf944bd93de50bef776d0 to your computer and use it in GitHub Desktop.
Save thraizz/e845e3e40addf944bd93de50bef776d0 to your computer and use it in GitHub Desktop.
NVIM: Copy cursor-location's path in json with treesitter
local copyJSONPath = function()
local ts_utils = require 'nvim-treesitter.ts_utils'
local result = {};
local node = ts_utils.get_node_at_cursor()
while node do
if tostring(node) == '<node pair>' then
local key_node = node:named_child(0):named_child(0)
table.insert(result, 1, ts_utils.get_node_text(key_node)[1])
end
node = node:parent()
end
vim.fn.setreg('"', vim.fn.join(result, '.'))
end
vim.keymap.set('n', '<leader>cp', copyJSONPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment