emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to store treesit compiled query to file?


From: Meow King
Subject: How to store treesit compiled query to file?
Date: Sun, 14 Jan 2024 11:50:28 +0800
User-agent: mu4e 1.10.8; emacs 30.0.50

# The reason I want to store treesit-compiled-query to file
(you can skip this if you don't care the reason)

I'm the developer of `typst-ts-mode`, which is a major mode supporting
Typst, a markup language like Markdown. Recently I've just done the raw
block (embedding other languages into the buffer) related features (like
dynamically loading other tree sitter languages settings and configuring
highlighting and indentation for the raw blocks.). Currently, my
implementation for dynamical loading settings is through merging lanauge
settings, and there are two way to get the target language settings:
1. pre-configured settings like
```
'((python . (:feature
             python
             :font-lock python--treesit-settings
             :indentation nil
             :ts-feature-list
             '(( comment definition)
             ( keyword string type)
             ( assignment builtin constant decorator
             escape-sequence number string-interpolation )
             ( bracket delimiter function operator variable property )))))
```
2. if the language is not in the pre-configured settings, we create a
temp buffer, then call the tree sitter major mode function like
`rust-ts-mode`, getting the settings from local variables like
`treesit-font-lock-settings`.

The first way is not very good since lanugage settings may vary from
different versions of a major mode pakcage (especially `ts-feature-list` since
many major mode doesn't provide it as a variable.)

I want to improve the first way to get language settings by letting user
executing a command, which will output all the tree sitter language settings of
their emacs to a emacs lisp file, so user can load them at their emacs
startup.

# Ways I tried to store treesit-query to file

1. use `prin1`

```
(with-temp-file "temp-temp"
     (prin1
     (with-temp-buffer
     (setq-local delay-mode-hooks t)  ; don't run hooks associated with MODE
     (rust-ts-mode)
     treesit-font-lock-settings)
     (current-buffer)))
```

However, the output is plain text, and doens't contains the actual tree sitter
query:

```
((#<treesit-compiled-query> t attribute nil) ...)
```


2. use `byte-compile`

```
(byte-compile treesit-font-lock-settings)
```

However, it produces an error:

```
Warning: Malformed function ‘(#<treesit-compiled-query> t comment nil)’
```

Can we store tree sitter compiled queries to file and what's the proper
way to store it?

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]