Skip to content

Commit d7a8bb0

Browse files
Merge pull request #21740 from ChayimFriedman2/prefer-underscore-import
feat: Allow crate authors to declare that their trait prefers to be imported `as _`
2 parents 4fc3cff + f42e02a commit d7a8bb0

12 files changed

Lines changed: 219 additions & 59 deletions

File tree

crates/hir-def/src/attrs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ fn match_attr_flags(attr_flags: &mut AttrFlags, attr: ast::Meta) -> ControlFlow<
258258
Some(second_segment) => match &*first_segment {
259259
"rust_analyzer" => match &*second_segment {
260260
"skip" => attr_flags.insert(AttrFlags::RUST_ANALYZER_SKIP),
261+
"prefer_underscore_import" => {
262+
attr_flags.insert(AttrFlags::PREFER_UNDERSCORE_IMPORT)
263+
}
261264
_ => {}
262265
},
263266
_ => {}
@@ -330,6 +333,8 @@ bitflags::bitflags! {
330333
const MACRO_STYLE_BRACES = 1 << 46;
331334
const MACRO_STYLE_BRACKETS = 1 << 47;
332335
const MACRO_STYLE_PARENTHESES = 1 << 48;
336+
337+
const PREFER_UNDERSCORE_IMPORT = 1 << 49;
333338
}
334339
}
335340

crates/hir/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3336,6 +3336,19 @@ impl Trait {
33363336
pub fn complete(self, db: &dyn HirDatabase) -> Complete {
33373337
Complete::extract(true, self.attrs(db).attrs)
33383338
}
3339+
3340+
// Feature: Prefer Underscore Import Attribute
3341+
// Crate authors can declare that their trait prefers to be imported `as _`. This can be used
3342+
// for example for extension traits. To do that, a trait has to include the attribute
3343+
// `#[rust_analyzer::prefer_underscore_import]`
3344+
//
3345+
// When a trait includes this attribute, flyimport will import it `as _`, and the quickfix
3346+
// to import it will prefer to import it `as _` (but allow to import it normally as well).
3347+
//
3348+
// Malformed attributes will be ignored without warnings.
3349+
pub fn prefer_underscore_import(self, db: &dyn HirDatabase) -> bool {
3350+
AttrFlags::query(db, self.id.into()).contains(AttrFlags::PREFER_UNDERSCORE_IMPORT)
3351+
}
33393352
}
33403353

33413354
impl HasVisibility for Trait {

crates/ide-assists/src/handlers/auto_import.rs

Lines changed: 108 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ide_db::{
66
active_parameter::ActiveParameter,
77
helpers::mod_path_to_ast,
88
imports::{
9-
import_assets::{ImportAssets, ImportCandidate, LocatedImport},
9+
import_assets::{ImportAssets, ImportCandidate, LocatedImport, TraitImportCandidate},
1010
insert_use::{ImportScope, insert_use, insert_use_as_alias},
1111
},
1212
};
@@ -123,44 +123,48 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
123123

124124
let (assist_id, import_name) =
125125
(AssistId::quick_fix("auto_import"), import_path.display(ctx.db(), edition));
126-
acc.add_group(
127-
&group_label,
128-
assist_id,
129-
format!("Import `{import_name}`"),
130-
range,
131-
|builder| {
126+
let add_normal_import = |acc: &mut Assists, label| {
127+
acc.add_group(&group_label, assist_id, label, range, |builder| {
132128
let scope = builder.make_import_scope_mut(scope.clone());
133129
insert_use(&scope, mod_path_to_ast(&import_path, edition), &ctx.config.insert_use);
134-
},
135-
);
136-
137-
match import_assets.import_candidate() {
138-
ImportCandidate::TraitAssocItem(name) | ImportCandidate::TraitMethod(name) => {
139-
let is_method =
140-
matches!(import_assets.import_candidate(), ImportCandidate::TraitMethod(_));
141-
let type_ = if is_method { "method" } else { "item" };
142-
let group_label = GroupLabel(format!(
143-
"Import a trait for {} {} by alias",
144-
type_,
145-
name.assoc_item_name.text()
146-
));
147-
acc.add_group(
148-
&group_label,
149-
assist_id,
150-
format!("Import `{import_name} as _`"),
151-
range,
152-
|builder| {
153-
let scope = builder.make_import_scope_mut(scope.clone());
154-
insert_use_as_alias(
155-
&scope,
156-
mod_path_to_ast(&import_path, edition),
157-
&ctx.config.insert_use,
158-
edition,
159-
);
160-
},
130+
})
131+
};
132+
let add_underscore_import = |acc: &mut Assists, name: &TraitImportCandidate<'_>, label| {
133+
let is_method =
134+
matches!(import_assets.import_candidate(), ImportCandidate::TraitMethod(_));
135+
let type_ = if is_method { "method" } else { "item" };
136+
let group_label = GroupLabel(format!(
137+
"Import a trait for {} {} by alias",
138+
type_,
139+
name.assoc_item_name.text()
140+
));
141+
acc.add_group(&group_label, assist_id, label, range, |builder| {
142+
let scope = builder.make_import_scope_mut(scope.clone());
143+
insert_use_as_alias(
144+
&scope,
145+
mod_path_to_ast(&import_path, edition),
146+
&ctx.config.insert_use,
147+
edition,
161148
);
162-
}
163-
_ => {}
149+
});
150+
};
151+
152+
if let ImportCandidate::TraitAssocItem(name) | ImportCandidate::TraitMethod(name) =
153+
import_assets.import_candidate()
154+
{
155+
if let hir::ItemInNs::Types(hir::ModuleDef::Trait(trait_to_import)) =
156+
import.item_to_import
157+
&& trait_to_import.prefer_underscore_import(ctx.db())
158+
{
159+
// Flip the order of the suggestions and show a preference for `as _` in the name.
160+
add_underscore_import(acc, name, format!("Import `{import_name}`"));
161+
add_normal_import(acc, format!("Import `{import_name}` without `as _`"));
162+
} else {
163+
add_normal_import(acc, format!("Import `{import_name}`"));
164+
add_underscore_import(acc, name, format!("Import `{import_name} as _`"));
165+
}
166+
} else {
167+
add_normal_import(acc, format!("Import `{import_name}`"));
164168
}
165169
}
166170
Some(())
@@ -1957,4 +1961,72 @@ fn main() {
19571961
"#,
19581962
);
19591963
}
1964+
1965+
#[test]
1966+
fn prefer_underscore_import() {
1967+
check_assist_by_label(
1968+
auto_import,
1969+
r#"
1970+
mod foo {
1971+
#[rust_analyzer::prefer_underscore_import]
1972+
pub trait Ext {
1973+
fn bar(&self) {}
1974+
}
1975+
impl<T> Ext for T {}
1976+
}
1977+
1978+
fn baz() {
1979+
1.b$0ar();
1980+
}
1981+
"#,
1982+
r#"
1983+
use foo::Ext as _;
1984+
1985+
mod foo {
1986+
#[rust_analyzer::prefer_underscore_import]
1987+
pub trait Ext {
1988+
fn bar(&self) {}
1989+
}
1990+
impl<T> Ext for T {}
1991+
}
1992+
1993+
fn baz() {
1994+
1.bar();
1995+
}
1996+
"#,
1997+
"Import `foo::Ext`",
1998+
);
1999+
check_assist_by_label(
2000+
auto_import,
2001+
r#"
2002+
mod foo {
2003+
#[rust_analyzer::prefer_underscore_import]
2004+
pub trait Ext {
2005+
fn bar(&self) {}
2006+
}
2007+
impl<T> Ext for T {}
2008+
}
2009+
2010+
fn baz() {
2011+
1.b$0ar();
2012+
}
2013+
"#,
2014+
r#"
2015+
use foo::Ext;
2016+
2017+
mod foo {
2018+
#[rust_analyzer::prefer_underscore_import]
2019+
pub trait Ext {
2020+
fn bar(&self) {}
2021+
}
2022+
impl<T> Ext for T {}
2023+
}
2024+
2025+
fn baz() {
2026+
1.bar();
2027+
}
2028+
"#,
2029+
"Import `foo::Ext` without `as _`",
2030+
);
2031+
}
19602032
}

crates/ide-completion/src/item.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,15 @@ pub struct CompletionItem {
8484
pub ref_match: Option<(CompletionItemRefMode, TextSize)>,
8585

8686
/// The import data to add to completion's edits.
87-
pub import_to_add: SmallVec<[String; 1]>,
87+
pub import_to_add: SmallVec<[CompletionItemImport; 1]>,
88+
}
89+
90+
#[derive(Clone, UpmapFromRaFixture)]
91+
pub struct CompletionItemImport {
92+
/// The path to import.
93+
pub path: String,
94+
/// Whether to import `as _`.
95+
pub as_underscore: bool,
8896
}
8997

9098
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
@@ -585,7 +593,18 @@ impl Builder {
585593
let import_to_add = self
586594
.imports_to_add
587595
.into_iter()
588-
.map(|import| import.import_path.display(db, self.edition).to_string())
596+
.map(|import| {
597+
let path = import.import_path.display(db, self.edition).to_string();
598+
let as_underscore =
599+
if let hir::ItemInNs::Types(hir::ModuleDef::Trait(trait_to_import)) =
600+
import.item_to_import
601+
{
602+
trait_to_import.prefer_underscore_import(db)
603+
} else {
604+
false
605+
};
606+
CompletionItemImport { path, as_underscore }
607+
})
589608
.collect();
590609

591610
CompletionItem {

crates/ide-completion/src/lib.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ use crate::{
3636
pub use crate::{
3737
config::{AutoImportExclusionType, CallableSnippets, CompletionConfig},
3838
item::{
39-
CompletionItem, CompletionItemKind, CompletionItemRefMode, CompletionRelevance,
40-
CompletionRelevancePostfixMatch, CompletionRelevanceReturnType,
39+
CompletionItem, CompletionItemImport, CompletionItemKind, CompletionItemRefMode,
40+
CompletionRelevance, CompletionRelevancePostfixMatch, CompletionRelevanceReturnType,
4141
CompletionRelevanceTypeMatch,
4242
},
4343
snippet::{Snippet, SnippetScope},
@@ -280,7 +280,7 @@ pub fn resolve_completion_edits(
280280
db: &RootDatabase,
281281
config: &CompletionConfig<'_>,
282282
FilePosition { file_id, offset }: FilePosition,
283-
imports: impl IntoIterator<Item = String>,
283+
imports: impl IntoIterator<Item = CompletionItemImport>,
284284
) -> Option<Vec<TextEdit>> {
285285
let _p = tracing::info_span!("resolve_completion_edits").entered();
286286
let sema = hir::Semantics::new(db);
@@ -299,12 +299,18 @@ pub fn resolve_completion_edits(
299299
let new_ast = scope.clone_for_update();
300300
let mut import_insert = TextEdit::builder();
301301

302-
imports.into_iter().for_each(|full_import_path| {
303-
insert_use::insert_use(
304-
&new_ast,
305-
make::path_from_text_with_edition(&full_import_path, current_edition),
306-
&config.insert_use,
307-
);
302+
imports.into_iter().for_each(|import| {
303+
let full_path = make::path_from_text_with_edition(&import.path, current_edition);
304+
if import.as_underscore {
305+
insert_use::insert_use_as_alias(
306+
&new_ast,
307+
full_path,
308+
&config.insert_use,
309+
current_edition,
310+
);
311+
} else {
312+
insert_use::insert_use(&new_ast, full_path, &config.insert_use);
313+
}
308314
});
309315

310316
diff(scope.as_syntax_node(), new_ast.as_syntax_node()).into_text_edit(&mut import_insert);

crates/ide-completion/src/tests/flyimport.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,3 +2057,38 @@ fn main() {
20572057
"#,
20582058
);
20592059
}
2060+
2061+
#[test]
2062+
fn prefer_underscore_import() {
2063+
check_edit(
2064+
"bar",
2065+
r#"
2066+
mod foo {
2067+
#[rust_analyzer::prefer_underscore_import]
2068+
pub trait Ext {
2069+
fn bar(&self) {}
2070+
}
2071+
impl<T> Ext for T {}
2072+
}
2073+
2074+
fn baz() {
2075+
1.bar$0
2076+
}
2077+
"#,
2078+
r#"
2079+
use foo::Ext as _;
2080+
2081+
mod foo {
2082+
#[rust_analyzer::prefer_underscore_import]
2083+
pub trait Ext {
2084+
fn bar(&self) {}
2085+
}
2086+
impl<T> Ext for T {}
2087+
}
2088+
2089+
fn baz() {
2090+
1.bar();$0
2091+
}
2092+
"#,
2093+
);
2094+
}

crates/ide/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ pub use ide_assists::{
128128
};
129129
pub use ide_completion::{
130130
CallableSnippets, CompletionConfig, CompletionFieldsToResolve, CompletionItem,
131-
CompletionItemKind, CompletionItemRefMode, CompletionRelevance, Snippet, SnippetScope,
131+
CompletionItemImport, CompletionItemKind, CompletionItemRefMode, CompletionRelevance, Snippet,
132+
SnippetScope,
132133
};
133134
pub use ide_db::{
134135
FileId, FilePosition, FileRange, RootDatabase, Severity, SymbolKind,
@@ -778,7 +779,7 @@ impl Analysis {
778779
&self,
779780
config: &CompletionConfig<'_>,
780781
position: FilePosition,
781-
imports: impl IntoIterator<Item = String> + std::panic::UnwindSafe,
782+
imports: impl IntoIterator<Item = CompletionItemImport> + std::panic::UnwindSafe,
782783
) -> Cancellable<Vec<TextEdit>> {
783784
Ok(self
784785
.with_db(|db| ide_completion::resolve_completion_edits(db, config, position, imports))?

crates/rust-analyzer/src/handlers/request.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use anyhow::Context;
77

88
use base64::{Engine, prelude::BASE64_STANDARD};
99
use ide::{
10-
AssistKind, AssistResolveStrategy, Cancellable, CompletionFieldsToResolve, FilePosition,
11-
FileRange, FileStructureConfig, FindAllRefsConfig, HoverAction, HoverGotoTypeData,
12-
InlayFieldsToResolve, Query, RangeInfo, Runnable, RunnableKind, SingleResolve, SourceChange,
13-
TextEdit,
10+
AssistKind, AssistResolveStrategy, Cancellable, CompletionFieldsToResolve,
11+
CompletionItemImport, FilePosition, FileRange, FileStructureConfig, FindAllRefsConfig,
12+
HoverAction, HoverGotoTypeData, InlayFieldsToResolve, Query, RangeInfo, Runnable, RunnableKind,
13+
SingleResolve, SourceChange, TextEdit,
1414
};
1515
use ide_db::{FxHashMap, SymbolKind};
1616
use itertools::Itertools;
@@ -1233,7 +1233,10 @@ pub(crate) fn handle_completion_resolve(
12331233
.resolve_completion_edits(
12341234
&forced_resolve_completions_config,
12351235
position,
1236-
resolve_data.imports.into_iter().map(|import| import.full_import_path),
1236+
resolve_data.imports.into_iter().map(|import| CompletionItemImport {
1237+
path: import.full_import_path,
1238+
as_underscore: import.as_underscore,
1239+
}),
12371240
)?
12381241
.into_iter()
12391242
.flat_map(|edit| edit.into_iter().map(|indel| to_proto::text_edit(&line_index, indel)))

crates/rust-analyzer/src/lsp.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use core::fmt;
44

55
use hir::Mutability;
6-
use ide::{CompletionItem, CompletionItemRefMode, CompletionRelevance};
6+
use ide::{CompletionItem, CompletionItemImport, CompletionItemRefMode, CompletionRelevance};
77
use tenthash::TentHash;
88

99
pub mod ext;
@@ -136,8 +136,10 @@ pub(crate) fn completion_item_hash(item: &CompletionItem, is_ref_completion: boo
136136

137137
hasher.update(item.import_to_add.len().to_ne_bytes());
138138
for import_path in &item.import_to_add {
139-
hasher.update(import_path.len().to_ne_bytes());
140-
hasher.update(import_path);
139+
let CompletionItemImport { path, as_underscore } = import_path;
140+
hasher.update(path.len().to_ne_bytes());
141+
hasher.update(path);
142+
hasher.update([u8::from(*as_underscore)]);
141143
}
142144

143145
hasher.finalize()

0 commit comments

Comments
 (0)