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
--- a/bindings/jrsonnet-web/src/lib.rs
+++ b/bindings/jrsonnet-web/src/lib.rs
@@ -614,17 +614,25 @@
 #[wasm_bindgen(js_name = FormatOptions)]
 pub struct WasmFormatOptions {
 	indent: u8,
+	use_tabs: bool,
+	max_width: u32,
 }
 #[wasm_bindgen(js_class = FormatOptions)]
 impl WasmFormatOptions {
 	#[wasm_bindgen(constructor)]
 	pub fn new() -> Self {
-		Self { indent: 0 }
+		Self {
+			indent: 4,
+			use_tabs: true,
+			max_width: 100,
+		}
 	}
 
 	fn build(&self) -> FormatOptions {
 		FormatOptions {
 			indent: self.indent,
+			use_tabs: self.use_tabs,
+			max_width: self.max_width,
 		}
 	}
 }
modifiedcmds/jrsonnet-fmt/src/main.rsdiffbeforeafterboth
25 #[arg(long)]25 #[arg(long)]
26 test: bool,26 test: bool,
27 /// Number of spaces to indent with27 /// Number of spaces to indent with
28 #[arg(long, default_value = "2")]28 #[arg(long, default_value = "4")]
29 indent: u8,29 indent: u8,
30 /// Force hard tab for indentation30 /// Force hard tab for indentation
31 #[arg(long)]31 #[arg(long, default_value = "true")]
32 hard_tabs: bool,32 use_tabs: bool,
33 /// Max formatted source width
34 #[arg(long, default_value = "100")]
35 max_width: u32,
3336
34 /// Debug option: how many times to call reformatting in case of unstable dprint output resolution.37 /// Debug option: how many times to call reformatting in case of unstable dprint output resolution.
35 ///38 ///
51}54}
5255
53fn main_result() -> Result<(), Error> {56fn main_result() -> Result<(), Error> {
54 eprintln!(
55 "jrsonnet-fmt is a prototype of a jsonnet code formatter, do not expect it to produce meaningful results right now."
56 );
57 eprintln!(
58 "It is not expected for its output to match other implementations, it will be completly separate implementation with maybe different name."
59 );
60 let mut opts = Opts::parse();57 let opts = Opts::parse();
61 let input = if opts.exec {58 let input = if opts.exec {
62 if opts.in_place {59 if opts.in_place {
63 return Err(Error::InPlaceExec);60 return Err(Error::InPlaceExec);
67 fs::read_to_string(&opts.input)?64 fs::read_to_string(&opts.input)?
68 };65 };
69
70 if opts.indent == 0 {
71 // Sane default.
72 // TODO: Implement actual guessing.
73 opts.hard_tabs = true;
74 }
7566
76 let mut iteration = 0;67 let mut iteration = 0;
77 let mut formatted = input.clone();68 let mut formatted = input.clone();
81 let reformatted = match format(72 let reformatted = match format(
82 &formatted,73 &formatted,
83 &FormatOptions {74 &FormatOptions {
84 indent: if opts.indent == 0 || opts.hard_tabs {75 indent: opts.indent,
85 076 use_tabs: opts.use_tabs,
86 } else {
87 opts.indent77 max_width: opts.max_width,
88 },
89 },78 },
90 ) {79 ) {
91 Ok(v) => v,80 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",
 		},
 	))