Skip to content
This repository has been archived by the owner on Jul 5, 2022. It is now read-only.

Dynamic update support #69

Closed
wookayin opened this issue Feb 7, 2022 · 8 comments
Closed

Dynamic update support #69

wookayin opened this issue Feb 7, 2022 · 8 comments
Labels
bug Something isn't working

Comments

@wookayin
Copy link
Contributor

wookayin commented Feb 7, 2022

It looks like that the context is not automatically updated while editing. Until I reload the file (so that GPS query can refresh), nvim-gps fails to show the context for newly added scopes.

Screenshot

It would be great if we can support dynamic update of the context information powered by treesitter!

@SmiteshP
Copy link
Owner

SmiteshP commented Feb 8, 2022

It should update dynamically. As soon as you exit insert mode, nvim-gps asks treesitter for new information to show. It seems that for some reason your treesitter isn't providing latest information 🤔

2022-02-08.11-11-02.mp4

@SmiteshP SmiteshP added the bug Something isn't working label Feb 8, 2022
@wookayin
Copy link
Contributor Author

wookayin commented Feb 8, 2022

Hmm interesting. I am new to treesitter, so have no concrete ideas yet on how to debug this. FYI, my treesitter parser and plugins are up-to-date (and neovim is nightly-master). I have nothing special in my config, and :checkhealth nvim-treesitter seems fine.

The TSPlayground window also doesn't update and gets out of sync, so this might be related to the treesitter or its config itself.

@wookayin
Copy link
Contributor Author

wookayin commented Feb 8, 2022

@SmiteshP Is treesitter-playground as well automatically updating as you change the text?

@SmiteshP
Copy link
Owner

SmiteshP commented Feb 8, 2022

@SmiteshP Is treesitter-playground as well automatically updating as you change the text?

Yep, playground should also update on the fly. So this means the issue is definitely with treesitter. You could try opening a issue on nvim-treesitter repo, they should be able to help.

@wookayin
Copy link
Contributor Author

wookayin commented Feb 8, 2022

Surprisingly, nvim-treesitter/playground#64 says that currently the module highlight is required to make on-the-fly update work properly.

@wookayin
Copy link
Contributor Author

wookayin commented Feb 9, 2022

@SmiteshP So according to the discussion nvim-treesitter/nvim-treesitter#2492 (-> nvim-treesitter/playground#64), it seems calling the parser to update the syntax tree for the current buffer (i.e., parser:parse(buf, lang)) is the responsibility of downstream treesitter plugins. Apparently both playground and nvim-gps missed the fact that TS syntax tree is only updated upon request (and many other treesitter plugins probably also aren't aware of this), which was naturally happening when highlight module is enabled.

I can confirm that adding the following lines somewhere in M.get_location() makes the syntax tree is updated, despite highlight { enabled = false }.

+	-- Request treesitter parser to update the syntax tree.
+	local parser = ts_parsers.get_parser(vim.fn.bufnr(), filelang)
+	parser:parse()

	local current_node = ts_utils.get_node_at_cursor()

But this would be too excessive, frequent call because get_location() is usually called from statusline (unless we have a smart cache mechanism). A better way with throttling would be to have some autocmds (like InsertLeave autocmd) or timers set up, but I'm not sure this is something nvim-gps is responsible for or users are supposed to have it configuring treesitter. Either way, if I get convinced, I will make a PR for nvim-gps.

wookayin added a commit to wookayin/nvim-gps that referenced this issue Feb 9, 2022
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.
@wookayin
Copy link
Contributor Author

wookayin commented Feb 9, 2022

@SmiteshP So the conclusion is that ensuring the parse tree to be up-to-date is an responsibility of downstream treesitter module/plugin, which is however often subsumed when the treesitter highlight module is enabled. I submitted a PR #70 that fixes this bug, even when the highlight module is absent.

@SmiteshP
Copy link
Owner

SmiteshP commented Feb 9, 2022

Oh interesting! I did use to have an InsertLeave autocommand to update the parser but at some point I realised everything works fine without it too, so I removed it. 😆

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants