@@ -50,13 +50,10 @@ local function close(args)
5050end
5151
5252local function close_all ()
53+ -- We can't close only certain windows based on the config because it might have changed.
5354 local render = require (' treesitter-context.render' )
54- if config .multiwindow then
55- for _ , winid in pairs (api .nvim_list_wins ()) do
56- render .close (winid )
57- end
58- else
59- render .close (api .nvim_get_current_win ())
55+ for _ , winid in pairs (api .nvim_list_wins ()) do
56+ render .close (winid )
6057 end
6158end
6259
@@ -133,7 +130,29 @@ local function autocmd(event, callback, opts)
133130 api .nvim_create_autocmd (event , opts )
134131end
135132
133+ --- @param bufnr integer
134+ --- @return boolean ?
135+ local function should_attach (bufnr )
136+ if not config .on_attach or config .on_attach (bufnr ) ~= false then
137+ return true
138+ end
139+ return nil
140+ end
141+
136142function M .enable ()
143+ if enabled then
144+ -- Some options may have changed.
145+ -- We need to reload all contexts and clear autocommands first.
146+ M .disable ()
147+ end
148+
149+ -- Restore attached table after reloading.
150+ for _ , bufnr in pairs (api .nvim_list_bufs ()) do
151+ if api .nvim_buf_is_loaded (bufnr ) then
152+ attached [bufnr ] = should_attach (bufnr )
153+ end
154+ end
155+
137156 local update_events = {
138157 ' WinScrolled' ,
139158 ' BufEnter' ,
@@ -152,10 +171,7 @@ function M.enable()
152171 autocmd (update_events , update )
153172
154173 autocmd (' BufReadPost' , function (args )
155- attached [args .buf ] = nil
156- if not config .on_attach or config .on_attach (args .buf ) ~= false then
157- attached [args .buf ] = true
158- end
174+ attached [args .buf ] = should_attach (args .buf )
159175 end )
160176
161177 autocmd (' BufDelete' , function (args )
@@ -217,6 +233,7 @@ local did_init = false
217233
218234--- @param options ? TSContext.UserConfig
219235function M .setup (options )
236+ -- NB: setup may be called several times.
220237 if options then
221238 config .update (options )
222239 end
0 commit comments