fatou 0.10.0

A language server, formatter, and linter for Julia
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
//! The layout engine: render an [`Ir`] document to a string, choosing flat or
//! broken layout per group with a best-fit (Wadler) algorithm.

use crate::formatter::ir::Ir;
use crate::formatter::style::FormatStyle;

#[derive(Clone, Copy, PartialEq, Eq)]
enum Mode {
    Flat,
    Break,
}

/// Render `doc` at the given style.
pub fn print(doc: &Ir, style: FormatStyle) -> String {
    print_at(doc, style, 0)
}

/// Render `doc` as if it sat at column `indent` (in spaces): line breaks
/// re-indent to `indent`, nested indents stack on top of it, and group fit
/// checks start from that column. **No leading indent is emitted for the first
/// line** — the caller places the output after existing text (range formatting
/// keeps the first line's original leading whitespace).
pub fn print_at(doc: &Ir, style: FormatStyle, indent: usize) -> String {
    let indent_step = style.indent_width as usize;
    let width = style.line_width as usize;
    let mut out = String::new();
    let mut col = indent;
    // Work stack of (indent, mode, node), processed depth-first.
    let mut stack: Vec<(usize, Mode, &Ir)> = vec![(indent, Mode::Break, doc)];

    while let Some((indent, mode, ir)) = stack.pop() {
        match ir {
            Ir::Text(s) => {
                out.push_str(s);
                // Text is normally newline-free, but the transparent lowering
                // passes raw source newlines through as `Text`; honor them so the
                // column tracking stays accurate for later groups' fit checks.
                match s.rfind('\n') {
                    Some(i) => col = s[i + 1..].chars().count(),
                    None => col += s.chars().count(),
                }
            }
            Ir::Concat(items) => {
                for item in items.iter().rev() {
                    stack.push((indent, mode, item));
                }
            }
            Ir::Indent(inner) => stack.push((indent + indent_step, mode, inner)),
            Ir::Line => match mode {
                Mode::Flat => {
                    out.push(' ');
                    col += 1;
                }
                Mode::Break => col = newline(&mut out, indent),
            },
            Ir::SoftLine => {
                if mode == Mode::Break {
                    col = newline(&mut out, indent);
                }
            }
            Ir::HardLine => col = newline(&mut out, indent),
            Ir::BlankLine => {
                out.push('\n');
                col = 0;
            }
            Ir::Group(inner) => {
                // A group fits flat only if its flat rendering *plus the trailing
                // content already on the current line* stays within the width. The
                // trailing content is exactly the rest of the work stack up to the
                // next line break, so `fits` walks `inner` (flat) and then `stack`.
                let mode = if fits(width.saturating_sub(col), inner, &stack) {
                    Mode::Flat
                } else {
                    Mode::Break
                };
                stack.push((indent, mode, inner));
            }
            Ir::IfBreak(broken, flat) => {
                let s = if mode == Mode::Break { broken } else { flat };
                out.push_str(s);
                col += s.chars().count();
            }
            Ir::HugGroup {
                prefix,
                body,
                close,
                explode,
            } => {
                // Hug when the hug layout's first line fits; otherwise fall back
                // to the standard explode group (re-measured by the normal Group
                // arm — it always breaks here, since the hug measure never
                // exceeds its flat measure).
                if hug_fits(width.saturating_sub(col), prefix, body, close, &stack) {
                    stack.push((indent, mode, close));
                    stack.push((indent, mode, body));
                    stack.push((indent, mode, prefix));
                } else {
                    stack.push((indent, mode, explode));
                }
            }
            Ir::CondGroup {
                primary,
                fallback,
                probe,
            } => {
                // The deciding line is the group's re-indented closing line, not the
                // current one, so measure `probe` (flat) from the *base indent*
                // rather than `col`. It fits exactly when breaking `primary`'s head
                // leaves the flat bound sitting on that closing line.
                let chosen = if fits(width.saturating_sub(indent), probe, &stack) {
                    primary
                } else {
                    fallback
                };
                stack.push((indent, mode, chosen));
            }
        }
    }

    out
}

/// Emit a newline followed by `indent` spaces; return the new column.
fn newline(out: &mut String, indent: usize) -> usize {
    out.push('\n');
    for _ in 0..indent {
        out.push(' ');
    }
    indent
}

/// Whether the group `inner`, rendered flat and followed by the trailing content
/// still pending on the print stack (`rest`), fits within `remaining` columns.
///
/// `inner` is measured flat; `rest` items keep the mode they were queued with, so
/// a line break in an already-broken enclosing group ends the measured line. The
/// scan stops — the group *fits* — as soon as the current line ends (a break-mode
/// [`Line`](Ir::Line)/[`SoftLine`](Ir::SoftLine), a [`HardLine`](Ir::HardLine)/
/// [`BlankLine`](Ir::BlankLine), or a raw embedded newline in trailing text). A
/// forced newline *inside* the group's own flat content instead means it cannot sit
/// flat, so the group must break.
fn fits(remaining: usize, inner: &Ir, rest: &[(usize, Mode, &Ir)]) -> bool {
    // Work stack of (in_group, mode, node). Push `rest` bottom-first (it is itself
    // a pop-from-end stack, so its last element prints next), then `inner` on top.
    let mut stack: Vec<(bool, Mode, &Ir)> = Vec::with_capacity(rest.len() + 1);
    for (_, mode, ir) in rest {
        stack.push((false, *mode, ir));
    }
    stack.push((true, Mode::Flat, inner));
    fits_stack(remaining as isize, stack)
}

/// Whether the hug layout of a [`HugGroup`](Ir::HugGroup) has a fitting first
/// line: `prefix` measured strictly flat (a forced break inside a leading
/// argument forbids hugging), then `body` up to its first break opportunity —
/// where its own group would end the line — and, only if the body cannot break,
/// `close` plus the trailing content still pending on the print stack.
fn hug_fits(
    remaining: usize,
    prefix: &Ir,
    body: &Ir,
    close: &Ir,
    rest: &[(usize, Mode, &Ir)],
) -> bool {
    let mut stack: Vec<(bool, Mode, &Ir)> = Vec::with_capacity(rest.len() + 3);
    for (_, mode, ir) in rest {
        stack.push((false, *mode, ir));
    }
    stack.push((false, Mode::Flat, close));
    // Break mode: the body's first `Line`/`SoftLine` ends the measured line, so
    // only the hugged construct's opening bracket counts toward the first line.
    stack.push((false, Mode::Break, body));
    stack.push((true, Mode::Flat, prefix));
    fits_stack(remaining as isize, stack)
}

/// The shared measurement loop behind [`fits`] and [`hug_fits`], walking a
/// prepared `(in_group, mode, node)` stack.
fn fits_stack(mut remaining: isize, mut stack: Vec<(bool, Mode, &Ir)>) -> bool {
    while let Some((in_group, mode, ir)) = stack.pop() {
        if remaining < 0 {
            return false;
        }
        match ir {
            // A raw embedded newline (only ever from transparent text): inside the
            // group it forbids a flat layout; in trailing content it ends the line.
            Ir::Text(s) => match s.find('\n') {
                Some(i) => {
                    remaining -= s[..i].chars().count() as isize;
                    return !in_group && remaining >= 0;
                }
                None => remaining -= s.chars().count() as isize,
            },
            Ir::Concat(items) => {
                for item in items.iter().rev() {
                    stack.push((in_group, mode, item));
                }
            }
            Ir::Indent(child) => stack.push((in_group, mode, child)),
            // A nested group inherits the carried mode: inside the tested group it
            // renders flat with it; in trailing content it keeps the break mode it
            // was queued with, so its first line break ends the measured line (the
            // tested group is judged as if the trailing group breaks at that point).
            Ir::Group(child) => stack.push((in_group, mode, child)),
            Ir::Line => match mode {
                Mode::Flat => remaining -= 1,
                Mode::Break => return true,
            },
            Ir::SoftLine => {
                if mode == Mode::Break {
                    return true;
                }
            }
            // A forced break ends the line: fatal inside the group, fitting after it.
            Ir::HardLine | Ir::BlankLine => return !in_group,
            Ir::IfBreak(broken, flat) => {
                let s = if mode == Mode::Break { broken } else { flat };
                remaining -= s.chars().count() as isize;
            }
            // Inside the group under test, the hug must sit flat, so measure it
            // flat (prefix + body + close): the hug's width equals the explode
            // group's flat width. As trailing content on an already-broken line,
            // though, the hug will render broken — its first line ends at the open
            // bracket, exactly as a plain trailing `Group` ends at its first
            // `SoftLine`. Measure the explode fallback's broken first line (open
            // bracket, then its `SoftLine` ends the line) so a preceding group is
            // not forced to break by the hug's full flat prefix width.
            Ir::HugGroup {
                prefix,
                body,
                close,
                explode,
            } => {
                if !in_group && mode == Mode::Break {
                    stack.push((false, Mode::Break, explode));
                } else {
                    stack.push((in_group, mode, close));
                    stack.push((in_group, mode, body));
                    stack.push((in_group, mode, prefix));
                }
            }
            // A `CondGroup` is measured through its `primary` (the flat-bound
            // layout): its flat width is the all-flat rendering, and as trailing
            // content its head group's first break ends the line, exactly like a
            // plain trailing `Group`.
            Ir::CondGroup { primary, .. } => stack.push((in_group, mode, primary)),
        }
    }
    remaining >= 0
}

#[cfg(test)]
mod tests {
    use super::*;

    fn list_doc() -> Ir {
        // group("[" indent(softline "a," line "b," line "c") softline "]")
        Ir::group(Ir::concat([
            Ir::text("["),
            Ir::indent(Ir::concat([
                Ir::SoftLine,
                Ir::text("a,"),
                Ir::Line,
                Ir::text("b,"),
                Ir::Line,
                Ir::text("c"),
            ])),
            Ir::SoftLine,
            Ir::text("]"),
        ]))
    }

    #[test]
    fn group_stays_flat_when_it_fits() {
        let style = FormatStyle {
            line_width: 80,
            indent_width: 4,
            ..FormatStyle::default()
        };
        assert_eq!(print(&list_doc(), style), "[a, b, c]");
    }

    #[test]
    fn group_breaks_when_too_wide() {
        let style = FormatStyle {
            line_width: 5,
            indent_width: 4,
            ..FormatStyle::default()
        };
        assert_eq!(print(&list_doc(), style), "[\n    a,\n    b,\n    c\n]");
    }

    #[test]
    fn print_at_starts_from_the_given_column() {
        // The flat rendering is 9 columns: it fits exactly at column 0, but
        // shifted to column 4 it must break — and every line break re-indents
        // relative to that base, with no leading indent on the first line.
        let style = FormatStyle {
            line_width: 9,
            indent_width: 4,
            ..FormatStyle::default()
        };
        assert_eq!(print_at(&list_doc(), style, 0), "[a, b, c]");
        assert_eq!(
            print_at(&list_doc(), style, 4),
            "[\n        a,\n        b,\n        c\n    ]"
        );
    }

    fn trailing_comma_doc() -> Ir {
        // group("(" indent(softline "a," line "b") ifbreak("," "") softline ")")
        Ir::group(Ir::concat([
            Ir::text("("),
            Ir::indent(Ir::concat([
                Ir::SoftLine,
                Ir::text("a,"),
                Ir::Line,
                Ir::text("b"),
                Ir::if_break(",", ""),
            ])),
            Ir::SoftLine,
            Ir::text(")"),
        ]))
    }

    #[test]
    fn if_break_is_empty_when_flat() {
        let style = FormatStyle {
            line_width: 80,
            indent_width: 4,
            ..FormatStyle::default()
        };
        assert_eq!(print(&trailing_comma_doc(), style), "(a, b)");
    }

    #[test]
    fn if_break_emits_when_broken() {
        let style = FormatStyle {
            line_width: 4,
            indent_width: 4,
            ..FormatStyle::default()
        };
        assert_eq!(print(&trailing_comma_doc(), style), "(\n    a,\n    b,\n)");
    }

    fn hug_doc() -> Ir {
        // f(aa, [x, y]) with a huggable last argument, as `lower_arg_list`
        // builds it: prefix `(aa, `, body the list's own group, explode the
        // standard width-driven group over both items.
        let body = || {
            Ir::group(Ir::concat([
                Ir::text("["),
                Ir::indent(Ir::concat([
                    Ir::SoftLine,
                    Ir::text("x,"),
                    Ir::Line,
                    Ir::text("y"),
                ])),
                Ir::SoftLine,
                Ir::text("]"),
            ]))
        };
        let explode = Ir::group(Ir::concat([
            Ir::text("("),
            Ir::indent(Ir::concat([
                Ir::SoftLine,
                Ir::text("aa"),
                Ir::text(","),
                Ir::Line,
                body(),
                Ir::if_break(",", ""),
            ])),
            Ir::SoftLine,
            Ir::text(")"),
        ]));
        Ir::concat([
            Ir::text("f"),
            Ir::hug_group(
                Ir::concat([Ir::text("("), Ir::text("aa"), Ir::text(", ")]),
                body(),
                Ir::text(")"),
                explode,
            ),
        ])
    }

    #[test]
    fn hug_group_stays_flat_when_it_fits() {
        let style = FormatStyle {
            line_width: 80,
            indent_width: 4,
            ..FormatStyle::default()
        };
        assert_eq!(print(&hug_doc(), style), "f(aa, [x, y])");
    }

    #[test]
    fn hug_group_hugs_when_first_line_fits() {
        // Flat (13) overflows, but the hug first line `f(aa, [` (7) fits.
        let style = FormatStyle {
            line_width: 8,
            indent_width: 4,
            ..FormatStyle::default()
        };
        assert_eq!(print(&hug_doc(), style), "f(aa, [\n    x,\n    y\n])");
    }

    #[test]
    fn hug_group_explodes_when_first_line_overflows() {
        // Even `f(aa, [` (7) overflows: the explode fallback breaks one item
        // per line, the list free to break further on its own.
        let style = FormatStyle {
            line_width: 6,
            indent_width: 4,
            ..FormatStyle::default()
        };
        assert_eq!(
            print(&hug_doc(), style),
            "f(\n    aa,\n    [\n        x,\n        y\n    ],\n)"
        );
    }
}