Re: Better c-ts-mode indentation when there are macros in the way

[email protected] Sun, 26 Jul 2026 07:32:32 +0200
Newsgroups gmane.emacs.devel
Message-ID <[email protected]>
On 7/25/26 5:59 AM, Yuan Fu <[email protected]> wrote:
> I read through it and I like it! Especially since we’re limiting ourselves to just indentation and further just the parent-is rule, we only need to handle the case where the directive is the first line in the if body, and that makes the heuristic very simple. Very clever! I’ll incorporate this into c-ts-mode with some changes.
> 
> BTW, since you’ve been using this for a while, do you see any edge cases that this doesn’t handle? You mentioned that it’s not as brittle as you imagined, but did you encounter any concrete cases where it didn’t work?
> 
> Yuan
> 

Thanks for having mercy on my code! :)

Maybe adding new matchers instead of changing how parent-is, n-p-gp,
and friends behave is safer since it could break existing setups?
Ideally, I'd like a "matcher" that says:

    (look-upwards-til-anchor anchor-bol c-ts-mode-indent-offset)

And the "anchor" is any node that modifies indentation: if_statement,
compound_statement, argument_list, etc., but not preproc_* or
expression_* nodes. Like how a C programmer actually reasons about
indentation.

Brittle is relative. :) Walking the tree upwards is easy with
tree-sitter and does not cause lag. Surprisingly robust. But
tree-sitter-c often fails to parse preprocessor heavy code:

    printf(
#ifndef BRUH
	"tttt\n"
#endif
    );

or

    if (blah)
        return;
    else
#define EEH
	printf("wwah");

It is unfortunate because such code is not extremely rare. The grammar
needs an overhaul to better incorporate macros and to make it easier
for code editing. But that doesn't seem to be in the cards.

Here is my new proof-of-concept:

https://gist.github.com/bjourne/63ffea0c420bf84104c3f98ca1623ea6

I have this matcher as the last rule:

    (my/c-parent-is-container my/c-parent-bol c-ts-mode-indent-offset)


--
mvh/best regards Björn Lindqvist