uben0 / tree-sitter-typst

Tree Sitter grammar for Typst
MIT License
114 stars 10 forks source link

`py` does not trigger nested Python tree-sitter to work, but `python` does #34

Closed Andrew15-5 closed 3 months ago

Andrew15-5 commented 5 months ago

This doesn't add Python syntax highlighting:

```py
print()

But this does:

````typ
```python
print()


While both work fine with [Markdown tree-sitter](https://github.com/tree-sitter-grammars/tree-sitter-markdown).

Checked on Neovim and Helix.
Andrew15-5 commented 5 months ago

With Helix, this fixed the problem: https://github.com/helix-editor/helix/pull/10785. But I have no idea what to do in Neovim/tree-sitter if anything can be done.

monaqa commented 3 months ago

Instead of https://github.com/uben0/tree-sitter-typst/blob/master/queries/typst/injections.scm#L4-L6, try the following:

(raw_blck
  lang: (ident) @_lang
  (blob) @injection.content
  (#set-lang-from-info-string! @_lang))
Andrew15-5 commented 3 months ago

It turns out that for Neovim, this exact code is a part of https://github.com/nvim-treesitter/nvim-treesitter. So I did:

diff --git a/queries/typst/injections.scm b/queries/typst/injections.scm
index 344c6a30..bc75d2b2 100644
--- a/queries/typst/injections.scm
+++ b/queries/typst/injections.scm
@@ -2,5 +2,6 @@
   (#set! injection.language "comment"))

 (raw_blck
-  (ident) @injection.language
-  (blob) @injection.content)
+  lang: (ident) @_lang
+  (blob) @injection.content
+  (#set-lang-from-info-string! @_lang))

And now it works! I will have to make 2 PRs, I guess.