Skip to content

Commit 7adf945

Browse files
committed
fix: Ensure treesitter parse tree is up-to-date (SmiteshP#69)
Whenever get_location() is called, we request the treesitter parser to refresh the parse tree for the current buffer up to date. This may be not needed when the treesitter-highlight module is enabled, but when the module is disabled the plugin may not display the correct context information (as reported in SmiteshP#69). Such calls will be throttled properly: refreshing the treesitter parse tree will happen only when the buffer has changed.
1 parent 6d6e936 commit 7adf945

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

Diff for: lua/nvim-gps/init.lua

+11
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ function M.setup(user_config)
219219
setup_complete = true
220220
end
221221

222+
-- Request treesitter parser to update the syntax tree,
223+
-- when the buffer content has changed.
224+
local update_tree = ts_utils.memoize_by_buf_tick(function(bufnr)
225+
local filelang = ts_parsers.ft_to_lang(vim.api.nvim_buf_get_option(bufnr, 'filetype'))
226+
local parser = ts_parsers.get_parser(bufnr, filelang)
227+
return parser:parse()
228+
end)
229+
222230
function M.get_location()
223231
-- Inserting text cause error nodes
224232
if vim.fn.mode() == 'i' then
@@ -234,6 +242,9 @@ function M.get_location()
234242
return "error"
235243
end
236244

245+
-- Request treesitter parser to update the syntax tree for the current buffer.
246+
update_tree(vim.fn.bufnr())
247+
237248
local current_node = ts_utils.get_node_at_cursor()
238249

239250
local node_text = {}

0 commit comments

Comments
 (0)