Manually highlighting a region of a buffer using an explicit treesitter parser #27940
-
I have a scenario where I am creating a custom buffer filetype and generating the content within the buffer. For parts of the buffer, I want to highlight using treesitter. Is there a way for me to load up a treesitter parser for a specific language, parse a region of a buffer, and apply highlights? The concrete example is I'm writing a port of org-roam which is getting close to complete, but trying to represent the org-roam buffer has proven challenging. I want to provide a preview of each backlink and provide orgmode syntax highlighting to the sections that are previews. Looking around, I can't find an example or discussion about this.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
In general to highlight a range of text the main function ins
For custom highlighting start with |
Beta Was this translation helpful? Give feedback.
In general to highlight a range of text the main function ins
nvim_buf_set_extmark()
. But I'm not sure how to combine that with a treesitter parser.:help vim.treesitter.get_string_parser()
seems related, but I think it's not bound to a buffer.For custom highlighting start with
:help :EditQuery
https://neovim.io/doc/user/treesitter.html#vim.treesitter.query.edit() , you could define a query that enables highlighting only for a certain section of a document. However, doing that dynamically might be weird.