From 752087cb905736b8b26c417c53effabec6deb9c9 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Wed, 06 May 2026 06:04:39 +0000 Subject: [PATCH] feat: more formatter options --- --- 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, } } } --- 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, --- 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", }, )) -- gitstuff