Skip to content

Instantly share code, notes, and snippets.

@cuducos
Last active June 9, 2021 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cuducos/f3c4d24ca705d875a1157a6c57190307 to your computer and use it in GitHub Desktop.
Save cuducos/f3c4d24ca705d875a1157a6c57190307 to your computer and use it in GitHub Desktop.
worldcup:
host: Brazil
semifinal:
- brazil: 1
germany: 7
- netherlands: 0
argentina: 0
local ts_utils = require("nvim-treesitter.ts_utils")
local function reverse(keys)
local n = #keys
local i = 1
while i < n do
keys[i], keys[n] = keys[n], keys[i]
i = i + 1
n = n - 1
end
end
local function get_value(node, bufnr)
while node ~= nil do
if node:type() == "block_mapping_pair" then
return ts_utils.get_node_text(node:field("value")[1], bufnr)[1]
end
node = node:parent()
end
end
local function get_keys(node, bufnr)
local keys = {}
while node ~= nil do
if node:type() == "block_mapping_pair" then
table.insert(keys, ts_utils.get_node_text(node:field("key")[1], bufnr)[1])
end
node = node:parent()
end
reverse(keys)
return table.concat(keys, ".")
end
_G.magic = function()
local bufnr = vim.api.nvim_get_current_buf()
local node = ts_utils.get_node_at_cursor(nil)
print(string.format("%s = %s", get_keys(node, bufnr), get_value(node, bufnr)))
end
vim.api.nvim_set_keymap("n", "<Leader>y", "<Cmd>lua _G.magic()<CR>", {})
vim.api.nvim_set_keymap("n", "<Leader>ll", "<Cmd>luafile %<CR><C-w>h", {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment