profmt 0.1.0

A fast, deterministic, configurable Protocol Buffers formatter built on tree-sitter
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
//! Turns a tree-sitter parse tree into a document. This module owns the
//! formatter state, statement lists, blank lines and comment placement; each
//! family module owns the layouts for its statement kinds.

mod decls;
mod fields;
mod header;
mod literals;

use std::cell::RefCell;

use anyhow::{Context, bail};
use tree_sitter::{Node, Parser, Tree};

use crate::FormatOptions;
use crate::doc::{self, Builder, HARD, PrintOptions};
use crate::options::{DeclarationSpacing, EmptyBlockLayout, EmptyStatements};

const BOM: &[u8] = b"\xEF\xBB\xBF";

thread_local! {
    static PARSER: RefCell<Parser> = RefCell::new(new_parser());
}

fn new_parser() -> Parser {
    let mut parser = Parser::new();
    parser
        .set_language(&tree_sitter_proto::LANGUAGE.into())
        .expect("the bundled proto grammar matches the tree-sitter runtime");
    parser
}

pub fn format(source: &[u8]) -> anyhow::Result<String> {
    format_with_options(source, &FormatOptions::default())
}

pub fn format_with_options(source: &[u8], options: &FormatOptions) -> anyhow::Result<String> {
    let document = document(source, options)?;
    let output = doc::print(
        &document.document,
        document.source,
        print_options(options, document.source.len()),
    );
    if document.has_bom {
        let mut with_bom = String::with_capacity(BOM.len().saturating_add(output.len()));
        with_bom.push('\u{feff}');
        with_bom.push_str(&output);
        Ok(with_bom)
    } else {
        Ok(output)
    }
}

/// Returns whether `source` is already formatted using the default policy,
/// without allocating the rendered output.
pub fn is_formatted(source: &[u8]) -> anyhow::Result<bool> {
    is_formatted_with_options(source, &FormatOptions::default())
}

/// Returns whether `source` is already formatted using `options`, without
/// allocating the rendered output.
pub fn is_formatted_with_options(source: &[u8], options: &FormatOptions) -> anyhow::Result<bool> {
    let document = document(source, options)?;
    Ok(doc::matches_source(
        &document.document,
        document.source,
        print_options(options, 0),
    ))
}

struct Document<'a> {
    source: &'a str,
    document: doc::Document,
    has_bom: bool,
}

fn document<'a>(source: &'a [u8], options: &FormatOptions) -> anyhow::Result<Document<'a>> {
    options.validate()?;
    let (has_bom, source) = match source.strip_prefix(BOM) {
        Some(source) => (true, source),
        None => (false, source),
    };
    if u32::try_from(source.len()).is_err() {
        bail!("source exceeds the 4 GiB document-offset limit");
    }
    let source_text = std::str::from_utf8(source).context("source is not valid UTF-8")?;
    let tree = PARSER.with(|parser| parser.borrow_mut().parse(source, None));
    let tree = tree.context("parse protobuf source")?;
    check_parse(&tree, source)?;

    let root = tree.root_node();
    // Statements, comments and the lines around them each contribute a
    // handful of layout nodes.
    let capacity = root.descendant_count().saturating_mul(3);
    let mut formatter = Formatter {
        source,
        options,
        b: Builder::with_capacity(capacity),
        unsupported: Vec::new(),
    };
    formatter.source_file(root);
    if !formatter.unsupported.is_empty() {
        formatter.unsupported.sort();
        formatter.unsupported.dedup();
        bail!("unsupported nodes: {}", formatter.unsupported.join(", "));
    }
    let document = formatter.b.finish();
    drop(tree);
    Ok(Document {
        source: source_text,
        document,
        has_bom,
    })
}

fn check_parse(tree: &Tree, source: &[u8]) -> anyhow::Result<()> {
    let root = tree.root_node();
    if !root.has_error() {
        return Ok(());
    }
    let mut cursor = root.walk();
    loop {
        let node = cursor.node();
        if node.is_missing() {
            bail!(
                "parse error at line {}: missing {}",
                node.start_position().row + 1,
                node.kind()
            );
        }
        if node.is_error() {
            let text = String::from_utf8_lossy(&source[node.byte_range()]);
            let excerpt: String = text.lines().next().unwrap_or_default().chars().take(40).collect();
            bail!(
                "parse error at line {}: unexpected `{excerpt}`",
                node.start_position().row + 1
            );
        }
        // Descend into the first child that carries the error, otherwise move
        // to the next sibling, climbing while there is none.
        if node.has_error() && cursor.goto_first_child() {
            while !cursor.node().has_error() && !cursor.node().is_missing() {
                if !cursor.goto_next_sibling() {
                    bail!("parse error at line {}", node.start_position().row + 1);
                }
            }
            continue;
        }
        if !cursor.goto_next_sibling() {
            bail!("parse error at line {}", node.start_position().row + 1);
        }
    }
}

fn print_options(options: &FormatOptions, output_capacity: usize) -> PrintOptions {
    PrintOptions {
        line_width: options.line_width,
        indent_width: options.indent_width,
        output_capacity: output_capacity.saturating_add(1),
    }
}

pub struct Formatter<'a> {
    pub source: &'a [u8],
    pub options: &'a FormatOptions,
    pub b: Builder,
    unsupported: Vec<String>,
}

/// One entry of a statement list: a statement, a comment on its own line, a
/// comment sharing a line with the entry before it, or a stray `;`.
#[derive(Clone, Copy, Debug)]
pub enum Item<'t> {
    Statement(Node<'t>),
    Comment(Node<'t>),
    Trailing(Node<'t>),
    Empty(Node<'t>),
}

impl<'t> Item<'t> {
    pub fn node(self) -> Node<'t> {
        match self {
            Self::Statement(node) | Self::Comment(node) | Self::Trailing(node) | Self::Empty(node) => node,
        }
    }
}

/// Blank lines before an entry: counted from the source, or fixed by a
/// rewrite that moved the entry away from its source neighbours.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Blank {
    Source,
    Exactly(usize),
}

#[derive(Clone, Copy, Debug)]
pub struct Entry<'t> {
    pub item: Item<'t>,
    pub blank: Blank,
}

/// Statement kinds that can appear directly in some statement list.
const STATEMENT_KINDS: &[&str] = &[
    "syntax",
    "edition",
    "import",
    "package",
    "option",
    "enum",
    "message",
    "extend",
    "service",
    "field",
    "oneof",
    "map_field",
    "reserved",
    "extensions",
    "group",
    "enum_field",
    "oneof_field",
    "rpc",
];

fn is_statement(node: Node<'_>) -> bool {
    node.is_named() && STATEMENT_KINDS.contains(&node.kind())
}

/// Consumes a node's children in order, emitting any comment it passes over
/// inline so that no comment inside a statement is lost.
pub struct Walk<'t> {
    children: Vec<Node<'t>>,
    index: usize,
}

impl<'t> Walk<'t> {
    pub fn new(node: Node<'t>) -> Self {
        let mut cursor = node.walk();
        let children = node.children(&mut cursor).collect();
        Self { children, index: 0 }
    }

    /// The kind of the next non-comment child, if any.
    pub fn peek(&self) -> Option<&'static str> {
        self.children[self.index..]
            .iter()
            .find(|child| child.kind() != "comment")
            .map(|child| child.kind())
    }

    pub fn next(&mut self, f: &mut Formatter<'_>) -> Option<Node<'t>> {
        while let Some(&child) = self.children.get(self.index) {
            self.index += 1;
            if child.kind() == "comment" {
                f.inline_comment(child);
            } else {
                return Some(child);
            }
        }
        None
    }

    pub fn expect(&mut self, f: &mut Formatter<'_>, kind: &str) -> Node<'t> {
        let node = self
            .next(f)
            .unwrap_or_else(|| panic!("grammar guarantees a `{kind}` child"));
        assert_eq!(node.kind(), kind, "grammar guarantees a `{kind}` child here");
        node
    }

    /// Consumes the next child if it has `kind`.
    pub fn accept(&mut self, f: &mut Formatter<'_>, kind: &str) -> Option<Node<'t>> {
        (self.peek() == Some(kind)).then(|| self.expect(f, kind))
    }

    /// Consumes the next child if its kind is one of `kinds`.
    pub fn accept_any(&mut self, f: &mut Formatter<'_>, kinds: &[&str]) -> Option<Node<'t>> {
        let kind = self.peek()?;
        kinds.contains(&kind).then(|| self.expect(f, kind))
    }

    /// Emits every comment left in the walk.
    pub fn finish(mut self, f: &mut Formatter<'_>) {
        if let Some(child) = self.next(f) {
            panic!("unconsumed `{}` child", child.kind());
        }
    }
}

impl<'a> Formatter<'a> {
    pub fn text_of(&self, node: Node<'_>) -> &'a str {
        std::str::from_utf8(&self.source[node.byte_range()]).expect("source was validated as UTF-8")
    }

    pub fn source_node(&mut self, node: Node<'_>) {
        let range = node.byte_range();
        self.b.source(range.start, range.end);
    }

    /// Emits a statement's source verbatim. The first line starts at the
    /// current indentation; later lines keep their own.
    fn verbatim(&mut self, node: Node<'_>) {
        self.source_node(node);
        // The grammar leaves a `oneof` member's `;` outside its node.
        if node.kind() == "oneof_field" {
            self.b.text(";");
        }
        self.b.break_parent();
    }

    fn unsupported(&mut self, node: Node<'_>) {
        self.unsupported.push(node.kind().to_owned());
    }

    fn source_file(&mut self, root: Node<'_>) {
        let items = self.items(root);
        let entries = header::arrange(self, items);
        self.entries(&entries);
        // An empty file has no statement to end with a newline.
        if !entries.is_empty() {
            self.b.line(HARD);
        }
    }

    /// Classifies a container's children into statement-list items. Children
    /// that are not statements or comments (keywords, names, braces) are
    /// skipped; the caller prints those itself.
    pub fn items<'t>(&self, node: Node<'t>) -> Vec<Item<'t>> {
        let mut cursor = node.walk();
        let mut items: Vec<Item<'t>> = Vec::new();
        let mut previous_end_row = None;
        // Everything before an opening brace is the container's own header,
        // printed by the caller.
        let mut in_body = !node.children(&mut cursor).any(|child| child.kind() == "{");
        for child in node.children(&mut cursor) {
            if !in_body {
                in_body = child.kind() == "{";
                if in_body {
                    // A comment sharing the brace's line trails the header.
                    previous_end_row = Some(child.end_position().row);
                }
                continue;
            }
            let item = match child.kind() {
                "comment" => match previous_end_row {
                    Some(row) if child.start_position().row == row => Item::Trailing(child),
                    _ => Item::Comment(child),
                },
                "empty_statement" => Item::Empty(child),
                _ if is_statement(child) => Item::Statement(child),
                _ => continue,
            };
            previous_end_row = Some(child.end_position().row);
            items.push(item);
        }
        items
    }

    /// Whether a list of items prints nothing, so its block can be empty.
    pub fn prints_nothing(&self, items: &[Item<'_>]) -> bool {
        items.iter().all(|item| match item {
            Item::Empty(_) => self.options.empty_statements == EmptyStatements::Remove,
            _ => false,
        })
    }

    /// Prints `{`, the items on their own indented lines, and `}`.
    pub fn block(&mut self, items: Vec<Item<'_>>) {
        if self.prints_nothing(&items) {
            match self.options.empty_block_layout {
                EmptyBlockLayout::Compact => self.b.text("{}"),
                EmptyBlockLayout::Expanded => {
                    self.b.text("{");
                    self.b.line(HARD);
                    self.b.text("}");
                }
            }
            return;
        }
        let entries: Vec<Entry<'_>> = items
            .into_iter()
            .map(|item| Entry {
                item,
                blank: Blank::Source,
            })
            .collect();
        self.b.text("{");
        // Comments sharing the brace's line stay on it.
        let body_start = entries
            .iter()
            .take_while(|entry| matches!(entry.item, Item::Trailing(_)))
            .inspect(|entry| self.trailing_comment(entry.item.node()))
            .count();
        self.b.open_indent();
        self.b.line(HARD);
        self.entries(&entries[body_start..]);
        self.b.close_indent();
        self.b.line(HARD);
        self.b.text("}");
    }

    /// Prints entries separated by line breaks, applying the blank-line and
    /// comment policies. The caller supplies the line breaks before the first
    /// and after the last entry.
    pub fn entries(&mut self, entries: &[Entry<'_>]) {
        let leading = leading_targets(entries);
        let mut previous: Option<Previous> = None;
        for (index, entry) in entries.iter().enumerate() {
            match entry.item {
                Item::Trailing(comment) => {
                    self.trailing_comment(comment);
                    if let Some(previous) = &mut previous {
                        previous.end_row = comment.end_position().row;
                    }
                }
                Item::Empty(empty) => {
                    let terminates_oneof_field = previous.is_some_and(|previous| previous.oneof_field);
                    if !terminates_oneof_field && self.options.empty_statements == EmptyStatements::Preserve {
                        self.b.text(";");
                    }
                    if let Some(previous) = &mut previous {
                        previous.end_row = empty.end_position().row;
                        previous.oneof_field = false;
                    }
                }
                Item::Comment(comment) => {
                    let target = leading[index].map(|target| entries[target].item.node());
                    let attached = leading[index].is_some()
                        && previous.is_some_and(|previous| previous.leading_of == leading[index]);
                    self.separate(entry, previous, target.map(|node| self.is_block(node)), attached);
                    self.own_line_comment(comment);
                    previous = Some(Previous {
                        end_row: comment.end_position().row,
                        block: false,
                        oneof_field: false,
                        leading_of: leading[index],
                    });
                }
                Item::Statement(statement) => {
                    let block = self.is_block(statement);
                    let attached = previous.is_some_and(|previous| previous.leading_of == Some(index));
                    self.separate(entry, previous, Some(block), attached);
                    if attached && self.is_ignored(entries, index) {
                        self.verbatim(statement);
                    } else {
                        self.statement(statement);
                    }
                    previous = Some(Previous {
                        end_row: statement.end_position().row,
                        block,
                        oneof_field: statement.kind() == "oneof_field",
                        leading_of: None,
                    });
                }
            }
        }
    }

    /// Emits the line breaks before an entry: one to leave the previous line,
    /// then one per blank line.
    fn separate(&mut self, entry: &Entry<'_>, previous: Option<Previous>, block: Option<bool>, attached: bool) {
        let Some(previous) = previous else {
            return;
        };
        self.b.line(HARD);
        let mut blank = match entry.blank {
            Blank::Exactly(count) => count,
            Blank::Source => {
                let start_row = entry.item.node().start_position().row;
                start_row
                    .saturating_sub(previous.end_row)
                    .saturating_sub(1)
                    .min(self.options.blank_line_limit)
            }
        };
        if attached {
            blank = 0;
        } else if self.options.declaration_spacing == DeclarationSpacing::Separated
            && (previous.block || block.unwrap_or(false))
        {
            blank = blank.max(1);
        }
        for _ in 0..blank {
            self.b.line(HARD);
        }
    }

    /// Whether a statement prints a multi-line body, so `separated`
    /// declaration spacing sets it apart with blank lines.
    fn is_block(&self, node: Node<'_>) -> bool {
        match node.kind() {
            "message" | "enum" | "extend" | "group" => {
                let mut cursor = node.walk();
                let body = node
                    .named_children(&mut cursor)
                    .find(|child| child.kind().ends_with("_body"));
                body.is_some_and(|body| self.body_is_multiline(body))
            }
            "service" | "oneof" | "rpc" => self.body_is_multiline(node),
            _ => false,
        }
    }

    fn body_is_multiline(&self, container: Node<'_>) -> bool {
        let items = self.items(container);
        !self.prints_nothing(&items) || self.options.empty_block_layout == EmptyBlockLayout::Expanded
    }

    /// Whether the comment run leading into `entries[index]` ends with an
    /// ignore directive.
    fn is_ignored(&self, entries: &[Entry<'_>], index: usize) -> bool {
        let Some(comment) = entries[..index].iter().rev().find_map(|entry| match entry.item {
            Item::Comment(comment) => Some(comment),
            _ => None,
        }) else {
            return false;
        };
        let Some(body) = self.text_of(comment).strip_prefix("//") else {
            return false;
        };
        let body = body.trim();
        self.options.ignore_directives.iter().any(|directive| directive == body)
    }

    fn statement(&mut self, node: Node<'_>) {
        match node.kind() {
            "syntax" => self.syntax(node),
            "edition" => self.edition(node),
            "import" => self.import(node),
            "package" => self.package(node),
            "option" => self.option(node),
            "message" | "enum" => self.named_block(node),
            "extend" => self.extend(node),
            "group" => self.group(node),
            "service" => self.service(node),
            "oneof" => self.oneof(node),
            "rpc" => self.rpc(node),
            "field" | "oneof_field" => self.field(node),
            "map_field" => self.map_field(node),
            "enum_field" => self.enum_field(node),
            "reserved" | "extensions" => self.reserved(node),
            _ => self.unsupported(node),
        }
    }
}

#[derive(Clone, Copy)]
struct Previous {
    end_row: usize,
    block: bool,
    oneof_field: bool,
    /// For an own-line comment: the statement entry it is attached to.
    leading_of: Option<usize>,
}

/// For each own-line comment, the index of the statement it leads into: the
/// comment run must be adjacent to the statement with no blank lines within.
fn leading_targets(entries: &[Entry<'_>]) -> Vec<Option<usize>> {
    let mut targets = vec![None; entries.len()];
    let mut next: Option<(usize, usize)> = None;
    for (index, entry) in entries.iter().enumerate().rev() {
        match entry.item {
            Item::Statement(node) => next = Some((index, node.start_position().row)),
            Item::Comment(node) => {
                let adjacent = next.is_some_and(|(_, row)| node.end_position().row + 1 >= row);
                if adjacent {
                    targets[index] = next.map(|(target, _)| target);
                    next = next.map(|(target, _)| (target, node.start_position().row));
                } else {
                    next = None;
                }
            }
            Item::Trailing(_) => {}
            Item::Empty(_) => next = None,
        }
    }
    targets
}