Subject: Re: 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: Wed, 22 Jan 2025 07:23:28 +0100

Hi Yuan:

When I try your patch I get an error:

Debugger entered--Lisp error: (treesit-no-parser cpp)
   (treesit-buffer-root-node cpp)
   (treesit-node-at 3091)
   (treesit-show-paren-data--categorize 3091)
   (treesit-show-paren-data)
   (show-paren-function)
   (apply show-paren-function nil)
   (timer-event-handler [t 0 0 125000 t show-paren-function nil idle 0 nil])

When I try:
(treesit-parser-list nil 'cpp) -> nil

So it it like the

I tried manually (setq-local treesit-primary-parser
(treesit-parser-create 'cpp))

but nothing.

On Mon, Jan 20, 2025 at 03:29:45PM -0800, Yuan Fu wrote:
>
>
>> On Jan 20, 2025, at 9:09 AM, Ergus <[email-masked]> 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 <[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))
>
>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
>