@@ -128,25 +128,19 @@ function Preview:preview(bufnr, start_pos, end_pos)
128128 return
129129 end
130130
131- if bufnr ~= self .bufnr then
132- self :setBuffer (bufnr )
133- end
134-
135- self :clearHighlight ()
131+ self :setBuffer (bufnr )
136132
137- self .bufnr = bufnr
138133 self .start_pos = start_pos
139134 self .end_pos = end_pos
140135
141136 self :reveal ()
142- self :highlight ()
137+ self :highlight_preview_range ()
143138end
144139
145140--- Reverts the preview and inactivates it, restoring the preview window to its previous state.
146141function Preview :revert ()
147142 self .active = false
148143 self :unsubscribe ()
149- self :clearHighlight ()
150144
151145 if not renderer .is_window_valid (self .winid ) then
152146 self .winid = nil
@@ -173,7 +167,6 @@ function Preview:revert()
173167 return
174168 end
175169 self :setBuffer (bufnr )
176- self .bufnr = bufnr
177170 if vim .api .nvim_win_is_valid (self .winid ) then
178171 vim .api .nvim_win_call (self .winid , function ()
179172 vim .fn .winrestview (self .truth .view )
@@ -254,6 +247,7 @@ function Preview:activate()
254247 return
255248 end
256249 if self .config .use_float then
250+ self .bufnr = vim .api .nvim_create_buf (false , true )
257251 self .truth = {}
258252 else
259253 self .truth = {
@@ -286,7 +280,6 @@ local function try_load_image_nvim_buf(winid, bufnr)
286280 end
287281 log .warn (" image.nvim was not setup. Calling require('image').setup()." )
288282 image .setup ()
289- image_augroup = vim .api .nvim_create_augroup (" image.nvim" , { clear = false })
290283 end
291284
292285 vim .opt .eventignore :remove (" BufWinEnter" )
@@ -304,19 +297,64 @@ local function try_load_image_nvim_buf(winid, bufnr)
304297 return true
305298end
306299
300+ --- @param bufnr number The buffer number of the buffer to set.
301+ --- @return number bytecount The number of bytes in the buffer
302+ local get_bufsize = function (bufnr )
303+ return vim .api .nvim_buf_call (bufnr , function ()
304+ return vim .fn .line2byte (vim .fn .line (" $" ) + 1 )
305+ end )
306+ end
307+
307308--- Set the buffer in the preview window without executing BufEnter or BufWinEnter autocommands.
308- -- @param bufnr number The buffer number of the buffer to set.
309+ --- @param bufnr number The buffer number of the buffer to set.
309310function Preview :setBuffer (bufnr )
311+ self :clearHighlight ()
312+ if bufnr == self .bufnr then
313+ return
314+ end
310315 local eventignore = vim .opt .eventignore
311316 vim .opt .eventignore :append (" BufEnter,BufWinEnter" )
312- vim .api .nvim_win_set_buf (self .winid , bufnr )
313- if self .config .use_image_nvim then
317+
318+ if self .config .use_image_nvim and try_load_image_nvim_buf (self .winid , bufnr ) then
319+ -- calling the try method twice should be okay here, image.nvim should cache the image and displaying the image takes
320+ -- really long anyways
321+ vim .api .nvim_win_set_buf (self .winid , bufnr )
314322 try_load_image_nvim_buf (self .winid , bufnr )
323+ goto finally
315324 end
325+
316326 if self .config .use_float then
317- -- I'm not sufe why float windows won;t show numbers without this
318- vim .api .nvim_win_set_option (self .winid , " number" , true )
327+ -- Workaround until https://github.com/neovim/neovim/issues/24973 is resolved or maybe 'previewpopup' comes in?
328+ vim .fn .bufload (bufnr )
329+ local lines = vim .api .nvim_buf_get_lines (bufnr , 0 , - 1 , false )
330+ vim .api .nvim_buf_set_lines (self .bufnr , 0 , - 1 , false , lines )
331+ vim .api .nvim_win_set_buf (self .winid , self .bufnr )
332+ -- I'm not sure why float windows won't show numbers without this
333+ vim .wo [self .winid ].number = true
334+
335+ -- code below is from mini.pick
336+ -- only starts treesitter parser if the filetype is matching
337+ local ft = vim .bo [bufnr ].filetype
338+ local bufsize = get_bufsize (bufnr )
339+ if bufsize > 1024 * 1024 or bufsize > 1000 * # lines then
340+ goto finally
341+ end
342+ local has_lang , lang = pcall (vim .treesitter .language .get_lang , ft )
343+ lang = has_lang and lang or ft
344+ local has_parser , parser = pcall (vim .treesitter .get_parser , self .bufnr , lang , { error = false })
345+ has_parser = has_parser and parser ~= nil
346+ if has_parser then
347+ has_parser = pcall (vim .treesitter .start , self .bufnr , lang )
348+ end
349+ if not has_parser then
350+ vim .bo [self .bufnr ].syntax = ft
351+ end
352+ else
353+ vim .api .nvim_win_set_buf (self .winid , bufnr )
354+ self .bufnr = bufnr
319355 end
356+
357+ :: finally::
320358 vim .opt .eventignore = eventignore
321359end
322360
@@ -333,7 +371,7 @@ function Preview:reveal()
333371end
334372
335373--- Highlight the previewed range
336- function Preview :highlight ()
374+ function Preview :highlight_preview_range ()
337375 if not self .active or not self .bufnr then
338376 return
339377 end
0 commit comments