Skip to content

Commit ec2f252

Browse files
committed
vim: Make sure treesitter updates parse tree when text changes (#32)
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
1 parent f9b36e3 commit ec2f252

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

nvim/lua/config/treesitter.lua

+55-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
-- Treesitter config
22
-- https://github.com/nvim-treesitter/nvim-treesitter
33

4-
require'nvim-treesitter.configs'.setup {
4+
local ts_configs = require("nvim-treesitter.configs")
5+
local ts_parsers = require("nvim-treesitter.parsers")
6+
7+
ts_configs.setup {
58
-- one of "all", "maintained" (parsers with maintainers), or a list of languages
69
ensure_installed = "maintained",
710

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

16+
highlight = {
17+
-- TreeSitter's highlight/syntax support is yet experimental and has some issues.
18+
-- It overrides legacy filetype-based vim syntax, and colorscheme needs to be treesitter-aware.
19+
enable = false, -- TODO: Enable again when it becomes mature and usable enough.
20+
21+
-- List of language that will be disabled.
22+
-- For example, some non-programming-language filetypes (e.g., fzf) should be
23+
-- explicitly turned off otherwise it will slow down the window.
24+
disable = { "fzf", "GV", "gitmessengerpopup", "fugitive", "NvimTree" },
25+
26+
-- Setting this to true will run `:h syntax` and tree-sitter at the same time.
27+
-- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
28+
-- Using this option may slow down your editor, and you may see some duplicate highlights.
29+
-- Instead of true it can also be a list of languages
30+
additional_vim_regex_highlighting = { "python" },
31+
},
32+
1333
playground = {
1434
enable = true,
1535
updatetime = 30,
@@ -28,6 +48,40 @@ require'nvim-treesitter.configs'.setup {
2848
},
2949
}
3050

51+
-- Make sure TS syntax tree is updated when needed by plugin (with some throttling)
52+
-- even if the `highlight` module is not enabled.
53+
-- See https://github.com/nvim-treesitter/nvim-treesitter/issues/2492
54+
_G.TreesitterParse = function()
55+
local lang = ts_parsers.ft_to_lang(vim.bo.filetype)
56+
local parser = ts_parsers.get_parser(vim.fn.bufnr(), lang)
57+
if parser then
58+
return parser:parse()
59+
else
60+
return false
61+
end
62+
end
63+
local function throttle(fn, ms)
64+
local timer = vim.loop.new_timer()
65+
local running = false
66+
return function(...)
67+
if not running then
68+
timer:start(ms, 0, function() running = false end)
69+
running = true
70+
pcall(vim.schedule_wrap(fn), select(1, ...))
71+
end
72+
end
73+
end
74+
if not ts_configs.get_module('highlight').enable then
75+
_G.TreesitterParseDebounce = throttle(_G.TreesitterParse, 100) -- 100 ms
76+
vim.cmd [[
77+
augroup TreesitterUpdateParsing
78+
autocmd!
79+
autocmd TextChanged,TextChangedI * call v:lua.TreesitterParseDebounce()
80+
augroup END
81+
]]
82+
end
83+
84+
3185
-- Folding support
3286
vim.o.foldmethod = 'expr'
3387
vim.o.foldexpr = 'nvim_treesitter#foldexpr()'

0 commit comments

Comments
 (0)