GNU bug report logs - #72388
31.0.50; Use tree-sitter-cuda grammar but with tree-sitter-cpp's font-lock/indentation rules

Previous Next

Package: emacs;

Reported by: Yuan Fu <casouri <at> gmail.com>

Date: Wed, 31 Jul 2024 00:07:01 UTC

Severity: normal

Found in version 31.0.50

Full log


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

From: Yuan Fu <casouri <at> gmail.com>
To: Ergus <spacibba <at> aol.com>
Cc: 72388 <at> debbugs.gnu.org
Subject: Re: bug#72388: 31.0.50; Use tree-sitter-cuda grammar but with
 tree-sitter-cpp's font-lock/indentation rules
Date: Mon, 20 Jan 2025 15:29:45 -0800
[Message part 1 (text/plain, inline)]

> On Jan 20, 2025, at 9:09 AM, Ergus <spacibba <at> aol.com> wrote:
> 
> Hi Yuan:
> 
> Very thanks for your reply and patches :) I think I am bothering too
> much :p

No no, sorry, I didn’t explain it clear enough.

> 
> On Sun, Jan 19, 2025 at 10:57:16PM -0800, Yuan Fu wrote:
>> 
>> 
>>> On Jan 18, 2025, at 4:42 AM, Ergus <spacibba <at> aol.com> wrote:
>>> 
>>> On Fri, Jan 17, 2025 at 05:29:00PM -0800, Yuan Fu wrote:
>>> 
>>> Hi Yuan:
>>> 
>>> Very thanks for your answer and quick fix ;)
>>> 
>>> However when I try this:
>>> 
>>> ```
>>> (define-derived-mode cuda-ts-mode c-ts-base-mode "Cuda"
>>> (when (treesit-ready-p 'cuda)
>>>   (setq-local treesit-primary-parser (treesit-parser-create 'cuda))
>>>   (setq-local treesit-language-remap-alist '((cpp . cuda)))))
>>> ```
>>> 
>>> `treesit-simple-indent-rules' and `treesit-font-lock-settings' are nil.
>>> 
>>> On the other hand:
>>> 
>>> If I do instead:
>>> 
>>> ```
>>> (define-derived-mode cuda-ts-mode c++-ts-mode "Cuda" ...
>>> ```
>>> 
>>> Then I have fontlock, but it seems taken from C++ grammar and
>>> indentation still doesn't work. It actually creates a 'cpp grammar,
>>> which I think is not desired in this case.
>>> 
>>> Indentation doesn't work in this case because:
>>> 
>>> ```
>>> (treesit-node-language (treesit-node-at (point)))
>>> ```
>>> 
>>> returns 'cuda, but `treesit-simple-indent-rules' has 'cpp key; and
>>> the `treesit-simple-indent' function has:
>>> 
>>> ```
>>> (language (treesit-node-language parent))
>>> (rules (alist-get language treesit-simple-indent-rules))
>>> ```
>>> 
>>> The code in `c-ts-mode--simple-indent-rules' ends with:
>>> 
>>> (pcase mode
>>>     ('c `((c . ,rules)))
>>>     ('cpp `((cpp . ,rules))))
>>> 
>>> So it can only create 'cpp or 'c keys and `treesit-node-language' uses
>>> `treesit-parser-language' (which is correct). So I still need to remap
>>> things manually.
>>> 
>>> In font-lock I cannot see in such details because the var
>>> `treesit-font-lock-settings' is more opaque, but I suppose that there
>>> may be happening something similar.
>>> 
>> 
>> Thanks Ergus. I was trying to avoid using the heavy hammer but it seems we have to make it completely transparent. I pushed a change to master. Now when you create a cuda parser using the remapping from cpp, it should completely look and talk like a cpp parser.
>> 
>> Font-lock should work though. It worked for me when I tested it locally.
>> 
>> Yuan
> 
> I have exactly the same error as before.
> 
> This is the code:
> 
> ```
> (define-derived-mode cuda-ts-mode c-ts-base-mode "Cuda"
>  "Major mode for editing Cuda, powered by tree-sitter.
> 
> This mode is independent from the classic cuda-mode.el, but inherits
> most of the properties from c++-ts-mode like `c-ts-mode-indent-style',
> `c-ts-mode-indent-offset' or `c-ts-mode-enable-doxygen'."
> 
>  (when (treesit-ready-p 'cuda)
> 
>    (setq treesit-language-remap-alist '((cpp . cuda)))
>    (setq-local treesit-primary-parser (treesit-parser-create 'cuda))

I think if you replace ‘cuda with ‘cpp here, everything should work. This parser will be a cuda parser but labeled as cpp parser. That also means if you want to add coda-specific font-lock rules or indentation, you need to use ‘cpp’ as the language key.

> 
>    (treesit-major-mode-setup)))
> ```
> 
> (treesit-node-language (treesit-node-at (point))) => cuda
> treesit-simple-indent-rules => nil
> treesit-font-lock-settings => nil
> 
> What I am doing wrong?
> 
> ...
> 
> Should I derive from c++-ts-mode instead?

You don’t need to, c-ts-base-mode is fine.

> 
> In that case, font-lock works but I don't know if it is with the cuda or
> cpp parser.
> 
> However, indentation doesn't work at all for the same explication in the
> last email.
> 
> (treesit-node-language (treesit-node-at (point))) returns cuda, but keys
> in indent-rules is cpp.

Please see this patch. With this patch I can get cuda work for both font-lock and indentation.

Yuan

[cuda-ts-mode.patch (application/octet-stream, attachment)]
[Message part 3 (text/plain, inline)]


This bug report was last modified 135 days ago.

Previous Next


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