Skip to content

Commit

Permalink
Make the cormatter cli use the config defaults.
Browse files Browse the repository at this point in the history
commit-id:8d2ad88e
  • Loading branch information
gilbens-starkware committed Dec 22, 2024
1 parent f973093 commit 070e6d9
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 61 deletions.
10 changes: 3 additions & 7 deletions corelib/src/circuit.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,7 @@ impl U96sIntoCircuitInputValue of IntoCircuitInputValue<[u96; 4]> {
fn into_circuit_input_value(self: [u96; 4]) -> [U96Guarantee; 4] {
let [val0, val1, val2, val3] = self;
[
into_u96_guarantee(val0),
into_u96_guarantee(val1),
into_u96_guarantee(val2),
into_u96_guarantee(val0), into_u96_guarantee(val1), into_u96_guarantee(val2),
into_u96_guarantee(val3),
]
}
Expand All @@ -299,10 +297,8 @@ impl U96sIntoCircuitInputValue of IntoCircuitInputValue<[u96; 4]> {
impl U384IntoCircuitInputValue of IntoCircuitInputValue<u384> {
fn into_circuit_input_value(self: u384) -> [U96Guarantee; 4] {
[
into_u96_guarantee(self.limb0),
into_u96_guarantee(self.limb1),
into_u96_guarantee(self.limb2),
into_u96_guarantee(self.limb3),
into_u96_guarantee(self.limb0), into_u96_guarantee(self.limb1),
into_u96_guarantee(self.limb2), into_u96_guarantee(self.limb3),
]
}
}
Expand Down
36 changes: 13 additions & 23 deletions crates/bin/cairo-format/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use std::path::Path;
use std::process::ExitCode;
use std::sync::atomic::{AtomicBool, Ordering};

use cairo_lang_formatter::{
CairoFormatter, CollectionsBreakingBehavior, FormatOutcome, FormatterConfig, StdinFmt,
};
use cairo_lang_formatter::{CairoFormatter, FormatOutcome, FormatterConfig, StdinFmt};
use cairo_lang_utils::logging::init_logging;
use clap::Parser;
use colored::Colorize;
Expand Down Expand Up @@ -39,24 +37,24 @@ struct FormatterArgs {
#[arg(short, long, default_value_t = false)]
print_parsing_errors: bool,
/// Enable sorting the module level items (imports, mod definitions...).
#[arg(short, long, default_value_t = false)]
sort_mod_level_items: bool,
#[arg(short, long)]
sort_mod_level_items: Option<bool>,
/// Controls tuple breaking behavior. Set to 'line-by-line' (default) to format each
/// tuple item on a new line, or 'single-break-point' to keep as many items as possible on the
/// same line (as space permits). Defaults to line-by-line.
#[arg(long, default_value_t = false)]
tuple_line_breaking: bool,
#[arg(long)]
tuple_line_breaking: Option<bool>,
/// Controls fixed array breaking behavior. Set to 'single-break-point' (default) to format
/// each array item on a new line, or 'line-by-line' to keep as many items as possible on the
/// same line (as space permits). Defaults to single line.
#[arg(long, default_value_t = true)]
fixed_array_line_breaking: bool,
#[arg(long)]
fixed_array_line_breaking: Option<bool>,
/// Enable merging of `use` items.
#[arg(long, default_value_t = false)]
merge_use_items: bool,
#[arg(long)]
merge_use_items: Option<bool>,
/// Enable duplicates in `use` items.
#[arg(long, default_value_t = false)]
allow_duplicates: bool,
#[arg(long)]
allow_duplicates: Option<bool>,
/// A list of files and directories to format. Use "-" for stdin.
files: Vec<String>,
}
Expand Down Expand Up @@ -203,16 +201,8 @@ fn main() -> ExitCode {
let args = FormatterArgs::parse();
let config = FormatterConfig::default()
.sort_module_level_items(args.sort_mod_level_items)
.tuple_breaking_behavior(if args.tuple_line_breaking {
CollectionsBreakingBehavior::SingleBreakPoint
} else {
CollectionsBreakingBehavior::LineByLine
})
.fixed_array_breaking_behavior(if args.fixed_array_line_breaking {
CollectionsBreakingBehavior::LineByLine
} else {
CollectionsBreakingBehavior::SingleBreakPoint
})
.tuple_breaking_behavior(args.tuple_line_breaking.map(Into::into))
.fixed_array_breaking_behavior(args.fixed_array_line_breaking.map(Into::into))
.merge_use_items(args.merge_use_items)
.allow_duplicate_uses(args.allow_duplicates);
let fmt = CairoFormatter::new(config);
Expand Down
48 changes: 38 additions & 10 deletions crates/cairo-lang-formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ pub enum CollectionsBreakingBehavior {
LineByLine,
}

/// Impl CollectionsBreakingBehavior from bool, where true is `LineByLine` and false is
/// `SingleBreakPoint`. This adheres to the existing behavior of the formatter CLI.
impl From<bool> for CollectionsBreakingBehavior {
fn from(b: bool) -> Self {
if b {
CollectionsBreakingBehavior::LineByLine
} else {
CollectionsBreakingBehavior::SingleBreakPoint
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct FormatterConfig {
Expand Down Expand Up @@ -107,26 +119,42 @@ impl FormatterConfig {
}
}

pub fn sort_module_level_items(mut self, sort_module_level_items: bool) -> Self {
self.sort_module_level_items = sort_module_level_items;
pub fn sort_module_level_items(mut self, sort_module_level_items: Option<bool>) -> Self {
if let Some(sort) = sort_module_level_items {
self.sort_module_level_items = sort;
}
self
}

pub fn tuple_breaking_behavior(mut self, behavior: CollectionsBreakingBehavior) -> Self {
self.tuple_breaking_behavior = behavior;
pub fn tuple_breaking_behavior(
mut self,
behavior: Option<CollectionsBreakingBehavior>,
) -> Self {
if let Some(behavior) = behavior {
self.tuple_breaking_behavior = behavior;
}
self
}

pub fn fixed_array_breaking_behavior(mut self, behavior: CollectionsBreakingBehavior) -> Self {
self.fixed_array_breaking_behavior = behavior;
pub fn fixed_array_breaking_behavior(
mut self,
behavior: Option<CollectionsBreakingBehavior>,
) -> Self {
if let Some(behavior) = behavior {
self.fixed_array_breaking_behavior = behavior;
}
self
}
pub fn merge_use_items(mut self, merge: bool) -> Self {
self.merge_use_items = merge;
pub fn merge_use_items(mut self, merge: Option<bool>) -> Self {
if let Some(merge) = merge {
self.merge_use_items = merge;
}
self
}
pub fn allow_duplicate_uses(mut self, allow: bool) -> Self {
self.allow_duplicate_uses = allow;
pub fn allow_duplicate_uses(mut self, allow: Option<bool>) -> Self {
if let Some(allow) = allow {
self.allow_duplicate_uses = allow;
}
self
}
}
Expand Down
20 changes: 6 additions & 14 deletions crates/cairo-lang-formatter/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cairo_lang_utils::Upcast;
use pretty_assertions::assert_eq;
use test_case::test_case;

use crate::{CollectionsBreakingBehavior, FormatterConfig, get_formatted_file};
use crate::{FormatterConfig, get_formatted_file};

#[salsa::database(SyntaxDatabase, FilesDatabase)]
#[derive(Default)]
Expand Down Expand Up @@ -146,19 +146,11 @@ fn format_and_compare_file(
));

let config = FormatterConfig::default()
.sort_module_level_items(use_sorting)
.tuple_breaking_behavior(if tuple_line_breaking {
CollectionsBreakingBehavior::LineByLine
} else {
CollectionsBreakingBehavior::SingleBreakPoint
})
.fixed_array_breaking_behavior(if fixed_array_line_breaking {
CollectionsBreakingBehavior::LineByLine
} else {
CollectionsBreakingBehavior::SingleBreakPoint
})
.merge_use_items(merge_use_statements)
.allow_duplicate_uses(allow_duplicate_uses);
.sort_module_level_items(Some(use_sorting))
.tuple_breaking_behavior(Some(tuple_line_breaking.into()))
.fixed_array_breaking_behavior(Some(fixed_array_line_breaking.into()))
.merge_use_items(Some(merge_use_statements))
.allow_duplicate_uses(Some(allow_duplicate_uses));

let formatted_file = get_formatted_file(db, &syntax_root, config);
let expected_file =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,10 @@ fn test_validate_gas_cost() {
to: contract_address,
selector: 0x2c0f7bf2d6cf5304c29171bf493feb222fef84bdaf17805a6574b0c2e8bcc87,
calldata: [
0x4db5d32,
0x0,
0x896ba264a31df2,
0x0,
0x2,
0x4db5d32, 0x0, 0x896ba264a31df2, 0x0, 0x2,
0x53c91253bc9682c04929ca02ed00b3e423f6710d2ee7e0d5ebb06f3ecf368a8,
0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7,
0x54767f773cc172172c3afc5265bd0a76089c24cdef409635d27ac1a1fa96ca8,
0x65586264,
0x54767f773cc172172c3afc5265bd0a76089c24cdef409635d27ac1a1fa96ca8, 0x65586264,
]
.span(),
},
Expand Down

0 comments on commit 070e6d9

Please sign in to comment.