diff options
Diffstat (limited to 'runtime/lua/vim/treesitter/languagetree.lua')
-rw-r--r-- | runtime/lua/vim/treesitter/languagetree.lua | 62 |
1 files changed, 41 insertions, 21 deletions
diff --git a/runtime/lua/vim/treesitter/languagetree.lua b/runtime/lua/vim/treesitter/languagetree.lua index c864fe5878..4168c1e365 100644 --- a/runtime/lua/vim/treesitter/languagetree.lua +++ b/runtime/lua/vim/treesitter/languagetree.lua @@ -6,14 +6,14 @@ local LanguageTree = {} LanguageTree.__index = LanguageTree -- Represents a single treesitter parser for a language. --- The language can contain child languages with in it's range, +-- The language can contain child languages with in its range, -- hence the tree. -- -- @param source Can be a bufnr or a string of text to parse -- @param lang The language this tree represents -- @param opts Options table --- @param opts.queries A table of language to injection query strings --- This is useful for overridding the built in runtime file +-- @param opts.queries A table of language to injection query strings. +-- This is useful for overriding the built-in runtime file -- searching for the injection language query per language. function LanguageTree.new(source, lang, opts) language.require_language(lang) @@ -21,8 +21,8 @@ function LanguageTree.new(source, lang, opts) local custom_queries = opts.queries or {} local self = setmetatable({ - _source=source, - _lang=lang, + _source = source, + _lang = lang, _children = {}, _regions = {}, _trees = {}, @@ -35,6 +35,7 @@ function LanguageTree.new(source, lang, opts) _callbacks = { changedtree = {}, bytes = {}, + detach = {}, child_added = {}, child_removed = {} }, @@ -44,12 +45,17 @@ function LanguageTree.new(source, lang, opts) return self end --- Invalidates this parser and all it's children -function LanguageTree:invalidate() +-- Invalidates this parser and all its children +function LanguageTree:invalidate(reload) self._valid = false + -- buffer was reloaded, reparse all trees + if reload then + self._trees = {} + end + for _, child in ipairs(self._children) do - child:invalidate() + child:invalidate(reload) end end @@ -97,7 +103,7 @@ function LanguageTree:parse() self._trees = {} -- If there are no ranges, set to an empty list - -- so the included ranges in the parser ar cleared. + -- so the included ranges in the parser are cleared. if self._regions and #self._regions > 0 then for i, ranges in ipairs(self._regions) do local old_tree = old_trees[i] @@ -214,7 +220,7 @@ function LanguageTree:remove_child(lang) end end --- Destroys this language tree and all it's children. +-- Destroys this language tree and all its children. -- Any cleanup logic should be performed here. -- Note, this DOES NOT remove this tree from a parent. -- `remove_child` must be called on the parent to remove it. @@ -241,19 +247,19 @@ end -- -- Note, this call invalidates the tree and requires it to be parsed again. -- --- @param regions A list of regions this tree should manange and parse. +-- @param regions A list of regions this tree should manage and parse. function LanguageTree:set_included_regions(regions) - -- Transform the tables from 4 element long to 6 element long (with byte offset) - for _, region in ipairs(regions) do - for i, range in ipairs(region) do - if type(range) == "table" and #range == 4 then - -- TODO(vigoux): I don't think string parsers are useful for now - if type(self._source) == "number" then + -- TODO(vigoux): I don't think string parsers are useful for now + if type(self._source) == "number" then + -- Transform the tables from 4 element long to 6 element long (with byte offset) + for _, region in ipairs(regions) do + for i, range in ipairs(region) do + if type(range) == "table" and #range == 4 then local start_row, start_col, end_row, end_col = unpack(range) -- Easy case, this is a buffer parser -- TODO(vigoux): proper byte computation here, and account for EOL ? - local start_byte = a.nvim_buf_get_offset(self.bufnr, start_row) + start_col - local end_byte = a.nvim_buf_get_offset(self.bufnr, end_row) + end_col + local start_byte = a.nvim_buf_get_offset(self._source, start_row) + start_col + local end_byte = a.nvim_buf_get_offset(self._source, end_row) + end_col region[i] = { start_row, start_col, start_byte, end_row, end_col, end_byte } end @@ -397,14 +403,24 @@ function LanguageTree:_on_bytes(bufnr, changed_tick, new_row, new_col, new_byte) end +function LanguageTree:_on_reload() + self:invalidate(true) +end + + +function LanguageTree:_on_detach(...) + self:invalidate(true) + self:_do_callback('detach', ...) +end + --- Registers callbacks for the parser -- @param cbs An `nvim_buf_attach`-like table argument with the following keys : -- `on_bytes` : see `nvim_buf_attach`, but this will be called _after_ the parsers callback. --- `on_changedtree` : a callback that will be called everytime the tree has syntactical changes. +-- `on_changedtree` : a callback that will be called every time the tree has syntactical changes. -- it will only be passed one argument, that is a table of the ranges (as node ranges) that -- changed. -- `on_child_added` : emitted when a child is added to the tree. --- `on_child_removed` : emitted when a child is remvoed from the tree. +-- `on_child_removed` : emitted when a child is removed from the tree. function LanguageTree:register_cbs(cbs) if not cbs then return end @@ -416,6 +432,10 @@ function LanguageTree:register_cbs(cbs) table.insert(self._callbacks.bytes, cbs.on_bytes) end + if cbs.on_detach then + table.insert(self._callbacks.detach, cbs.on_detach) + end + if cbs.on_child_added then table.insert(self._callbacks.child_added, cbs.on_child_added) end |