diff options
author | Thomas Vigouroux <tomvig38@gmail.com> | 2020-09-11 15:57:08 +0200 |
---|---|---|
committer | Björn Linse <bjorn.linse@gmail.com> | 2020-09-13 07:46:39 +0200 |
commit | e4b5efa51e5320f4b0bc2c74409391e76b30336f (patch) | |
tree | b2bdd4d402066aa2d994c13fe9d7e5a15bedc0f3 /runtime/lua/vim/treesitter/highlighter.lua | |
parent | 18a3a8982288fc41fd902e412d624071e79fc627 (diff) | |
download | rneovim-e4b5efa51e5320f4b0bc2c74409391e76b30336f.tar.gz rneovim-e4b5efa51e5320f4b0bc2c74409391e76b30336f.tar.bz2 rneovim-e4b5efa51e5320f4b0bc2c74409391e76b30336f.zip |
fix: use luahl in treesitter
Diffstat (limited to 'runtime/lua/vim/treesitter/highlighter.lua')
-rw-r--r-- | runtime/lua/vim/treesitter/highlighter.lua | 60 |
1 files changed, 42 insertions, 18 deletions
diff --git a/runtime/lua/vim/treesitter/highlighter.lua b/runtime/lua/vim/treesitter/highlighter.lua index 718088e0ad..772016ee57 100644 --- a/runtime/lua/vim/treesitter/highlighter.lua +++ b/runtime/lua/vim/treesitter/highlighter.lua @@ -3,7 +3,6 @@ local a = vim.api -- support reload for quick experimentation local TSHighlighter = rawget(vim.treesitter, 'TSHighlighter') or {} TSHighlighter.__index = TSHighlighter -local ts_hs_ns = a.nvim_create_namespace("treesitter_hl") -- These are conventions defined by tree-sitter, though it -- needs to be user extensible also. @@ -69,8 +68,15 @@ function TSHighlighter.new(query, bufnr, ft) self.edit_count = 0 self.redraw_count = 0 self.line_count = {} + self.root = self.parser:parse():root() a.nvim_buf_set_option(self.buf, "syntax", "") + a.nvim__buf_set_luahl(self.buf, { + on_start=function(...) return self:on_start(...) end, + on_window=function(...) return self:on_window(...) end, + on_line=function(...) return self:on_line(...) end, + }) + -- Tricky: if syntax hasn't been enabled, we need to reload color scheme -- but use synload.vim rather than syntax.vim to not enable -- syntax FileType autocmds. Later on we should integrate with the @@ -100,6 +106,12 @@ function TSHighlighter:get_hl_from_capture(capture) end end +function TSHighlighter:on_changedtree(changes) + for _, ch in ipairs(changes or {}) do + a.nvim__buf_redraw_range(self.buf, ch[1], ch[3]+1) + end +end + function TSHighlighter:set_query(query) if type(query) == "string" then query = vim.treesitter.parse_query(self.parser.lang, query) @@ -123,26 +135,38 @@ function TSHighlighter:set_query(query) end }) - self:on_changedtree({{self.parser:parse():root():range()}}) + a.nvim__buf_redraw_range(self.buf, 0, a.nvim_buf_line_count(self.buf)) end -function TSHighlighter:on_changedtree(changes) - -- Get a fresh root - local root = self.parser:parse():root() +function TSHighlighter:on_window(_, _win, _buf, _topline, botline) + self.iter = nil + self.nextrow = 0 + self.botline = botline + self.redraw_count = self.redraw_count + 1 +end - for _, ch in ipairs(changes or {}) do - a.nvim_buf_clear_namespace(self.buf, ts_hs_ns, ch[1], ch[3]+1) - - for capture, node in self.query:iter_captures(root, self.buf, ch[1], ch[3] + 1) do - local start_row, start_col, end_row, end_col = node:range() - local hl = self.hl_cache[capture] - if hl then - a.nvim_buf_set_extmark(self.buf, ts_hs_ns, start_row, start_col, { - end_col = end_col, - end_line = end_row, - hl_group = hl - }) - end +function TSHighlighter:on_start(_, _buf, _tick) + local tree = self.parser:parse() + self.root = tree:root() +end + + +function TSHighlighter:on_line(_, _win, buf, line) + if self.iter == nil then + self.iter = self.query:iter_captures(self.root,buf,line,self.botline) + end + while line >= self.nextrow do + local capture, node = self.iter() + if capture == nil then + break + end + local start_row, start_col, end_row, end_col = node:range() + local hl = self.hl_cache[capture] + if hl and end_row >= line then + a.nvim__put_attr(start_row, start_col, { end_line = end_row, end_col = end_col, hl_group = hl }) + end + if start_row > line then + self.nextrow = start_row end end end |