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)
}
}
pub fn is_formatted(source: &[u8]) -> anyhow::Result<bool> {
is_formatted_with_options(source, &FormatOptions::default())
}
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();
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
);
}
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>,
}
#[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,
}
}
}
#[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,
}
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())
}
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 }
}
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
}
pub fn accept(&mut self, f: &mut Formatter<'_>, kind: &str) -> Option<Node<'t>> {
(self.peek() == Some(kind)).then(|| self.expect(f, kind))
}
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))
}
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);
}
fn verbatim(&mut self, node: Node<'_>) {
self.source_node(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);
if !entries.is_empty() {
self.b.line(HARD);
}
}
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;
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 {
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
}
pub fn prints_nothing(&self, items: &[Item<'_>]) -> bool {
items.iter().all(|item| match item {
Item::Empty(_) => self.options.empty_statements == EmptyStatements::Remove,
_ => false,
})
}
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("{");
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("}");
}
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,
});
}
}
}
}
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);
}
}
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
}
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,
leading_of: Option<usize>,
}
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
}