emacs-devel
[Top][All Lists]
Advanced

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

Re: Tree-sitter highlights


From: Juri Linkov
Subject: Re: Tree-sitter highlights
Date: Tue, 25 Mar 2025 20:03:33 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu)

> For the generic modes, we should call them xxx-ts-generic-mode (or
> xxx-generic-ts-mode?) rather than xxx-ts-mode, in case someone wants to
> implement a fullblown major mode. Also makes it clear to users that it’s
> a generic mode, with all the limitations of one.

Also I'm trying to create treesit generic minor modes.
For example, to enable liquid markup in html files,
it should be possible just to add in the init file:

  (add-hook 'mhtml-ts-mode-hook 'liquid-generic-minor-ts-mode)

Possible name variants:

- liquid-generic-minor-ts-mode
- liquid-generic-ts-minor-mode

Probably there is no need to add 'html' to the mode name
since the host lang could be defined by a keyword.

Also no need to define a new macro since the minor mode
could be specified by another keyword.  For example:

  (define-treesit-generic-mode liquid-generic-minor-ts-mode
    "Tree-sitter generic mode for liquid markup."
    :lang 'liquid
    :source "https://github.com/hankthetank27/tree-sitter-liquid";
    :minor t
    :host '(html))

Also there is a need to specify somewhere a query for range-rules.
There is the file queries/injections.scm in the repo,
but it doesn't look usable:

  ((template_content) @injection.content
    (#set! injection.language "html")
    (#set! injection.combined))

I don't even know how this is supposed to work,
there is not enough information here.

OTOH, the below definition added for testing to mhtml-ts-mode
works nicely and highlights liquid markup in html files:

#+begin_src emacs-lisp
    (when (treesit-ready-p 'liquid t)
      (treesit-parser-create 'liquid)
      (setq-local treesit-range-settings
                  (append treesit-range-settings
                          (treesit-range-rules
                           :embed 'liquid
                           :host 'html
                           `(((text) @cap
                              (:match ,(rx (or "{" "}")) @cap))))))
      (when-let* ((query (treesit-generic-mode-font-lock-query 'liquid)))
        (setq-local treesit-font-lock-settings
                    (append treesit-font-lock-settings
                            (treesit-font-lock-rules
                             :language 'liquid
                             :feature 'highlights
                             query)))
        (setq-local treesit-font-lock-feature-list
                    (treesit-merge-font-lock-feature-list
                     treesit-font-lock-feature-list
                     '((highlights)))))

      (setq treesit-thing-settings
            (append
             `((liquid (list ,(rx (or "if_statement"
                                      "for_loop_statement")))))
             treesit-thing-settings)))
#+end_src



reply via email to

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