rust-lang / rust-lang/rustfmt

Add max_content_width option

Open
#3,991 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-feature-request P-low
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Currently, splitting behaviour appears to be mostly dependent on max_width, unless some specific option overrides/augments this. I don't fully understand why, either.

Example, with max_width = 140 (line = 105 chars, 85 excluding indent):

                    accum = accum.checked_add(x).unwrap_or(Duration::new(u64::max_value(), 999_999_999));

Again, with max_width = 120 (longest line = 81 chars, 57 excluding indent):

                    accum = accum
                        .checked_add(x)
                        .unwrap_or(Duration::new(u64::max_value(), 999_999_999));

As I see it, there are two reasons to break long lines:

  1. Available width in editors. This may or may not be an issue (it's common to have lots of spare width; some people like myself like to use variable-width fonts).
  2. Readability. Too much content on one line is hard to parse.

Note that there are already a couple of more specific width rules:

  1. comment_width: since comments are somewhat different to code, it makes sense to have this separate
  2. inline_attribute_width: controls two things; (a) whether to use inline attributes at all (default: no), and (b) when, specifically, to allow them

Proposal:

  • Add a max_content_width option; split lines when either the total line length exceeds max_width or the length minus indent exceeds max_content_width
  • Replace inline_attribute_width with inline_attributes (true/false), using max_content_width to control behaviour

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

The issue names no files or tests. Start by locating the handling for max_width, comment_width, and inline_attribute_width in rustfmt, then trace how line splitting and inline attributes are decided. Done means implementing max_content_width as proposed, deciding the replacement behavior for inline_attribute_width, and adding coverage for both width rules.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.