ruff_python_formatter 0.0.2

This is an internal component crate of Ruff
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
use ruff_formatter::{Argument, Arguments, FormatError, write};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::{
    ElifElseClause, ExceptHandlerExceptHandler, MatchCase, StmtClassDef, StmtFor, StmtFunctionDef,
    StmtIf, StmtMatch, StmtTry, StmtWhile, StmtWith, Suite,
};
use ruff_python_trivia::{SimpleToken, SimpleTokenKind, SimpleTokenizer};
use ruff_source_file::LineRanges;
use ruff_text_size::{Ranged, TextRange, TextSize};

use crate::comments::{SourceComment, leading_alternate_branch_comments, trailing_comments};
use crate::statement::suite::{SuiteKind, as_only_an_ellipsis};
use crate::verbatim::{verbatim_text, write_suppressed_clause_header};
use crate::{has_skip_comment, prelude::*};

/// The header of a compound statement clause.
///
/// > A compound statement consists of one or more ‘clauses.’ A clause consists of a header and a ‘suite.’
/// > The clause headers of a particular compound statement are all at the same indentation level.
/// > Each clause header begins with a uniquely identifying keyword and ends with a colon.
///
/// [source](https://docs.python.org/3/reference/compound_stmts.html#compound-statements)
#[derive(Copy, Clone)]
pub(crate) enum ClauseHeader<'a> {
    Class(&'a StmtClassDef),
    Function(&'a StmtFunctionDef),
    If(&'a StmtIf),
    ElifElse(&'a ElifElseClause),
    Try(&'a StmtTry),
    ExceptHandler(&'a ExceptHandlerExceptHandler),
    TryFinally(&'a StmtTry),
    Match(&'a StmtMatch),
    MatchCase(&'a MatchCase),
    For(&'a StmtFor),
    While(&'a StmtWhile),
    With(&'a StmtWith),
    OrElse(ElseClause<'a>),
}

impl<'a> ClauseHeader<'a> {
    /// Returns the last child in the clause body immediately following this clause header.
    ///
    /// For most clauses, this is the last statement in
    /// the primary body. For clauses like `try`, it specifically returns the last child
    /// in the `try` body, not the `except`/`else`/`finally` clauses.
    ///
    /// This is similar to [`ruff_python_ast::AnyNodeRef::last_child_in_body`]
    /// but restricted to the clause.
    pub(crate) fn last_child_in_clause(self) -> Option<AnyNodeRef<'a>> {
        match self {
            ClauseHeader::Class(StmtClassDef { body, .. })
            | ClauseHeader::Function(StmtFunctionDef { body, .. })
            | ClauseHeader::If(StmtIf { body, .. })
            | ClauseHeader::ElifElse(ElifElseClause { body, .. })
            | ClauseHeader::Try(StmtTry { body, .. })
            | ClauseHeader::MatchCase(MatchCase { body, .. })
            | ClauseHeader::For(StmtFor { body, .. })
            | ClauseHeader::While(StmtWhile { body, .. })
            | ClauseHeader::With(StmtWith { body, .. })
            | ClauseHeader::ExceptHandler(ExceptHandlerExceptHandler { body, .. })
            | ClauseHeader::OrElse(
                ElseClause::Try(StmtTry { orelse: body, .. })
                | ElseClause::For(StmtFor { orelse: body, .. })
                | ElseClause::While(StmtWhile { orelse: body, .. }),
            )
            | ClauseHeader::TryFinally(StmtTry {
                finalbody: body, ..
            }) => body.last().map(AnyNodeRef::from),
            ClauseHeader::Match(StmtMatch { cases, .. }) => cases
                .last()
                .and_then(|case| case.body.last().map(AnyNodeRef::from)),
        }
    }

    /// The range from the clause keyword up to and including the final colon.
    pub(crate) fn range(self, source: &str) -> FormatResult<TextRange> {
        let keyword_range = self.first_keyword_range(source)?;

        let mut last_child_end = None;

        self.visit(&mut |child| last_child_end = Some(child.end()));

        let end = match self {
            ClauseHeader::Class(class) => Some(last_child_end.unwrap_or(class.name.end())),
            ClauseHeader::Function(function) => Some(last_child_end.unwrap_or(function.name.end())),
            ClauseHeader::ElifElse(_)
            | ClauseHeader::Try(_)
            | ClauseHeader::If(_)
            | ClauseHeader::TryFinally(_)
            | ClauseHeader::Match(_)
            | ClauseHeader::MatchCase(_)
            | ClauseHeader::For(_)
            | ClauseHeader::While(_)
            | ClauseHeader::With(_)
            | ClauseHeader::OrElse(_) => last_child_end,

            ClauseHeader::ExceptHandler(handler) => {
                handler.name.as_ref().map(Ranged::end).or(last_child_end)
            }
        };

        let colon = colon_range(end.unwrap_or(keyword_range.end()), source)?;

        Ok(TextRange::new(keyword_range.start(), colon.end()))
    }

    /// Visits the nodes in the case header.
    pub(crate) fn visit<F>(self, visitor: &mut F)
    where
        F: FnMut(AnyNodeRef),
    {
        fn visit<'a, N, F>(node: N, visitor: &mut F)
        where
            N: Into<AnyNodeRef<'a>>,
            F: FnMut(AnyNodeRef<'a>),
        {
            visitor(node.into());
        }

        match self {
            ClauseHeader::Class(StmtClassDef {
                type_params,
                arguments,
                range: _,
                node_index: _,
                decorator_list: _,
                name: _,
                body: _,
            }) => {
                if let Some(type_params) = type_params.as_deref() {
                    visit(type_params, visitor);
                }

                if let Some(arguments) = arguments {
                    visit(arguments.as_ref(), visitor);
                }
            }
            ClauseHeader::Function(StmtFunctionDef {
                type_params,
                parameters,
                range: _,
                node_index: _,
                is_async: _,
                decorator_list: _,
                name: _,
                returns,
                body: _,
            }) => {
                if let Some(type_params) = type_params.as_deref() {
                    visit(type_params, visitor);
                }
                visit(parameters.as_ref(), visitor);

                if let Some(returns) = returns.as_deref() {
                    visit(returns, visitor);
                }
            }
            ClauseHeader::If(StmtIf {
                test,
                range: _,
                node_index: _,
                body: _,
                elif_else_clauses: _,
            }) => {
                visit(test.as_ref(), visitor);
            }
            ClauseHeader::ElifElse(ElifElseClause {
                test,
                range: _,
                node_index: _,
                body: _,
            }) => {
                if let Some(test) = test.as_ref() {
                    visit(test, visitor);
                }
            }

            ClauseHeader::ExceptHandler(ExceptHandlerExceptHandler {
                type_: type_expr,
                range: _,
                node_index: _,
                name: _,
                body: _,
            }) => {
                if let Some(type_expr) = type_expr.as_deref() {
                    visit(type_expr, visitor);
                }
            }
            ClauseHeader::Match(StmtMatch {
                subject,
                range: _,
                node_index: _,
                cases: _,
            }) => {
                visit(subject.as_ref(), visitor);
            }
            ClauseHeader::MatchCase(MatchCase {
                guard,
                pattern,
                range: _,
                node_index: _,
                body: _,
            }) => {
                visit(pattern, visitor);

                if let Some(guard) = guard.as_deref() {
                    visit(guard, visitor);
                }
            }
            ClauseHeader::For(StmtFor {
                target,
                iter,
                range: _,
                node_index: _,
                is_async: _,
                body: _,
                orelse: _,
            }) => {
                visit(target.as_ref(), visitor);
                visit(iter.as_ref(), visitor);
            }
            ClauseHeader::While(StmtWhile {
                test,
                range: _,
                node_index: _,
                body: _,
                orelse: _,
            }) => {
                visit(test.as_ref(), visitor);
            }
            ClauseHeader::With(StmtWith {
                items,
                range: _,
                node_index: _,
                is_async: _,
                body: _,
            }) => {
                for item in items {
                    visit(item, visitor);
                }
            }
            ClauseHeader::Try(_) | ClauseHeader::TryFinally(_) | ClauseHeader::OrElse(_) => {}
        }
    }

    /// Returns the range of the first keyword that marks the start of the clause header.
    fn first_keyword_range(self, source: &str) -> FormatResult<TextRange> {
        match self {
            ClauseHeader::Class(header) => {
                let start_position = header
                    .decorator_list
                    .last()
                    .map_or_else(|| header.start(), Ranged::end);
                find_keyword(
                    StartPosition::ClauseStart(start_position),
                    SimpleTokenKind::Class,
                    source,
                )
            }
            ClauseHeader::Function(header) => {
                let start_position = header
                    .decorator_list
                    .last()
                    .map_or_else(|| header.start(), Ranged::end);
                let keyword = if header.is_async {
                    SimpleTokenKind::Async
                } else {
                    SimpleTokenKind::Def
                };
                find_keyword(StartPosition::ClauseStart(start_position), keyword, source)
            }
            ClauseHeader::If(header) => find_keyword(
                StartPosition::clause_start(header),
                SimpleTokenKind::If,
                source,
            ),
            ClauseHeader::ElifElse(ElifElseClause {
                test: None, range, ..
            }) => find_keyword(
                StartPosition::clause_start(range),
                SimpleTokenKind::Else,
                source,
            ),
            ClauseHeader::ElifElse(ElifElseClause {
                test: Some(_),
                range,
                ..
            }) => find_keyword(
                StartPosition::clause_start(range),
                SimpleTokenKind::Elif,
                source,
            ),
            ClauseHeader::Try(header) => find_keyword(
                StartPosition::clause_start(header),
                SimpleTokenKind::Try,
                source,
            ),
            ClauseHeader::ExceptHandler(header) => find_keyword(
                StartPosition::clause_start(header),
                SimpleTokenKind::Except,
                source,
            ),
            ClauseHeader::TryFinally(header) => {
                let last_statement = header
                    .orelse
                    .last()
                    .map(AnyNodeRef::from)
                    .or_else(|| header.handlers.last().map(AnyNodeRef::from))
                    .or_else(|| header.body.last().map(AnyNodeRef::from))
                    .unwrap();

                find_keyword(
                    StartPosition::LastStatement(last_statement.end()),
                    SimpleTokenKind::Finally,
                    source,
                )
            }
            ClauseHeader::Match(header) => find_keyword(
                StartPosition::clause_start(header),
                SimpleTokenKind::Match,
                source,
            ),
            ClauseHeader::MatchCase(header) => find_keyword(
                StartPosition::clause_start(header),
                SimpleTokenKind::Case,
                source,
            ),
            ClauseHeader::For(header) => {
                let keyword = if header.is_async {
                    SimpleTokenKind::Async
                } else {
                    SimpleTokenKind::For
                };
                find_keyword(StartPosition::clause_start(header), keyword, source)
            }
            ClauseHeader::While(header) => find_keyword(
                StartPosition::clause_start(header),
                SimpleTokenKind::While,
                source,
            ),
            ClauseHeader::With(header) => {
                let keyword = if header.is_async {
                    SimpleTokenKind::Async
                } else {
                    SimpleTokenKind::With
                };

                find_keyword(StartPosition::clause_start(header), keyword, source)
            }
            ClauseHeader::OrElse(header) => match header {
                ElseClause::Try(try_stmt) => {
                    let last_statement = try_stmt
                        .handlers
                        .last()
                        .map(AnyNodeRef::from)
                        .or_else(|| try_stmt.body.last().map(AnyNodeRef::from))
                        .unwrap();

                    find_keyword(
                        StartPosition::LastStatement(last_statement.end()),
                        SimpleTokenKind::Else,
                        source,
                    )
                }
                ElseClause::For(StmtFor { body, .. })
                | ElseClause::While(StmtWhile { body, .. }) => find_keyword(
                    StartPosition::LastStatement(body.last().unwrap().end()),
                    SimpleTokenKind::Else,
                    source,
                ),
            },
        }
    }
}

impl<'a> From<ClauseHeader<'a>> for AnyNodeRef<'a> {
    fn from(value: ClauseHeader<'a>) -> Self {
        match value {
            ClauseHeader::Class(stmt_class_def) => stmt_class_def.into(),
            ClauseHeader::Function(stmt_function_def) => stmt_function_def.into(),
            ClauseHeader::If(stmt_if) => stmt_if.into(),
            ClauseHeader::ElifElse(elif_else_clause) => elif_else_clause.into(),
            ClauseHeader::Try(stmt_try) => stmt_try.into(),
            ClauseHeader::ExceptHandler(except_handler_except_handler) => {
                except_handler_except_handler.into()
            }
            ClauseHeader::TryFinally(stmt_try) => stmt_try.into(),
            ClauseHeader::Match(stmt_match) => stmt_match.into(),
            ClauseHeader::MatchCase(match_case) => match_case.into(),
            ClauseHeader::For(stmt_for) => stmt_for.into(),
            ClauseHeader::While(stmt_while) => stmt_while.into(),
            ClauseHeader::With(stmt_with) => stmt_with.into(),
            ClauseHeader::OrElse(else_clause) => else_clause.into(),
        }
    }
}

#[derive(Copy, Clone)]
pub(crate) enum ElseClause<'a> {
    Try(&'a StmtTry),
    For(&'a StmtFor),
    While(&'a StmtWhile),
}

impl<'a> From<ElseClause<'a>> for AnyNodeRef<'a> {
    fn from(value: ElseClause<'a>) -> Self {
        match value {
            ElseClause::Try(stmt_try) => stmt_try.into(),
            ElseClause::For(stmt_for) => stmt_for.into(),
            ElseClause::While(stmt_while) => stmt_while.into(),
        }
    }
}

pub(crate) struct FormatClauseHeader<'a, 'ast> {
    header: ClauseHeader<'a>,
    /// How to format the clause header
    formatter: Argument<'a, PyFormatContext<'ast>>,

    /// Leading comments coming before the branch, together with the previous node, if any. Only relevant
    /// for alternate branches.
    leading_comments: Option<(&'a [SourceComment], Option<AnyNodeRef<'a>>)>,

    /// The trailing comments coming after the colon.
    trailing_colon_comment: &'a [SourceComment],
}

/// Formats a clause header, handling the case where the clause header is suppressed and should not be formatted.
///
/// Calls the `formatter` to format the content of the `header`, except if the `trailing_colon_comment` is a `fmt: skip` suppression comment.
/// Takes care of formatting the `trailing_colon_comment` and adds the `:` at the end of the header.
pub(crate) fn clause_header<'a, 'ast, Content>(
    header: ClauseHeader<'a>,
    trailing_colon_comment: &'a [SourceComment],
    formatter: &'a Content,
) -> FormatClauseHeader<'a, 'ast>
where
    Content: Format<PyFormatContext<'ast>>,
{
    FormatClauseHeader {
        header,
        formatter: Argument::new(formatter),
        leading_comments: None,
        trailing_colon_comment,
    }
}

impl<'ast> Format<PyFormatContext<'ast>> for FormatClauseHeader<'_, 'ast> {
    fn fmt(&self, f: &mut Formatter<PyFormatContext<'ast>>) -> FormatResult<()> {
        if let Some((leading_comments, last_node)) = self.leading_comments {
            leading_alternate_branch_comments(leading_comments, last_node).fmt(f)?;
        }

        if has_skip_comment(self.trailing_colon_comment, f.context().source()) {
            write_suppressed_clause_header(self.header, f)?;
        } else {
            // Write a source map entry for the colon for range formatting to support formatting the clause header without
            // the clause body. Avoid computing `self.header.range()` otherwise because it's somewhat involved.
            let clause_end = if f.options().source_map_generation().is_enabled() {
                Some(source_position(
                    self.header.range(f.context().source())?.end(),
                ))
            } else {
                None
            };

            write!(
                f,
                [Arguments::from(&self.formatter), token(":"), clause_end]
            )?;
        }

        trailing_comments(self.trailing_colon_comment).fmt(f)
    }
}

struct FormatClauseBody<'a> {
    body: &'a Suite,
    kind: SuiteKind,
    trailing_comments: &'a [SourceComment],
}

fn clause_body<'a>(
    body: &'a Suite,
    kind: SuiteKind,
    trailing_comments: &'a [SourceComment],
) -> FormatClauseBody<'a> {
    FormatClauseBody {
        body,
        kind,
        trailing_comments,
    }
}

impl Format<PyFormatContext<'_>> for FormatClauseBody<'_> {
    fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
        // In stable, stubs are only collapsed in stub files, in preview stubs in functions
        // or classes are collapsed too
        let should_collapse_stub = f.options().source_type().is_stub()
            || matches!(self.kind, SuiteKind::Function | SuiteKind::Class);

        if should_collapse_stub
            && let Some(ellipsis) = as_only_an_ellipsis(self.body, f.context().comments())
            && self.trailing_comments.is_empty()
        {
            write!(f, [space(), ellipsis.format(), hard_line_break()])
        } else {
            write!(
                f,
                [
                    trailing_comments(self.trailing_comments),
                    block_indent(&self.body.format().with_options(self.kind))
                ]
            )
        }
    }
}

pub(crate) struct FormatClause<'a, 'ast> {
    header: ClauseHeader<'a>,
    /// How to format the clause header
    header_formatter: Argument<'a, PyFormatContext<'ast>>,
    /// Leading comments coming before the branch, together with the previous node, if any. Only relevant
    /// for alternate branches.
    leading_comments: Option<(&'a [SourceComment], Option<AnyNodeRef<'a>>)>,
    /// The trailing comments coming after the colon.
    trailing_colon_comment: &'a [SourceComment],
    body: &'a Suite,
    kind: SuiteKind,
}

impl<'a, 'ast> FormatClause<'a, 'ast> {
    /// Sets the leading comments that precede an alternate branch.
    #[must_use]
    pub(crate) fn with_leading_comments<N>(
        mut self,
        comments: &'a [SourceComment],
        last_node: Option<N>,
    ) -> Self
    where
        N: Into<AnyNodeRef<'a>>,
    {
        self.leading_comments = Some((comments, last_node.map(Into::into)));
        self
    }

    fn clause_header(&self) -> FormatClauseHeader<'a, 'ast> {
        FormatClauseHeader {
            header: self.header,
            formatter: self.header_formatter,
            leading_comments: self.leading_comments,
            trailing_colon_comment: self.trailing_colon_comment,
        }
    }

    fn clause_body(&self) -> FormatClauseBody<'a> {
        clause_body(self.body, self.kind, self.trailing_colon_comment)
    }
}

/// Formats a clause, handling the case where the compound
/// statement lies on a single line with `# fmt: skip` and
/// should be suppressed.
pub(crate) fn clause<'a, 'ast, Content>(
    header: ClauseHeader<'a>,
    header_formatter: &'a Content,
    trailing_colon_comment: &'a [SourceComment],
    body: &'a Suite,
    kind: SuiteKind,
) -> FormatClause<'a, 'ast>
where
    Content: Format<PyFormatContext<'ast>>,
{
    FormatClause {
        header,
        header_formatter: Argument::new(header_formatter),
        leading_comments: None,
        trailing_colon_comment,
        body,
        kind,
    }
}

impl<'ast> Format<PyFormatContext<'ast>> for FormatClause<'_, 'ast> {
    fn fmt(&self, f: &mut Formatter<PyFormatContext<'ast>>) -> FormatResult<()> {
        match should_suppress_clause(self, f)? {
            SuppressClauseHeader::Yes {
                last_child_in_clause,
            } => write_suppressed_clause(self, f, last_child_in_clause),
            SuppressClauseHeader::No => {
                write!(f, [self.clause_header(), self.clause_body()])
            }
        }
    }
}

/// Finds the range of `keyword` starting the search at `start_position`.
///
/// If the start position is at the end of the previous statement, the
/// search will skip the optional semi-colon at the end of that statement.
/// Other than this, we expect only trivia between the `start_position`
/// and the keyword.
fn find_keyword(
    start_position: StartPosition,
    keyword: SimpleTokenKind,
    source: &str,
) -> FormatResult<TextRange> {
    let next_token = match start_position {
        StartPosition::ClauseStart(text_size) => SimpleTokenizer::starts_at(text_size, source)
            .skip_trivia()
            .next(),
        StartPosition::LastStatement(text_size) => {
            let mut tokenizer = SimpleTokenizer::starts_at(text_size, source).skip_trivia();

            let mut token = tokenizer.next();

            // If the last statement ends with a semi-colon, skip it.
            if matches!(
                token,
                Some(SimpleToken {
                    kind: SimpleTokenKind::Semi,
                    ..
                })
            ) {
                token = tokenizer.next();
            }
            token
        }
    };

    match next_token {
        Some(token) if token.kind() == keyword => Ok(token.range()),
        Some(other) => {
            debug_assert!(
                false,
                "Expected the keyword token {keyword:?} but found the token {other:?} instead."
            );
            Err(FormatError::syntax_error(
                "Expected the keyword token but found another token instead.",
            ))
        }
        None => {
            debug_assert!(
                false,
                "Expected the keyword token {keyword:?} but reached the end of the source instead."
            );
            Err(FormatError::syntax_error(
                "Expected the case header keyword token but reached the end of the source instead.",
            ))
        }
    }
}

/// Offset directly before clause header.
///
/// Can either be the beginning of the clause header
/// or the end of the last statement preceding the clause.
#[derive(Clone, Copy)]
enum StartPosition {
    /// The beginning of a clause header
    ClauseStart(TextSize),
    /// The end of the last statement in the suite preceding a clause.
    ///
    /// For example:
    /// ```python
    /// if cond:
    ///     a
    ///     b
    ///     c;
    /// # ...^here
    /// else:
    ///     d
    /// ```
    LastStatement(TextSize),
}

impl StartPosition {
    fn clause_start(ranged: impl Ranged) -> Self {
        Self::ClauseStart(ranged.start())
    }
}

/// Returns the range of the `:` ending the clause header or `Err` if the colon can't be found.
fn colon_range(after_keyword_or_condition: TextSize, source: &str) -> FormatResult<TextRange> {
    let mut tokenizer = SimpleTokenizer::starts_at(after_keyword_or_condition, source)
        .skip_trivia()
        .skip_while(|token| {
            matches!(
                token.kind(),
                SimpleTokenKind::RParen | SimpleTokenKind::Comma
            )
        });

    match tokenizer.next() {
        Some(SimpleToken {
            kind: SimpleTokenKind::Colon,
            range,
        }) => Ok(range),
        Some(token) => {
            debug_assert!(
                false,
                "Expected the colon marking the end of the case header but found {token:?} instead."
            );
            Err(FormatError::syntax_error(
                "Expected colon marking the end of the case header but found another token instead.",
            ))
        }
        None => {
            debug_assert!(
                false,
                "Expected the colon marking the end of the case header but found the end of the range."
            );
            Err(FormatError::syntax_error(
                "Expected the colon marking the end of the case header but found the end of the range.",
            ))
        }
    }
}

fn should_suppress_clause<'a>(
    clause: &FormatClause<'a, '_>,
    f: &mut Formatter<PyFormatContext<'_>>,
) -> FormatResult<SuppressClauseHeader<'a>> {
    let source = f.context().source();

    let Some(last_child_in_clause) = clause.header.last_child_in_clause() else {
        return Ok(SuppressClauseHeader::No);
    };

    // Early return if we don't have a skip comment
    // to avoid computing header range in the common case
    if !has_skip_comment(
        f.context().comments().trailing(last_child_in_clause),
        source,
    ) {
        return Ok(SuppressClauseHeader::No);
    }

    let clause_start = clause.header.range(source)?.end();

    let clause_range = TextRange::new(clause_start, last_child_in_clause.end());

    // Only applies to clauses on a single line
    if source.contains_line_break(clause_range) {
        return Ok(SuppressClauseHeader::No);
    }

    Ok(SuppressClauseHeader::Yes {
        last_child_in_clause,
    })
}

#[cold]
fn write_suppressed_clause(
    clause: &FormatClause,
    f: &mut Formatter<PyFormatContext<'_>>,
    last_child_in_clause: AnyNodeRef,
) -> FormatResult<()> {
    if let Some((leading_comments, last_node)) = clause.leading_comments {
        leading_alternate_branch_comments(leading_comments, last_node).fmt(f)?;
    }

    let header = clause.header;
    let clause_start = header.first_keyword_range(f.context().source())?.start();

    let comments = f.context().comments().clone();

    let clause_end = last_child_in_clause.end();

    // Write the outer comments and format the node as verbatim
    write!(
        f,
        [
            source_position(clause_start),
            verbatim_text(TextRange::new(clause_start, clause_end)),
            source_position(clause_end),
            trailing_comments(comments.trailing(last_child_in_clause)),
            hard_line_break()
        ]
    )?;

    // We mark comments in the header as formatted as in
    // the implementation of [`write_suppressed_clause_header`].
    //
    // Note that the header may be multi-line and contain
    // various comments since we only require that the range
    // starting at the _colon_ and ending at the `# fmt: skip`
    // fits on one line.
    header.visit(&mut |child| {
        for comment in comments.leading_trailing(child) {
            comment.mark_formatted();
        }
        comments.mark_verbatim_node_comments_formatted(child);
    });

    // Similarly we mark the comments in the body as formatted.
    // Note that the trailing comments for the last child in the
    // body have already been handled above.
    for stmt in clause.body {
        comments.mark_verbatim_node_comments_formatted(stmt.into());
    }

    Ok(())
}

enum SuppressClauseHeader<'a> {
    No,
    Yes {
        last_child_in_clause: AnyNodeRef<'a>,
    },
}