git.delta.rocks / jrsonnet / refs/commits / 752087cb9057

difftreelog

feat more formatter options

uqnknwssYaroslav Bolyukin2026-05-06parent: #aa77bfb.patch.diff
in: master

3 files changed

modifiedbindings/jrsonnet-web/src/lib.rsdiffbeforeafterboth
614#[wasm_bindgen(js_name = FormatOptions)]614#[wasm_bindgen(js_name = FormatOptions)]
615pub struct WasmFormatOptions {615pub struct WasmFormatOptions {
616 indent: u8,616 indent: u8,
617 use_tabs: bool,
618 max_width: u32,
617}619}
618#[wasm_bindgen(js_class = FormatOptions)]620#[wasm_bindgen(js_class = FormatOptions)]
619impl WasmFormatOptions {621impl WasmFormatOptions {
620 #[wasm_bindgen(constructor)]622 #[wasm_bindgen(constructor)]
621 pub fn new() -> Self {623 pub fn new() -> Self {
622 Self { indent: 0 }624 Self {
625 indent: 4,
626 use_tabs: true,
627 max_width: 100,
628 }
623 }629 }
624630
625 fn build(&self) -> FormatOptions {631 fn build(&self) -> FormatOptions {
626 FormatOptions {632 FormatOptions {
627 indent: self.indent,633 indent: self.indent,
634 use_tabs: self.use_tabs,
635 max_width: self.max_width,
628 }636 }
629 }637 }
630}638}
modifiedcmds/jrsonnet-fmt/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet-fmt/src/main.rs
+++ b/cmds/jrsonnet-fmt/src/main.rs
@@ -25,11 +25,14 @@
 	#[arg(long)]
 	test: bool,
 	/// Number of spaces to indent with
-	#[arg(long, default_value = "2")]
+	#[arg(long, default_value = "4")]
 	indent: u8,
 	/// Force hard tab for indentation
-	#[arg(long)]
-	hard_tabs: bool,
+	#[arg(long, default_value = "true")]
+	use_tabs: bool,
+	/// Max formatted source width
+	#[arg(long, default_value = "100")]
+	max_width: u32,
 
 	/// Debug option: how many times to call reformatting in case of unstable dprint output resolution.
 	///
@@ -51,13 +54,7 @@
 }
 
 fn main_result() -> Result<(), Error> {
-	eprintln!(
-		"jrsonnet-fmt is a prototype of a jsonnet code formatter, do not expect it to produce meaningful results right now."
-	);
-	eprintln!(
-		"It is not expected for its output to match other implementations, it will be completly separate implementation with maybe different name."
-	);
-	let mut opts = Opts::parse();
+	let opts = Opts::parse();
 	let input = if opts.exec {
 		if opts.in_place {
 			return Err(Error::InPlaceExec);
@@ -66,12 +63,6 @@
 	} else {
 		fs::read_to_string(&opts.input)?
 	};
-
-	if opts.indent == 0 {
-		// Sane default.
-		// TODO: Implement actual guessing.
-		opts.hard_tabs = true;
-	}
 
 	let mut iteration = 0;
 	let mut formatted = input.clone();
@@ -81,11 +72,9 @@
 		let reformatted = match format(
 			&formatted,
 			&FormatOptions {
-				indent: if opts.indent == 0 || opts.hard_tabs {
-					0
-				} else {
-					opts.indent
-				},
+				indent: opts.indent,
+				use_tabs: opts.use_tabs,
+				max_width: opts.max_width,
 			},
 		) {
 			Ok(v) => v,
modifiedcrates/jrsonnet-formatter/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-formatter/src/lib.rs
+++ b/crates/jrsonnet-formatter/src/lib.rs
@@ -901,14 +901,25 @@
 	}
 }
 
-#[derive(Default)]
 pub struct FormatOptions {
-	// 0 for hard tabs, otherwise number of spaces
 	pub indent: u8,
+	pub use_tabs: bool,
+	pub max_width: u32,
 }
+
 impl FormatOptions {
 	pub fn new() -> Self {
-		Self::default()
+		Self {
+			indent: 4,
+			use_tabs: true,
+			max_width: 100,
+		}
+	}
+}
+
+impl Default for FormatOptions {
+	fn default() -> Self {
+		Self::new()
 	}
 }
 
@@ -947,14 +958,9 @@
 			out
 		},
 		PrintOptions {
-			indent_width: if opts.indent == 0 {
-				// Reasonable max length for both 2 and 4 space sized tabs.
-				3
-			} else {
-				opts.indent
-			},
-			max_width: 100,
-			use_tabs: opts.indent == 0,
+			indent_width: opts.indent,
+			max_width: opts.max_width,
+			use_tabs: opts.use_tabs,
 			new_line_text: "\n",
 		},
 	))