rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

Feat: "One" import granularity

Open CAD97 opened this issue 3 years ago • 8 comments

The current options for rust-analyzer.assist.importGranularity are "preserve", "crate", "module", and "item". Personally, I group all of my imports into a single use block (and don't really understand why nobody else seems to do so); rust-analyzer should support (enforcing) this grouping along the existing use groupings.

CAD97 avatar Jan 28 '22 09:01 CAD97

This should be called One like in rustfmt.

flodiebold avatar Jan 28 '22 10:01 flodiebold

Relevant code is mainly here https://github.com/rust-analyzer/rust-analyzer/blob/93036aa0e201474d43c38be3862f4a899defe267/crates/ide_db/src/helpers/insert_use.rs#L155-L193

We also should adjust our guessing infra to be able to understand that form here https://github.com/rust-analyzer/rust-analyzer/blob/93036aa0e201474d43c38be3862f4a899defe267/crates/ide_db/src/helpers/insert_use.rs#L246-L318

And config changes are needed here https://github.com/rust-analyzer/rust-analyzer/blob/93036aa0e201474d43c38be3862f4a899defe267/crates/rust-analyzer/src/config.rs

Veykril avatar Jan 28 '22 10:01 Veykril

can you please assign this issue to me?

petr-tik avatar Feb 07 '22 22:02 petr-tik

We also should adjust our guessing infra to be able to understand that form here

What's the most resilient way to identify the correct ImportGranularityGuess?

Looking at the rustfmt implementation, it looks like having merged use trees one for each visibility level is obviously One, but what about mixed cases? What should it guess?

https://github.com/rust-lang/rustfmt/pull/4680/files#diff-7bd4f14a75a37432af3ab896bbbe38f88428b490b5d76ed1586f7f473a745e98R1772-R1781

petr-tik avatar Feb 08 '22 23:02 petr-tik

To identify we probably just want to check if there is only one tree that starts as use { ... };

If mixed it should fall back to Unknown I'd say.

Veykril avatar Feb 14 '22 20:02 Veykril

i was thinking about writing a test that checks for one tree for each of the possible publicity types

use { ... };
pub use { ... };
pub(crate) use { ... };

thoughts?

petr-tik avatar Feb 14 '22 23:02 petr-tik

Sounds good

Veykril avatar Feb 15 '22 08:02 Veykril

Some personal stuff made me quite unproductive in February. Looks like @jeremyBanks is actively working on it, so it's only fair I am un-assigned from this issue

petr-tik avatar Jul 21 '22 22:07 petr-tik

@rustbot claim

davidsemakula avatar Jan 14 '24 08:01 davidsemakula