Skip to content

Instantly share code, notes, and snippets.

@Kurama622
Created March 31, 2026 13:31
Show Gist options
  • Select an option

  • Save Kurama622/f9090d22cd29459bc8fb23f9ae75f9fe to your computer and use it in GitHub Desktop.

Select an option

Save Kurama622/f9090d22cd29459bc8fb23f9ae75f9fe to your computer and use it in GitHub Desktop.
treesitter context
vim.keymap.set("n", "<C-g>", function()
local hl_keywords = {
["class"] = true,
["function"] = true,
["namespace"] = true,
["method"] = true,
["type"] = true,
["enum"] = true,
["field"] = true,
}
local node = vim.treesitter.get_node()
local sep_list = {
cpp = "::",
c = "::",
}
local tbl = {}
while node do
local t = node:type()
-- declarator|definition|specifier: void print(const char*) { ... }
if t:find("declaration$") or t:find("definition$") or t:find("specifier$") then
local word = t:match("(%w+)_")
local hl_type = hl_keywords[word] and word or "Conceal"
-- signature: print(const char*)
for signature_node in node:iter_children() do
local signature_type = signature_node:type()
if
(signature_type:find("declarator$") or signature_type:find("identifier$"))
and signature_type ~= "qualified_identifier"
then
hl_type = hl_keywords[hl_type] and hl_type or signature_type:match("(%w+)_")
-- name: print
for name_node in signature_node:iter_children() do
local name_type = name_node:type()
if name_type:find("identifier$") or name_type == "destructor_name" then
hl_type = hl_keywords[hl_type] and hl_type or name_type:match("(%w+)_")
if hl_keywords[hl_type] then
table.insert(tbl, 1, "%#@lsp.type." .. hl_type .. "#" .. vim.treesitter.get_node_text(name_node, 0))
hl_type = "Conceal"
end
end
end
if hl_keywords[hl_type] then
table.insert(tbl, 1, "%#@lsp.type." .. hl_type .. "#" .. vim.treesitter.get_node_text(signature_node, 0))
hl_type = "Conceal"
end
end
end
end
node = node:parent()
end
if not vim.tbl_isempty(tbl) then
vim.o.statusline = " %=%##(" .. table.concat(tbl, "%#Conceal#" .. (sep_list[vim.o.ft] or "->")) .. "%##)%= "
end
end, { desc = "Show Treesitter Context" })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment