difftreelog
feat more formatter options
in: master
3 files changed
bindings/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,
}
}
}
cmds/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,
crates/jrsonnet-formatter/src/lib.rsdiffbeforeafterboth901 }901 }902}902}903903904#[derive(Default)]905pub struct FormatOptions {904pub struct FormatOptions {906 // 0 for hard tabs, otherwise number of spaces907 pub indent: u8,905 pub indent: u8,906 pub use_tabs: bool,907 pub max_width: u32,908}908}909910impl FormatOptions {911 pub fn new() -> Self {912 Self {913 indent: 4,914 use_tabs: true,915 max_width: 100,916 }917 }918}919909impl FormatOptions {920impl Default for FormatOptions {910 pub fn new() -> Self {921 fn default() -> Self {911 Self::default()922 Self::new()912 }923 }913}924}914925947 out958 out948 },959 },949 PrintOptions {960 PrintOptions {950 indent_width: if opts.indent == 0 {961 indent_width: opts.indent,951 // Reasonable max length for both 2 and 4 space sized tabs.952 3953 } else {954 opts.indent955 },956 max_width: 100,962 max_width: opts.max_width,957 use_tabs: opts.indent == 0,963 use_tabs: opts.use_tabs,958 new_line_text: "\n",964 new_line_text: "\n",959 },965 },960 ))966 ))