rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

[unstable option] imports_layout

Open scampi opened this issue 6 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

It seems like there are some weird interactions with the imports_granularity option. For example, with import_granularity = "Crate" and imports_layout = "Horizontal":

use foo::{
    a, b, b::{f, g}, c, d::e
};
use qux::{h, i};

HorizontalVertical

use foo::{
    a,
    b,
    b::{f, g},
    c,
    d::e,
};
use qux::{h, i};

Mixed (default)

use foo::{
    a, b,
    b::{f, g},
    c,
    d::e,
};
use qux::{h, i};

None of those really match up with what I would expect based on the examples, it seems like it jumps to multiline too quick.

tgross35 avatar Apr 03 '23 17:04 tgross35

@tgross35 I believe the current formatting is correct based on the Nested import rules defined in the style guide

ytmimi avatar Apr 03 '23 18:04 ytmimi

If there are any nested imports in a list import, then use the multi-line form, even if the import fits on one line. Each nested import must be on its own line, but non-nested imports must be grouped on as few lines as possible.

I guess that does line up. Though I'm not sure why "Horizontal" breaks into a multiline group when it's not close to line length

tgross35 avatar Apr 03 '23 18:04 tgross35

Though I'm not sure why "Horizontal" breaks into a multiline group when it's not close to line length

@tgross35 From the style guide: "If there are any nested imports in a list import, then use the multi-line form, even if the import fits on one line."

ytmimi avatar Apr 03 '23 18:04 ytmimi

It was just my initial understanding that Horizontal breaks the rules to keep everything on one line, e.g., in the docs it says that it may exceed line length. There just isn't an example with nested modules in the docs. (fwiw, I don't have any problem with the behavior, it just didn't line up with my impression from the docs)

tgross35 avatar Apr 03 '23 18:04 tgross35