Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated max depth for multi + crate & super #6912

Merged
merged 1 commit into from
Dec 23, 2024

Conversation

dean-starkware
Copy link
Collaborator

This PR addresses the formatting of crate and super and limits the allowed depth of chaining in UsePathMulti.

Please note:
Unlike Rust, we do not insert a blank line between the "regular" use statements and crate & super.

@reviewable-StarkWare
Copy link

This change is Reviewable

@dean-starkware dean-starkware marked this pull request as ready for review December 23, 2024 09:38
Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed all commit messages.
Reviewable status: 0 of 5 files reviewed, 1 unresolved discussion (waiting on @dean-starkware and @gilbens-starkware)


crates/cairo-lang-formatter/src/formatter_impl.rs line 117 at r1 (raw file):

        (nested_paths, false)
    }

Suggestion:

    /// Merge and organize the `use` paths in a hierarchical structure.
    pub fn create_merged_use_items(
        self,
        allow_duplicate_uses: bool,
        top_level: bool,
    ) -> (Vec<String>, bool) {
        let leaf_paths = self
            .leaves
            .into_iter()
            .map(|leaf| {
                if let Some(alias) = leaf.alias {
                    format!("{} as {alias}", leaf.name)
                } else {
                    leaf.name
                }
            })
            .collect_vec();

        let mut nested_paths = vec![];
        for (segment, subtree) in self.children {
            let next_depth = current_depth + 1;
            let subtree_merged_use_items =
                subtree.create_merged_use_items(allow_duplicate_uses, next_depth, false);

            nested_paths.extend(subtree_merged_use_items.into_iter().map(|child| format!("{segment}::{child}")));

            if is_single_leaf && next_depth >= MAX_DEPTH_FOR_MULTI {
                leaf_paths.extend(formatted_subtree_paths);
            } else {
                formatted_subtree_paths);
            }
        }

        if !allow_duplicate_uses {
            leaf_paths.sort();
            leaf_paths.dedup();
        }

        match leaf_paths.len() {
            0 => {}
            1 if nested_paths.is_empty() => return (leaf_paths, true),
            1 => nested_paths.extend(leaf_paths),
            _ if top_level => nested_paths.extend(leaf_paths),
            _ => nested_paths.push(format!("{{{}}}", leaf_paths.join(", "))),
        }

        (nested_paths, false)
    }

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 5 files reviewed, 1 unresolved discussion (waiting on @dean-starkware and @gilbens-starkware)


crates/cairo-lang-formatter/src/formatter_impl.rs line 117 at r1 (raw file):

        (nested_paths, false)
    }

Suggestion:

    /// Merge and organize the `use` paths in a hierarchical structure.
    pub fn create_merged_use_items(
        self,
        allow_duplicate_uses: bool,
        top_level: bool,
    ) -> Vec<String> {
        let leaf_paths = self
            .leaves
            .into_iter()
            .map(|leaf| {
                if let Some(alias) = leaf.alias {
                    format!("{} as {alias}", leaf.name)
                } else {
                    leaf.name
                }
            })
            .collect_vec();

        let mut nested_paths = vec![];
        for (segment, subtree) in self.children {
            let next_depth = current_depth + 1;
            let subtree_merged_use_items =
                subtree.create_merged_use_items(allow_duplicate_uses, next_depth, false);

            nested_paths.extend(subtree_merged_use_items.into_iter().map(|child| format!("{segment}::{child}")));

            if is_single_leaf && next_depth >= MAX_DEPTH_FOR_MULTI {
                leaf_paths.extend(formatted_subtree_paths);
            } else {
                formatted_subtree_paths);
            }
        }

        if !allow_duplicate_uses {
            leaf_paths.sort();
            leaf_paths.dedup();
        }

        match leaf_paths.len() {
            0 => {}
            1 if nested_paths.is_empty() => return leaf_paths,
            1 => nested_paths.extend(leaf_paths),
            _ if top_level => nested_paths.extend(leaf_paths),
            _ => nested_paths.push(format!("{{{}}}", leaf_paths.join(", "))),
        }

        nested_paths
    }

@dean-starkware dean-starkware force-pushed the dean/formatter_depth_for_multi branch from 813d577 to cfe1d57 Compare December 23, 2024 14:04
Copy link
Collaborator Author

@dean-starkware dean-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 5 files reviewed, 1 unresolved discussion (waiting on @gilbens-starkware and @orizi)


crates/cairo-lang-formatter/src/formatter_impl.rs line 117 at r1 (raw file):

        (nested_paths, false)
    }

leaf_paths must be mut.

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 4 of 5 files at r1, 1 of 1 files at r2.
Reviewable status: all files reviewed (commit messages unreviewed), all discussions resolved (waiting on @gilbens-starkware)

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @gilbens-starkware)

@dean-starkware dean-starkware added this pull request to the merge queue Dec 23, 2024
Merged via the queue into main with commit a0a7f6e Dec 23, 2024
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants