Skip to content

Instantly share code, notes, and snippets.

@wookayin
Created February 9, 2022 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wookayin/9c665e210e33ff58c25dbfd02b4c4e02 to your computer and use it in GitHub Desktop.
Save wookayin/9c665e210e33ff58c25dbfd02b4c4e02 to your computer and use it in GitHub Desktop.
-- Make sure TS syntax tree is updated when needed by plugin (with some throttling)
-- even if the `highlight` module is not enabled.
-- See https://github.com/nvim-treesitter/nvim-treesitter/issues/2492
_G.TreesitterParse = function()
local lang = ts_parsers.ft_to_lang(vim.bo.filetype)
local parser = ts_parsers.get_parser(vim.fn.bufnr(), lang)
if parser then
parser:parse()
return true
else
return false
end
end
local function throttle(fn, ms)
local timer = vim.loop.new_timer()
local running = false
return function(...)
if not running then
timer:start(ms, 0, function() running = false end)
running = true
pcall(vim.schedule_wrap(fn), select(1, ...))
end
end
end
if not ts_configs.get_module('highlight').enable then
_G.TreesitterParseDebounce = throttle(_G.TreesitterParse, 100) -- 100 ms
vim.cmd [[
augroup TreesitterUpdateParsing
autocmd!
autocmd TextChanged,TextChangedI * call v:lua.TreesitterParseDebounce()
augroup END
]]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment