Last active
May 5, 2025 13:53
-
-
Save pmouraguedes/c0d51609fbaf1db46c9ad945fe716e6f to your computer and use it in GitHub Desktop.
tree-sitter sql traversing in neovim
This file contains hidden or 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 bufnr = vim.api.nvim_get_current_buf() | |
local ok, parser = pcall(vim.treesitter.get_parser, bufnr, "sql") | |
if not ok or not parser then | |
vim.notify("Failed to get SQL parser", vim.log.levels.ERROR) | |
return | |
end | |
local tree = parser:parse()[1] | |
if not tree then | |
vim.notify("No parse tree available", vim.log.levels.WARN) | |
return | |
end | |
local root = tree:root() | |
if not root then | |
vim.notify("No root node in parse tree", vim.log.levels.WARN) | |
return | |
end | |
-- Iterate over statement nodes | |
for _, statement_node in root:iter_children() do | |
if get_node_type(statement_node) == "statement" then | |
for child in statement_node:iter_children() do | |
if get_node_type(child) == "insert" then | |
add_ghost_text_for_insert(child, bufnr) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment