git.delta.rocks / jrsonnet / refs/commits / 8bc6498a805d

difftreelog

test import formatting test-cases from rustanka

umopspmtYaroslav Bolyukin2026-02-12parent: #fc45c6c.patch.diff
in: master

45 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -156,6 +156,16 @@
 ]
 
 [[package]]
+name = "bstr"
+version = "1.12.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "63044e1ae8e69f3b5a92c736ca6269b8d12fa7efe39bf34ddb06d102cf0e2cab"
+dependencies = [
+ "memchr",
+ "serde",
+]
+
+[[package]]
 name = "bumpalo"
 version = "3.19.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -453,6 +463,19 @@
 ]
 
 [[package]]
+name = "globset"
+version = "0.4.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "52dfc19153a48bde0cbd630453615c8151bce3a5adfac7a0aebfbf0a1e1f57e3"
+dependencies = [
+ "aho-corasick",
+ "bstr",
+ "log",
+ "regex-automata",
+ "regex-syntax",
+]
+
+[[package]]
 name = "hashbrown"
 version = "0.14.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -548,9 +571,11 @@
 checksum = "e82db8c87c7f1ccecb34ce0c24399b8a73081427f3c7c50a5d597925356115e4"
 dependencies = [
  "console",
+ "globset",
  "once_cell",
  "similar",
  "tempfile",
+ "walkdir",
 ]
 
 [[package]]
@@ -797,6 +822,12 @@
 checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039"
 
 [[package]]
+name = "log"
+version = "0.4.29"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"
+
+[[package]]
 name = "logos"
 version = "0.16.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1157,6 +1188,15 @@
 checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
 
 [[package]]
+name = "same-file"
+version = "1.0.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
+dependencies = [
+ "winapi-util",
+]
+
+[[package]]
 name = "saphyr-parser-bw"
 version = "0.0.607"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1491,6 +1531,16 @@
 ]
 
 [[package]]
+name = "walkdir"
+version = "2.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
+dependencies = [
+ "same-file",
+ "winapi-util",
+]
+
+[[package]]
 name = "wasip2"
 version = "1.0.2+wasi-0.2.9"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1545,6 +1595,15 @@
 ]
 
 [[package]]
+name = "winapi-util"
+version = "0.1.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
+dependencies = [
+ "windows-sys 0.59.0",
+]
+
+[[package]]
 name = "windows-link"
 version = "0.2.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
modifiedCargo.tomldiffbeforeafterboth
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -38,7 +38,7 @@
 # Parsing, manifestification is implemented manually everywhere
 serde = "1.0.228"
 serde_json = "1.0.149"
-serde-saphyr = {version = "0.0.17", default-features = false}
+serde-saphyr = { version = "0.0.17", default-features = false }
 
 # Error handling
 anyhow = "1.0.101"
@@ -63,7 +63,7 @@
 
 mimallocator = "0.1.3"
 indoc = "2.0"
-insta = "1.46"
+insta = { version = "1.46", features = ["glob"] }
 tempfile = "3.24"
 pathdiff = "0.2.3"
 hashbrown = "0.16.1"
modifiedcrates/jrsonnet-formatter/src/lib.rsdiffbeforeafterboth
after · crates/jrsonnet-formatter/src/lib.rs
1use std::{any::type_name, rc::Rc};23use children::{children_between, trivia_before};4use dprint_core::formatting::{5	condition_helpers::is_multiple_lines,6	condition_resolvers::true_resolver,7	ir_helpers::{new_line_group, with_indent},8	ConditionResolver, ConditionResolverContext, LineNumber, PrintItems, PrintOptions,9};10use hi_doc::{Formatting, SnippetBuilder};11use jrsonnet_rowan_parser::{12	nodes::{13		Arg, ArgsDesc, Assertion, BinaryOperator, Bind, CompSpec, Destruct, DestructArrayPart,14		DestructRest, Expr, ExprBase, FieldName, ForSpec, IfSpec, ImportKind, Literal, Member,15		Name, Number, ObjBody, ObjLocal, ParamsDesc, SliceDesc, SourceFile, Stmt, Suffix, Text,16		TextKind, UnaryOperator, Visibility,17	},18	AstNode, AstToken as _, SyntaxToken,19};2021use crate::{22	children::{trivia_after, Child, EndingComments},23	comments::{format_comments, CommentLocation},24};2526mod children;27mod comments;28mod tests;2930fn with_indent_eoi(cond: ConditionResolver, o: PrintItems, e: EndingComments) -> PrintItems {31	let end_comments_items = {32		let mut items = PrintItems::new();33		if e.should_start_with_newline {34			p!(&mut items, nl);35		}36		format_comments(&e.trivia, CommentLocation::EndOfItems, &mut items);37		items.into_rc_path()38	};39	let items =40		new_line_group(pi!(@i; items(o.into()) items(end_comments_items.into()))).into_rc_path();4142	let indented = with_indent(pi!(@i; nl items(items.into())));4344	pi!(@i; if_else("indented body", cond, items(indented))(str(" ") items(items.into())))45}4647pub trait Printable {48	fn print(&self, out: &mut PrintItems);49}5051macro_rules! pi {52	(@i; $($t:tt)*) => {{53		#[allow(unused_mut)]54		let mut o = dprint_core::formatting::PrintItems::new();55		pi!(@s; o: $($t)*);56		o57	}};58	(@s; $o:ident: str($e:expr $(,)?) $($t:tt)*) => {{59		$o.push_string($e.to_owned());60		pi!(@s; $o: $($t)*);61	}};62	(@s; $o:ident: string($e:expr $(,)?) $($t:tt)*) => {{63		$o.push_string($e);64		pi!(@s; $o: $($t)*);65	}};66	(@s; $o:ident: nl $($t:tt)*) => {{67		$o.push_signal(dprint_core::formatting::Signal::NewLine);68		pi!(@s; $o: $($t)*);69	}};70	(@s; $o:ident: sonl $($t:tt)*) => {{71		$o.push_signal(dprint_core::formatting::Signal::SpaceOrNewLine);72		pi!(@s; $o: $($t)*);73	}};74	(@s; $o:ident: tab $($t:tt)*) => {{75		$o.push_signal(dprint_core::formatting::Signal::Tab);76		pi!(@s; $o: $($t)*);77	}};78	(@s; $o:ident: >i $($t:tt)*) => {{79		$o.push_signal(dprint_core::formatting::Signal::StartIndent);80		pi!(@s; $o: $($t)*);81	}};82	(@s; $o:ident: <i $($t:tt)*) => {{83		$o.push_signal(dprint_core::formatting::Signal::FinishIndent);84		pi!(@s; $o: $($t)*);85	}};86	(@s; $o:ident: info($v:expr) $($t:tt)*) => {{87		$o.push_info($v);88		pi!(@s; $o: $($t)*);89	}};90	(@s; $o:ident: ln_anchor($v:expr) $($t:tt)*) => {{91		$o.push_anchor(LineNumberAnchor::new($v));92		pi!(@s; $o: $($t)*);93	}};94	(@s; $o:ident: if($s:literal, $cond:expr, $($i:tt)*) $($t:tt)*) => {{95		$o.push_condition(dprint_core::formatting::conditions::if_true(96			$s,97			$cond.clone(),98			{99				let mut o = PrintItems::new();100				p!(o, $($i)*);101				o102			},103		));104		pi!(@s; $o: $($t)*);105	}};106	(@s; $o:ident: if_else($s:literal, $cond:expr, $($i:tt)*)($($e:tt)+) $($t:tt)*) => {{107		$o.push_condition(dprint_core::formatting::conditions::if_true_or(108			$s,109			$cond.clone(),110			{111				let mut o = PrintItems::new();112				p!(o, $($i)*);113				o114			},115			{116				let mut o = PrintItems::new();117				p!(o, $($e)*);118				o119			},120		));121		pi!(@s; $o: $($t)*);122	}};123	(@s; $o:ident: if_not($s:literal, $cond:expr, $($e:tt)*) $($t:tt)*) => {{124		$o.push_condition(dprint_core::formatting::conditions::if_true_or(125			$s,126			$cond.clone(),127			{128				let o = PrintItems::new();129				o130			},131			{132				let mut o = PrintItems::new();133				p!(o, $($e)*);134				o135			},136		));137		pi!(@s; $o: $($t)*);138	}};139	(@s; $o:ident: {$expr:expr} $($t:tt)*) => {{140		$expr.print($o);141		pi!(@s; $o: $($t)*);142	}};143	(@s; $o:ident: items($expr:expr) $($t:tt)*) => {{144		$o.extend($expr);145		pi!(@s; $o: $($t)*);146	}};147	(@s; $o:ident: if ($e:expr)($($then:tt)*) $($t:tt)*) => {{148		if $e {149			pi!(@s; $o: $($then)*);150		}151		pi!(@s; $o: $($t)*);152	}};153	(@s; $o:ident: ifelse ($e:expr)($($then:tt)*)($($else:tt)*) $($t:tt)*) => {{154		if $e {155			pi!(@s; $o: $($then)*);156		} else {157			pi!(@s; $o: $($else)*);158		}159		pi!(@s; $o: $($t)*);160	}};161	(@s; $i:ident:) => {}162}163macro_rules! p {164	($o:ident, $($t:tt)*) => {165		pi!(@s; $o: $($t)*)166	};167	(&mut $o:ident, $($t:tt)*) => {168		let om = &mut $o;169		pi!(@s; om: $($t)*)170	};171}172pub(crate) use p;173pub(crate) use pi;174175impl<P> Printable for Option<P>176where177	P: Printable,178{179	fn print(&self, out: &mut PrintItems) {180		if let Some(v) = self {181			v.print(out);182		} else {183			p!(184				out,185				string(format!(186					"/*missing {}*/",187					type_name::<P>().replace("jrsonnet_rowan_parser::generated::nodes::", "")188				),)189			);190		}191	}192}193194impl Printable for SyntaxToken {195	fn print(&self, out: &mut PrintItems) {196		p!(out, string(self.to_string()));197	}198}199200impl Printable for Text {201	fn print(&self, out: &mut PrintItems) {202		if matches!(self.kind(), TextKind::StringBlock) {203			let text = self.text();204205			for (i, ele) in text.split("\n").enumerate() {206				if i != 0 {207					p!(out, nl);208				}209				// TODO: Trim and recreate whitespace210				p!(out, string(ele.to_string()));211			}212			return;213		}214		p!(out, string(format!("{}", self)));215	}216}217impl Printable for Number {218	fn print(&self, out: &mut PrintItems) {219		p!(out, string(format!("{}", self)));220	}221}222223impl Printable for Name {224	fn print(&self, out: &mut PrintItems) {225		p!(out, { self.ident_lit() });226	}227}228229impl Printable for DestructRest {230	fn print(&self, out: &mut PrintItems) {231		p!(out, str("..."));232		if let Some(name) = self.into() {233			p!(out, { name });234		}235	}236}237238impl Printable for Destruct {239	fn print(&self, out: &mut PrintItems) {240		match self {241			Self::DestructFull(f) => {242				p!(out, { f.name() });243			}244			Self::DestructSkip(_) => p!(out, str("?")),245			Self::DestructArray(a) => {246				p!(out, str("[") >i nl);247				for el in a.destruct_array_parts() {248					match el {249						DestructArrayPart::DestructArrayElement(e) => {250							p!(out, {e.destruct()} str(",") nl);251						}252						DestructArrayPart::DestructRest(d) => {253							p!(out, {d} str(",") nl);254						}255					}256				}257				p!(out, <i str("]"));258			}259			Self::DestructObject(o) => {260				p!(out, str("{") >i nl);261				for item in o.destruct_object_fields() {262					p!(out, { item.field() });263					if let Some(des) = item.destruct() {264						p!(out, str(": ") {des});265					}266					if let Some(def) = item.expr() {267						p!(out, str(" = ") {def});268					}269					p!(out, str(",") nl);270				}271				if let Some(rest) = o.destruct_rest() {272					p!(out, {rest} nl);273				}274				p!(out, <i str("}"));275			}276		}277	}278}279280impl Printable for FieldName {281	fn print(&self, out: &mut PrintItems) {282		match self {283			Self::FieldNameFixed(f) => {284				if let Some(id) = f.id() {285					p!(out, { id });286				} else if let Some(str) = f.text() {287					p!(out, { str });288				} else {289					p!(out, str("/*missing FieldName*/"));290				}291			}292			Self::FieldNameDynamic(d) => {293				p!(out, str("[") {d.expr()} str("]"));294			}295		}296	}297}298299impl Printable for Visibility {300	fn print(&self, out: &mut PrintItems) {301		p!(out, string(self.to_string()));302	}303}304305impl Printable for ObjLocal {306	fn print(&self, out: &mut PrintItems) {307		p!(out, str("local ") {self.bind()});308	}309}310311impl Printable for Assertion {312	fn print(&self, out: &mut PrintItems) {313		p!(out, str("assert ") {self.condition()});314		if self.colon_token().is_some() || self.message().is_some() {315			p!(out, str(": ") {self.message()});316		}317	}318}319320impl Printable for ParamsDesc {321	fn print(&self, out: &mut PrintItems) {322		p!(out, str("(") >i nl);323		for param in self.params() {324			p!(out, { param.destruct() });325			if param.assign_token().is_some() || param.expr().is_some() {326				p!(out, str(" = ") {param.expr()});327			}328			p!(out, str(",") nl);329		}330		p!(out, <i str(")"));331	}332}333impl Printable for ArgsDesc {334	fn print(&self, out: &mut PrintItems) {335		let start = LineNumber::new("args start line");336		let end = LineNumber::new("args end line");337		let multi_line = Rc::new(move |condition_context: &mut ConditionResolverContext| {338			is_multiple_lines(condition_context, start, end)339		});340341		let (children, end_comments) = children_between::<Arg>(342			self.syntax().clone(),343			self.l_paren_token().map(Into::into).as_ref(),344			self.r_paren_token().map(Into::into).as_ref(),345			None,346		);347348		fn gen_args(children: Vec<Child<Arg>>, multi_line: ConditionResolver) -> PrintItems {349			let mut _out = PrintItems::new();350			let out = &mut _out;351352			let mut args = children.into_iter().peekable();353			while let Some(ele) = args.next() {354				if ele.should_start_with_newline {355					p!(out, nl);356				}357				format_comments(&ele.before_trivia, CommentLocation::AboveItem, out);358				let arg = ele.value;359				if arg.name().is_some() || arg.assign_token().is_some() {360					p!(out, {arg.name()} str(" = "));361				}362				p!(out, { arg.expr() });363				let has_more = args.peek().is_some();364				if has_more {365					p!(out, str(","));366				} else {367					p!(out, if("trailing comma", multi_line, str(",")));368				}369				format_comments(&ele.inline_trivia, CommentLocation::ItemInline, out);370				if has_more {371					p!(out, if_else("arg separator", multi_line, nl)(sonl));372				}373			}374			_out375		}376377		let args_items = new_line_group(gen_args(children, multi_line.clone())).into_rc_path();378		let args_indented = with_indent(pi!(@i; nl items(args_items.into())));379380		p!(out, str("(") info(start));381		p!(out, if_else("args body", multi_line, items(args_indented) nl)(items(args_items.into())));382		if end_comments.should_start_with_newline {383			p!(out, nl);384		}385		format_comments(&end_comments.trivia, CommentLocation::EndOfItems, out);386		p!(out, str(")") info(end));387	}388}389impl Printable for SliceDesc {390	fn print(&self, out: &mut PrintItems) {391		p!(out, str("["));392		if self.from().is_some() {393			p!(out, { self.from() });394		}395		p!(out, str(":"));396		if self.end().is_some() {397			p!(out, { self.end().map(|e| e.expr()) });398		}399		// Keep only one : in case if we don't need step400		if self.step().is_some() {401			p!(out, str(":") {self.step().map(|e|e.expr())});402		}403		p!(out, str("]"));404	}405}406407impl Printable for Member {408	fn print(&self, out: &mut PrintItems) {409		match self {410			Self::MemberBindStmt(b) => {411				p!(out, { b.obj_local() });412			}413			Self::MemberAssertStmt(ass) => {414				p!(out, { ass.assertion() });415			}416			Self::MemberFieldNormal(n) => {417				p!(out, {n.field_name()} if(n.plus_token().is_some())({n.plus_token()}) {n.visibility()} str(" ") {n.expr()});418			}419			Self::MemberFieldMethod(m) => {420				p!(out, {m.field_name()} {m.params_desc()} {m.visibility()} str(" ") {m.expr()});421			}422		}423	}424}425426impl Printable for ObjBody {427	fn print(&self, out: &mut PrintItems) {428		match self {429			Self::ObjBodyComp(l) => {430				let (children, mut end_comments) = children_between::<Member>(431					l.syntax().clone(),432					l.l_brace_token().map(Into::into).as_ref(),433					Some(434						&(l.comp_specs()435							.next()436							.expect("at least one spec is defined")437							.syntax()438							.clone())439						.into(),440					),441					None,442				);443				let trailing_for_comp = end_comments.extract_trailing();444				p!(out, str("{") >i nl);445				for mem in children {446					if mem.should_start_with_newline {447						p!(out, nl);448					}449					format_comments(&mem.before_trivia, CommentLocation::AboveItem, out);450					p!(out, {mem.value} str(","));451					format_comments(&mem.inline_trivia, CommentLocation::ItemInline, out);452					p!(out, nl);453				}454455				if end_comments.should_start_with_newline {456					p!(out, nl);457				}458				format_comments(&end_comments.trivia, CommentLocation::EndOfItems, out);459460				let (compspecs, end_comments) = children_between::<CompSpec>(461					l.syntax().clone(),462					l.member_comps()463						.last()464						.map(|m| m.syntax().clone())465						.map(Into::into)466						.or_else(|| l.l_brace_token().map(Into::into))467						.as_ref(),468					l.r_brace_token().map(Into::into).as_ref(),469					Some(trailing_for_comp),470				);471				for mem in compspecs {472					if mem.should_start_with_newline {473						p!(out, nl);474					}475					format_comments(&mem.before_trivia, CommentLocation::AboveItem, out);476					p!(out, { mem.value });477					format_comments(&mem.inline_trivia, CommentLocation::ItemInline, out);478				}479				if end_comments.should_start_with_newline {480					p!(out, nl);481				}482				format_comments(&end_comments.trivia, CommentLocation::EndOfItems, out);483484				p!(out, nl <i str("}"));485			}486			Self::ObjBodyMemberList(l) => {487				let (children, end_comments) = children_between::<Member>(488					l.syntax().clone(),489					l.l_brace_token().map(Into::into).as_ref(),490					l.r_brace_token().map(Into::into).as_ref(),491					None,492				);493				if children.is_empty() && end_comments.is_empty() {494					p!(out, str("{ }"));495					return;496				}497498				let source_is_multiline = children.iter().any(|c| c.triggers_multiline)499					|| end_comments.should_start_with_newline;500501				let start = LineNumber::new("obj start line");502				let end = LineNumber::new("obj end line");503				let multi_line: ConditionResolver = if source_is_multiline {504					true_resolver()505				} else {506					Rc::new(move |ctx: &mut ConditionResolverContext| {507						is_multiple_lines(ctx, start, end)508					})509				};510511				fn gen_members(512					children: Vec<Child<Member>>,513					multi_line: ConditionResolver,514				) -> PrintItems {515					let mut _out = PrintItems::new();516					let out = &mut _out;517					let mut members = children.into_iter().peekable();518					while let Some(mem) = members.next() {519						if mem.should_start_with_newline {520							p!(out, nl);521						}522						format_comments(&mem.before_trivia, CommentLocation::AboveItem, out);523						p!(out, { mem.value });524						let has_more = members.peek().is_some();525						if has_more {526							p!(out, str(","));527						} else {528							p!(out, if("trailing comma", multi_line, str(",")));529						}530						format_comments(&mem.inline_trivia, CommentLocation::ItemInline, out);531						p!(out, if_else("member separator", multi_line, nl)(sonl));532					}533					_out534				}535536				let members_items =537					new_line_group(gen_members(children, multi_line.clone())).into_rc_path();538539				let members = with_indent_eoi(multi_line, members_items.into(), end_comments);540541				p!(out, str("{") info(start));542				p!(out, items(members));543				p!(out, str("}") info(end));544			}545		}546	}547}548impl Printable for UnaryOperator {549	fn print(&self, out: &mut PrintItems) {550		p!(out, string(self.text().to_string()));551	}552}553impl Printable for BinaryOperator {554	fn print(&self, out: &mut PrintItems) {555		p!(out, string(self.text().to_string()));556	}557}558impl Printable for Bind {559	fn print(&self, out: &mut PrintItems) {560		match self {561			Self::BindDestruct(d) => {562				p!(out, {d.into()} str(" = ") {d.value()});563			}564			Self::BindFunction(f) => {565				p!(out, {f.name()} {f.params()} str(" = ") {f.value()});566			}567		}568	}569}570impl Printable for Literal {571	fn print(&self, out: &mut PrintItems) {572		p!(out, string(self.syntax().to_string()));573	}574}575impl Printable for ImportKind {576	fn print(&self, out: &mut PrintItems) {577		p!(out, string(self.syntax().to_string()));578	}579}580impl Printable for ForSpec {581	fn print(&self, out: &mut PrintItems) {582		p!(out, str("for ") {self.bind()} str(" in ") {self.expr()});583	}584}585impl Printable for IfSpec {586	fn print(&self, out: &mut PrintItems) {587		p!(out, str("if ") {self.expr()});588	}589}590impl Printable for CompSpec {591	fn print(&self, out: &mut PrintItems) {592		match self {593			Self::ForSpec(f) => f.print(out),594			Self::IfSpec(i) => i.print(out),595		}596	}597}598impl Printable for Expr {599	fn print(&self, out: &mut PrintItems) {600		let (stmts, _ending) = children_between::<Stmt>(601			self.syntax().clone(),602			None,603			self.expr_base()604				.as_ref()605				.map(ExprBase::syntax)606				.cloned()607				.map(Into::into)608				.as_ref(),609			None,610		);611		for stmt in stmts {612			p!(out, { stmt.value });613		}614		p!(out, { self.expr_base() });615		let (suffixes, _ending) = children_between::<Suffix>(616			self.syntax().clone(),617			self.expr_base()618				.as_ref()619				.map(ExprBase::syntax)620				.cloned()621				.map(Into::into)622				.as_ref(),623			None,624			None,625		);626		for suffix in suffixes {627			p!(out, { suffix.value });628		}629	}630}631impl Printable for Suffix {632	fn print(&self, out: &mut PrintItems) {633		match self {634			Self::SuffixIndex(i) => {635				if i.question_mark_token().is_some() {636					p!(out, str("?"));637				}638				p!(out, str(".") {i.index()});639			}640			Self::SuffixIndexExpr(e) => {641				if e.question_mark_token().is_some() {642					p!(out, str(".?"));643				}644				p!(out, str("[") {e.index()} str("]"));645			}646			Self::SuffixSlice(d) => {647				p!(out, { d.slice_desc() });648			}649			Self::SuffixApply(a) => {650				p!(out, { a.args_desc() });651			}652		}653	}654}655impl Printable for Stmt {656	fn print(&self, out: &mut PrintItems) {657		match self {658			Self::StmtLocal(l) => {659				let (binds, end_comments) = children_between::<Bind>(660					l.syntax().clone(),661					l.local_kw_token().map(Into::into).as_ref(),662					l.semi_token().map(Into::into).as_ref(),663					None,664				);665				if binds.len() == 1 {666					let bind = &binds[0];667					format_comments(&bind.before_trivia, CommentLocation::AboveItem, out);668					p!(out, str("local ") {bind.value});669				// TODO: keep end_comments, child.inline_trivia somehow, force multiple locals formatting in case of presence?670				} else {671					p!(out,str("local") >i nl);672					for bind in binds {673						if bind.should_start_with_newline {674							p!(out, nl);675						}676						format_comments(&bind.before_trivia, CommentLocation::AboveItem, out);677						p!(out, {bind.value} str(","));678						format_comments(&bind.inline_trivia, CommentLocation::ItemInline, out);679						p!(out, nl);680					}681					if end_comments.should_start_with_newline {682						p!(out, nl);683					}684					format_comments(&end_comments.trivia, CommentLocation::EndOfItems, out);685					p!(out,<i);686				}687				p!(out,str(";") nl);688			}689			Self::StmtAssert(a) => {690				p!(out, {a.assertion()} str(";") nl);691			}692		}693	}694}695impl Printable for ExprBase {696	fn print(&self, out: &mut PrintItems) {697		match self {698			Self::ExprBinary(b) => {699				p!(out, {b.lhs()} str(" ") {b.binary_operator()} str(" ") {b.rhs()});700			}701			Self::ExprUnary(u) => p!(out, {u.unary_operator()} {u.rhs()}),702			// Self::ExprSlice(s) => {703			// 	p!(new: {s.expr()} {s.slice_desc()})704			// }705			// Self::ExprIndex(i) => {706			// 	p!(new: {i.expr()} str(".") {i.index()})707			// }708			// Self::ExprIndexExpr(i) => p!(new: {i.base()} str("[") {i.index()} str("]")),709			// Self::ExprApply(a) => {710			// 	let mut pi = p!(new: {a.expr()} {a.args_desc()});711			// 	if a.tailstrict_kw_token().is_some() {712			// 		p!(out,str(" tailstrict"));713			// 	}714			// 	pi715			// }716			Self::ExprObjExtend(ex) => {717				p!(out, {ex.lhs_work()} str(" ") {ex.rhs_work()});718			}719			Self::ExprParened(p) => {720				p!(out, str("(") {p.expr()} str(")"));721			}722			Self::ExprString(s) => p!(out, { s.text() }),723			Self::ExprNumber(n) => p!(out, { n.number() }),724			Self::ExprArray(a) => {725				p!(out, str("[") >i nl);726				for el in a.exprs() {727					p!(out, {el} str(",") nl);728				}729				p!(out, <i str("]"));730			}731			Self::ExprObject(obj) => {732				p!(out, { obj.obj_body() });733			}734			Self::ExprArrayComp(arr) => {735				p!(out, str("[") {arr.expr()});736				for spec in arr.comp_specs() {737					p!(out, str(" ") {spec});738				}739				p!(out, str("]"));740			}741			Self::ExprImport(v) => {742				p!(out, {v.import_kind()} str(" ") {v.text()});743			}744			Self::ExprVar(n) => p!(out, { n.name() }),745			// Self::ExprLocal(l) => {746			// }747			Self::ExprIfThenElse(ite) => {748				p!(out, str("if ") {ite.cond()} str(" then ") {ite.then().map(|t| t.expr())});749				if ite.else_kw_token().is_some() || ite.else_().is_some() {750					p!(out, str(" else ") {ite.else_().map(|t| t.expr())});751				}752			}753			Self::ExprFunction(f) => p!(out, str("function") {f.params_desc()} nl {f.expr()}),754			// Self::ExprAssert(a) => p!(new: {a.assertion()} str("; ") {a.expr()}),755			Self::ExprError(e) => p!(out, str("error ") {e.expr()}),756			Self::ExprLiteral(l) => {757				p!(out, { l.literal() });758			}759		}760	}761}762763impl Printable for SourceFile {764	fn print(&self, out: &mut PrintItems) {765		let before = trivia_before(766			self.syntax().clone(),767			self.expr()768				.map(|e| e.syntax().clone())769				.map(Into::into)770				.as_ref(),771		);772		let after = trivia_after(773			self.syntax().clone(),774			self.expr()775				.map(|e| e.syntax().clone())776				.map(Into::into)777				.as_ref(),778		);779		format_comments(&before, CommentLocation::AboveItem, out);780		p!(out, {self.expr()} nl);781		format_comments(&after, CommentLocation::EndOfItems, out);782	}783}784785pub struct FormatOptions {786	// 0 for hard tabs787	pub indent: u8,788}789pub fn format(input: &str, opts: &FormatOptions) -> Result<String, SnippetBuilder> {790	let (parsed, errors) = jrsonnet_rowan_parser::parse(input);791	if !errors.is_empty() {792		let mut builder = hi_doc::SnippetBuilder::new(input);793		for error in errors {794			builder795				.error(hi_doc::Text::fragment(796					format!("{:?}", error.error),797					Formatting::default(),798				))799				.range(800					error.range.start().into()801						..=(usize::from(error.range.end()) - 1).max(error.range.start().into()),802				)803				.build();804		}805		// let snippet = builder.build();806		return Err(builder);807		// It is possible to recover from this failure, but the output may be broken, as formatter is free to skip808		// ERROR rowan nodes.809		// Recovery needs to be enabled for LSP, though.810	}811	Ok(dprint_core::formatting::format(812		|| {813			let mut out = PrintItems::new();814			parsed.print(&mut out);815			out816		},817		PrintOptions {818			indent_width: if opts.indent == 0 {819				// Reasonable max length for both 2 and 4 space sized tabs.820				3821			} else {822				opts.indent823			},824			max_width: 100,825			use_tabs: opts.indent == 0,826			new_line_text: "\n",827		},828	))829}
deletedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__args.snapdiffbeforeafterboth
--- a/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__args.snap
+++ /dev/null
@@ -1,36 +0,0 @@
----
-source: crates/jrsonnet-formatter/src/tests.rs
-expression: "reformat(indoc!(\"\n\t\t\t{\n\t\t\t\tshort: aaa(1,2,3,4,5),\n\t\t\t\tlong: bbb(123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123),\n\t\t\t\tshort_in_long: bbb(aaa(1,2,3,4,5), 123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123),\n\t\t\t\tlong_in_short: aaa(1,2,3,4,5,bbb(123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123)),\n\t\t\t}\n\t\t\"))"
----
-{
-	short: aaa(1, 2, 3, 4, 5),
-	long: bbb(
-		123123123123123123123,
-		12312312321123123123,
-		123123123123312123123,
-		123123123123123123312,
-		123123123123312321123,
-	),
-	short_in_long: bbb(
-		aaa(1, 2, 3, 4, 5),
-		123123123123123123123,
-		12312312321123123123,
-		123123123123312123123,
-		123123123123123123312,
-		123123123123312321123,
-	),
-	long_in_short: aaa(
-		1,
-		2,
-		3,
-		4,
-		5,
-		bbb(
-			123123123123123123123,
-			12312312321123123123,
-			123123123123312123123,
-			123123123123123123312,
-			123123123123312321123,
-		),
-	),
-}
deletedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__asserts.snapdiffbeforeafterboth
--- a/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__asserts.snap
+++ /dev/null
@@ -1,9 +0,0 @@
----
-source: crates/jrsonnet-formatter/src/tests.rs
-expression: "reformat(indoc!(\"\n\t\t\t{\n\t\t\t\tassert 1 > 0 : 'one should be greater than zero',\n\t\t\t\tassert true,\n\t\t\t\tvalue: 42,\n\t\t\t}\n\t\t\"))"
----
-{
-	assert 1 > 0: 'one should be greater than zero',
-	assert true,
-	value: 42,
-}
deletedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__complex_comments.snapdiffbeforeafterboth
--- a/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__complex_comments.snap
+++ /dev/null
@@ -1,62 +0,0 @@
----
-source: crates/jrsonnet-formatter/src/tests.rs
-expression: "reformat(indoc!(\"{\n\t\t  comments: {\n\t\t\t_: '',\n\t\t\t//     Plain comment\n\t\t\ta: '',\n\n\t\t\t#    Plain comment with empty line before\n\t\t\tb: '',\n\t\t\t/*Single-line multiline comment\n\n\t\t\t*/\n\t\t\tc: '',\n\n\t\t\t/**Single-line multiline doc comment\n\n\t\t\t*/\n\t\t\tc: '',\n\n\t\t\t/**Multiline doc\n\t\t\tComment\n\t\t\t*/\n\t\t\tc: '',\n\n\t\t\t/*\n\n\tMulti-line\n\n\tcomment\n\t\t\t*/\n\t\t\td: '',\n\n\t\t\te: '', // Inline comment\n\n\t\t\tk: '',\n\n\t\t\t// Text after everything\n\t\t  },\n\t\t  comments2: {\n\t\t\tk: '',\n\t\t\t// Text after everything, but no newline above\n\t\t  },\n          spacing: {\n            a: '',\n\n            b: '',\n          },\n          noSpacing: {\n            a: '',\n            b: '',\n          },\n\n\t\t\t smallObjectWithEnding: {/*Ending comment*/},\n\t\t\t smallObjectWithFieldAndEnding: {a: 11/*Ending comment*/},\n\t\t\t smallObjectWithFieldAndEnding2: {/*Start*/a: 11/*Ending comment*/},\n        }\"))"
----
-{
-	comments: {
-		_: '',
-		// Plain comment
-		a: '',
-
-		# Plain comment with empty line before
-		b: '',
-		/* Single-line multiline comment */
-		c: '',
-
-		/**
-		 * Single-line multiline doc comment
-		 */
-		c: '',
-
-		/**
-		 * Multiline doc
-		 * Comment
-		 */
-		c: '',
-
-		/*
-		Multi-line
-
-		comment
-		*/
-		d: '',
-
-		e: '', // Inline comment
-
-		k: '',
-
-		// Text after everything
-	},
-	comments2: {
-		k: '',
-		// Text after everything, but no newline above
-	},
-	spacing: {
-		a: '',
-
-		b: '',
-	},
-	noSpacing: {
-		a: '',
-		b: '',
-	},
-
-	smallObjectWithEnding: {
-		/* Ending comment */
-	},
-	smallObjectWithFieldAndEnding: { a: 11 /* Ending comment */ },
-	smallObjectWithFieldAndEnding2: {
-		/* Start */
-		a: 11, /* Ending comment */
-	},
-}
deletedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__complex_nested.snapdiffbeforeafterboth
--- a/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__complex_nested.snap
+++ /dev/null
@@ -1,41 +0,0 @@
----
-source: crates/jrsonnet-formatter/src/tests.rs
-expression: "reformat(indoc!(\"\n\t\t\t{\n\t\t\t\tkubernetes: {\n\t\t\t\t deployment: {\n\t\t\t\t\tapiVersion: 'apps/v1',\n\t\t\t\t\tkind: 'Deployment',\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t  name: 'myapp',\n\t\t\t\t\t  labels: { app: 'myapp', version: 'v1' },\n\t\t\t\t\t},\n\t\t\t\t\tspec: {\n\t\t\t\t\t  replicas: 3,\n\t\t\t\t\t  selector: { matchLabels: { app: 'myapp' } },\n\t\t\t\t\t  template: {\n\t\t\t\t\t\t metadata: { labels: { app: 'myapp' } },\n\t\t\t\t\t\t spec: {\n\t\t\t\t\t\t\tcontainers: [\n\t\t\t\t\t\t\t  {\n\t\t\t\t\t\t\t\t name: 'myapp',\n\t\t\t\t\t\t\t\t image: 'myapp:latest',\n\t\t\t\t\t\t\t\t ports: [{ containerPort: 8080 }],\n\t\t\t\t\t\t\t\t env: [\n\t\t\t\t\t\t\t\t\t{ name: 'FOO', value: 'bar' },\n\t\t\t\t\t\t\t\t\t{ name: 'BAZ', valueFrom: { secretKeyRef: { name: 'mysecret', key: 'password' } } },\n\t\t\t\t\t\t\t\t ],\n\t\t\t\t\t\t\t  },\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t },\n\t\t\t\t\t  },\n\t\t\t\t\t},\n\t\t\t\t },\n\t\t\t  },\n\t\t\t}\n\t\t\"))"
----
-{
-	kubernetes: {
-		deployment: {
-			apiVersion: 'apps/v1',
-			kind: 'Deployment',
-			metadata: {
-				name: 'myapp',
-				labels: { app: 'myapp', version: 'v1' },
-			},
-			spec: {
-				replicas: 3,
-				selector: { matchLabels: { app: 'myapp' } },
-				template: {
-					metadata: { labels: { app: 'myapp' } },
-					spec: {
-						containers: [
-							{
-								name: 'myapp',
-								image: 'myapp:latest',
-								ports: [
-									{ containerPort: 8080 },
-								],
-								env: [
-									{ name: 'FOO', value: 'bar' },
-									{
-										name: 'BAZ',
-										valueFrom: { secretKeyRef: { name: 'mysecret', key: 'password' } },
-									},
-								],
-							},
-						],
-					},
-				},
-			},
-		},
-	},
-}
deletedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__self_super.snapdiffbeforeafterboth
--- a/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__self_super.snap
+++ /dev/null
@@ -1,13 +0,0 @@
----
-source: crates/jrsonnet-formatter/src/tests.rs
-expression: "reformat(indoc!(\"\n\t\t\tlocal base = {\n\t\t\t  foo: 'bar',\n\t\t\t  method():: self.foo,\n\t\t\t};\n\n\t\t\tbase {\n\t\t\t  foo: super.foo + '-extended',\n\t\t\t  result: self.method(),\n\t\t\t}\n\t\t\"))"
----
-local base = {
-	foo: 'bar',
-	method(
-	):: self.foo,
-};
-base {
-	foo: super.foo + '-extended',
-	result: self.method(),
-}
addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@args.jsonnet.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@args.jsonnet.snap
@@ -0,0 +1,37 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: reformat(&input)
+input_file: crates/jrsonnet-formatter/src/tests/args.jsonnet
+---
+{
+   short: aaa(1, 2, 3, 4, 5),
+   long: bbb(
+      123123123123123123123,
+      12312312321123123123,
+      123123123123312123123,
+      123123123123123123312,
+      123123123123312321123,
+   ),
+   short_in_long: bbb(
+      aaa(1, 2, 3, 4, 5),
+      123123123123123123123,
+      12312312321123123123,
+      123123123123312123123,
+      123123123123123123312,
+      123123123123312321123,
+   ),
+   long_in_short: aaa(
+      1,
+      2,
+      3,
+      4,
+      5,
+      bbb(
+         123123123123123123123,
+         12312312321123123123,
+         123123123123312123123,
+         123123123123123123312,
+         123123123123312321123,
+      ),
+   ),
+}
addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@asserts.jsonnet.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@asserts.jsonnet.snap
@@ -0,0 +1,10 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: reformat(&input)
+input_file: crates/jrsonnet-formatter/src/tests/asserts.jsonnet
+---
+{
+   assert 1 > 0: 'one should be greater than zero',
+   assert true,
+   value: 42,
+}
addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@basic_array.jsonnet.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@basic_array.jsonnet.snap
@@ -0,0 +1,12 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: reformat(&input)
+input_file: crates/jrsonnet-formatter/src/tests/basic_array.jsonnet
+---
+[
+   1,
+   2,
+   3,
+   4,
+   5,
+]
addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@basic_object.jsonnet.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@basic_object.jsonnet.snap
@@ -0,0 +1,6 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: reformat(&input)
+input_file: crates/jrsonnet-formatter/src/tests/basic_object.jsonnet
+---
+{ foo: 'bar', baz: 'qux', nested: { a: 1, b: 2 } }
addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@comments.jsonnet.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@comments.jsonnet.snap
@@ -0,0 +1,16 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: reformat(&input)
+input_file: crates/jrsonnet-formatter/src/tests/comments.jsonnet
+---
+// File header comment
+{
+   // Comment above field
+   foo: 'bar',
+   /* Block comment */
+   baz: 'qux',
+   nested: {
+      // Hash comment
+      value: 42,
+   },
+}
addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@complex_comments.jsonnet.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@complex_comments.jsonnet.snap
@@ -0,0 +1,63 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: reformat(&input)
+input_file: crates/jrsonnet-formatter/src/tests/complex_comments.jsonnet
+---
+{
+   comments: {
+      _: '',
+      // Plain comment
+      a: '',
+
+      # Plain comment with empty line before
+      b: '',
+      /* Single-line multiline comment */
+      c: '',
+
+      /**
+       * Single-line multiline doc comment
+       */
+      c: '',
+
+      /**
+       * Multiline doc
+       * Comment
+       */
+      c: '',
+
+      /*
+      Multi-line
+
+      comment
+      */
+      d: '',
+
+      e: '', // Inline comment
+
+      k: '',
+
+      // Text after everything
+   },
+   comments2: {
+      k: '',
+      // Text after everything, but no newline above
+   },
+   spacing: {
+      a: '',
+
+      b: '',
+   },
+   noSpacing: {
+      a: '',
+      b: '',
+   },
+
+   smallObjectWithEnding: {
+      /* Ending comment */
+   },
+   smallObjectWithFieldAndEnding: { a: 11 /* Ending comment */ },
+   smallObjectWithFieldAndEnding2: {
+      /* Start */
+      a: 11, /* Ending comment */
+   },
+}
addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@complex_nested.jsonnet.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@complex_nested.jsonnet.snap
@@ -0,0 +1,42 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: reformat(&input)
+input_file: crates/jrsonnet-formatter/src/tests/complex_nested.jsonnet
+---
+{
+   kubernetes: {
+      deployment: {
+         apiVersion: 'apps/v1',
+         kind: 'Deployment',
+         metadata: {
+            name: 'myapp',
+            labels: { app: 'myapp', version: 'v1' },
+         },
+         spec: {
+            replicas: 3,
+            selector: { matchLabels: { app: 'myapp' } },
+            template: {
+               metadata: { labels: { app: 'myapp' } },
+               spec: {
+                  containers: [
+                     {
+                        name: 'myapp',
+                        image: 'myapp:latest',
+                        ports: [
+                           { containerPort: 8080 },
+                        ],
+                        env: [
+                           { name: 'FOO', value: 'bar' },
+                           {
+                              name: 'BAZ',
+                              valueFrom: { secretKeyRef: { name: 'mysecret', key: 'password' } },
+                           },
+                        ],
+                     },
+                  ],
+               },
+            },
+         },
+      },
+   },
+}
addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@comprehensions.jsonnet.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@comprehensions.jsonnet.snap
@@ -0,0 +1,29 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: reformat(&input)
+input_file: crates/jrsonnet-formatter/src/tests/comprehensions.jsonnet
+---
+{
+   arr: [x for x in [
+      1,
+      2,
+      3,
+   ]],
+   filtered: [x for x in [
+      1,
+      2,
+      3,
+      4,
+      5,
+   ] if x > 2],
+   obj: {
+      [k]: v,
+      for k in [
+         'a',
+         'b',
+      ]for v in [
+         1,
+         2,
+      ]
+   },
+}
addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@conditionals.jsonnet.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@conditionals.jsonnet.snap
@@ -0,0 +1,9 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: reformat(&input)
+input_file: crates/jrsonnet-formatter/src/tests/conditionals.jsonnet
+---
+{
+   simple: if true then 'yes' else 'no',
+   nested: if 1 > 0 then if 2 > 1 then 'a' else 'b' else 'c',
+}
addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@functions.jsonnet.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@functions.jsonnet.snap
@@ -0,0 +1,20 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: reformat(&input)
+input_file: crates/jrsonnet-formatter/src/tests/functions.jsonnet
+---
+{
+   simple(
+      x,
+   ):: x * 2,
+   with_default(
+      x,
+      y = 10,
+   ):: x + y,
+   multiline(
+      a,
+      b,
+      c,
+   ):: a + b + c,
+   called: self.simple(5),
+}
addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@imports.jsonnet.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@imports.jsonnet.snap
@@ -0,0 +1,11 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: reformat(&input)
+input_file: crates/jrsonnet-formatter/src/tests/imports.jsonnet
+---
+local a = import 'a.libsonnet';
+local m = import 'm.libsonnet';
+local z = import 'z.libsonnet';
+{
+   result: a + m + z,
+}
addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@local_vars.jsonnet.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@local_vars.jsonnet.snap
@@ -0,0 +1,12 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: reformat(&input)
+input_file: crates/jrsonnet-formatter/src/tests/local_vars.jsonnet
+---
+local x = 10;
+local y = 20;
+local sum = x + y;
+{
+   local inner = 5,
+   result: sum + inner,
+}
addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@operators.jsonnet.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@operators.jsonnet.snap
@@ -0,0 +1,18 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: reformat(&input)
+input_file: crates/jrsonnet-formatter/src/tests/operators.jsonnet
+---
+{
+   arithmetic: 1 + 2 * 3 - 4 / 2,
+   comparison: 1 < 2 && 3 > 2 || false,
+   string_concat: 'hello' + ' ' + 'world',
+   object_concat: { a: 1 } + { b: 2 },
+   array_concat: [
+      1,
+      2,
+   ] + [
+      3,
+      4,
+   ],
+}
addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@self_super.jsonnet.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@self_super.jsonnet.snap
@@ -0,0 +1,14 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: reformat(&input)
+input_file: crates/jrsonnet-formatter/src/tests/self_super.jsonnet
+---
+local base = {
+   foo: 'bar',
+   method(
+   ):: self.foo,
+};
+base {
+   foo: super.foo + '-extended',
+   result: self.method(),
+}
addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@std_functions.jsonnet.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@std_functions.jsonnet.snap
@@ -0,0 +1,22 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: reformat(&input)
+input_file: crates/jrsonnet-formatter/src/tests/std_functions.jsonnet
+---
+{
+   length: std.length(
+      [
+         1,
+         2,
+         3,
+      ],
+   ),
+   type: std.type('hello'),
+   format: std.format(
+      'Hello, %s!',
+      [
+         'world',
+      ],
+   ),
+   manifest: std.manifestJsonEx({ foo: 'bar' }, '  '),
+}
addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@string_styles.jsonnet.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@string_styles.jsonnet.snap
@@ -0,0 +1,14 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: reformat(&input)
+input_file: crates/jrsonnet-formatter/src/tests/string_styles.jsonnet
+---
+{
+   double_quote: 'hello world',
+   single_quote: 'hello world',
+   escaped: 'line1\nline2',
+   multiline: |||
+       This is a
+       multiline string
+     |||,
+}
modifiedcrates/jrsonnet-formatter/src/tests.rsdiffbeforeafterboth
--- a/crates/jrsonnet-formatter/src/tests.rs
+++ b/crates/jrsonnet-formatter/src/tests.rs
@@ -1,5 +1,10 @@
+#![cfg(test)]
+
+use std::fs;
+
 use dprint_core::formatting::{PrintItems, PrintOptions};
 use indoc::indoc;
+use insta::{assert_snapshot, glob};
 
 use crate::Printable;
 
@@ -13,155 +18,18 @@
 			out
 		},
 		PrintOptions {
-			indent_width: 2,
+			indent_width: 3,
 			max_width: 100,
-			use_tabs: true,
+			use_tabs: false,
 			new_line_text: "\n",
 		},
 	)
 }
 
 #[test]
-fn complex_comments() {
-	insta::assert_snapshot!(reformat(indoc!(
-		"{
-		  comments: {
-			_: '',
-			//     Plain comment
-			a: '',
-
-			#    Plain comment with empty line before
-			b: '',
-			/*Single-line multiline comment
-
-			*/
-			c: '',
-
-			/**Single-line multiline doc comment
-
-			*/
-			c: '',
-
-			/**Multiline doc
-			Comment
-			*/
-			c: '',
-
-			/*
-
-	Multi-line
-
-	comment
-			*/
-			d: '',
-
-			e: '', // Inline comment
-
-			k: '',
-
-			// Text after everything
-		  },
-		  comments2: {
-			k: '',
-			// Text after everything, but no newline above
-		  },
-          spacing: {
-            a: '',
-
-            b: '',
-          },
-          noSpacing: {
-            a: '',
-            b: '',
-          },
-
-			 smallObjectWithEnding: {/*Ending comment*/},
-			 smallObjectWithFieldAndEnding: {a: 11/*Ending comment*/},
-			 smallObjectWithFieldAndEnding2: {/*Start*/a: 11/*Ending comment*/},
-        }"
-	)));
-}
-
-#[test]
-fn args() {
-	insta::assert_snapshot!(reformat(indoc!(
-		"
-			{
-				short: aaa(1,2,3,4,5),
-				long: bbb(123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123),
-				short_in_long: bbb(aaa(1,2,3,4,5), 123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123),
-				long_in_short: aaa(1,2,3,4,5,bbb(123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123)),
-			}
-		"
-	)));
-}
-
-#[test]
-fn asserts() {
-	insta::assert_snapshot!(reformat(indoc!(
-		"
-			{
-				assert 1 > 0 : 'one should be greater than zero',
-				assert true,
-				value: 42,
-			}
-		"
-	)));
-}
-
-#[test]
-fn complex_nested() {
-	insta::assert_snapshot!(reformat(indoc!(
-		"
-			{
-				kubernetes: {
-				 deployment: {
-					apiVersion: 'apps/v1',
-					kind: 'Deployment',
-					metadata: {
-					  name: 'myapp',
-					  labels: { app: 'myapp', version: 'v1' },
-					},
-					spec: {
-					  replicas: 3,
-					  selector: { matchLabels: { app: 'myapp' } },
-					  template: {
-						 metadata: { labels: { app: 'myapp' } },
-						 spec: {
-							containers: [
-							  {
-								 name: 'myapp',
-								 image: 'myapp:latest',
-								 ports: [{ containerPort: 8080 }],
-								 env: [
-									{ name: 'FOO', value: 'bar' },
-									{ name: 'BAZ', valueFrom: { secretKeyRef: { name: 'mysecret', key: 'password' } } },
-								 ],
-							  },
-							],
-						 },
-					  },
-					},
-				 },
-			  },
-			}
-		"
-	)));
-}
-
-#[test]
-fn self_super() {
-	insta::assert_snapshot!(reformat(indoc!(
-		"
-			local base = {
-			  foo: 'bar',
-			  method():: self.foo,
-			};
-
-			base {
-			  foo: super.foo + '-extended',
-			  result: self.method(),
-			}
-		"
-	)));
+fn snapshots() {
+	glob!("tests/*.jsonnet", |path| {
+		let input = fs::read_to_string(path).expect("read test file");
+		assert_snapshot!(reformat(&input));
+	});
 }
addedcrates/jrsonnet-formatter/src/tests/args.jsonnetdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/tests/args.jsonnet
@@ -0,0 +1,7 @@
+
+			{
+				short: aaa(1,2,3,4,5),
+				long: bbb(123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123),
+				short_in_long: bbb(aaa(1,2,3,4,5), 123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123),
+				long_in_short: aaa(1,2,3,4,5,bbb(123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123)),
+			}
addedcrates/jrsonnet-formatter/src/tests/asserts.jsonnetdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/tests/asserts.jsonnet
@@ -0,0 +1,6 @@
+
+			{
+				assert 1 > 0 : 'one should be greater than zero',
+				assert true,
+				value: 42,
+			}
addedcrates/jrsonnet-formatter/src/tests/basic_array.jsonnetdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/tests/basic_array.jsonnet
@@ -0,0 +1 @@
+[1, 2, 3, 4, 5]
addedcrates/jrsonnet-formatter/src/tests/basic_object.jsonnetdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/tests/basic_object.jsonnet
@@ -0,0 +1 @@
+{ foo: 'bar', baz: 'qux', nested: { a: 1, b: 2 } }
addedcrates/jrsonnet-formatter/src/tests/comments.jsonnetdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/tests/comments.jsonnet
@@ -0,0 +1,11 @@
+// File header comment
+{
+  // Comment above field
+  foo: 'bar',
+  /* Block comment */
+  baz: 'qux',
+  nested: {
+    // Hash comment
+    value: 42,
+  },
+}
addedcrates/jrsonnet-formatter/src/tests/complex_comments.jsonnetdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/tests/complex_comments.jsonnet
@@ -0,0 +1,55 @@
+{
+		  comments: {
+			_: '',
+			//     Plain comment
+			a: '',
+
+			#    Plain comment with empty line before
+			b: '',
+			/*Single-line multiline comment
+
+			*/
+			c: '',
+
+			/**Single-line multiline doc comment
+
+			*/
+			c: '',
+
+			/**Multiline doc
+			Comment
+			*/
+			c: '',
+
+			/*
+
+	Multi-line
+
+	comment
+			*/
+			d: '',
+
+			e: '', // Inline comment
+
+			k: '',
+
+			// Text after everything
+		  },
+		  comments2: {
+			k: '',
+			// Text after everything, but no newline above
+		  },
+          spacing: {
+            a: '',
+
+            b: '',
+          },
+          noSpacing: {
+            a: '',
+            b: '',
+          },
+
+			 smallObjectWithEnding: {/*Ending comment*/},
+			 smallObjectWithFieldAndEnding: {a: 11/*Ending comment*/},
+			 smallObjectWithFieldAndEnding2: {/*Start*/a: 11/*Ending comment*/},
+        }
addedcrates/jrsonnet-formatter/src/tests/complex_nested.jsonnetdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/tests/complex_nested.jsonnet
@@ -0,0 +1,33 @@
+
+			{
+				kubernetes: {
+				 deployment: {
+					apiVersion: 'apps/v1',
+					kind: 'Deployment',
+					metadata: {
+					  name: 'myapp',
+					  labels: { app: 'myapp', version: 'v1' },
+					},
+					spec: {
+					  replicas: 3,
+					  selector: { matchLabels: { app: 'myapp' } },
+					  template: {
+						 metadata: { labels: { app: 'myapp' } },
+						 spec: {
+							containers: [
+							  {
+								 name: 'myapp',
+								 image: 'myapp:latest',
+								 ports: [{ containerPort: 8080 }],
+								 env: [
+									{ name: 'FOO', value: 'bar' },
+									{ name: 'BAZ', valueFrom: { secretKeyRef: { name: 'mysecret', key: 'password' } } },
+								 ],
+							  },
+							],
+						 },
+					  },
+					},
+				 },
+			  },
+			}
addedcrates/jrsonnet-formatter/src/tests/comprehensions.jsonnetdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/tests/comprehensions.jsonnet
@@ -0,0 +1,5 @@
+{
+  arr: [x for x in [1, 2, 3]],
+  filtered: [x for x in [1, 2, 3, 4, 5] if x > 2],
+  obj: { [k]: v for k in ['a', 'b'] for v in [1, 2] },
+}
addedcrates/jrsonnet-formatter/src/tests/conditionals.jsonnetdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/tests/conditionals.jsonnet
@@ -0,0 +1,7 @@
+{
+  simple: if true then 'yes' else 'no',
+  nested: if 1 > 0 then
+    if 2 > 1 then 'a' else 'b'
+  else
+    'c',
+}
addedcrates/jrsonnet-formatter/src/tests/functions.jsonnetdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/tests/functions.jsonnet
@@ -0,0 +1,10 @@
+{
+  simple(x):: x * 2,
+  with_default(x, y=10):: x + y,
+  multiline(
+    a,
+    b,
+    c,
+  ):: a + b + c,
+  called: self.simple(5),
+}
addedcrates/jrsonnet-formatter/src/tests/imports.jsonnetdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/tests/imports.jsonnet
@@ -0,0 +1,7 @@
+local a = import 'a.libsonnet';
+local m = import 'm.libsonnet';
+local z = import 'z.libsonnet';
+
+{
+  result: a + m + z,
+}
addedcrates/jrsonnet-formatter/src/tests/local_vars.jsonnetdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/tests/local_vars.jsonnet
@@ -0,0 +1,8 @@
+local x = 10;
+local y = 20;
+local sum = x + y;
+
+{
+  local inner = 5,
+  result: sum + inner,
+}
addedcrates/jrsonnet-formatter/src/tests/operators.jsonnetdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/tests/operators.jsonnet
@@ -0,0 +1,7 @@
+{
+  arithmetic: 1 + 2 * 3 - 4 / 2,
+  comparison: 1 < 2 && 3 > 2 || false,
+  string_concat: 'hello' + ' ' + 'world',
+  object_concat: { a: 1 } + { b: 2 },
+  array_concat: [1, 2] + [3, 4],
+}
addedcrates/jrsonnet-formatter/src/tests/self_super.jsonnetdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/tests/self_super.jsonnet
@@ -0,0 +1,9 @@
+local base = {
+  foo: 'bar',
+  method():: self.foo,
+};
+
+base {
+  foo: super.foo + '-extended',
+  result: self.method(),
+}
addedcrates/jrsonnet-formatter/src/tests/std_functions.jsonnetdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/tests/std_functions.jsonnet
@@ -0,0 +1,6 @@
+{
+  length: std.length([1, 2, 3]),
+  type: std.type('hello'),
+  format: std.format('Hello, %s!', ['world']),
+  manifest: std.manifestJsonEx({ foo: 'bar' }, '  '),
+}
addedcrates/jrsonnet-formatter/src/tests/string_styles.jsonnetdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/tests/string_styles.jsonnet
@@ -0,0 +1,9 @@
+{
+  double_quote: 'hello world',
+  single_quote: 'hello world',
+  escaped: 'line1\nline2',
+  multiline: |||
+    This is a
+    multiline string
+  |||,
+}
modifiedcrates/jrsonnet-rowan-parser/src/snapshots/jrsonnet_rowan_parser__tests__plain_call.snapdiffbeforeafterboth
--- a/crates/jrsonnet-rowan-parser/src/snapshots/jrsonnet_rowan_parser__tests__plain_call.snap
+++ b/crates/jrsonnet-rowan-parser/src/snapshots/jrsonnet_rowan_parser__tests__plain_call.snap
@@ -5,49 +5,49 @@
 SOURCE_FILE@0..37
   EXPR@0..36
     EXPR_BINARY@0..36
-      EXPR@0..3
+      EXPR@0..31
         EXPR_VAR@0..3
           NAME@0..3
             IDENT@0..3 "std"
-      SUFFIX_INDEX@3..10
-        DOT@3..4 "."
-        NAME@4..10
-          IDENT@4..10 "substr"
-      SUFFIX_APPLY@10..31
-        ARGS_DESC@10..31
-          L_PAREN@10..11 "("
-          ARG@11..12
-            EXPR@11..12
-              EXPR_VAR@11..12
-                NAME@11..12
-                  IDENT@11..12 "a"
-          COMMA@12..13 ","
-          WHITESPACE@13..14 " "
-          ARG@14..15
-            EXPR@14..15
-              EXPR_NUMBER@14..15
-                FLOAT@14..15 "0"
-          COMMA@15..16 ","
-          WHITESPACE@16..17 " "
-          ARG@17..30
-            EXPR@17..30
-              EXPR_VAR@17..20
-                NAME@17..20
-                  IDENT@17..20 "std"
-              SUFFIX_INDEX@20..27
-                DOT@20..21 "."
-                NAME@21..27
-                  IDENT@21..27 "length"
-              SUFFIX_APPLY@27..30
-                ARGS_DESC@27..30
-                  L_PAREN@27..28 "("
-                  ARG@28..29
-                    EXPR@28..29
-                      EXPR_VAR@28..29
-                        NAME@28..29
-                          IDENT@28..29 "b"
-                  R_PAREN@29..30 ")"
-          R_PAREN@30..31 ")"
+        SUFFIX_INDEX@3..10
+          DOT@3..4 "."
+          NAME@4..10
+            IDENT@4..10 "substr"
+        SUFFIX_APPLY@10..31
+          ARGS_DESC@10..31
+            L_PAREN@10..11 "("
+            ARG@11..12
+              EXPR@11..12
+                EXPR_VAR@11..12
+                  NAME@11..12
+                    IDENT@11..12 "a"
+            COMMA@12..13 ","
+            WHITESPACE@13..14 " "
+            ARG@14..15
+              EXPR@14..15
+                EXPR_NUMBER@14..15
+                  FLOAT@14..15 "0"
+            COMMA@15..16 ","
+            WHITESPACE@16..17 " "
+            ARG@17..30
+              EXPR@17..30
+                EXPR_VAR@17..20
+                  NAME@17..20
+                    IDENT@17..20 "std"
+                SUFFIX_INDEX@20..27
+                  DOT@20..21 "."
+                  NAME@21..27
+                    IDENT@21..27 "length"
+                SUFFIX_APPLY@27..30
+                  ARGS_DESC@27..30
+                    L_PAREN@27..28 "("
+                    ARG@28..29
+                      EXPR@28..29
+                        EXPR_VAR@28..29
+                          NAME@28..29
+                            IDENT@28..29 "b"
+                    R_PAREN@29..30 ")"
+            R_PAREN@30..31 ")"
       WHITESPACE@31..32 " "
       EQ@32..34 "=="
       WHITESPACE@34..35 " "
addedcrates/jrsonnet-rowan-parser/src/snapshots/jrsonnet_rowan_parser__tests__string_block_trim.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-rowan-parser/src/snapshots/jrsonnet_rowan_parser__tests__string_block_trim.snap
@@ -0,0 +1,9 @@
+---
+source: crates/jrsonnet-rowan-parser/src/tests.rs
+expression: "|||-\n\tTrimmed text block\n|||\n"
+---
+SOURCE_FILE@0..29
+  EXPR@0..28
+    EXPR_STRING@0..28
+      STRING_BLOCK@0..28 "|||-\n\tTrimmed text bl ..."
+  WHITESPACE@28..29 "\n"
modifiedcrates/jrsonnet-rowan-parser/src/string_block.rsdiffbeforeafterboth
--- a/crates/jrsonnet-rowan-parser/src/string_block.rs
+++ b/crates/jrsonnet-rowan-parser/src/string_block.rs
@@ -52,6 +52,14 @@
 			self.rest().chars().next()
 		}
 
+		fn eat_if(&mut self, f: impl Fn(char) -> bool) -> usize {
+			if self.peek().map(f).unwrap_or(false) {
+				self.index += 1;
+				return 1;
+			}
+			0
+		}
+
 		fn eat_while(&mut self, f: impl Fn(char) -> bool) -> usize {
 			if self.index == self.source.len() {
 				return 0;
@@ -133,6 +141,8 @@
 		offset: lex.span().end,
 	};
 
+	ctx.eat_if(|v| v == '-');
+
 	// Skip whitespaces
 	ctx.eat_while(|r| r == ' ' || r == '\t' || r == '\r');
 
modifiedcrates/jrsonnet-rowan-parser/src/tests.rsdiffbeforeafterboth
--- a/crates/jrsonnet-rowan-parser/src/tests.rs
+++ b/crates/jrsonnet-rowan-parser/src/tests.rs
@@ -55,7 +55,6 @@
 				let src = indoc::indoc!($test);
 				let result = process(&src);
 				insta::assert_snapshot!(stringify!($name), result, src);
-
 			}
 		)+};
 	}
@@ -204,6 +203,12 @@
 	super_nesting => r#"
 		super.a + super.b
 	"#
+
+	string_block_trim => r#"
+		|||-
+			Trimmed text block
+		|||
+	"#
 );
 
 #[test]