elm-ast 0.1.5

A syn-quality Rust library for parsing and constructing Elm 0.19.1 ASTs
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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
//! Pure predicates over Elm code lines and doc-comment blocks.
//!
//! Small read-only inspectors used by the reformat/assertion logic to
//! decide whether a line looks like a declaration, an assertion, a
//! redundant-paren expression, etc.

pub(in crate::print) fn looks_like_code_block_decl(line: &str) -> bool {
    if line.starts_with("type ")
        || line.starts_with("type alias ")
        || line.starts_with("port ")
        || line.starts_with("infix ")
    {
        return true;
    }
    // `name : ...` — first token is a lowercase identifier and the rest
    // of the line starts with ` : `.
    let mut chars = line.chars();
    let first = match chars.next() {
        Some(c) => c,
        None => return false,
    };
    if !first.is_ascii_lowercase() && first != '_' {
        return false;
    }
    let mut idx = first.len_utf8();
    while idx < line.len() {
        let c = line.as_bytes()[idx] as char;
        if c.is_ascii_alphanumeric() || c == '_' || c == '\'' {
            idx += 1;
        } else {
            break;
        }
    }
    let rest = &line[idx..];
    // `name :` — type annotation.
    if rest.starts_with(" : ") || rest == " :" {
        return true;
    }
    // `name ... = ...` — value binding, but only if the `=` sits at the
    // top level (not inside parens, brackets, braces, or a record literal).
    let bytes = rest.as_bytes();
    let mut depth_round: i32 = 0;
    let mut depth_square: i32 = 0;
    let mut depth_curly: i32 = 0;
    let mut in_string = false;
    let mut in_char = false;
    let mut j = 0;
    while j < bytes.len() {
        let b = bytes[j];
        if in_string {
            if b == b'\\' && j + 1 < bytes.len() {
                j += 2;
                continue;
            }
            if b == b'"' {
                in_string = false;
            }
            j += 1;
            continue;
        }
        if in_char {
            if b == b'\\' && j + 1 < bytes.len() {
                j += 2;
                continue;
            }
            if b == b'\'' {
                in_char = false;
            }
            j += 1;
            continue;
        }
        match b {
            b'"' => in_string = true,
            b'\'' => in_char = true,
            b'(' => depth_round += 1,
            b')' => depth_round -= 1,
            b'[' => depth_square += 1,
            b']' => depth_square -= 1,
            b'{' => depth_curly += 1,
            b'}' => depth_curly -= 1,
            b'=' if depth_round == 0 && depth_square == 0 && depth_curly == 0 => {
                let prev = if j > 0 { bytes[j - 1] as char } else { ' ' };
                let next = if j + 1 < bytes.len() {
                    bytes[j + 1] as char
                } else {
                    ' '
                };
                // Exclude `==`, `/=`, `>=`, `<=`.
                if prev != '=' && prev != '/' && prev != '>' && prev != '<' && next != '=' {
                    return true;
                }
            }
            _ => {}
        }
        j += 1;
    }
    false
}

/// Insert an extra blank line before an indented code block when that block
/// ends with a `-- line comment` whose only trailing lines are blanks.
///
pub(in crate::print) fn looks_like_value_decl_start(line: &str) -> bool {
    let bytes = line.as_bytes();
    if bytes.is_empty() {
        return false;
    }
    let first = bytes[0];
    if !(first.is_ascii_lowercase() || first == b'_') {
        return false;
    }
    // Walk identifier chars.
    let mut i = 0;
    while i < bytes.len()
        && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_' || bytes[i] == b'\'')
    {
        i += 1;
    }
    if i == 0 || i >= bytes.len() {
        return false;
    }
    if bytes[i] != b' ' {
        return false;
    }
    // Scan for an `=` (surrounded by spaces) at outer level.
    let mut depth = 0i32;
    let mut in_str = false;
    let mut in_char = false;
    let mut esc = false;
    while i < bytes.len() {
        let b = bytes[i];
        if esc {
            esc = false;
            i += 1;
            continue;
        }
        if in_str {
            if b == b'\\' {
                esc = true;
            } else if b == b'"' {
                in_str = false;
            }
            i += 1;
            continue;
        }
        if in_char {
            if b == b'\\' {
                esc = true;
            } else if b == b'\'' {
                in_char = false;
            }
            i += 1;
            continue;
        }
        match b {
            b'"' => in_str = true,
            b'\'' => in_char = true,
            b'(' | b'[' | b'{' => depth += 1,
            b')' | b']' | b'}' => depth -= 1,
            b'=' if depth == 0 => {
                // Ensure it's not `==`, `>=`, `<=`, `/=`, `=>`, `::=`.
                let prev = if i > 0 { bytes[i - 1] } else { b' ' };
                let next = if i + 1 < bytes.len() {
                    bytes[i + 1]
                } else {
                    b' '
                };
                if prev != b' ' {
                    i += 1;
                    continue;
                }
                if next == b'=' {
                    i += 1;
                    continue;
                }
                return true;
            }
            _ => {}
        }
        i += 1;
    }
    false
}

pub(in crate::print) fn looks_like_type_annotation(line: &str) -> bool {
    let bytes = line.as_bytes();
    let mut in_string = false;
    let mut escape = false;
    let mut i = 0;
    while i < bytes.len() {
        let c = bytes[i];
        if escape {
            escape = false;
        } else if in_string {
            if c == b'\\' {
                escape = true;
            } else if c == b'"' {
                in_string = false;
            }
        } else if c == b'"' {
            in_string = true;
        } else if c == b':'
            && i + 1 < bytes.len()
            && bytes[i + 1] == b' '
            && i > 0
            && bytes[i - 1] == b' '
        {
            return true;
        }
        i += 1;
    }
    false
}

pub(in crate::print) fn is_assertion_only_paragraph(para: &[String]) -> bool {
    let non_empty: Vec<&String> = para.iter().filter(|l| !l.trim().is_empty()).collect();
    if non_empty.len() < 2 {
        return false;
    }
    let mut assertion_count = 0usize;
    for line in &non_empty {
        // Must start at column 0 (no leading whitespace beyond what was stripped).
        if line.starts_with(' ') || line.starts_with('\t') {
            return false;
        }
        let trimmed = line.trim();
        // Allow `--` line comments mixed in, as long as at least one
        // line is a real assertion. elm-format treats a `-- comment` line
        // as attached to the following assertion.
        if trimmed.starts_with("--") {
            continue;
        }
        if !looks_like_assertion(trimmed) {
            return false;
        }
        assertion_count += 1;
    }
    assertion_count >= 1
}

pub(in crate::print) fn looks_like_assertion(trimmed: &str) -> bool {
    // Three accepted shapes for "example lines" inside doc code blocks:
    //   1. `expr == value` (optionally with trailing ` -- comment`)
    //   2. `expr -- comment` (expression followed by a line comment)
    //   3. Simple standalone expression (starts with identifier or constructor,
    //      balanced delimiters, doesn't end with an operator).
    // Lines beginning with `--` are standalone comments, not assertions.
    if trimmed.starts_with("--") {
        return false;
    }
    if let Some(eq) = trimmed.find(" == ") {
        let (left, right) = (&trimmed[..eq], &trimmed[eq + 4..]);
        if left.is_empty() || right.is_empty() {
            return false;
        }
        if right.starts_with('=') {
            return false;
        }
        let last_ch = left.chars().last().unwrap();
        if "+-*/|&<>".contains(last_ch) {
            return false;
        }
        return true;
    }
    // Shape 2: `expr -- comment`. Require ` -- ` separator and non-empty left.
    if let Some(dash) = trimmed.find(" -- ") {
        let left = &trimmed[..dash];
        if left.is_empty() {
            return false;
        }
        let last_ch = left.chars().last().unwrap();
        if "+-*/|&<>=".contains(last_ch) {
            return false;
        }
        return true;
    }
    // Shape 3: a simple standalone expression line.
    looks_like_simple_expr_line(trimmed)
}

pub(in crate::print) fn looks_like_simple_expr_line(trimmed: &str) -> bool {
    // Must begin with identifier (lower/upper) or opening delimiter.
    let first = match trimmed.chars().next() {
        Some(c) => c,
        None => return false,
    };
    if !(first.is_ascii_alphabetic()
        || first.is_ascii_digit()
        || first == '_'
        || first == '('
        || first == '['
        || first == '\''
        || first == '"'
        || first == '-')
    {
        return false;
    }
    // `-` only allowed as a leading negation when followed by a digit or paren.
    if first == '-' {
        let second = trimmed.chars().nth(1);
        match second {
            Some(c) if c.is_ascii_digit() || c == '(' => {}
            _ => return false,
        }
    }
    // Reject keyword-led lines (they are parts of a larger expression).
    let first_word_end = trimmed
        .find(|c: char| !c.is_ascii_alphanumeric() && c != '_' && c != '.')
        .unwrap_or(trimmed.len());
    let first_word = &trimmed[..first_word_end];
    match first_word {
        "type" | "port" | "module" | "import" | "let" | "in" | "if" | "then" | "else" | "case"
        | "of" | "where" | "alias" | "exposing" | "as" | "effect" | "infix" => return false,
        _ => {}
    }
    // Must have balanced parens/brackets, counting string/char literals.
    let mut paren = 0i32;
    let mut bracket = 0i32;
    let mut in_str = false;
    let mut in_char = false;
    let mut esc = false;
    for c in trimmed.chars() {
        if esc {
            esc = false;
            continue;
        }
        if in_str {
            if c == '\\' {
                esc = true;
            } else if c == '"' {
                in_str = false;
            }
            continue;
        }
        if in_char {
            if c == '\\' {
                esc = true;
            } else if c == '\'' {
                in_char = false;
            }
            continue;
        }
        match c {
            '"' => in_str = true,
            '\'' => in_char = true,
            '(' => paren += 1,
            ')' => {
                paren -= 1;
                if paren < 0 {
                    return false;
                }
            }
            '[' => bracket += 1,
            ']' => {
                bracket -= 1;
                if bracket < 0 {
                    return false;
                }
            }
            _ => {}
        }
    }
    if paren != 0 || bracket != 0 || in_str || in_char {
        return false;
    }
    // Must not end with an operator character (continuation to next line).
    let last_non_ws = trimmed.trim_end();
    if let Some(lc) = last_non_ws.chars().last()
        && "+-*/|&<>=,:".contains(lc)
    {
        return false;
    }
    true
}

/// Add spaces around tight binary operators (`1/2` → `1 / 2`, `2^3` → `2 ^ 3`)
/// outside of string and char literals. Does NOT modify text inside `-- comments`.
/// Conservative: only applies when the operator is flanked by identifier/digit
/// characters on both sides.
pub(in crate::print) fn block_has_single_line_if(block_lines: &[&str]) -> bool {
    for &line in block_lines {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }
        let leading = line.len() - line.trim_start().len();
        if leading < 4 {
            continue;
        }
        if line_has_single_line_if_then_else(trimmed) {
            return true;
        }
    }
    false
}

/// True when the trimmed line contains both ` then ` and ` else ` outside
/// string/char literals and comments — markers of an inline if-then-else that
/// elm-format breaks across multiple lines.
pub(in crate::print) fn line_has_single_line_if_then_else(line: &str) -> bool {
    let bytes = line.as_bytes();
    let mut in_str = false;
    let mut in_char = false;
    let mut in_triple = false;
    let mut esc = false;
    let mut i = 0;
    let mut saw_then = false;
    let mut saw_else = false;
    while i < bytes.len() {
        let b = bytes[i];
        if esc {
            esc = false;
            i += 1;
            continue;
        }
        if in_triple {
            if i + 2 < bytes.len() && &bytes[i..i + 3] == b"\"\"\"" {
                in_triple = false;
                i += 3;
                continue;
            }
            i += 1;
            continue;
        }
        if in_str {
            if b == b'\\' {
                esc = true;
            } else if b == b'"' {
                in_str = false;
            }
            i += 1;
            continue;
        }
        if in_char {
            if b == b'\\' {
                esc = true;
            } else if b == b'\'' {
                in_char = false;
            }
            i += 1;
            continue;
        }
        if b == b'-' && i + 1 < bytes.len() && bytes[i + 1] == b'-' {
            // line comment — stop scanning.
            break;
        }
        if i + 2 < bytes.len() && &bytes[i..i + 3] == b"\"\"\"" {
            in_triple = true;
            i += 3;
            continue;
        }
        if b == b'"' {
            in_str = true;
            i += 1;
            continue;
        }
        if b == b'\'' {
            in_char = true;
            i += 1;
            continue;
        }
        // Match " then " and " else " as whole keywords.
        if i + 6 <= bytes.len() && &bytes[i..i + 6] == b" then " {
            saw_then = true;
        }
        if i + 6 <= bytes.len() && &bytes[i..i + 6] == b" else " {
            saw_else = true;
        }
        i += 1;
    }
    saw_then && saw_else
}

/// Detect a code block containing multiple assertion-shaped lines with no
/// blank-line separation between them. elm-format renders each assertion as
/// its own paragraph separated by blank lines, so such blocks need reformat.
/// Only considers runs of 2+ consecutive assertion lines (possibly interleaved
/// with `--` comments that attach to the following assertion).
pub(in crate::print) fn block_has_unseparated_assertions(block_lines: &[&str]) -> bool {
    let mut run_assert_count = 0usize;
    for &line in block_lines {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            if run_assert_count >= 2 {
                return true;
            }
            run_assert_count = 0;
            continue;
        }
        // Only consider lines at the 4-space base indent (code-block content).
        let leading = line.len() - line.trim_start().len();
        if leading != 4 {
            if run_assert_count >= 2 {
                return true;
            }
            run_assert_count = 0;
            continue;
        }
        if trimmed.starts_with("--") {
            // `-- comment` attaches to the following assertion; skip without
            // resetting the run.
            continue;
        }
        if looks_like_assertion(trimmed) {
            run_assert_count += 1;
        } else {
            if run_assert_count >= 2 {
                return true;
            }
            run_assert_count = 0;
        }
    }
    run_assert_count >= 2
}

/// Detect a line that is a single parenthesized operator expression like
/// `(true || false)` or `(a + b)` — where the outer parens are redundant
/// at top level. Conservative: requires a `(` at the very start of the
/// trimmed line, a matching `)` at the end, no commas at the outer level
/// (so tuples are excluded), and at least one binary-operator character
/// at the outer level between the parens.
pub(in crate::print) fn is_redundant_paren_expr(trimmed: &str) -> bool {
    let bytes = trimmed.as_bytes();
    if bytes.len() < 4 || bytes[0] != b'(' || *bytes.last().unwrap() != b')' {
        return false;
    }
    let mut depth = 0i32;
    let mut in_str = false;
    let mut in_char = false;
    let mut esc = false;
    let mut saw_outer_op = false;
    for (i, &b) in bytes.iter().enumerate() {
        if esc {
            esc = false;
            continue;
        }
        if in_str {
            if b == b'\\' {
                esc = true;
            } else if b == b'"' {
                in_str = false;
            }
            continue;
        }
        if in_char {
            if b == b'\\' {
                esc = true;
            } else if b == b'\'' {
                in_char = false;
            }
            continue;
        }
        match b {
            b'"' => in_str = true,
            b'\'' => in_char = true,
            b'(' => depth += 1,
            b')' => {
                depth -= 1;
                if depth == 0 && i != bytes.len() - 1 {
                    // parens closed before end — not a fully-wrapped expression
                    return false;
                }
            }
            b',' if depth == 1 => return false,
            b'|' | b'&' | b'+' | b'*' | b'/' | b'<' | b'>' | b'=' if depth == 1 => {
                saw_outer_op = true;
            }
            b'-' if depth == 1 && i > 1 => {
                let prev = bytes[i - 1];
                if prev == b' ' {
                    saw_outer_op = true;
                }
            }
            _ => {}
        }
    }
    depth == 0 && saw_outer_op
}

/// Detect a hex literal whose digit count is not one of elm-format's
/// canonical widths (2, 4, 8, or 16). Scans for `0x[0-9A-Fa-f]+` tokens
/// outside strings and char literals.
pub(in crate::print) fn line_has_unpadded_hex(line: &str) -> bool {
    let bytes = line.as_bytes();
    let mut in_str = false;
    let mut in_char = false;
    let mut esc = false;
    let mut i = 0;
    while i < bytes.len() {
        let b = bytes[i];
        if esc {
            esc = false;
            i += 1;
            continue;
        }
        if in_str {
            if b == b'\\' {
                esc = true;
            } else if b == b'"' {
                in_str = false;
            }
            i += 1;
            continue;
        }
        if in_char {
            if b == b'\\' {
                esc = true;
            } else if b == b'\'' {
                in_char = false;
            }
            i += 1;
            continue;
        }
        if b == b'"' {
            in_str = true;
            i += 1;
            continue;
        }
        if b == b'\'' {
            in_char = true;
            i += 1;
            continue;
        }
        // Look for `0x` not preceded by an identifier character.
        if b == b'0' && i + 1 < bytes.len() && (bytes[i + 1] == b'x' || bytes[i + 1] == b'X') {
            let prev_ok = if i == 0 {
                true
            } else {
                let p = bytes[i - 1];
                !(p.is_ascii_alphanumeric() || p == b'_')
            };
            if prev_ok {
                let start = i + 2;
                let mut j = start;
                while j < bytes.len() && bytes[j].is_ascii_hexdigit() {
                    j += 1;
                }
                let width = j - start;
                if width > 0 && width != 2 && width != 4 && width != 8 && width != 16 {
                    return true;
                }
                i = j;
                continue;
            }
        }
        i += 1;
    }
    false
}

/// Detect a compact tuple like `(Float, Float)` or `(1,2)` where `(` is
/// immediately followed by a literal / identifier character (no space) and
/// at least one comma at outer depth closes into a `)`. elm-format
/// normalizes to `( Float, Float )`.
pub(in crate::print) fn has_compact_tuple(line: &str) -> bool {
    let bytes = line.as_bytes();
    let mut in_str = false;
    let mut in_char = false;
    let mut esc = false;
    let mut i = 0;
    while i < bytes.len() {
        let b = bytes[i];
        if esc {
            esc = false;
            i += 1;
            continue;
        }
        if in_str {
            if b == b'\\' {
                esc = true;
            } else if b == b'"' {
                in_str = false;
            }
            i += 1;
            continue;
        }
        if in_char {
            if b == b'\\' {
                esc = true;
            } else if b == b'\'' {
                in_char = false;
            }
            i += 1;
            continue;
        }
        if b == b'"' {
            in_str = true;
            i += 1;
            continue;
        }
        if b == b'\'' {
            in_char = true;
            i += 1;
            continue;
        }
        if b == b'(' && i + 1 < bytes.len() {
            let next = bytes[i + 1];
            // A space inside `( ` means already normalized; not compact.
            if next == b' ' || next == b')' {
                i += 1;
                continue;
            }
            // Scan for a matching `)` at the same depth, tracking commas.
            let mut depth = 1i32;
            let mut j = i + 1;
            let mut inner_in_str = false;
            let mut inner_in_char = false;
            let mut inner_esc = false;
            let mut found_comma = false;
            while j < bytes.len() && depth > 0 {
                let c = bytes[j];
                if inner_esc {
                    inner_esc = false;
                    j += 1;
                    continue;
                }
                if inner_in_str {
                    if c == b'\\' {
                        inner_esc = true;
                    } else if c == b'"' {
                        inner_in_str = false;
                    }
                    j += 1;
                    continue;
                }
                if inner_in_char {
                    if c == b'\\' {
                        inner_esc = true;
                    } else if c == b'\'' {
                        inner_in_char = false;
                    }
                    j += 1;
                    continue;
                }
                match c {
                    b'"' => inner_in_str = true,
                    b'\'' => inner_in_char = true,
                    b'(' | b'[' | b'{' => depth += 1,
                    b')' | b']' | b'}' => depth -= 1,
                    b',' if depth == 1 => found_comma = true,
                    _ => {}
                }
                j += 1;
            }
            if found_comma && j > 0 {
                // Check that closing `)` isn't preceded by a space (`... )`).
                // If there's a space before `)` the tuple is already normalized.
                if bytes[j - 1] == b')' {
                    let before_close = if j >= 2 { bytes[j - 2] } else { b' ' };
                    if before_close != b' ' {
                        return true;
                    }
                }
            }
            i = j;
            continue;
        }
        i += 1;
    }
    false
}

/// Detect a float literal in scientific form without a decimal point, e.g.
/// `1e-42` or `6e23`. elm-format normalizes these to `1.0e-42` / `6.0e23`.
pub(in crate::print) fn line_has_sci_float_without_dot(line: &str) -> bool {
    let bytes = line.as_bytes();
    let mut in_str = false;
    let mut in_char = false;
    let mut esc = false;
    let mut i = 0;
    while i < bytes.len() {
        let b = bytes[i];
        if esc {
            esc = false;
            i += 1;
            continue;
        }
        if in_str {
            if b == b'\\' {
                esc = true;
            } else if b == b'"' {
                in_str = false;
            }
            i += 1;
            continue;
        }
        if in_char {
            if b == b'\\' {
                esc = true;
            } else if b == b'\'' {
                in_char = false;
            }
            i += 1;
            continue;
        }
        if b == b'"' {
            in_str = true;
            i += 1;
            continue;
        }
        if b == b'\'' {
            in_char = true;
            i += 1;
            continue;
        }
        // Look for a digit that starts a numeric literal.
        if b.is_ascii_digit() {
            let prev_ok = if i == 0 {
                true
            } else {
                let p = bytes[i - 1];
                !(p.is_ascii_alphanumeric() || p == b'_' || p == b'.')
            };
            if prev_ok {
                let start = i;
                let mut j = i;
                while j < bytes.len() && bytes[j].is_ascii_digit() {
                    j += 1;
                }
                let has_dot = j < bytes.len() && bytes[j] == b'.';
                if has_dot {
                    // Skip `.digits...`
                    j += 1;
                    while j < bytes.len() && bytes[j].is_ascii_digit() {
                        j += 1;
                    }
                }
                let has_exp = j < bytes.len() && (bytes[j] == b'e' || bytes[j] == b'E');
                if has_exp && !has_dot {
                    // Check that a digit follows (possibly after +/-).
                    let mut k = j + 1;
                    if k < bytes.len() && (bytes[k] == b'+' || bytes[k] == b'-') {
                        k += 1;
                    }
                    if k < bytes.len() && bytes[k].is_ascii_digit() {
                        // Don't flag hex literals like `0x1e` (handled elsewhere).
                        // Here our start was at digit; if digits were `0` then
                        // `x` then hex — but we already separated hex via `0x` prefix.
                        let _ = start;
                        return true;
                    }
                }
                i = j;
                continue;
            }
        }
        i += 1;
    }
    false
}

/// Detect tight infix operators like `3^2` or `a^b` with no spaces.
/// Conservative: checks for `^` operator specifically, only when flanked
/// by identifier/digit characters on both sides (and not inside a string).
pub(in crate::print) fn has_tight_binary_op(line: &str) -> bool {
    let bytes = line.as_bytes();
    let mut in_str = false;
    let mut escape = false;
    let mut i = 0;
    while i < bytes.len() {
        let b = bytes[i];
        if escape {
            escape = false;
            i += 1;
            continue;
        }
        if in_str {
            if b == b'\\' {
                escape = true;
            } else if b == b'"' {
                in_str = false;
            }
            i += 1;
            continue;
        }
        if b == b'"' {
            in_str = true;
            i += 1;
            continue;
        }
        if b == b'^' && i > 0 && i + 1 < bytes.len() {
            let prev = bytes[i - 1];
            let next = bytes[i + 1];
            let is_ident = |c: u8| c.is_ascii_alphanumeric() || c == b'_';
            if is_ident(prev) && is_ident(next) {
                return true;
            }
        }
        if b == b'/' && i > 0 && i + 1 < bytes.len() {
            let prev = bytes[i - 1];
            let next = bytes[i + 1];
            // Skip over `//` (integer division) and line comments.
            // Handle both single `/` and `//` as tight operators.
            let is_ident = |c: u8| c.is_ascii_alphanumeric() || c == b'_';
            if next == b'/' {
                // `//` integer division: look at char before and char after `//`.
                if i + 2 < bytes.len() {
                    let after = bytes[i + 2];
                    if is_ident(prev) && is_ident(after) {
                        return true;
                    }
                }
                i += 2;
                continue;
            }
            if is_ident(prev) && is_ident(next) {
                return true;
            }
        }
        i += 1;
    }
    false
}

/// Returns true if `line` is an `import ... exposing (a, b, c)` line whose
/// exposing list items are not alphabetically sorted.
pub(in crate::print) fn import_has_unsorted_exposing(line: &str) -> bool {
    let t = line.trim();
    if !t.starts_with("import ") {
        return false;
    }
    let exp_idx = match t.find(" exposing (") {
        Some(i) => i,
        None => return false,
    };
    let rest = &t[exp_idx + " exposing (".len()..];
    let close_idx = match rest.rfind(')') {
        Some(i) => i,
        None => return false,
    };
    let inner = &rest[..close_idx];
    // Ignore wildcard exposing; don't try to handle nested parentheses
    // (e.g. `Type(..)`) — item key is the head before any `(`.
    if inner.trim() == ".." {
        return false;
    }
    let items: Vec<String> = inner
        .split(',')
        .map(|s| {
            let s = s.trim();
            let head = s.split('(').next().unwrap_or(s).trim();
            head.to_string()
        })
        .filter(|s| !s.is_empty())
        .collect();
    if items.len() < 2 {
        return false;
    }
    let mut sorted = items.clone();
    sorted.sort_by_key(|a| a.to_lowercase());
    items != sorted
}

/// Detect `name = expr` on a single line, where expr is non-empty and the
/// `=` is not part of `==`, `/=`, `<=`, `>=`. This is the shape elm-format
/// always expands into two lines inside doc-comment code blocks.
pub(in crate::print) fn is_single_line_value_decl(trimmed: &str) -> bool {
    // Must start with a lowercase identifier character.
    let first = match trimmed.chars().next() {
        Some(c) => c,
        None => return false,
    };
    if !(first.is_ascii_lowercase() || first == '_') {
        return false;
    }
    // Reject keyword-led lines: these are handled by the parser/printer
    // directly and don't fit the `name = expr` value-decl shape.
    let first_word_end = trimmed
        .find(|c: char| !c.is_ascii_alphanumeric() && c != '_')
        .unwrap_or(trimmed.len());
    let first_word = &trimmed[..first_word_end];
    match first_word {
        "type" | "port" | "module" | "import" | "let" | "in" | "if" | "then" | "else" | "case"
        | "of" | "where" | "alias" | "exposing" | "as" | "effect" | "infix" => return false,
        _ => {}
    }
    // Find ` = ` that isn't part of `== `, `/= `, etc.
    let bytes = trimmed.as_bytes();
    let mut i = 0;
    while i + 2 < bytes.len() {
        if bytes[i] == b' ' && bytes[i + 1] == b'=' && bytes[i + 2] == b' ' {
            // Reject `== `, `/= `, `<= `, `>= ` (char before the `=` is an op-char).
            if i > 0 {
                let prev = bytes[i - 1];
                if prev == b'='
                    || prev == b'/'
                    || prev == b'<'
                    || prev == b'>'
                    || prev == b'!'
                    || prev == b':'
                {
                    i += 1;
                    continue;
                }
            }
            // Reject `= =` (next char after `= ` is `=`).
            if i + 3 < bytes.len() && bytes[i + 3] == b'=' {
                i += 1;
                continue;
            }
            // Left side must be an identifier (plus optional argument pattern).
            let left = trimmed[..i].trim();
            if left.is_empty() {
                return false;
            }
            let left_first = left.chars().next().unwrap();
            if !(left_first.is_ascii_lowercase() || left_first == '_') {
                return false;
            }
            // Right side must be non-empty.
            let right = trimmed[i + 3..].trim();
            if right.is_empty() {
                return false;
            }
            return true;
        }
        i += 1;
    }
    false
}