reddit / emacs

tree-sitter-hl-mode does not do anything

cat-head · 2022-12-04 11:06
I've been trying to get syntax highlighting with tree-sitter to work but it doesn't seem to do anything. I tried setting it up with use-package but that does not work. If I do the following I get no highlighting (where I expected I should, but maybe not?): ``` emacs -Q ; to open emacs with no configs M-x find-file ; open some .cpp or .py file with example code (package-initialize) (require 'tree-sitter) (require 'tree-sitter-langs) (require 'tree-sitter-hl) (require 'tree-sitter-debug) (require 'tree-sitter-query) M-x font-lock-mode ; to deactivate font-lock highlighting M-x tree-sitter-mode M-x tree-sitter-hl-mode ``` And nothing happens. Not sure if what I'm missing here. Thanks!
grimscythe_ · 2022-12-04 11:14
You don't need to deactivate font-lock-mode. You also should add (global-tree-sitter-mode) so that tree sitter is enabled for all currently active modes described in tree-sitter-langs. Mind that the setup steps should be done before opening any files of interest, just in case. For more info head to https://emacs-tree-sitter.github.io/
cat-head · 2022-12-04 11:21
But the highlighting **is** being done by font-lock-mode. If I open a new file, and then deactivate font-lock-mode then there is no syntax highlighting at all, even if I then call `tree-sitter-hl-mode` explicitely.
grimscythe_ · 2022-12-04 11:25
Then don't disable it. Tree sitter just uses a different way of parsing the file, a more accurate and detailed way. I don't see anywhere in the docs that one should disable font-lock-mode. It is possible that the current implementation of cpp-mode provides the same definitions for font-lock as the tree-sitter package. Currently I work on a lot of typescript and tree-sitter is not only a TON faster, but it provides way more accurate syntax highlight.
cat-head · 2022-12-04 11:51
Ok, then that's just super confusing. But you're right. I can change the highlighting with ts queries. I was under the impression that font-lock-mode was no longer necessary.
grimscythe_ · 2022-12-04 12:08
I completely understand what you mean, I was pretty confused at first how tree-sitter operates. After reading the docs and running it I realised that all it does is replacing the standard regex syntax definitions provided by packages with its own tree based tokenization, which is faster and more accurate. Well...more accurate if the definition file for the language is accurate. Realistically tree-sitter is like LSP, but for syntax highlighting, but it still uses font-lock-mode under the hood in Emacs.
odeuteronomy · 2022-12-06 10:59
Perhaps this would help - https://olddeuteronomy.github.io/post/a-tree-sitter-config-that-works/
cat-head · 2022-12-06 11:01
No. There was in fact no problem. I was just confused because I thought I had to deactivate font-lock-mode, I didn't realize that tree-sitter was working.
odeuteronomy · 2022-12-06 11:31
Ah, I see. Thanks for your response.