c++emacstreesitter

Emacs and tree-sitter: Problems with installing c++ language grammar


I've problems with installing the tree-sitter grammar for c++ in Emacs. I use Emacs 29.4 with build-in tree-sitter on Arch Linux. This is what I did so far:

First, I defined a build receipt:

(setq treesit-language-source-alist
      '((c++ "https://github.com/tree-sitter/tree-sitter-cpp")))

Then I called:

M-x treesit-install-language-grammar RET c++ RET

I got the following message in *Warnings*:

⛔ Warning (treesit): The installed language grammar for c++ cannot be located or has problems (symbol-error): /home/tilman/.emacs.d/tree-sitter/libtree-sitter-c++.so: undefined symbol: tree_sitter_c++

Nevertheless, the library was build and installed in ~/.emacs.d/tree-sitter/libtree-sitter-c++.so.

I opened a ++ source file and typed

M-X c++-ts-mode RET

But there was almost no syntax highlighting, and in *Messages* the following occurred:

Error during redisplay: (jit-lock-function 1) signaled (treesit-query-error "Node type error at" 575 "[\"_Atomic\" \"break\" \"case\" \"const\" \"continue\" \"default\" \"do\" \"else\" \"enum\" \"extern\" \"for\" \"goto\" \"if\" \"inline\" \"register\" \"restrict\" \"return\" \"sizeof\" \"static\" \"struct\" \"switch\" \"typedef\" \"union\" \"volatile\" \"while\" \"and\" \"and_eq\" \"bitand\" \"bitor\" \"catch\" \"class\" \"co_await\" \"co_return\" \"co_yield\" \"compl\" \"concept\" \"consteval\" \"constexpr\" \"constinit\" \"decltype\" \"delete\" \"explicit\" \"final\" \"friend\" \"mutable\" \"namespace\" \"new\" \"noexcept\" \"not\" \"not_eq\" \"operator\" \"or\" \"or_eq\" \"override\" \"private\" \"protected\" \"public\" \"requires\" \"template\" \"throw\" \"try\" \"typename\" \"using\" \"virtual\" \"xor\" \"xor_eq\"] @font-lock-keyword-face (auto) @font-lock-keyword-face (this) @font-lock-keyword-face" "Debug the query with `treesit-query-validate'")

I searched the internet for that error, and found a few posts saying that the version of the grammar might be too new for Emacs. This seems to be a general problem. Especially for c++, I found this reddit thread. The users there had the same problem as I have. Some said it worked after they downgraded to version v0.22.0. Thus, I added the version to the receipt,

(setq treesit-language-source-alist
      '((c++ "https://github.com/tree-sitter/tree-sitter-cpp" "v0.22.0")))

and tried again - same result. It doesn't work.

Any help is greatly appreciated!

Best regards, Tilman


Solution

  • The name of the C++ grammar is cpp not c++, so the treesit-language-source-alist should be

    (setq treesit-language-source-alist
          '((cpp "https://github.com/tree-sitter/tree-sitter-cpp" "v0.22.0")))
    

    You can find the name of a grammar in the associated grammar.js. Or, by running strings libtree-sitter-cpp.so on the compiled library to find the tree_sitter_<parser_name> symbol.

    Generally, the grammar name is the repo name without the tree-sitter- prefix.