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

From: Ergus <[email-masked]>
Date: Mon, 20 Jan 2025 18:09:27 +0100

Hi Yuan:

Very thanks for your reply and patches :) I think I am bothering too
much :p

On Sun, Jan 19, 2025 at 10:57:16PM -0800, Yuan Fu wrote:
>
>
>> On Jan 18, 2025, at 4:42 AM, Ergus <[email-masked]> 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))

     (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?

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.

Once again, sorry for bothering so much,
Best,
Ergus