Expanding folds in treesitter for injected languages

Look for clarification I guess on how nvim-treesitter handles injected languages and folds.

For example, with tools/languages like Vue, they embed html, css, and javascript into a single filetype.


<template>
  <div>Hello World</div>
</template>

<style>
  div {
    color: red
  }
</style>

<script>....</script>

Now Treesitter can parse this and provide the correct syntax highlighting, and I can confirm this with TSPlayground. But I cannot figure out how folds are applied/triggered based on this.

In queries/vue/folds.scm, there are queries for the template, script, and style tags that define a “vue” file.

[
  (element)
  (template_element)
  (script_element)
  (style_element)
] @fold

So my understanding is that these nodes will only get folds applied to them. If that’s the case, trying to expand them to include more from javascript leads to an “Invalid Node Type”.

I guess this is a long way of asking, are fold defs for injected languages handled by the injected language, or the parent language?

1 Like

I believe it needs some rewrite. I also asked the same thing but with indent on nvim-treesitter zulip a while ago. I think they managed to get cross-language folding works. There is this branch if you want to try it, but I think it’s not done yet.

1 Like

Just tried it out and it works really well! Wonder what the hold up was and if there’s anything that could be done to get it over the finish line.