3939
4040local attached = {} --- @type table<integer,true>
4141
42- local function close (args )
43- local render = require (' treesitter-context.render' )
44- if args .event == " WinClosed" then
45- -- Closing current window instead of intended window may lead to context window flickering.
46- render .close (tonumber (args .match ))
47- else
48- render .close (api .nvim_get_current_win ())
49- end
50- end
51-
52- local function close_all ()
53- 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 ())
60- end
42+ local function close ()
43+ require (' treesitter-context.render' ).close ()
6144end
6245
6346--- @param bufnr integer
@@ -67,29 +50,30 @@ local function cannot_open(bufnr, winid)
6750 or vim .bo [bufnr ].filetype == ' '
6851 or vim .bo [bufnr ].buftype ~= ' '
6952 or vim .wo [winid ].previewwindow
53+ or vim .fn .getcmdtype () ~= ' '
7054 or api .nvim_win_get_height (winid ) < config .min_window_height
7155end
7256
7357--- @param winid integer
7458local update_single_context = throttle_by_id (function (winid )
7559 -- Since the update is performed asynchronously, the window may be closed at this moment.
7660 -- Therefore, we need to check if it is still valid.
77- if not api .nvim_win_is_valid (winid ) or vim . fn . getcmdtype () ~= ' ' then
61+ if not api .nvim_win_is_valid (winid ) then
7862 return
7963 end
8064
8165 local bufnr = api .nvim_win_get_buf (winid )
8266
83- if cannot_open (bufnr , winid ) or not config . multiwindow and winid ~= api . nvim_get_current_win () then
84- require ( ' treesitter-context.render ' ). close (winid )
67+ if cannot_open (bufnr , winid ) then
68+ close ()
8569 return
8670 end
8771
8872 local context_ranges , context_lines = require (' treesitter-context.context' ).get (bufnr , winid )
8973 all_contexts [bufnr ] = context_ranges
9074
9175 if not context_ranges or # context_ranges == 0 then
92- require ( ' treesitter-context.render ' ). close (winid )
76+ close ()
9377 return
9478 end
9579
@@ -98,23 +82,8 @@ local update_single_context = throttle_by_id(function(winid)
9882 require (' treesitter-context.render' ).open (bufnr , winid , context_ranges , context_lines )
9983end )
10084
101- --- @param args table
102- local function update (args )
103- if args .event == " OptionSet" and args .match ~= ' number' and args .match ~= ' relativenumber' then
104- return
105- end
106-
107- local multiwindow_events = { " WinResized" , " User" }
108-
109- if config .multiwindow and vim .tbl_contains (multiwindow_events , args .event ) then
110- -- Resizing a single window may cause many resizes in different windows,
111- -- so it is necessary to iterate over all windows when a WinResized event is received.
112- for _ , winid in pairs (api .nvim_list_wins ()) do
113- update_single_context (winid )
114- end
115- else
116- update_single_context (api .nvim_get_current_win ())
117- end
85+ local function update ()
86+ update_single_context (api .nvim_get_current_win ())
11887end
11988
12089local M = {
@@ -134,22 +103,7 @@ local function autocmd(event, callback, opts)
134103end
135104
136105function M .enable ()
137- local update_events = {
138- ' WinScrolled' ,
139- ' BufEnter' ,
140- ' WinEnter' ,
141- ' VimResized' ,
142- ' DiagnosticChanged' ,
143- ' CursorMoved' ,
144- ' OptionSet' ,
145- }
146-
147- if config .multiwindow then
148- table.insert (update_events , ' WinResized' )
149- table.insert (update_events , ' WinLeave' )
150- end
151-
152- autocmd (update_events , update )
106+ autocmd ({ ' WinScrolled' , ' BufEnter' , ' WinEnter' , ' VimResized' , ' DiagnosticChanged' }, update )
153107
154108 autocmd (' BufReadPost' , function (args )
155109 attached [args .buf ] = nil
@@ -162,30 +116,27 @@ function M.enable()
162116 attached [args .buf ] = nil
163117 end )
164118
165- if config .multiwindow then
166- autocmd ({ ' WinClosed' }, close )
167- else
168- autocmd ({ ' BufLeave' , ' WinLeave' , ' WinClosed' }, close )
169- end
119+ autocmd (' CursorMoved' , update )
120+
121+ autocmd (' OptionSet' , function (args )
122+ if args .match == ' number' or args .match == ' relativenumber' then
123+ update ()
124+ end
125+ end )
126+
127+ autocmd ({ ' BufLeave' , ' WinLeave' }, close )
170128
171129 autocmd (' User' , close , { pattern = ' SessionSavePre' })
172130 autocmd (' User' , update , { pattern = ' SessionSavePost' })
173131
174- if config .multiwindow then
175- for _ , winid in pairs (api .nvim_list_wins ()) do
176- update_single_context (winid )
177- end
178- else
179- update_single_context (api .nvim_get_current_win ())
180- end
181-
132+ update ()
182133 enabled = true
183134end
184135
185136function M .disable ()
186137 augroup (' treesitter_context_update' , {})
187138 attached = {}
188- close_all ()
139+ close ()
189140 enabled = false
190141end
191142
0 commit comments