From 2e757539f4b20f05de440298fa2f0ce258441f74 Mon Sep 17 00:00:00 2001 From: Tomer Cohen Date: Thu, 31 Oct 2024 11:36:27 +0200 Subject: [PATCH] Handle use star in the formatter. --- .../src/formatter_impl.rs | 25 +++++++------------ .../src/node_properties.rs | 7 +++++- .../cairo_files/sort_inner_use.cairo | 4 +++ .../test_data/cairo_files/use_sorting.cairo | 2 ++ .../expected_results/sort_inner_use.cairo | 4 +++ .../expected_results/use_sorting.cairo | 2 ++ .../src/parser_test_data/partial_trees/use | 6 ++--- 7 files changed, 30 insertions(+), 20 deletions(-) diff --git a/crates/cairo-lang-formatter/src/formatter_impl.rs b/crates/cairo-lang-formatter/src/formatter_impl.rs index 3baab3ee2c0..b04cb0c6590 100644 --- a/crates/cairo-lang-formatter/src/formatter_impl.rs +++ b/crates/cairo-lang-formatter/src/formatter_impl.rs @@ -1040,14 +1040,6 @@ impl<'a> FormatterImpl<'a> { /// Compares two `UsePath` nodes to determine their ordering. fn compare_use_paths(a: &UsePath, b: &UsePath, db: &dyn SyntaxGroup) -> Ordering { match (a, b) { - // Case for multi vs non-multi: multi paths are always ordered before non-multi paths. - (UsePath::Multi(_), UsePath::Leaf(_) | UsePath::Single(_) | UsePath::Star(_)) => { - Ordering::Greater - } - (UsePath::Leaf(_) | UsePath::Single(_) | UsePath::Star(_), UsePath::Multi(_)) => { - Ordering::Less - } - // Case for multi vs multi. (UsePath::Multi(a_multi), UsePath::Multi(b_multi)) => { let get_min_child = |multi: &ast::UsePathMulti| { @@ -1065,6 +1057,10 @@ fn compare_use_paths(a: &UsePath, b: &UsePath, db: &dyn SyntaxGroup) -> Ordering } } + // Case for multi is always after other types of paths. + (UsePath::Multi(_), _) => Ordering::Greater, + (_, UsePath::Multi(_)) => Ordering::Less, + // Case for Leaf vs Single and Single vs Leaf. (UsePath::Leaf(a_leaf), UsePath::Single(b_single)) => { let a_str = a_leaf.extract_ident(db); @@ -1111,14 +1107,11 @@ fn compare_use_paths(a: &UsePath, b: &UsePath, db: &dyn SyntaxGroup) -> Ordering other => other, } } - // TODO(Tomer-StarkWare): Handle these cases - (UsePath::Star(_), _) => { - todo!() - } - // TODO(Tomer-StarkWare): Handle these cases - (_, UsePath::Star(_)) => { - todo!() - } + + // Star is always considered first. + (UsePath::Star(_), UsePath::Star(_)) => Ordering::Equal, + (UsePath::Star(_), _) => Ordering::Less, + (_, UsePath::Star(_)) => Ordering::Greater, } } diff --git a/crates/cairo-lang-formatter/src/node_properties.rs b/crates/cairo-lang-formatter/src/node_properties.rs index 65abf0cef58..83a0978a89f 100644 --- a/crates/cairo-lang-formatter/src/node_properties.rs +++ b/crates/cairo-lang-formatter/src/node_properties.rs @@ -648,7 +648,12 @@ impl SyntaxNodeFormat for SyntaxNode { true, )) } - SyntaxKind::TerminalMul if parent_kind(db, self) != Some(SyntaxKind::ExprUnary) => { + SyntaxKind::TerminalMul + if !matches!( + parent_kind(db, self), + Some(SyntaxKind::ExprUnary | SyntaxKind::UsePathStar) + ) => + { BreakLinePointsPositions::Leading(BreakLinePointProperties::new( 10, BreakLinePointIndentation::Indented, diff --git a/crates/cairo-lang-formatter/test_data/cairo_files/sort_inner_use.cairo b/crates/cairo-lang-formatter/test_data/cairo_files/sort_inner_use.cairo index 07a638a0a07..03d4805b1a8 100644 --- a/crates/cairo-lang-formatter/test_data/cairo_files/sort_inner_use.cairo +++ b/crates/cairo-lang-formatter/test_data/cairo_files/sort_inner_use.cairo @@ -1,16 +1,20 @@ use std::collections::HashMap; use crate::utils::{a, b, d, c}; +use c::{c, a, *, d, b}; use b::{d, c, b, a}; use a::{d, b, a, c}; use a::{c, d}; use a::{d, b}; +use a::*; use aba; use a::{d, b as bee, b as aee, c as cee, a}; use a::{d, b}; use a::{a as bc, a as ab}; +use a::{a as r, *, b as t}; use a::{{d}, e, ab, c}; use a::{a, a::{c, {a}}, a::{b}, a::{{a},b}}; +use a::{c, d::{a, *}, r, t::{*, c::a}}; diff --git a/crates/cairo-lang-formatter/test_data/cairo_files/use_sorting.cairo b/crates/cairo-lang-formatter/test_data/cairo_files/use_sorting.cairo index e78501dd07f..db3d6a33717 100644 --- a/crates/cairo-lang-formatter/test_data/cairo_files/use_sorting.cairo +++ b/crates/cairo-lang-formatter/test_data/cairo_files/use_sorting.cairo @@ -1,6 +1,7 @@ //! Header comment, should not be moved by the formatter. /// Doc comment, should be moved by the formatter. use openzeppelin::introspection::interface; +use openzeppelin::*; use openzeppelin::introspection::first; #[starknet::contract] @@ -9,6 +10,7 @@ mod SRC5 { /// Doc comment, should be moved by the formatter. use openzeppelin::introspection::interface; use openzeppelin::introspection::{interface, AB}; + use openzeppelin::introspection::*; #[storage] struct Storage { diff --git a/crates/cairo-lang-formatter/test_data/expected_results/sort_inner_use.cairo b/crates/cairo-lang-formatter/test_data/expected_results/sort_inner_use.cairo index 24583be6f6d..1fc43901134 100644 --- a/crates/cairo-lang-formatter/test_data/expected_results/sort_inner_use.cairo +++ b/crates/cairo-lang-formatter/test_data/expected_results/sort_inner_use.cairo @@ -1,3 +1,5 @@ +use a::*; +use a::{*, a as r, b as t}; use a::{a, b, c, d}; use a::{a, b as aee, b as bee, c as cee, d}; @@ -8,9 +10,11 @@ use a::{b, d}; use a::{b, d}; use a::{c, d}; +use a::{c, d::{*, a}, r, t::{*, c::a}}; use a::{ab, c, e, {d}}; use aba; use b::{a, b, c, d}; +use c::{*, a, b, c, d}; use crate::utils::{a, b, c, d}; use std::collections::HashMap; diff --git a/crates/cairo-lang-formatter/test_data/expected_results/use_sorting.cairo b/crates/cairo-lang-formatter/test_data/expected_results/use_sorting.cairo index e5635b10a17..19e9d714623 100644 --- a/crates/cairo-lang-formatter/test_data/expected_results/use_sorting.cairo +++ b/crates/cairo-lang-formatter/test_data/expected_results/use_sorting.cairo @@ -1,4 +1,5 @@ //! Header comment, should not be moved by the formatter. +use openzeppelin::*; use openzeppelin::introspection::first; /// Doc comment, should be moved by the formatter. use openzeppelin::introspection::interface; @@ -6,6 +7,7 @@ use openzeppelin::introspection::interface; #[starknet::contract] mod SRC5 { //! Header comment, should not be moved by the formatter. + use openzeppelin::introspection::*; /// Doc comment, should be moved by the formatter. use openzeppelin::introspection::interface; use openzeppelin::introspection::{AB, interface}; diff --git a/crates/cairo-lang-parser/src/parser_test_data/partial_trees/use b/crates/cairo-lang-parser/src/parser_test_data/partial_trees/use index ee185ea27d6..05a8a2f2b1f 100644 --- a/crates/cairo-lang-parser/src/parser_test_data/partial_trees/use +++ b/crates/cairo-lang-parser/src/parser_test_data/partial_trees/use @@ -82,7 +82,7 @@ test_partial_parser_tree(expect_diagnostics: false) //! > cairo_code fn foo() { - use X:: *; + use X::*; } //! > top_level_kind @@ -115,7 +115,7 @@ test_partial_parser_tree(expect_diagnostics: false) //! > cairo_code fn foo() { #[attribute] - use X:: *; + use X::*; } //! > top_level_kind @@ -155,7 +155,7 @@ test_partial_parser_tree(expect_diagnostics: false) //! > cairo_code fn foo() { - use X::{A, *, B}; + use X::{A, *, B}; } //! > top_level_kind