Rustfmt: reorder-imports should have option to preserve import groups

Created on 1 Nov 2016  路  3Comments  路  Source: rust-lang/rustfmt

Currently (version 0.6.2) rustfmt will change this:

use a;
use x;
use b;

use std::io;

to

use a;
use b;

use std::io;
use x;

By group i mean consecutive lines of imports. Groups are divided by empty newlines. I write my imports such that imports from the crate i'm working on are in one group and imports from other crates/std get put in different group. Rustfmt mixes them, which defeats the purpose of grouping. On the other hand i would like to have both groups sorted within themselves. Is there a way to achieve that with current version of rustfmt and if not could this be considered?
For the example above i'd like rustfmt to change it like so:

use a;
use b;
use x;

use std::io;

Most helpful comment

I'd like this as well. I typically sort my imports in three groups like this:

use std::cmp;
use std::fmt;

use byteorder;
use socketcan;

use same_crate_a;
use same_crate_b;

At least the formatters I used for Python and Java do similar things

All 3 comments

I'd like this as well. I typically sort my imports in three groups like this:

use std::cmp;
use std::fmt;

use byteorder;
use socketcan;

use same_crate_a;
use same_crate_b;

At least the formatters I used for Python and Java do similar things

Seconded. I also picked up that habit in the Python world and I currently keep my imports sorted manually because I value the groups more than the convenience of rustfmt.

Issue #1302 is somewhat related to this one.

Was this page helpful?
0 / 5 - 0 ratings