Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork6.5k
Simulate treesitter manually (in an arbitrary buffer region)#35855
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
EDIT: issue#35907 Suppose there is a read-only buffer and I manually placing some text into it via nvim lua api. This text has a structure of list, where each element consists of some elements, despite it's just plain text. For example its a list of paragraphs where each one has heading and content. I want to be able to fold, conceal and highlight some parts of this text despite there is no treesitter parser for this, and it is even impossible to create such a parser. The example of how it might look is neogit plugin, where it can fold-unfold git diff without a parser. So. Can I manually point out to neovim which parts of text are "treesitter" nodes, create AST by hands so I can treat to my text as parsed by treesitter? Probably using some internal neovim functions. I've tried to figure out can I do it with extmarks, but I didn't found about it in docs. |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 4 comments 3 replies
-
There is |
BetaWas this translation helpful?Give feedback.
All reactions
-
After some thought, I think the idea of a "null grammar" follows from what I mentionedearlier, since this is a valid case. |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
@justinmk, I did not accurately describe the problem. I need to turn the entire buffer into nodes, not an arbitrary part. The problem is that there is not and will not be a parser for my buffer, but when generating text for the buffer, I could manually set these nodes so that I could then treat the buffer text as if it were some kind of language (folding, highlighting). I looked at how it is implemented in neogit. There, the author implemented these functions completely independently of the treesitter mechanisms, so he duplicated all the logic for working with folding and highlighting (this function for example, as well as entire file looks like handmade treesitter), and generally wrote his own "class" for the node and the recursive traversalfunction. It seems to me that this is unnecessary, and if I could simulate the behavior of a parser, I could use the existing treesitter logic. PS. My idea is to make a plugin forge.nvim which would relate to neogit as forge (emacs package) relates to magit. So PR's or issues should be presented as list, with concealable descriptions, and respective highlights for assigners, labels, statuses, etc. Obviously there is no parser for that, and I don't think it is possible to do since there is just arbitrary text. But while I generating, for example, issue "page", I have issue title, issue author, conversation and other stuff, so I could mark all that elements as treesitter nodes and then just write some |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
BetaWas this translation helpful?Give feedback.
All reactions
❤️ 4👀 1
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Thanks a lot!! This is the exact what I needed. I think I'll work on PR |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
After some research I see 2 ways of doing that. 1First is similar to what@vanaigr suggested, like create TSNode and TSTree lua type, (re)define all the logic implementedhere, and then integrate new types to existing mechanisms. Problem is it's necessary to reimplement all existing treesitter's C logic (regarding trees and nodes). It doesn't take much effort if we're going to provide the minimum of features that treesitter has. But the idea of duplicating some types and logic are not ideal, as for me. 2The second way seems more accurate to me. We need to trick treesitter's What's problematic about that is to create such a // Use it in neovim C core// non existent treesitters functionts_parser_set_pseudo_language(parser,tree_sitter_dummy_lang()); and be happy. Then add something like this to lua api: node=require('vim.treesitter.customnode')(--[[node's type, visibility, pos, children, etc]])-- custom nodetree=require('vim.treesitter.customtree')(node)-- custom treevim.treesitter.language.add('custom_lang', {--[[new option]]tree=tree}) So the benefit is we are able to use all treesitter's features and almost zero changes to Although it should be borne in mind that I have only superficially figured out how neovim C core works and its interaction with treesitter, so I may be mistaken or missing something. Any thoughts? |
BetaWas this translation helpful?Give feedback.
All reactions
-
I started to work on that feature. Will try to send PR anytime soon |
BetaWas this translation helpful?Give feedback.
