Skip to content
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

Feature Request: Custom auto-import order #13960

Open
Nugine opened this issue Jan 16, 2023 · 4 comments
Open

Feature Request: Custom auto-import order #13960

Nugine opened this issue Jan 16, 2023 · 4 comments
Labels
A-config configuration C-feature Category: feature request E-has-instructions Issue has some instructions and pointers to code to get started

Comments

@Nugine
Copy link

Nugine commented Jan 16, 2023

My favour is the following order, sorted by priority.

  1. Sub module
  2. Super module
  3. Current crate
  4. Workspace crates
  5. std/alloc/core items
  6. External crates

However, the current auto-import feature enforces a built-in order which I cannot adjust.
https://rust-analyzer.github.io/manual.html#auto-import

It would be better if I don't have to reorder imports manually.

@jonas-schievink jonas-schievink added A-completion autocompletion C-feature Category: feature request and removed A-completion autocompletion labels Jan 27, 2023
@Veykril
Copy link
Member

Veykril commented Jan 30, 2023

The order is currently strictly defined via

#[derive(Eq, PartialEq, PartialOrd, Ord)]
enum ImportGroup {
// the order here defines the order of new group inserts
Std,
ExternCrate,
ThisCrate,
ThisModule,
SuperModule,
}

The main question is how to expose ordering in a config in a decent way

@Veykril Veykril added A-config configuration E-has-instructions Issue has some instructions and pointers to code to get started labels Jan 30, 2023
@Nugine
Copy link
Author

Nugine commented Jan 31, 2023

The config may look like this typescript definition

type ImportGroup = "std" | "extern-crate" | "this-crate" | "this-module" | "super-module" | "workspace-crate";

type ImportOrder = ImportGroup[]; // unique elements

const A: ImportOrder = [
    "std",
    "extern-crate",
    "workspace-crate",
    "this-crate",
    "this-module",
    "super-module"
];

const B: ImportOrder = [
    "this-module",
    "super-module",
    "this-crate",
    "workspace-crate",
    "std",
    "extern-crate"
];

@Nugine
Copy link
Author

Nugine commented Jan 31, 2023

I also want a fmt tool which can enforce custom import orders. While rustfmt has an unstable option, it still has a number of issues. I would be appreciated if anyone can help with this problem.

@cmfcmf
Copy link

cmfcmf commented Jun 12, 2024

The config may look like this typescript definition

type ImportGroup = "std" | "extern-crate" | "this-crate" | "this-module" | "super-module" | "workspace-crate";

type ImportOrder = ImportGroup[]; // unique elements

const A: ImportOrder = [
    "std",
    "extern-crate",
    "workspace-crate",
    "this-crate",
    "this-module",
    "super-module"
];

const B: ImportOrder = [
    "this-module",
    "super-module",
    "this-crate",
    "workspace-crate",
    "std",
    "extern-crate"
];

I think what's missing from this is the ability to merge some of them together into a single block. E.g., I would like to combine "std" | "extern-crate" into a single block without a new line in between.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-config configuration C-feature Category: feature request E-has-instructions Issue has some instructions and pointers to code to get started
Projects
None yet
Development

No branches or pull requests

4 participants