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

Intention to nest/flatten imports #2577

Closed
matklad opened this issue Jun 9, 2018 · 8 comments · Fixed by #3778
Closed

Intention to nest/flatten imports #2577

matklad opened this issue Jun 9, 2018 · 8 comments · Fixed by #3778
Labels
E-easy Issue required minimal knowledge about platform and project codebase feature help wanted subsystem::code insight General label for issues related to code understanding: highlighting, completion, annotation, etc.

Comments

@matklad
Copy link
Member

matklad commented Jun 9, 2018

use std::error::Error;
use std::io::Write;
use std::path::PathBuf;

<-->

use std::{
    error::Error,
    io::Write,
    path::PathBuf,
};
@Undin Undin added help wanted E-easy Issue required minimal knowledge about platform and project codebase subsystem::code insight General label for issues related to code understanding: highlighting, completion, annotation, etc. feature labels Jun 21, 2018
@dvtomas
Copy link

dvtomas commented Jun 23, 2018

In an ideal world, there would be a configurable import optimizer (see also #2259), that would be able to group imports by std/cargo crates/workspace crates/local imports, sorting imports in each group, nesting/flattening them according to the settings, removing curly braces for single imports etc. Unused imports should be automatically removed. When doing a move or copy refactoring to another file, relevant imports should be copied as well. All reformatting and removing unused imports should happen just on pressing Ctrl-Alt-O, one should almost never have to touch the imports section manually, that is a job for a machine, not for human. This is how it works in the Scala plugin, I've used that for a couple of years so now I see just how much time is wasted by unnecessary manual import maintenance. I'm really looking forward this area being worked on. That said, even the "Import" action for unknown symbols has been a huge help, thank you for that!

Sorry for the digress. IMO this should be a configuration setting in an automatic import optimizer, not an intention, at least in the long run.

@majecty
Copy link
Contributor

majecty commented Oct 15, 2018

I impelmented NestUseStatementsIntention.
You can find it at #2938

When implementing the feature, I met some issues.

  1. Should we nest use statements full depth?

full depth

use std::{
    io::Write,
    sync::{
      Arc,
      Mutex
    },
};

only 1 depth

use std::{
    io::Write,
    sync::Arc,
    sync::Mutex,
};;
  1. Should we preserve statements order or sort them by dictionary order?

If someone has an opinion, please say it freely.

bors bot added a commit that referenced this issue Oct 22, 2018
2938: INT: Implement "NestUseStatementsIntention" r=vlad20012 a=majecty

I implemented "NestUseStatementsIntention".
The issue is here #2577


<!--
Hello and thank you for the pull request!

We don't have any strict rules about pull requests, but you might check
https://github.com/intellij-rust/intellij-rust/blob/master/CONTRIBUTING.md
for some hints!

Note that we need an electronic CLA for contributions:
https://github.com/intellij-rust/intellij-rust/blob/master/CONTRIBUTING.md#cla

After you sign the CLA, please add your name to
https://github.com/intellij-rust/intellij-rust/blob/master/CONTRIBUTORS.txt

:)
-->


Co-authored-by: Park Juhyung <majecty@gmail.com>
bors bot added a commit that referenced this issue Oct 23, 2018
2938: INT: Implement "NestUseStatementsIntention" r=vlad20012 a=majecty

I implemented "NestUseStatementsIntention".
The issue is here #2577


<!--
Hello and thank you for the pull request!

We don't have any strict rules about pull requests, but you might check
https://github.com/intellij-rust/intellij-rust/blob/master/CONTRIBUTING.md
for some hints!

Note that we need an electronic CLA for contributions:
https://github.com/intellij-rust/intellij-rust/blob/master/CONTRIBUTING.md#cla

After you sign the CLA, please add your name to
https://github.com/intellij-rust/intellij-rust/blob/master/CONTRIBUTORS.txt

:)
-->


Co-authored-by: Park Juhyung <majecty@gmail.com>
@majecty
Copy link
Contributor

majecty commented Oct 25, 2018

I implemented nest intention to only nest 1 depth and preserve original order.

@majecty
Copy link
Contributor

majecty commented Oct 25, 2018

@vlad20012
This issue's subject is "nest/flatten imports".
An intention that flattens imports is not implemented yet.
I think this issue should be reopened.

@vlad20012
Copy link
Member

Oh, I'm sorry

@vlad20012 vlad20012 reopened this Oct 25, 2018
@fan-tom
Copy link
Contributor

fan-tom commented Mar 3, 2019

Are we need to add an inspection to show programmer that nesting is possible? Because I even didn't know about this feature until I saw that issue

@fan-tom
Copy link
Contributor

fan-tom commented Mar 4, 2019

There is bug when nesting use statements like this

use foo;
use foo::bar;

transformed to

use foo::{foo, bar};

instead of

use foo::{self, bar};

bors bot added a commit that referenced this issue Mar 9, 2019
3501: INT: Make "unelide lifetimes" intention unavailable in doc comments r=vlad20012 a=vlad20012

It can be too annoying that this intention is available
inside all doctest injections

3503: INT: handle conversion of module to self in nest use intention r=vlad20012 a=Kobzol

This PR fixes an error in the `Nest use statements` intention mentioned here: #2577 (comment).

Before the intention would change this code:
```
use foo;
use foo::bar;
```
into
```
use foo::{foo, bar}`;
```
which is incorrect.

Now it checks if the grouped path is the same as the base path and converts it to `self` if necessary.

Co-authored-by: vlad20012 <beskvlad@gmail.com>
Co-authored-by: Jakub Beránek <berykubik@gmail.com>
@stepancheg
Copy link

It would be helpful if intellij-rust had the opposite intention: expand grouped imports into one import per line.

bors bot added a commit that referenced this issue May 5, 2019
3778: INT: Implement "FlattenUseStatementsIntention" r=mchernyavsky a=mibac138

I believe this closes #2577.
Please feel free to give me your feedback


Co-authored-by: mibac138 <5672750+mibac138@users.noreply.github.com>
@bors bors bot closed this as completed in #3778 May 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Issue required minimal knowledge about platform and project codebase feature help wanted subsystem::code insight General label for issues related to code understanding: highlighting, completion, annotation, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants