rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

[unstable option] imports_layout

Open scampi opened this issue 5 years ago • 19 comments

Tracking issue for unstable option: imports_layout

scampi avatar Feb 13 '19 22:02 scampi

What's necessary to stabilize this?

jimmycuadra avatar Jun 01 '19 13:06 jimmycuadra

There are some steps described in https://github.com/rust-lang/rustfmt/blob/master/Processes.md#stabilising-an-option. https://github.com/rust-lang/rustfmt/pull/3581 is an attempt at this.

scampi avatar Jun 02 '19 16:06 scampi

Why does Horizontal have to exceed max_width? How does this behavior interact with merge_imports = true?

I think it should be possible to try to fit all import statements into max_width, replicating the path prefix for any spill-over items into the next line(s).

mzabaluev avatar Jun 06 '19 14:06 mzabaluev

One good reason to prefer the Horizontal layout (and stabilize this setting after the behavioral aspects are ironed out) is to keep the imports searchable with grep-like tools that don't easily match across multiple lines.

mzabaluev avatar Jun 06 '19 14:06 mzabaluev

A counter-argument for Vertical sorting is that it makes rebases significantly easier.

I recently had to do a rebase of a fairly complicated refactoring that moved lots of types around, and git rebase tools for in-line changes is not great.

I would like to use this feature in mozilla-central once it’s stablised.

andreastt avatar Oct 19 '19 17:10 andreastt

I would love independent imports:

use foo::{
    aaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbb, cccccccccccccccccc, dddddddddddddddddd,
    eeeeeeeeeeeeeeeeee, ffffffffffffffffff,
};

=>

use foo::aaaaaaaaaaaaaaaaaa;
use foo::bbbbbbbbbbbbbbbbbb;
use foo::cccccccccccccccccc;
use foo::dddddddddddddddddd;
use foo::eeeeeeeeeeeeeeeeee;
use foo::ffffffffffffffffff;

As a possible alternative, I find this style super-friendly to git, immediately readable to my tastes, easy to play around with commenting-uncommenting.

Is it possible to have it as an ~~independent~~ alternative option?

UPDATE: this has been implemented in the (currently unstable) option imports_granularity :tada:

vn971 avatar May 22 '20 12:05 vn971

I think "independent imports" (https://github.com/rust-lang/rustfmt/issues/3361#issuecomment-632661079) is outside of what this option is for, and is a discussion for https://github.com/rust-lang/rustfmt/issues/3362 instead. It's already been brought up there in https://github.com/rust-lang/rustfmt/issues/3362#issuecomment-641125438.

AIUI the two options are orthogonal and the distinction is as follows: you can think of merge_imports as determining the tokens of our imports i.e. how/whether they should be grouped i.e. which parts of which specific paths should go inside of the same curly braces; while imports_layout determines the whitespace of each import after the point that the tokens have already been decided.

dtolnay avatar Jun 11 '20 10:06 dtolnay

Shouldn't "mixed' be called "compressed" to match other similar options?

Lokathor avatar Aug 02 '20 17:08 Lokathor

To explain for new people reading this issue. imports_layout is not going to be stabilized and is instead replaced with imports_granularity #3362 (comment)

This is incorrect. merge_imports was superseded by imports_granularity, there was no impact and no change to imports_layout which was a different option that controlled different formatting aspects.

calebcartwright avatar Mar 18 '21 13:03 calebcartwright

Is this the same thing as imports_granularity? What kind of timeline / issues are there for stabilizing that?

Fishrock123 avatar Apr 29 '21 19:04 Fishrock123

Deno project is very interested in this setting as well (because we only use latests stable Rust toolchain).

We'd prefer to format to have a single import per line (which is is very git diff friendly).

bartlomieju avatar Aug 07 '21 22:08 bartlomieju

Is this the same thing as imports_granularity? What kind of timeline / issues are there for stabilizing that?

I've opened #4991 to track imports_granularity.

9999years avatar Sep 15 '21 16:09 9999years

Is there anything missing to stabilize this option?

Progdrasil avatar Oct 27 '21 20:10 Progdrasil

@Progdrasil One of the big things missing is dealing with comments in imports; see https://github.com/rust-lang/rustfmt/issues/4991#issuecomment-939537120

9999years avatar Oct 28 '21 16:10 9999years

linking #3286

ytmimi avatar Jul 20 '22 02:07 ytmimi

The option LimitedHorizontalVertical isn't documented

blyxyas avatar Sep 17 '22 15:09 blyxyas

@blyxyas PRs for doc improvements are always welcome 😁

ytmimi avatar Sep 18 '22 18:09 ytmimi

I haven't made a PR because I don't know what it does Sorry

blyxyas avatar Sep 18 '22 19:09 blyxyas

@blyxyas no worries. I also couldn't have told you what it did before looking into it. From what I can tell it's meant to let you control the width at which imports wrap from being laid out horizontally to being laid out vertically, however I don't think we support parsing config options that take a value, and that might be why its not documented.

imports_layout config option maps to the ListTactic enum. https://github.com/rust-lang/rustfmt/blob/38659ec6ad5f341cf8eb3139725bf695872c6de7/src/config/mod.rs#L90 https://github.com/rust-lang/rustfmt/blob/38659ec6ad5f341cf8eb3139725bf695872c6de7/src/config/lists.rs#L26-L41

I tried setting it from the command line and got an error

rustfmt --config="imports_layout=LimitedHorizontalVertical" 
invalid key=val pair: `imports_layout=LimitedHorizontalVertical`

rustfmt --config="imports_layout=LimitedHorizontalVertical(50)"
invalid key=val pair: `imports_layout=LimitedHorizontalVertical(50)`

The ListTactic enum is pretty general and is used internally to format many different types of lists. Maybe it makes more sense to create a new ImportsListTactic enum that exposes the same options as ListTactic except the LimitedHorizontalVertical(50) variant. Given that LimitedHorizontalVertical(_) can't be set (at least not from the command line) maybe it's not worth it to make that change.

ytmimi avatar Sep 19 '22 15:09 ytmimi