rust-lang / rust-lang/rustfmt

Best effort on long lines

Open
#3,697 2 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-feature-request I-max-width I-poor-formatting
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Input file:

fn main() {
    aaaaaaaaaaaaaaaaaaaaa :: bbbbbbbbbbbbbbbbbb :: ccccccccccccccccccccccccccccccccccccccc :: dddddddddddddddddddd ( eeeeeeeeeeeeeeeeeeee :: ffffffffffffffffffff ( ggggggggg ( hhhhhhhhhhhhhhhh ) , iiiiiiiii ( jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj ) ) );
}

As of current master (c0e616bc1d19eb9b08a5db2f265b8332569e7259), cargo run --manifest-path path/to/rustfmt/Cargo.toml --release --bin rustfmt <main.rs leaves the long line in this file unchanged.

Shortening the input line such that it can fit within rustfmt's default max_width produces good formatting, as expected:

fn main() {
    bbbbbbbbbbbbbbbbbb::ccccccccccccccccccccccccccccccccccccccc::dddddddddddddddddddd(
        eeeeeeeeeeeeeeeeeeee::ffffffffffffffffffff(
            ggggggggg(hhhhhhhhhhhhhhhh),
            iiiiiiiii(jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj),
        ),
    );
}

If I configure max_width = 1000, I get correct but very wide formatting, as expected:

fn main() {
    aaaaaaaaaaaaaaaaaaaaa::bbbbbbbbbbbbbbbbbb::ccccccccccccccccccccccccccccccccccccccc::dddddddddddddddddddd(eeeeeeeeeeeeeeeeeeee::ffffffffffffffffffff(ggggggggg(hhhhhhhhhhhhhhhh), iiiiiiiii(jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj)));
}

Is there anything I can set to end up with best-effort formatting, even for content wider than max_width, rather than leaving long lines totally unformatted? Here is what I would want:

fn main() {
    aaaaaaaaaaaaaaaaaaaaa::bbbbbbbbbbbbbbbbbb::ccccccccccccccccccccccccccccccccccccccc::dddddddddddddddddddd(
        eeeeeeeeeeeeeeeeeeee::ffffffffffffffffffff(
            ggggggggg(hhhhhhhhhhhhhhhh),
            iiiiiiiii(jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj),
        ),
    );
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the case from main.rs with the rustfmt Cargo command shown in the issue, first at the default max_width and then at 1000. Compare the unchanged long line with the requested best-effort output. Done means rustfmt applies the demonstrated wrapping even when content exceeds max_width.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.