From 9437327d5ee627ddb15bfbc88112bd77e4e3ffcf Mon Sep 17 00:00:00 2001 From: Björn Linse Date: Sat, 21 Mar 2020 19:55:19 +0100 Subject: treesitter: use new on_bytes interface This will significantly reduce the parsing work needed e.g. when rehighlighting after every keypress in insert mode. Also add safety check for tree-sitter trying to read past the end of a line. This can happen after we sent an incorrect buffer update. --- runtime/lua/vim/treesitter.lua | 39 ++++++++++++++++++------------ runtime/lua/vim/treesitter/highlighter.lua | 2 +- 2 files changed, 24 insertions(+), 17 deletions(-) (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/treesitter.lua b/runtime/lua/vim/treesitter.lua index 550dee1e3f..3a475b8f98 100644 --- a/runtime/lua/vim/treesitter.lua +++ b/runtime/lua/vim/treesitter.lua @@ -33,16 +33,23 @@ function Parser:parse() return self.tree, changes end -function Parser:_on_lines(bufnr, changed_tick, start_row, old_stop_row, stop_row, old_byte_size) - local start_byte = a.nvim_buf_get_offset(bufnr,start_row) - local stop_byte = a.nvim_buf_get_offset(bufnr,stop_row) - local old_stop_byte = start_byte + old_byte_size - self._parser:edit(start_byte,old_stop_byte,stop_byte, - start_row,0,old_stop_row,0,stop_row,0) +function Parser:_on_bytes(bufnr, changed_tick, + start_row, start_col, start_byte, + old_row, old_col, old_byte, + new_row, new_col, new_byte) + local old_end_col = old_col + ((old_row == 0) and start_col or 0) + local new_end_col = new_col + ((new_row == 0) and start_col or 0) + self._parser:edit(start_byte,start_byte+old_byte,start_byte+new_byte, + start_row, start_col, + start_row+old_row, old_end_col, + start_row+new_row, new_end_col) self.valid = false - for _, cb in ipairs(self.lines_cbs) do - cb(bufnr, changed_tick, start_row, old_stop_row, stop_row, old_byte_size) + for _, cb in ipairs(self.bytes_cbs) do + cb(bufnr, changed_tick, + start_row, start_col, start_byte, + old_row, old_col, old_byte, + new_row, new_col, new_byte) end end @@ -88,12 +95,12 @@ function M._create_parser(bufnr, lang, id) local self = setmetatable({bufnr=bufnr, lang=lang, valid=false}, Parser) self._parser = vim._create_ts_parser(lang) self.changedtree_cbs = {} - self.lines_cbs = {} + self.bytes_cbs = {} self:parse() - -- TODO(bfredl): use weakref to self, so that the parser is free'd is no plugin is - -- using it. - local function lines_cb(_, ...) - return self:_on_lines(...) + -- TODO(bfredl): use weakref to self, so that the parser is free'd is no plugin is + -- using it. + local function bytes_cb(_, ...) + return self:_on_bytes(...) end local detach_cb = nil if id ~= nil then @@ -103,7 +110,7 @@ function M._create_parser(bufnr, lang, id) end end end - a.nvim_buf_attach(self.bufnr, false, {on_lines=lines_cb, on_detach=detach_cb}) + a.nvim_buf_attach(self.bufnr, false, {on_bytes=bytes_cb, on_detach=detach_cb}) return self end @@ -138,8 +145,8 @@ function M.get_parser(bufnr, lang, buf_attach_cbs) table.insert(parsers[id].changedtree_cbs, buf_attach_cbs.on_changedtree) end - if buf_attach_cbs and buf_attach_cbs.on_lines then - table.insert(parsers[id].lines_cbs, buf_attach_cbs.on_lines) + if buf_attach_cbs and buf_attach_cbs.on_bytes then + table.insert(parsers[id].bytes_cbs, buf_attach_cbs.on_bytes) end return parsers[id] diff --git a/runtime/lua/vim/treesitter/highlighter.lua b/runtime/lua/vim/treesitter/highlighter.lua index b261d51d4a..8944cc5256 100644 --- a/runtime/lua/vim/treesitter/highlighter.lua +++ b/runtime/lua/vim/treesitter/highlighter.lua @@ -60,7 +60,7 @@ function TSHighlighter.new(query, bufnr, ft) ft, { on_changedtree = function(...) self:on_changedtree(...) end, - on_lines = function() self.root = self.parser:parse():root() end + on_bytes = function() self.root = self.parser:parse():root() end } ) -- cgit From cf0e1bc1fe272b6948b88cfe83e84ba4214d9b9b Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Thu, 3 Sep 2020 15:36:11 +0200 Subject: wip trying to fix the highlighter --- runtime/lua/vim/treesitter/highlighter.lua | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/treesitter/highlighter.lua b/runtime/lua/vim/treesitter/highlighter.lua index 8944cc5256..718088e0ad 100644 --- a/runtime/lua/vim/treesitter/highlighter.lua +++ b/runtime/lua/vim/treesitter/highlighter.lua @@ -60,14 +60,11 @@ function TSHighlighter.new(query, bufnr, ft) ft, { on_changedtree = function(...) self:on_changedtree(...) end, - on_bytes = function() self.root = self.parser:parse():root() end + on_bytes = function() self.parser:parse() end } ) self.buf = self.parser.bufnr - - local tree = self.parser:parse() - self.root = tree:root() self:set_query(query) self.edit_count = 0 self.redraw_count = 0 @@ -126,17 +123,17 @@ function TSHighlighter:set_query(query) end }) - self:on_changedtree({{self.root:range()}}) + self:on_changedtree({{self.parser:parse():root():range()}}) end function TSHighlighter:on_changedtree(changes) -- Get a fresh root - self.root = self.parser.tree:root() + local root = self.parser:parse():root() for _, ch in ipairs(changes or {}) do - a.nvim_buf_clear_namespace(self.buf, ts_hs_ns, ch[1], ch[3] + 1) + a.nvim_buf_clear_namespace(self.buf, ts_hs_ns, ch[1], ch[3]+1) - for capture, node in self.query:iter_captures(self.root, self.buf, ch[1], ch[3] + 1) do + 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 -- cgit