Skip to content

Commit

Permalink
vim: Make sure treesitter updates parse tree when text changes (#32)
Browse files Browse the repository at this point in the history
In the current implementation of treesitter, the parse tree for the
current buffer would not be automatically updated/refreshed when
text changes, unless treesitter-highlight module is enabled.
However, I don't want to use treesitter's highlight/syntax feature yet
due to some issues and conflicts.

To make the parse tree up-to-date as text changes, parser:parse()
needs to be explicitly called in the absence of treesitter-highlight.

Ref: nvim-treesitter/nvim-treesitter#2492
  • Loading branch information
wookayin committed Feb 9, 2022
1 parent f9b36e3 commit ec2f252
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion nvim/lua/config/treesitter.lua
@@ -1,7 +1,10 @@
-- Treesitter config
-- https://github.com/nvim-treesitter/nvim-treesitter

require'nvim-treesitter.configs'.setup {
local ts_configs = require("nvim-treesitter.configs")
local ts_parsers = require("nvim-treesitter.parsers")

ts_configs.setup {
-- one of "all", "maintained" (parsers with maintainers), or a list of languages
ensure_installed = "maintained",

Expand All @@ -10,6 +13,23 @@ require'nvim-treesitter.configs'.setup {
"phpdoc", -- Not compatible with M1 mac
},

highlight = {
-- TreeSitter's highlight/syntax support is yet experimental and has some issues.
-- It overrides legacy filetype-based vim syntax, and colorscheme needs to be treesitter-aware.
enable = false, -- TODO: Enable again when it becomes mature and usable enough.

-- List of language that will be disabled.
-- For example, some non-programming-language filetypes (e.g., fzf) should be
-- explicitly turned off otherwise it will slow down the window.
disable = { "fzf", "GV", "gitmessengerpopup", "fugitive", "NvimTree" },

-- Setting this to true will run `:h syntax` and tree-sitter at the same time.
-- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
-- Using this option may slow down your editor, and you may see some duplicate highlights.
-- Instead of true it can also be a list of languages
additional_vim_regex_highlighting = { "python" },
},

playground = {
enable = true,
updatetime = 30,
Expand All @@ -28,6 +48,40 @@ require'nvim-treesitter.configs'.setup {
},
}

-- 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
return parser:parse()
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


-- Folding support
vim.o.foldmethod = 'expr'
vim.o.foldexpr = 'nvim_treesitter#foldexpr()'
Expand Down

1 comment on commit ec2f252

@wookayin
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.