Created
July 5, 2022 08:27
-
-
Save thraizz/e845e3e40addf944bd93de50bef776d0 to your computer and use it in GitHub Desktop.
NVIM: Copy cursor-location's path in json with treesitter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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