1

I'm running emacs 30.2 (built locally from source), on Ubuntu, using doom. Today, I was editing some c++ code that uses iostreams, and I had multiple lines that started with the '<<' operator. My preferred indentation style looks like:

std::cout << "here's some text" << " and some more text\n"; 

What tree-sitter prefers is:

std::cout << "here's some text" << " and some more text\n"; 

I've been looking around for information about configuring tree-sitter modes, and have found it to be somewhat impenetrable. Before I go completely down the rabbit-hole, I thought I'd ask here to see if this is a solved problem.

3
  • How are you enabling tree-sitter mode in your C++ file? Are you doing the equivalent of M-x c++-ts-mode? That's a naive question: I don't know what the proper setup is and, given @Ian's answer (with multiple options given), I'm wondering how exactly you go about it. Commented Oct 7 at 3:09
  • @NickD I don't think it's a naive question; I'm in the process of sorting it out myself. There's the confusing history of the external tree-sitter package (which I had been building with) vs the built-in treesit machinery, and how existing cc-mode settings are handled. I focused on the '<<' issue because that was an obvious place where c-ts-mode wasn't doing what I wanted. Commented Oct 7 at 11:48
  • I'm not sure I understand what @Ian is driving at in his answer, but my take on tree-sitter support in Emacs can be found at this answer - although N.B. it does not answer your indentation question. But if the linked answer is indeed the way to use tree-sitter in Emacs, then the answer to your question is to customize treesit-simple-indent-rules (how to customize it is TBD: I have not gone through the exercise). Commented Oct 13 at 1:58

1 Answer 1

1
+50

Your question is related to indentation style category. See this page. To solve, there are two possible solutions:

  • cc-mode is built-in. Beside the reading that documentation, I can add only a small hint: use a complete C/C++ file which already has the correct indentation, then ask Emacs to guess the style used, ask to show the guessed style, name it as you wish and save it for further use.

  • treesitter + clangd + clang-format. In Emacs30 there is built-in package treesit, you need to add some grammars to it, use this package

Add the clang-format and read/use this page to modify the styles as you need. Useful to use with clangd.

Please note the built-in treesit mode has only two styles implemented: GNU and Linux, and you cannot use it with cc-mode, so you must generate and eventually modify the generated .clang-format file.

Of course, you can add the built-in eglot mode or lsp-mode. For doomemacs, please read the documentation - in modules/lang/cc-mode you have a Readme.org file to help you with installing these modes. AFAIK at this moment, treesit mode is recently added (up to now was tree-sitter mode, external package).

8
  • In reading the documentation for treesit-auto, the author expresses the expectation that it will be made obsolete in emacs30. Did that come to pass? Commented Oct 6 at 17:45
  • No; still working. The author speaks about an "eventually obsolescence", nothing is sure. Commented Oct 7 at 5:51
  • 1
    I'm accepting your answer. I didn't actually end up using any of the approaches you provided, but it did prompt me to rebuild emacs without using the "--with-tree-sitter" configuration setting. Commented Oct 7 at 12:28
  • Yes, that rebuild is mandatory. The actual doomemacs uses the built-in trees it, you need to provide +treesit switch in your configuration for cc (see modules/tools/tree-sitter). An alternate solution is to make your own file/package for c/cpp mode. You can add clang-formatting in your configuration if you still have problems, and generate a .clang-format file; there is on Melpa a clang-format package. Commented Oct 7 at 13:34
  • See. modules/editor/format - this uses the package apheleia, is a good one to have formatting for various programming language, it uses clang-format, so it is good to have it. Commented Oct 7 at 13:40

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.