Skip to content

Instantly share code, notes, and snippets.

@melMass
Last active February 2, 2025 15:15
Show Gist options
  • Save melMass/48f469b79fd0b940884b374a57bbf25a to your computer and use it in GitHub Desktop.
Save melMass/48f469b79fd0b940884b374a57bbf25a to your computer and use it in GitHub Desktop.
Helper to automatically open `InspectTree`
auto-inspect.mp4

Usage

I usually put this in a local config of my tree-sitter grammars (./.nvim.lua, requires vim.opt.exrc = true, see :h exrc)

For instance:

  • .nvim.lua
local aui = require("auto_inspect")
aui.install({
	-- file = vim.fn.getcwd() .. "/sample.nk",
	pattern = "*.nk",
	-- inspector_name = "Nuke Inspector",
	-- lang = "nuke",
	-- logger_name = "nuke-inspector",
	-- group_name = "TSTmuxInspectorGroup",
	-- desc = "Automatically inspect tree on entering sample.nk",
})
local M = {}
---@class TreeInspectorOptions
---@field file string|nil # The path to the file to inspect.
---@field pattern string|nil # A Vim file pattern (e.g., "*.nk") for dynamic matching.
---@field inspector_name string|nil # The name of the inspector buffer (default: "TreeInspector").
---@field lang string|nil # The language for the tree-sitter parser (default: nil / infered).
---@field width number|nil # Width of the inspector window (default: 30% of editor width).
---@field height number|nil # Height of the inspector window (default: full height minus two lines).
---@field vertical boolean|nil # Whether to use a vertical split (default: true).
---@field style string|nil # Style for the inspector window (default: "minimal").
---@field title? string|fun(bufnr:integer):string|nil # Custom title function for the inspector window.
---@field logger_name string|nil # Name of the logger (default: "tree-inspector").
---@field group_name string|nil # Name of the autocommand group for setup_auto_inspect.
---@field desc string|nil # Description for the autocommand (default: "Automatically run InspectTree").
---@param opts TreeInspectorOptions
M.open_tree_inspector = function(opts)
-- local Logger = require('mtb.utils.log')
-- local logger = Logger.new(opts.logger_name or 'tree-inspector')
local file = opts.file
if not file then
-- logger:error("'file' must be specified for tree inspection.")
return
end
local inspector_base_name = opts.inspector_name or 'TreeInspector'
local unique_name = inspector_base_name .. '-' .. vim.fn.fnamemodify(file, ':t')
local lang = opts.lang
-- logger:info('Opening tree inspector for ' .. file)
local file_bufnr = vim.fn.bufnr(file)
if file_bufnr == -1 then
-- logger:warn('Sample file buffer not loaded, nothing to inspect.')
return
end
local file_winid = vim.fn.bufwinid(file_bufnr)
-- logger:info('File bufnr: ' .. file_bufnr)
local inspector_bufnr = vim.fn.bufnr(unique_name)
if inspector_bufnr == -1 then
-- logger:info('Creating new inspector buffer: ' .. unique_name)
inspector_bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(inspector_bufnr, unique_name)
end
local inspector_winid = vim.fn.bufwinid(inspector_bufnr)
if inspector_winid == -1 then
-- logger:info('Creating a new inspector window')
inspector_winid = vim.api.nvim_open_win(inspector_bufnr, false, {
win = file_winid,
width = opts.width or math.floor(vim.o.columns * 0.3),
height = opts.height or vim.o.lines - 2,
vertical = opts.vertical or true,
style = opts.style or 'minimal',
})
end
-- logger:info('Inspector winid: ' .. inspector_winid)
-- Open the tree inspector
vim.treesitter.inspect_tree({
bufnr = inspector_bufnr,
lang = lang,
winid = inspector_winid,
title = opts.title or function(buf)
return 'Tree Inspector for: ' .. vim.fn.bufname(buf)
end,
})
end
---@param opts TreeInspectorOptions
M.install = function(opts)
-- local Logger = require('mtb.utils.log')
-- local logger = Logger.new(opts.logger_name or 'tree-inspector')
if not opts.file and not opts.pattern then
error("setup_auto_inspect: Either 'file' or 'pattern' is required.")
end
local group_name = opts.group_name or 'TreeInspectorGroup'
local group = vim.api.nvim_create_augroup(group_name, { clear = true })
local pattern = opts.pattern or opts.file
local inspector_base_name = opts.inspector_name or 'TreeInspector'
vim.api.nvim_create_autocmd('BufEnter', {
group = group,
pattern = pattern,
callback = function()
local matched_file = vim.fn.expand('<afile>')
local unique_name = inspector_base_name .. '-' .. vim.fn.fnamemodify(matched_file, ':t')
-- logger:info('Matched file for pattern: ' .. matched_file)
local dynamic_opts = vim.tbl_extend('force', opts, {
file = matched_file,
inspector_name = unique_name,
})
M.open_tree_inspector(dynamic_opts)
end,
desc = opts.desc or 'Automatically run InspectTree',
})
end
return M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment