difftreelog
feat more formatter options
in: master
3 files changed
bindings/jrsonnet-web/src/lib.rsdiffbeforeafterboth614#[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 }624630625 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}cmds/jrsonnet-fmt/src/main.rsdiffbeforeafterboth25 #[arg(long)]25 #[arg(long)]26 test: bool,26 test: bool,27 /// Number of spaces to indent with27 /// Number of spaces to indent with28 #[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 indentation31 #[arg(long)]31 #[arg(long, default_value = "true")]32 hard_tabs: bool,32 use_tabs: bool,33 /// Max formatted source width34 #[arg(long, default_value = "100")]35 max_width: u32,333634 /// 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}525553fn 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 };6970 if opts.indent == 0 {71 // Sane default.72 // TODO: Implement actual guessing.73 opts.hard_tabs = true;74 }756676 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,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 ))