Skip to content

Formatter does not fold a long line #5412

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

Closed
megahomyak opened this issue Jun 28, 2022 · 3 comments
Closed

Formatter does not fold a long line #5412

megahomyak opened this issue Jun 28, 2022 · 3 comments

Comments

@megahomyak
Copy link

megahomyak commented Jun 28, 2022

if is_action {
    if let Some(action) = all_actions.borrow_mut().iter_mut().find(|action| action.shortcut == shortcut) {
        stack.push((action.callback)().into());
    } else {
        ui.invoke_show_message(
            format!("Command with shortcut \"{shortcut}\" (found at line {line_number}) not found!").into(),
        );
    }
}

^ two lines here are longer than 100 characters (even not considering the additional indentation in my code), but none of those are folded

rustfmt version: rustfmt 1.4.38-stable (fe5b13d6 2022-05-18)

@ytmimi
Copy link
Contributor

ytmimi commented Jun 28, 2022

Thanks for reaching out.

I'm unable to reproduce this. I'm using rustfmt 1.4.38-stable (fe5b13d6 2022-05-18) with no configuration options. The issue may be that you need to wrap you snippet in a function for it to parse properly. Here's how I've modified your snippet (just wrapping it in a main function):

fn main() {
    if is_action {
        if let Some(action) = all_actions.borrow_mut().iter_mut().find(|action| action.shortcut == shortcut) {
            stack.push((action.callback)().into());
        } else {
            ui.invoke_show_message(
                format!("Command with shortcut \"{shortcut}\" (found at line {line_number}) not found!").into(),
            );
        }
    }
}

After running rustfmt I get:

fn main() {
    if is_action {
        if let Some(action) = all_actions
            .borrow_mut()
            .iter_mut()
            .find(|action| action.shortcut == shortcut)
        {
            stack.push((action.callback)().into());
        } else {
            ui.invoke_show_message(
                format!(
                    "Command with shortcut \"{shortcut}\" (found at line {line_number}) not found!"
                )
                .into(),
            );
        }
    }
}

When you get a chance can you confirm that the issue was the snippet or perhaps provide a different snippet that illustrates the problem that you're having.

@megahomyak
Copy link
Author

megahomyak commented Jun 28, 2022

Oh, it really works fine without the context. Here's the full snippet: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9a0eaa300539229fd54f8d84da914295
Line 158 is the line of interest

@ytmimi (if tagging is inappropriate, sorry: I don't know whether it is a good practice, I rarely file or view issues)

@ytmimi
Copy link
Contributor

ytmimi commented Jun 28, 2022

Thanks for including the full snippet. The extra context really helps! I see now that we're dealing with a deeply nested structure. Almost certain this is a duplicate of #3416, and #3863 so going to close accordingly.

If you're using a nightly rustfmt, you can try setting version=Two and format_strings=true to allow rustfmt to wrap the long string literal, which should solve the issue. I noticed that other format!() calls already manually wrap strings with line continuation characters.

You can run rustfmt manually on your file with the following:

rustfmt --config=version=Two,format_strings=true <filename>

If you're using a stable rustfmt you can try bumping up the max_width to some value that's sufficiently large. I tested it out with 120, but you might find that a smaller value also works.

rustfmt --config=max_width <filename>

Passing config values via the command line is tedious so you might consider adding a rustfmt.toml file to the root of your project with your desired configuration.

Also, tagging me is just fine 👍🏼

@ytmimi ytmimi closed this as not planned Won't fix, can't repro, duplicate, stale Jun 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants