Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions lua/treesitter-context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,10 @@ local function close(args)
end

local function close_all()
-- We can't close only certain windows based on the config because it might have changed.
local render = require('treesitter-context.render')
if config.multiwindow then
for _, winid in pairs(api.nvim_list_wins()) do
render.close(winid)
end
else
render.close(api.nvim_get_current_win())
for _, winid in pairs(api.nvim_list_wins()) do
render.close(winid)
end
end

Expand Down Expand Up @@ -133,7 +130,29 @@ local function autocmd(event, callback, opts)
api.nvim_create_autocmd(event, opts)
end

--- @param bufnr integer
--- @return boolean?
local function should_attach(bufnr)
if not config.on_attach or config.on_attach(bufnr) ~= false then
return true
end
return nil
end

function M.enable()
if enabled then
-- Some options may have changed.
-- We need to reload all contexts and clear autocommands first.
M.disable()
end

-- Restore attached table after reloading.
for _, bufnr in pairs(api.nvim_list_bufs()) do
if api.nvim_buf_is_loaded(bufnr) then
attached[bufnr] = should_attach(bufnr)
end
end

local update_events = {
'WinScrolled',
'BufEnter',
Expand All @@ -152,10 +171,7 @@ function M.enable()
autocmd(update_events, update)

autocmd('BufReadPost', function(args)
attached[args.buf] = nil
if not config.on_attach or config.on_attach(args.buf) ~= false then
attached[args.buf] = true
end
attached[args.buf] = should_attach(args.buf)
end)

autocmd('BufDelete', function(args)
Expand Down Expand Up @@ -217,6 +233,7 @@ local did_init = false

---@param options? TSContext.UserConfig
function M.setup(options)
-- NB: setup may be called several times.
if options then
config.update(options)
end
Expand Down