neovim icon indicating copy to clipboard operation
neovim copied to clipboard

Ensure `tstree:parse()` always return trees and changes

Open bew opened this issue 3 years ago • 6 comments

Hello!

While reading the lua source code around treesitter, I noticed the following:

When parsing from an invalidated state, tstree:parse() returns the trees and the changes. https://github.com/neovim/neovim/blob/6cc6e11929ad76a2dc5204aed95cb9ed1dafde23/runtime/lua/vim/treesitter/languagetree.lua#L167

But when calling tstree:parse() again, with a valid state, the function returns only the trees. :point_right: This would change the behavior of calls like tstree:parse()[1] expecting to get the list of trees, and instead getting the first tree. https://github.com/neovim/neovim/blob/6cc6e11929ad76a2dc5204aed95cb9ed1dafde23/runtime/lua/vim/treesitter/languagetree.lua#L99

I believe this is a mistake


It seems that none of the functions have their return value type documented, is that intentional? :point_right: it's a bit hard to know what to expect from calling the function, other than trying them..

bew avatar Aug 23 '22 20:08 bew

It seems that none of the functions have their return value type documented, is that intentional?

It's not; documentation is lacking since tree-sitter support is still experimental and in quite a bit of flux.

clason avatar Aug 23 '22 20:08 clason

I think that we'd need to go the other way around: never return the changes an only notify them using the on_changed_tree callback.

The problem is that (as you noticed) it is not guaranteed for a plugin that it'll be the first one to call tstree:parse() and thus get the changes.

Regarding the documentation, types are lacking, and even some documentation are wrong, but that'll be done in a followup PR.

vigoux avatar Aug 24 '22 06:08 vigoux

I think that we'd need to go the other way around: never return the changes an only notify them using the on_changed_tree callback.

I've never used these directly, so I don't have an opinion. But I'd be happy to do the changes if you think it's right

bew avatar Aug 24 '22 11:08 bew

I've never used these directly, so I don't have an opinion. But I'd be happy to do the changes if you think it's right

I indeed think it is, but that makes it a breaking change. Though this functionnality was not really used (I think), so it does not matter that much yet.

vigoux avatar Aug 24 '22 13:08 vigoux

bump -- do we still want this for 0.8?

clason avatar Sep 06 '22 13:09 clason

Not sure, I'd rather push that for 0.9 as it is not a really important change feature-wise.

vigoux avatar Sep 06 '22 13:09 vigoux

@lewis6991 is this still relevant after your refactors?

clason avatar Mar 08 '23 09:03 clason

Yes kind of. I'm not sure what the annotations are but we could go either way. I do see the appeal of returning non-nil.

lewis6991 avatar Mar 08 '23 09:03 lewis6991

This would change the behavior of calls like tstree:parse()[1] expecting to get the list of trees, and instead getting the first tree.

I don't think this is true. varargs and tables work very differently.

lewis6991 avatar Mar 08 '23 09:03 lewis6991

@bew can you rebase then, so we can test this with our current code/annotations?

clason avatar Mar 10 '23 11:03 clason

I think that we'd need to go the other way around: never return the changes an only notify them using the on_changed_tree callback.

The problem is that (as you noticed) it is not guaranteed for a plugin that it'll be the first one to call tstree:parse() and thus get the changes.

I completely agree with this.

lewis6991 avatar Mar 10 '23 11:03 lewis6991

Briefly tested removing the second return value, and nothing broke (including functionaltests and nvim-treesitter). Want to switch the PR to do that instead?

clason avatar Mar 10 '23 13:03 clason

Yeah, the change here is really minimal, good call to move on to another one 👍

bew avatar Mar 10 '23 16:03 bew