Re: Better c-ts-mode indentation when there are macros in the way
[email protected] Tue, 28 Jul 2026 04:41:37 +0200
| Newsgroups | gmane.emacs.devel |
|---|---|
| Message-ID | <[email protected]> |
On 7/26/26 7:32 AM, [email protected] wrote: > 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 Hello again! Here is a complete c-ts-mode indentation setup, in case anyone want to test it: https://gist.github.com/bjourne/31f53043c72728cb4d8b90feedd0625b In addition to correcting some tree-sitter-c preprocessor macro misparses, it also handles cases where you need to arbitrarily walk up the tree. For example, in return c1 || c2 || c3 || c4 || c5; I want c3 to "dangle" because it continues the return statement. But in return (c1 || c2 || c3 || c4 || c5); I want c3 to line up because the opening paranthesis isn't alone. Since binary_expression nodes (and similar node types) can be arbitrarily nested, you need to walk past them when looking for the ancestor you should anchor identation to. -- mvh/best regards Björn Lindqvist