GNU bug report logs - #79363
31.0.50; Font locking issue in go-ts-mode when tree-sitter grammar is automatically installed

Previous Next

Package: emacs;

Reported by: Roi Martin <jroi.martin <at> gmail.com>

Date: Mon, 1 Sep 2025 21:33:02 UTC

Severity: normal

Found in version 31.0.50

Full log


Message #17 received at 79363 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Roi Martin <jroi.martin <at> gmail.com>
Cc: 79363 <at> debbugs.gnu.org
Subject: Re: bug#79363: 31.0.50; Font locking issue in go-ts-mode when
 tree-sitter grammar is automatically installed
Date: Tue, 02 Sep 2025 19:56:51 +0300
>> One idea is to turn defvar into defun that memoizes its return value.
>
> Do you mean something like this?

Exactly.

>   (defvar go-ts-mode--font-lock-settings nil
>     "Tree-sitter font-lock settings for `go-ts-mode'.")
>   
>   (defun go-ts-mode--font-lock-settings ()
>     "Return tree-sitter font-lock settings for `go-ts-mode'.
>   
>   Tree-sitter font-lock rules are evaluated the first time this function
>   is called.  Subsequent calls return the first evaluated value."
>     (or go-ts-mode--font-lock-settings
>         (setq go-ts-mode--font-lock-settings
>               (treesit-font-lock-rules
>                ...))))
>   
>   (define-derived-mode go-ts-mode prog-mode "Go"
>     ...
>     (when (treesit-ensure-installed 'go)
>       ...
>       (setq-local treesit-font-lock-settings (go-ts-mode--font-lock-settings))
>       ...))
>
> I tried and I can confirm it works fine.  I can work on a patch for all
> the affected ts modes if you want.

Thanks, this would be nice.

> BTW is this a common pattern?  If it is, does the Elisp core library
> provide something similar?  At the end of the day it is a sort of
> "lazy defvar", isn't it?

At the moment I don't see a better solution.  For example,
like the existing `with-eval-after-load' we could add
`with-eval-after-grammar-installed' with some hook called
when the grammar gets installed, e.g.:

  (defun go-ts-mode--font-lock-settings ()
    (with-eval-after-grammar-installed 'go
      (setq go-ts-mode--font-lock-settings
            (treesit-font-lock-rules
             ...))))

But even with this additional complexity its usage
is not simpler than you suggested above.




This bug report was last modified 4 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.