profmt 0.1.0

A fast, deterministic, configurable Protocol Buffers formatter built on tree-sitter
Documentation
//! Fields, enum values, and reserved or extension ranges.

use tree_sitter::Node;

use super::{Formatter, Walk};
use crate::doc::{SOFT, SPACE};
use crate::options::FieldOptionsLayout;

const LABELS: &[&str] = &["optional", "required", "repeated"];

impl Formatter<'_> {
    /// A message field or a `oneof` member. The grammar leaves a `oneof`
    /// member's `;` outside the node; both print one.
    pub fn field(&mut self, node: Node<'_>) {
        let mut walk = Walk::new(node);
        while let Some(label) = walk.accept_any(self, LABELS) {
            self.source_node(label);
            self.b.text(" ");
        }
        let field_type = walk.expect(self, "type");
        self.compact(field_type);
        self.field_tail(&mut walk);
        walk.accept(self, ";");
        self.b.text(";");
        walk.finish(self);
    }

    pub fn map_field(&mut self, node: Node<'_>) {
        let mut walk = Walk::new(node);
        walk.expect(self, "map");
        walk.expect(self, "<");
        self.b.text("map<");
        let key = walk.expect(self, "key_type");
        self.source_node(key);
        walk.expect(self, ",");
        self.b.text(", ");
        let value = walk.expect(self, "type");
        self.compact(value);
        walk.expect(self, ">");
        self.b.text(">");
        self.field_tail(&mut walk);
        walk.expect(self, ";");
        self.b.text(";");
        walk.finish(self);
    }

    /// ` name = number [options]` after a field's type.
    fn field_tail(&mut self, walk: &mut Walk<'_>) {
        self.b.text(" ");
        let name = walk.expect(self, "identifier");
        self.source_node(name);
        walk.expect(self, "=");
        self.b.text(" = ");
        let number = walk.expect(self, "field_number");
        self.compact(number);
        if walk.accept(self, "[").is_some() {
            let options = walk.expect(self, "field_options");
            self.bracketed(|f| {
                let mut walk = Walk::new(options);
                while let Some(child) = walk.next(f) {
                    match child.kind() {
                        "," => {
                            f.b.text(",");
                            f.option_separator();
                        }
                        _ => f.option_assignment(child),
                    }
                }
            });
            walk.expect(self, "]");
        }
    }

    pub fn enum_field(&mut self, node: Node<'_>) {
        let mut walk = Walk::new(node);
        let name = walk.expect(self, "identifier");
        self.source_node(name);
        walk.expect(self, "=");
        self.b.text(" = ");
        if let Some(sign) = walk.accept(self, "-") {
            self.source_node(sign);
        }
        let value = walk.expect(self, "int_lit");
        self.compact(value);
        if walk.accept(self, "[").is_some() {
            self.bracketed(|f| {
                while let Some(kind) = walk.peek()
                    && kind != "]"
                {
                    let child = walk.next(f).expect("peeked");
                    match child.kind() {
                        "," => {
                            f.b.text(",");
                            f.option_separator();
                        }
                        _ => f.option_assignment(child),
                    }
                }
            });
            walk.expect(self, "]");
        }
        walk.expect(self, ";");
        self.b.text(";");
        walk.finish(self);
    }

    /// ` [` options `]`. Options are separated by `,` followed by
    /// [`Formatter::option_separator`].
    fn bracketed(&mut self, options: impl FnOnce(&mut Self)) {
        self.b.text(" [");
        match self.options.field_options_layout {
            FieldOptionsLayout::SingleLine => {
                options(self);
                self.b.text("]");
            }
            FieldOptionsLayout::OnePerLine => {
                self.b.open_group();
                self.b.open_indent();
                self.b.line(SOFT);
                options(self);
                self.b.close_indent();
                self.b.line(SOFT);
                self.b.text("]");
                self.b.close_group();
            }
        }
    }

    /// The whitespace after the `,` between two options in a bracket.
    fn option_separator(&mut self) {
        match self.options.field_options_layout {
            FieldOptionsLayout::SingleLine => self.b.text(" "),
            FieldOptionsLayout::OnePerLine => self.b.line(SPACE),
        }
    }

    /// `reserved` numbers or names, or `extensions` ranges, on one line.
    pub fn reserved(&mut self, node: Node<'_>) {
        let mut walk = Walk::new(node);
        let keyword = walk.next(self).expect("keyword");
        self.source_node(keyword);
        let list = walk.next(self).expect("ranges or names");
        let mut items = Walk::new(list);
        while let Some(child) = items.next(self) {
            match child.kind() {
                "," => self.b.text(","),
                "range" => {
                    self.b.text(" ");
                    self.range(child);
                }
                "reserved_identifier" => {
                    self.b.text(" ");
                    self.reserved_identifier(child);
                }
                other => unreachable!("unexpected `{other}` in a reserved list"),
            }
        }
        walk.expect(self, ";");
        self.b.text(";");
        walk.finish(self);
    }

    fn range(&mut self, node: Node<'_>) {
        let mut walk = Walk::new(node);
        let start = walk.expect(self, "int_lit");
        self.compact(start);
        if walk.accept(self, "to").is_some() {
            self.b.text(" to ");
            let end = walk.next(self).expect("range end");
            self.compact(end);
        }
        walk.finish(self);
    }

    fn reserved_identifier(&mut self, node: Node<'_>) {
        let range = node.byte_range();
        if matches!(self.source[range.start], b'"' | b'\'') {
            self.quoted(range.start, range.end);
        } else {
            self.source_node(node);
        }
    }
}