@@ -875,12 +875,29 @@ function Previewer.base:update_ts_context()
875875 then
876876 return
877877 end
878- TSContext .update (self .win .preview_winid , self .preview_bufnr , vim .tbl_extend (" force" ,
879- type (self .treesitter .context ) == " table" and self .treesitter .context or {}, {
880- -- `zindex` and `multiwindow` must be set regardless of user options
881- multiwindow = true ,
882- zindex = self .win .winopts .zindex + 20
883- }))
878+ -- HACK: since TS async parsing commit we cannot guarantee the TSContext ranges as these will
879+ -- return empty unless parsing is complete and we have no access to the `on_parse` event
880+ -- https://github.com/neovim/neovim/commit/45e606b1fddbfeee8fe28385b5371ca6f2fba71b
881+ -- For more info see #1922
882+ local lang = vim .treesitter .language .get_lang (vim .b [self .preview_bufnr ]._ft )
883+ local parser = vim .treesitter .get_parser (self .preview_bufnr , lang )
884+ local context_updated
885+ for _ , t in ipairs ({ 0 , 20 , 50 , 100 }) do
886+ vim .defer_fn (function ()
887+ if context_updated or not vim .api .nvim_buf_is_valid (self .preview_bufnr ) then
888+ return
889+ end
890+ if parser :is_valid (true ) then
891+ context_updated = true
892+ TSContext .update (self .win .preview_winid , self .preview_bufnr , vim .tbl_extend (" force" ,
893+ type (self .treesitter .context ) == " table" and self .treesitter .context or {}, {
894+ -- `zindex` and `multiwindow` must be set regardless of user options
895+ multiwindow = true ,
896+ zindex = self .win .winopts .zindex + 20
897+ }))
898+ end
899+ end , t )
900+ end
884901end
885902
886903function Previewer .base :update_render_markdown ()
0 commit comments