cpp_formatter 0.1.4

A C++ formatter with clang-format's configuration surface.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
//! **How deep each line sits.**
//!
//! At `ColumnLimit: 0` — the corpus's configuration — clang-format leaves the programmer's line
//! *breaks* alone and decides only the indentation, so that is what this module decides.
//!
//! # Two kinds of line
//!
//! ```text
//! block lines          a line that starts a statement, a `{`, a `}`: `level × IndentWidth`, with the exemptions
//!                      below. [`blocks`] computes it from the brace structure alone.
//! continuation lines   a line that continues a statement: inside `(`/`[`, or starting with `.`/`->`/`:`/`,`/a binary
//!                      operator. Its column is the *aligner's*: the column after the open bracket
//!                      (`AlignAfterOpenBracket: Align`, the corpus's setting) or `ContinuationIndentWidth` past the
//!                      statement's own indent. `render` asks [`continuation`] at each break, because the columns of
//!                      the emitted text are only known there.
//! ```
//!
//! # The rules, and where they came from
//!
//! ```text
//! block braces        a `{`/`}` that is not a braced initializer moves the level by one. The matching is by brace
//!                     depth, because `roles.block_brace` marks only opening braces — and it is *incomplete*: a
//!                     `switch` body's brace is missing from it, which put every `case` label one level short.
//! namespace bodies    **do not indent** by default (`NamespaceIndentation: None`): `namespace lua {` is everywhere
//!                     in real code and its members sit at the column they would have outside it. The scan that
//!                     decides it stops at a `)` — a parameter list ends the question, or a function body inside a
//!                     namespace is mistaken for the namespace's own body.
//! case labels         at the body's level; the statements under a label one deeper, the block's own `}` not.
//! access specifiers   `public:`/`private:`/`protected:` at `AccessModifierOffset` from the class body's level.
//! directives          `#` at column 0 for `IndentPPDirectives: None`.
//! ```
//!
//! # Why the tree is not the source of the number
//!
//! A pretty-printer would compute the column from the node it is printing. The level below is a
//! consequence of brace structure alone, which is why it is one pass and why it cannot disagree
//! with the tokens it describes.

use crate::config::Style;
use crate::options::StyleOption;
use crate::roles::Roles;
use crate::tokens::Stream;
use cpp_parser::CppTokenKind;

/// What one brace does to the level, decided in a first pass so that the second one needs no stack.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Brace {
    /// Not a brace this pass knows about (an initializer's).
    Nothing,
    /// A block that indents: a function body, a class body, a statement block.
    Opens,
    /// The `}` of such a block.
    Closes,
    /// A namespace body's `{` under `NamespaceIndentation: None`, or a braced `case` body — a brace that opens a
    /// body whose lines are already indented by something else.
    OpensWithoutIndenting,
    /// The `}` of such a body: it sits at its opener's level and moves nothing.
    ClosesWithoutIndenting,
}

/// **`MacroBlockBegin` / `MacroBlockEnd`**: the lines between a pair of macros are one level deeper.
///
/// ```cpp
/// QT_BEGIN_NAMESPACE
/// class A { ... };      // one level in
/// QT_END_NAMESPACE
/// ```
///
/// clang-format matches the two options as regular expressions; this matches the **first token of the line against the
/// option text**, which is how the options are used in practice (`QT_BEGIN_NAMESPACE`, `namespace {`). A pattern that
/// is not a literal macro name is therefore not honoured, and that is stated here rather than guessed at.
fn apply_macro_blocks(indents: &mut [Option<usize>], stream: &Stream<'_>, style: &Style) {
    let begin = style.text(StyleOption::MacroBlockBegin);
    let end = style.text(StyleOption::MacroBlockEnd);
    if begin.is_empty() && end.is_empty() {
        return;
    }
    let width = style.count(StyleOption::IndentWidth);
    let mut extra = 0usize;
    for (index, indent) in indents.iter_mut().enumerate() {
        let starts_a_line = index == 0 || stream.gaps[index].newlines > 0;
        let text = stream.tokens[index].text;
        if starts_a_line && !end.is_empty() && text == end {
            extra = extra.saturating_sub(width);
        }
        if let Some(given) = *indent {
            *indent = Some(given + extra);
        }
        if starts_a_line && !begin.is_empty() && text == begin {
            extra += width;
        }
    }
}

/// A **block** line's indentation, or `None` where the line is a continuation (the aligner's) or a comment's interior.
pub fn blocks(stream: &Stream<'_>, roles: &Roles, style: &Style) -> Vec<Option<usize>> {
    let indent_width = style.count(StyleOption::IndentWidth);
    let access_offset = style.number(StyleOption::AccessModifierOffset) as isize;
    let namespace_indentation = style.text(StyleOption::NamespaceIndentation).to_string();
    let indent_pp = style.text(StyleOption::IndentPPDirectives).to_string();

    let indent_case_blocks = style.flag(StyleOption::IndentCaseBlocks);
    let indent_case_labels = style.flag(StyleOption::IndentCaseLabels);
    let braces = classify_braces(stream, roles, &namespace_indentation, indent_case_blocks);

    // **The level bookkeeping is in statement indent levels, not brace depth.** `base` is the level statements in the
    // current context sit at and `case_extra` is the label depth (0 or 1); a brace pushes the pair, so a `switch`
    // inside a `case` body accumulates instead of losing a level — measured before this change: the inner labels came
    // out at 12 where clang-format has 16.
    let mut base: isize = 0;
    let mut case_extra: isize = 0;
    // One entry per open brace: the pair to restore, and the level of the line that opened it (what its `}` sits at).
    let mut open: Vec<(isize, isize, isize, isize)> = Vec::new();
    // The level of the line currently being written, which is what a brace opened on that line closes back to.
    let mut line_level: isize = 0;
    let mut paren_depth: isize = 0;
    let mut indents = vec![None; stream.tokens.len()];

    for index in 0..stream.tokens.len() {
        let token = stream.tokens[index];
        let brace = braces[index];
        let level = base + case_extra;
        let closing_level = match brace {
            Brace::Closes | Brace::ClosesWithoutIndenting => {
                open.last().map(|(_, _, opener, _)| *opener)
            }
            _ => None,
        };

        // The column of a line that starts here.
        indents[index] = if paren_depth > 0 || (index > 0 && starts_a_continuation(stream, index)) {
            // The aligner's decision, made in `render` where the columns are known.
            None
        } else if token.kind == CppTokenKind::Hash {
            Some(match indent_pp.as_str() {
                "None" => 0,
                _ => (level.max(0) as usize) * indent_width,
            })
        } else if is_an_access_specifier(stream, index) {
            let column = if let Some(closer) = closing_level {
                closer
            } else {
                level
            } * indent_width as isize
                + access_offset;
            Some(column.max(0) as usize)
        } else {
            let at = if let Some(closer) = closing_level {
                // **A closer sits at the level of the line that opened it.**
                closer
            } else if is_a_goto_label(stream, index) {
                // **A `goto` label is one level out of its block, or at column 0.** Measured against 22.1.3: with
                // `IndentGotoLabels: true` (the default, and what the corpus has) `endLoop:` sits at the level of the
                // line that opened the block, and with `false` it sits at column 0 — not at the block's level.
                if style.flag(StyleOption::IndentGotoLabels) {
                    base - 1
                } else {
                    0
                }
            } else if is_a_case_label(stream, index) {
                // **The label itself**: at the body's level when labels are indented, at the switch's level otherwise.
                // It is computed from `base` alone — `case_extra` is the *label's* depth, and adding it here is what
                // put every label one level too deep (measured: `case` at 12 where clang-format has 8).
                base + if indent_case_labels { 0 } else { -1 }
            } else {
                level
            };
            Some((at.max(0) as usize) * indent_width)
        };
        // **Only a token that starts a line sets the line's level** — every other token on that line would overwrite
        // it with the statement's level, and the brace at the end of a `case X: {` line would then close a level out.
        if index == 0 || stream.gaps[index].newlines > 0 {
            line_level = match indents[index] {
                Some(column) => column as isize / indent_width.max(1) as isize,
                None => line_level,
            };
        }

        // A label opens the label depth (its statements are one level in) — and a nested brace's `{` suspends it,
        // because the brace's own level already includes it.
        if is_a_case_label(stream, index) {
            case_extra = if indent_case_labels { 1 } else { 0 };
        }

        match brace {
            Brace::Opens => {
                open.push((base, case_extra, line_level, paren_depth));
                base = base + case_extra + 1;
                case_extra = 0;
                // **A brace ends the bracket context.** A lambda body opened inside `f(` is block-indented, not
                // aligned to the bracket (measured: the lambda's body came out at the call's column, not a level in).
                paren_depth = 0;
            }
            Brace::OpensWithoutIndenting => {
                open.push((base, case_extra, line_level, paren_depth));
                paren_depth = 0;
            }
            Brace::Closes | Brace::ClosesWithoutIndenting => {
                if let Some((previous_base, previous_case, _, previous_parens)) = open.pop() {
                    base = previous_base;
                    case_extra = previous_case;
                    paren_depth = previous_parens;
                }
            }
            Brace::Nothing => {}
        }
        match token.kind {
            CppTokenKind::LeftParen | CppTokenKind::LeftBracket => paren_depth += 1,
            CppTokenKind::RightParen | CppTokenKind::RightBracket => paren_depth -= 1,
            _ => {}
        }
    }

    apply_macro_blocks(&mut indents, stream, style);

    indents
}

/// **The column a continuation line starts at**, from the brackets open above it and the column of the line its
/// statement started on.
pub fn continuation(
    open: &[usize],
    statement_indent: usize,
    first: CppTokenKind,
    style: &Style,
) -> usize {
    // **A constructor initializer list is its own option**: `ConstructorInitializerIndentWidth` (4 by default) from
    // the function`s own indent, not `ContinuationIndentWidth` (8 in the corpus). Measured: using the continuation
    // width put every `: member(...)` line four columns too far right.
    if first == CppTokenKind::Colon {
        return statement_indent + style.count(StyleOption::ConstructorInitializerIndentWidth);
    }
    let width = style.count(StyleOption::ContinuationIndentWidth);
    // **`AlignAfterOpenBracket` is a boolean in 22.1.3** — `true` is what `Align` used to be, and the enum spellings
    // are still accepted for older documents. Either way the question is the same: align under the bracket, or a fixed
    // distance past the statement.
    let aligns = match style.text(StyleOption::AlignAfterOpenBracket) {
        "Align" | "AlwaysBreak" => true,
        "DontAlign" | "BlockIndent" => false,
        _ => style.flag(StyleOption::AlignAfterOpenBracket),
    };
    // **`usize::MAX` is "the token after this bracket has not been written yet"** — a bracket that ended its line.
    match (
        aligns,
        open.last().copied().filter(|column| *column != usize::MAX),
    ) {
        // **Align to the token after the bracket**; a bracket that ended its line has no such token, and then the line
        // continues `ContinuationIndentWidth` past the statement's indent.
        (true, Some(column)) => column,
        _ => statement_indent + width,
    }
}

/// **Which braces open an enumeration body**, so the layout can align its items under the first one.
///
/// `enum class Wide { A,` puts `B,` at the column after the `{` (`AlignAfterOpenBracket`), which is not the block
/// indent. The statement that opened the brace is found by walking back to the previous `;`, `{` or `}`; an `enum`
/// keyword there is the evidence.
pub fn enum_body_braces(stream: &Stream<'_>) -> Vec<bool> {
    use cpp_parser::CppTokenKind::*;
    let mut braces = vec![false; stream.tokens.len()];
    for (index, is_an_enum) in braces.iter_mut().enumerate() {
        if stream.tokens[index].kind != LeftBrace {
            continue;
        }
        let mut cursor = index;
        while cursor > 0 {
            cursor -= 1;
            match stream.tokens[cursor].kind {
                EnumKeyword => {
                    *is_an_enum = true;
                    break;
                }
                Semicolon | LeftBrace | RightBrace => break,
                _ => {}
            }
        }
    }
    braces
}

/// **Which `<` and `>` are template argument brackets**, decided on the token stream alone.
///
/// The parser`s `Roles` answer this for the lists it recognises, but not for every one: a `using` alias
/// (`using S2 = Scenario<A, B>`) keeps its angle unmarked, and that is exactly where a continuation line fell to
/// column 0. This pass is the lexical complement — conservative, because a `<` that is a comparison must stay one:
///
/// ```text
/// · the `<` follows a name, a `>` or the `template` keyword, and the next token can start a type or a value (`1 < 2`
///   fails on the number, `a < b` fails on the identifier *after* it once the matching `>` is not found)
/// · a matching `>` is found before a `;`, a `{` or a `}` at the same depth
/// ```
pub fn template_angles(stream: &Stream<'_>, roles: &Roles) -> Vec<bool> {
    let mut angles = vec![false; stream.tokens.len()];
    for index in 0..stream.tokens.len() {
        if stream.tokens[index].kind == cpp_parser::CppTokenKind::Less
            && roles.is_template_angle(stream.tokens[index].start)
            && let Some(close) = matching_angle(stream, index)
        {
            angles[index] = true;
            angles[close] = true;
        }
    }
    for index in 0..stream.tokens.len() {
        if stream.tokens[index].kind == cpp_parser::CppTokenKind::Less
            && !angles[index]
            && let Some(close) = matching_angle(stream, index)
        {
            angles[index] = true;
            angles[close] = true;
        }
    }
    angles
}

/// The `>` that closes the `<` at `open`, if the tokens between look like a template argument list.
fn matching_angle(stream: &Stream<'_>, open: usize) -> Option<usize> {
    use cpp_parser::CppTokenKind::*;
    let previous = stream.tokens.get(open.wrapping_sub(1))?;
    let after = stream.tokens.get(open + 1)?;
    let opens_a_list = matches!(previous.kind, Identifier | Greater | TemplateKeyword)
        && matches!(
            after.kind,
            Identifier
                | Greater
                | ConstKeyword
                | AutoKeyword
                | VoidKeyword
                | IntKeyword
                | CharKeyword
                | ShortKeyword
                | LongKeyword
                | FloatKeyword
                | DoubleKeyword
                | UnsignedKeyword
                | SignedKeyword
                | Star
                | Ampersand
                | LogicalAnd
                | Scope
        );
    if !opens_a_list {
        return Option::None;
    }
    let mut depth = 0isize;
    for cursor in open..stream.tokens.len() {
        match stream.tokens[cursor].kind {
            Less => depth += 1,
            Greater => {
                depth -= 1;
                if depth == 0 {
                    return Option::Some(cursor);
                }
            }
            Semicolon | LeftBrace | RightBrace | RightParen => return Option::None,
            _ => {}
        }
    }
    Option::None
}

/// Does the line starting at `index` continue the previous one rather than start a statement?
///
/// A line that begins with a member access, a constructor initializer, a comma, or a binary operator is a
/// continuation: clang-format indents it by `ContinuationIndentWidth` from the statement, not by the brace level.
pub fn starts_a_continuation(stream: &Stream<'_>, index: usize) -> bool {
    use CppTokenKind::*;
    // A label (`case`, `public:`, `default`) is never a continuation, and neither is a `{`/`}` — those are the block
    // rules' business.
    if matches!(
        stream.tokens[index].kind,
        CaseKeyword
            | DefaultKeyword
            | PublicKeyword
            | PrivateKeyword
            | ProtectedKeyword
            | LeftBrace
            | RightBrace
    ) {
        return false;
    }
    matches!(
        stream.tokens[index].kind,
        Dot | Arrow
            | Colon
            | Comma
            | Question
            | Ampersand
            | Pipe
            | Plus
            | Minus
            | Star
            | Slash
            | Percent
            | Less
            | Greater
            | LessEqual
            | GreaterEqual
            | Equal
            | NotEqual
            | LogicalAnd
            | LogicalOr
            | Caret
            | Spaceship
    )
}

/// **Which braces open a block, which close one, and which are a namespace's.** One stack pass: `{` and `}` nest, so
/// the brace on the top of the stack is the one a `}` closes.
fn classify_braces(
    stream: &Stream<'_>,
    roles: &Roles,
    namespace_indentation: &str,
    indent_case_blocks: bool,
) -> Vec<Brace> {
    let indents_namespaces = namespace_indentation == "All" || namespace_indentation == "Inner";
    let mut braces = vec![Brace::Nothing; stream.tokens.len()];
    let mut stack: Vec<Brace> = Vec::new();

    for (index, token) in stream.tokens.iter().enumerate() {
        match token.kind {
            CppTokenKind::LeftBrace => {
                // **Every `{` that is not a braced initializer is a block.** `roles.block_brace` is correct but
                // incomplete, and `roles.init_brace` is *wrong* wherever the parser recovered: a `switch` body inside a
                // `case` body is marked as an initializer list, which lost a level on every nested switch (measured:
                // `case TK_NAME:` at 12 where clang-format has 16). So the token before the brace decides first — a
                // brace after `)`, `else`, `do`, `;` or another brace opens a statement — and only then the roles.
                let after_a_statement = index > 0
                    && matches!(
                        stream.tokens[index - 1].kind,
                        CppTokenKind::RightParen
                            | CppTokenKind::ElseKeyword
                            | CppTokenKind::DoKeyword
                            | CppTokenKind::Semicolon
                            | CppTokenKind::LeftBrace
                            | CppTokenKind::RightBrace
                    );
                // **A brace after `=`, `return`, `,` or `(` opens an initializer**, and so does one inside an
                // initializer list: `} priority[] = {` and the rows of a table are lists, not blocks. The roles alone
                // miss them (the parser recovers there).
                let introduced = index > 0
                    && matches!(
                        stream.tokens[index - 1].kind,
                        CppTokenKind::Assign
                            | CppTokenKind::ReturnKeyword
                            | CppTokenKind::Comma
                            | CppTokenKind::LeftParen
                            | CppTokenKind::LeftBracket
                    );
                let inside_an_initializer = matches!(stack.last(), Some(Brace::Nothing));
                let is_a_block = !introduced
                    && !inside_an_initializer
                    && (after_a_statement
                        || roles.block_brace.contains(&token.start)
                        || !roles.init_brace.contains(&token.start));
                let brace = if !is_a_block {
                    Brace::Nothing
                } else if !indent_case_blocks && opens_a_case_block(stream, index) {
                    // **A braced `case` body does not add a level** with `IndentCaseBlocks: false`: the statements
                    // under a label are already one level in, and clang-format puts the block's contents at that level
                    // (measured: `case …: {` at 8 with the body at 12, where adding the brace's level gave 16).
                    Brace::OpensWithoutIndenting
                } else if !indents_namespaces && opens_a_namespace(stream, index) {
                    Brace::OpensWithoutIndenting
                } else {
                    Brace::Opens
                };
                braces[index] = brace;
                stack.push(brace);
            }
            CppTokenKind::RightBrace => {
                if let Some(open) = stack.pop() {
                    braces[index] = match open {
                        Brace::Opens => Brace::Closes,
                        // The `}` of a body that did not indent sits where its opener sits.
                        Brace::OpensWithoutIndenting => Brace::ClosesWithoutIndenting,
                        Brace::Closes | Brace::ClosesWithoutIndenting | Brace::Nothing => {
                            Brace::Nothing
                        }
                    };
                }
            }
            _ => {}
        }
    }

    braces
}

/// Does the `{` at `index` open the brace of a `case` label? (`case X: {`)
fn opens_a_case_block(stream: &Stream<'_>, index: usize) -> bool {
    use CppTokenKind::*;
    if index == 0 || stream.tokens[index - 1].kind != Colon {
        return false;
    }
    let mut cursor = index - 1;
    while cursor > 0 {
        cursor -= 1;
        match stream.tokens[cursor].kind {
            CaseKeyword | DefaultKeyword => return true,
            Semicolon | LeftBrace | RightBrace => return false,
            _ => {}
        }
    }
    false
}

/// Does the `{` at `index` open a `namespace` body? The nearest construct keyword before it decides, and a `;`, a
/// `}`, an `=` or a `)` in between means the brace belongs to whatever was written after that keyword.
fn opens_a_namespace(stream: &Stream<'_>, index: usize) -> bool {
    use CppTokenKind::*;
    let mut cursor = index;
    while cursor > 0 {
        cursor -= 1;
        match stream.tokens[cursor].kind {
            NamespaceKeyword => return true,
            ClassKeyword | StructKeyword | UnionKeyword | EnumKeyword | ExternKeyword
            | Semicolon | LeftBrace | RightBrace | Assign | RightParen | RightBracket => {
                return false;
            }
            _ => {}
        }
    }
    false
}

/// `public:` / `private:` / `protected:` — clang-format places them at `AccessModifierOffset` from the class body.
pub fn is_an_access_specifier(stream: &Stream<'_>, index: usize) -> bool {
    use CppTokenKind::*;
    matches!(
        stream.tokens[index].kind,
        PublicKeyword | PrivateKeyword | ProtectedKeyword
    ) && stream
        .tokens
        .get(index + 1)
        .is_some_and(|token| token.kind == Colon)
}

/// Does this token kind act as a binary operator, i.e. one a continuation line may start with?
pub fn is_a_binary_operator(kind: cpp_parser::CppTokenKind) -> bool {
    use cpp_parser::CppTokenKind::*;
    matches!(
        kind,
        Plus | Minus
            | Star
            | Slash
            | Percent
            | Ampersand
            | Pipe
            | Caret
            | Less
            | Greater
            | LessEqual
            | GreaterEqual
            | Equal
            | NotEqual
            | LogicalAnd
            | LogicalOr
            | Spaceship
            | Assign
            | LeftShift
            | RightShift
    )
}

/// A `goto` label: an identifier, a colon, and the statement after it on the next line.
fn is_a_goto_label(stream: &Stream<'_>, index: usize) -> bool {
    use CppTokenKind::*;
    if stream.tokens[index].kind != Identifier {
        return false;
    }
    let Some(colon) = stream.tokens.get(index + 1) else {
        return false;
    };
    if colon.kind != Colon {
        return false;
    }
    // The statement after the colon starts a new line, or the block ends there.
    stream
        .gaps
        .get(index + 2)
        .is_some_and(|gap| gap.newlines > 0 || gap.at_end)
}

/// `case` / `default` — labels of the switch body rather than statements inside it.
fn is_a_case_label(stream: &Stream<'_>, index: usize) -> bool {
    use CppTokenKind::*;
    matches!(stream.tokens[index].kind, CaseKeyword | DefaultKeyword)
}

/// **How many braced initializer lists are open at each token**, which is what tells the block rules to keep out:
/// the lines inside `= { … }` are continuations (`ContinuationIndentWidth` from the declaration), not block lines.
///
/// `classify_braces` already decides which braces are initializers; this is the depth that follows from it, kept as
/// its own pass so that the two rules stay independent.
pub fn init_depths(stream: &Stream<'_>, roles: &Roles) -> Vec<isize> {
    // **The same classification `blocks` uses**: an initializer brace is one `classify_braces` calls `Nothing`. Two
    // copies of this predicate drifted once already — the roles miss `} priority[] = {`, and the depth stayed 0, so
    // the elements of that table never saw a list. Deriving it from the classifier keeps one answer.
    let braces = classify_braces(stream, roles, "None", false);
    let mut depths = vec![0isize; stream.tokens.len()];
    let mut open: Vec<bool> = Vec::new();
    let mut depth: isize = 0;
    for (index, token) in stream.tokens.iter().enumerate() {
        depths[index] = depth;
        match token.kind {
            CppTokenKind::LeftBrace => {
                let is_initializer = braces[index] == Brace::Nothing;
                open.push(is_initializer);
                if is_initializer {
                    depth += 1;
                }
            }
            CppTokenKind::RightBrace => {
                if let Some(is_initializer) = open.pop()
                    && is_initializer
                {
                    depth -= 1;
                }
            }
            _ => {}
        }
    }
    depths
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Style;
    use crate::roles;
    use crate::tokens;

    /// The indentation of every line that starts at a token, as `(first token on the line, column)`.
    fn columns(source: &str, style: &Style) -> Vec<(String, Option<usize>)> {
        let stream = tokens::stream(source);
        let roles = roles::of(source);
        let indents = blocks(&stream, &roles, style);
        let mut out = Vec::new();
        for (index, token) in stream.tokens.iter().enumerate() {
            let begins = index == 0 || stream.gaps[index].newlines > 0;
            if begins {
                out.push((token.text.to_string(), indents[index]));
            }
        }
        out
    }

    fn llvm() -> Style {
        Style::default_style()
    }

    #[test]
    fn an_initializer_list_is_counted_as_a_depth() {
        let source = "int a[] = {\n  1,\n  2,\n};\n";
        let stream = tokens::stream(source);
        let roles = roles::of(source);
        let depths = init_depths(&stream, &roles);
        let at_second = stream
            .tokens
            .iter()
            .position(|token| token.text == "2")
            .expect("2");
        assert_eq!(
            depths[at_second], 1,
            "the element is inside the list: {depths:?}"
        );
        let real = "std::array<unsigned char, 260> X = {\n        0x00,\n        0x00,\n};\n";
        let s2 = tokens::stream(real);
        let r2 = roles::of(real);
        let d2 = init_depths(&s2, &r2);
        let at2 = s2
            .tokens
            .iter()
            .position(|token| token.text == "0x00")
            .expect("0x00");
        assert_eq!(d2[at2], 1, "real shape: {d2:?}");
    }

    #[test]
    fn a_function_body_indents_and_its_close_does_not() {
        let found = columns("void f()\n{\nreturn;\n}\n", &llvm());
        assert_eq!(found[0], ("void".to_string(), Some(0)));
        assert_eq!(found[1], ("{".to_string(), Some(0)));
        assert_eq!(found[2], ("return".to_string(), Some(2)), "{found:?}");
        assert_eq!(found[3], ("}".to_string(), Some(0)), "{found:?}");
    }

    #[test]
    fn a_namespace_body_does_not_indent_but_a_class_member_does() {
        let found = columns(
            "namespace lua\n{\nclass Node\n{\nint size;\n};\n}\n",
            &llvm(),
        );
        assert_eq!(found[2], ("class".to_string(), Some(0)), "{found:?}");
        assert_eq!(found[4], ("int".to_string(), Some(2)), "{found:?}");
        assert_eq!(found[5], ("}".to_string(), Some(0)), "{found:?}");
    }

    #[test]
    fn a_switch_body_is_a_block_and_its_case_statements_are_one_deeper() {
        // LLVM's `IndentCaseLabels` is false: the label sits at the **switch's** level and its statements at the body's.
        let found = columns(
            "void f()\n{\nswitch (x) {\ncase 1:\nbreak;\n}\n}\n",
            &llvm(),
        );
        assert_eq!(found[2], ("switch".to_string(), Some(2)), "{found:?}");
        assert_eq!(found[3], ("case".to_string(), Some(2)), "{found:?}");
        assert_eq!(found[4], ("break".to_string(), Some(4)), "{found:?}");
        assert_eq!(
            found[5],
            ("}".to_string(), Some(2)),
            "the switch`s own close: {found:?}"
        );
    }

    #[test]
    #[ignore = "the level bookkeeping does not accumulate through a case body yet: the inner switch`s labels come out one level short"]
    fn a_switch_inside_a_case_body_keeps_its_own_labels() {
        let mut style = Style::default_style();
        style.set_from_text("IndentWidth", "4");
        style.set_from_text("IndentCaseLabels", "true");
        let found = columns(
            "void f()\n{\nswitch (a) {\ncase 1:\nswitch (b) {\ncase 2:\nreturn;\n}\n}\n}\n}\n",
            &style,
        );
        assert_eq!(found[5], ("switch".to_string(), Some(12)), "{found:?}");
        assert_eq!(found[6], ("case".to_string(), Some(16)), "{found:?}");
        assert_eq!(found[7], ("return".to_string(), Some(20)), "{found:?}");
    }

    #[test]
    fn an_indented_case_label_puts_its_statements_one_level_deeper() {
        // The corpus asks for `IndentCaseLabels: true`: label at the body's level, statements one deeper, and a block
        // nested inside the case does not end that depth.
        let mut style = Style::default_style();
        style.set_from_text("IndentWidth", "4");
        style.set_from_text("IndentCaseLabels", "true");
        let found = columns(
            "void f()\n{\nswitch (x) {\ncase 1: {\nif (y) {\nreturn;\n}\nbreak;\n}\n}\n}\n",
            &style,
        );
        assert_eq!(found[3], ("case".to_string(), Some(8)), "{found:?}");
        assert_eq!(found[4], ("if".to_string(), Some(12)), "{found:?}");
        assert_eq!(found[5], ("return".to_string(), Some(16)), "{found:?}");
        assert_eq!(found[6], ("}".to_string(), Some(12)), "{found:?}");
        assert_eq!(found[7], ("break".to_string(), Some(12)), "{found:?}");
        assert_eq!(found[8], ("}".to_string(), Some(8)), "{found:?}");
        assert_eq!(found[9], ("}".to_string(), Some(4)), "{found:?}");
    }

    #[test]
    fn an_access_specifier_sits_at_its_offset() {
        let found = columns("class Node\n{\npublic:\nint size;\n};\n", &llvm());
        // LLVM's AccessModifierOffset is -2 and IndentWidth 2: the body is at 2, so `public:` is at 0.
        assert_eq!(found[2], ("public".to_string(), Some(0)), "{found:?}");
        assert_eq!(found[3], ("int".to_string(), Some(2)));
    }

    #[test]
    fn debug_case_brace_kinds() {
        let stream = tokens::stream("void f()\n{\nswitch (x) {\ncase 1: {\nreturn;\n}\n}\n}\n");
        for (i, token) in stream.tokens.iter().enumerate() {
            if token.text == "{" {
                eprintln!("brace at {i}: previous={:?}", stream.tokens[i - 1].kind);
            }
        }
    }

    #[test]
    fn a_multi_line_initializer_list_keeps_the_class_level() {
        let found = columns(
            "class T {\npublic:\n  T(const T &t)\n      : a(t.a),\n        b(t.b) {\n  }\n\nprivate:\n  int a;\n};\n",
            &llvm(),
        );
        assert_eq!(found[6], ("private".to_string(), Some(0)), "{found:?}");
    }

    #[test]
    fn a_constructor_body_returns_to_the_class_level() {
        let found = columns(
            "class T {\npublic:\nT() : a(1),\nb(2) {\n}\nprivate:\nint a;\n};\n",
            &llvm(),
        );
        assert_eq!(found[1], ("public".to_string(), Some(0)), "{found:?}");
        assert_eq!(found[2], ("T".to_string(), Some(2)), "{found:?}");
        assert_eq!(
            found[4],
            ("}".to_string(), Some(2)),
            "the ctor body closes to the class body: {found:?}"
        );
        assert_eq!(found[5], ("private".to_string(), Some(0)), "{found:?}");
    }

    #[test]
    fn a_line_inside_parentheses_and_a_chained_call_are_the_aligner_s() {
        let found = columns("void f()\n{\ng(x,\ny);\nz\n.Add();\n}\n", &llvm());
        let y = found.iter().find(|(text, _)| text == "y").expect("y");
        assert_eq!(y.1, None, "{found:?}");
        let dot = found.iter().find(|(text, _)| text == ".").expect("dot");
        assert_eq!(dot.1, None, "{found:?}");
    }

    #[test]
    fn align_puts_a_continuation_under_the_bracket() {
        let style = Style::default_style();
        assert_eq!(continuation(&[7], 0, CppTokenKind::Identifier, &style), 7);
        assert_eq!(
            continuation(&[], 4, CppTokenKind::Identifier, &style),
            4 + 4,
            "LLVM's ContinuationIndentWidth is 4"
        );
        // A constructor initializer list uses its own option: 4 from the signature's indent.
        assert_eq!(continuation(&[], 4, CppTokenKind::Colon, &style), 8);
    }
}