Skip to content

WIP: Add an option for maximizing lines #2447

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
wants to merge 4 commits into from

Conversation

radix
Copy link
Contributor

@radix radix commented Feb 15, 2018

#Fixes #2437

This makes use_small_heuristics into a tri-state option with an explicit enum.

  • Minimum -- equivalent to old use_small_heuristics: false
  • Medium -- equivalent to old use_small_heuristics: true (default)
  • Maximum -- new behavior: set width heuristics equal to max_width.

TODO:

  • Update existing tests
  • document the new behavior (a better description would be greatly appreciated)
  • Add new tests (just a copy of chains.rs for now, should I do something more specific?)
  • use_small_heuristics obviously isn't a good name for this any more
  • determine if this is even what other people want

Feedback appreciated!

This just sets the heuristic widths == max_width.
@radix
Copy link
Contributor Author

radix commented Feb 16, 2018

here's a great example of why I wanted this change, from my real codebase.

This is what rustfmt generated previously:

      TransferItem {
        from,
        to,
        item_id,
        count,
      } => self.change_with(GameLog::TransferItem {
        from,
        to,
        item_id,
        count,
      }),
      RemoveItem {
        owner,
        item_id,
        count,
      } => self.change_with(GameLog::RemoveItem {
        owner,
        item_id,
        count,
      }),

and now, with my changes, it's:

      TransferItem { from, to, item_id, count } => {
        self.change_with(GameLog::TransferItem { from, to, item_id, count })
      }
      RemoveItem { owner, item_id, count } => {
        self.change_with(GameLog::RemoveItem { owner, item_id, count })
      }

which, I notice, even fits within 80 lines.

radix added a commit to arpeggiorpg/arpeggiorpg that referenced this pull request Feb 16, 2018
Copy link
Member

@nrc nrc left a comment

Choose a reason for hiding this comment

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

Looks good! My many worry is about naming, see inline comments for details.

@@ -48,8 +48,7 @@ create_config! {
tab_spaces: usize, 4, true, "Number of spaces per tab";
newline_style: NewlineStyle, NewlineStyle::Unix, true, "Unix or Windows line endings";
indent_style: IndentStyle, IndentStyle::Block, false, "How do we indent expressions or items.";
use_small_heuristics: bool, true, false, "Whether to use different formatting for items and\
expressions if they satisfy a heuristic notion of 'small'.";
use_small_heuristics: LineWidth, LineWidth::Medium, false, "The general width of lines.";
Copy link
Member

Choose a reason for hiding this comment

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

I think it is better to talk about how aggressively we put items across multiple lines, rather than 'general width of lines', which seems to invite confusion with max_width. So, rather than LineWidth::Minimum, we talk about making items as vertically formatted as possible, whereas Maximum is about making items maximally horizontal. (Or mutli-line vs single-line, if you prefer)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is still the biggest thing holding me back from finishing up this PR. The line length heuristics are confusing. I don't think "more horizontal" vs "more vertical" even really describes the options.

use_small_heuristics: true: (default)

  • function calls: 60%
  • struct literals: 18%
  • struct variants: 35%
  • arrays: 60%
  • chains: 60%
  • if/else: 50%

use_small_heuristics: false

  • function calls: 100%
  • struct literals: 0%
  • struct variants: 0%
  • arrays: 100%
  • chains: 100%
  • if/else: 0%

This means that enabling heuristics leads to code being sometimes more vertical and sometimes less vertical. This makes it really hard for me to describe what use_small_heuristics: false does in plain English, which also makes it hard to come up with a non-boolean name for what it does :(

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, yeah I know what you mean. The logic is that with use_small_heuristics false, we use the same formatting for small things as we do for large things, whereas when true we do things differently depending on the size - we've never tried to frame it as making lines longer or shorted, just 'better' (easier to read, prettier, etc). I guess the trouble with describing this is that even without use_small_heuristics, the set of things which is put on one line, rather than multiple lines is somewhat arbitrary.

Perhaps values like None (today's false), Default (today's true), and Max (for the new stuff) is OK. They're not very self-explanatory, but I don't think we'll do better.

struct_variant_width: max_width,
array_width: max_width,
chain_width: max_width,
single_line_if_else_max_width: max_width,
Copy link
Member

Choose a reason for hiding this comment

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

I would use usize::max_value() rather than max_width means one less arg, plus no ordering constraint on initialisation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fwiw, the scaled() constructor also takes max_width, but I can still change this np.

Copy link
Member

Choose a reason for hiding this comment

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

scaled needs it though since it uses the max width as input (we could probably use max_value there for the 100% ones).

@@ -189,6 +189,15 @@ configuration_option_enum! { Color:
Auto,
}

configuration_option_enum! { LineWidth:
// Always format lines as short as possible (?)
Minimum,
Copy link
Member

Choose a reason for hiding this comment

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

The null width heuristic doesn't make lines as short as possible, it tries to layout stuff in its 'default' layout without taking into consideration the width of the sub-item. I.e., it will make function calls as wide as possible, but will never put if/else on one line (since multi-line is the default).

@radix
Copy link
Contributor Author

radix commented Mar 12, 2018

Hi, just an update on this PR to state that I do still intend to finish this off, I have just been busy with other things lately. I'm closing this PR to unclutter the PR list for the maintainers, but I will re-open it once I've addressed the comments.

Thanks @nrc for being so helpful with the reviews!

@radix radix closed this Mar 12, 2018
@nrc
Copy link
Member

nrc commented Mar 15, 2018

Hi, just an update on this PR to state that I do still intend to finish this off, I have just been busy with other things lately. I'm closing this PR to unclutter the PR list for the maintainers, but I will re-open it once I've addressed the comments.

Great, look forward to seeing it again, thanks!

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.

2 participants