Skip to content

Commit

Permalink
Handle use star in the formatter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomer-StarkWare committed Nov 24, 2024
1 parent c356fb7 commit 2e75753
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 20 deletions.
25 changes: 9 additions & 16 deletions crates/cairo-lang-formatter/src/formatter_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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);
Expand Down Expand Up @@ -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,
}
}

Expand Down
7 changes: 6 additions & 1 deletion crates/cairo-lang-formatter/src/node_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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}};
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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;
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! 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;

#[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};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test_partial_parser_tree(expect_diagnostics: false)

//! > cairo_code
fn foo() {
use X:: *;
use X::*;
}

//! > top_level_kind
Expand Down Expand Up @@ -115,7 +115,7 @@ test_partial_parser_tree(expect_diagnostics: false)
//! > cairo_code
fn foo() {
#[attribute]
use X:: *;
use X::*;
}

//! > top_level_kind
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2e75753

Please sign in to comment.