@@ -123,12 +123,10 @@ function IndentMod:render(range, opts)
123123 local conf = self .conf
124124
125125 if not opts .lazy then
126- self :clear (range )
127- for i = range .start , range .finish do
128- indent_cache :clear (bufnr , i )
129- pos2id :clear (bufnr , i )
130- pos2info :clear (bufnr , i )
131- end
126+ self :clear ({ bufnr = bufnr , start = 0 , finish = api .nvim_buf_line_count (bufnr ) - 1 })
127+ indent_cache :clear (bufnr )
128+ pos2id :clear (bufnr )
129+ pos2info :clear (bufnr )
132130 end
133131
134132 local narrowed_range = narrowRange (range )
@@ -159,14 +157,14 @@ function IndentMod:render(range, opts)
159157 self :setmark (bufnr , render_info )
160158end
161159
162- function IndentMod :createAutocmd ()
163- BaseMod .createAutocmd (self )
164- local render_cb = function (event , opts )
160+ function IndentMod :createRenderCallback ()
161+ return function (event , opts )
165162 opts = opts or { lazy = false }
166163 local bufnr = event .buf
167164 if not self :shouldRender (bufnr ) then
168165 return
169166 end
167+
170168 local wins = fn .win_findbuf (bufnr ) or {}
171169 for _ , winid in ipairs (wins ) do
172170 local win_bufnr = api .nvim_win_get_buf (winid )
@@ -185,33 +183,40 @@ function IndentMod:createAutocmd()
185183 end )
186184 end
187185 end
188- local throttle_render_cb = throttle (render_cb , self .conf .delay )
189- local throttle_render_cb_with_pre_hook = function (event , opts )
186+ end
187+
188+ function IndentMod :createThrottledCallback (callback )
189+ local throttledCallback = throttle (callback , self .conf .delay )
190+ return function (event , opts )
190191 opts = opts or { lazy = false }
191192 local bufnr = event .buf
192193 if not (api .nvim_buf_is_valid (bufnr ) and self :shouldRender (bufnr )) then
193194 return
194195 end
195- throttle_render_cb (event , opts )
196+ throttledCallback (event , opts )
196197 end
198+ end
197199
198- api .nvim_create_autocmd ({ " WinScrolled" }, {
199- group = self .meta .augroup_name ,
200- callback = function (e )
201- throttle_render_cb_with_pre_hook (e , { lazy = true })
202- end ,
203- })
204- api .nvim_create_autocmd ({ " TextChanged" , " TextChangedI" , " BufWinEnter" }, {
205- group = self .meta .augroup_name ,
206- callback = function (e )
207- throttle_render_cb_with_pre_hook (e , { lazy = false })
208- end ,
209- })
210- api .nvim_create_autocmd ({ " OptionSet" }, {
211- group = self .meta .augroup_name ,
212- pattern = " list,listchars,shiftwidth,tabstop,expandtab" ,
213- callback = throttle_render_cb_with_pre_hook ,
214- })
200+ function IndentMod :createAutocmd ()
201+ BaseMod .createAutocmd (self )
202+ local renderCallback = self :createRenderCallback ()
203+ local throttledCallback = self :createThrottledCallback (renderCallback )
204+
205+ local autocommands = {
206+ { events = { " WinScrolled" }, opts = { lazy = true } },
207+ { events = { " TextChanged" , " TextChangedI" , " BufWinEnter" }, opts = { lazy = false } },
208+ { events = { " OptionSet" }, pattern = " list,listchars,shiftwidth,tabstop,expandtab" , opts = {} },
209+ }
210+
211+ for _ , cmd in ipairs (autocommands ) do
212+ api .nvim_create_autocmd (cmd .events , {
213+ group = self .meta .augroup_name ,
214+ pattern = cmd .pattern ,
215+ callback = function (e )
216+ throttledCallback (e , cmd .opts )
217+ end ,
218+ })
219+ end
215220end
216221
217222return IndentMod
0 commit comments