Skip to content

Commit 81ab52c

Browse files
bors[bot]Veykril
andauthored
Merge #10109
10109: fix: Enable flyimport for ident patterns r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 1b8a294 + 40a2fae commit 81ab52c

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed

crates/ide_completion/src/completions/flyimport.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ pub(crate) fn position_for_import<'a>(
163163
import_candidate: Option<&ImportCandidate>,
164164
) -> Option<&'a SyntaxNode> {
165165
Some(match import_candidate {
166-
Some(ImportCandidate::Path(_)) => ctx.name_ref_syntax.as_ref()?.syntax(),
166+
Some(ImportCandidate::Path(_)) => ctx.name_syntax.as_ref()?.syntax(),
167167
Some(ImportCandidate::TraitAssocItem(_)) => ctx.path_qual()?.syntax(),
168168
Some(ImportCandidate::TraitMethod(_)) => ctx.dot_receiver()?.syntax(),
169169
None => ctx
170-
.name_ref_syntax
170+
.name_syntax
171171
.as_ref()
172172
.map(|name_ref| name_ref.syntax())
173173
.or_else(|| ctx.path_qual().map(|path| path.syntax()))
@@ -1203,4 +1203,21 @@ mod mud {
12031203
"#]],
12041204
);
12051205
}
1206+
1207+
#[test]
1208+
fn flyimport_pattern() {
1209+
check(
1210+
r#"
1211+
mod module {
1212+
pub struct Struct;
1213+
}
1214+
fn function() {
1215+
let Str$0
1216+
}
1217+
"#,
1218+
expect![[r#"
1219+
st Struct (use module::Struct)
1220+
"#]],
1221+
);
1222+
}
12061223
}

crates/ide_completion/src/completions/lifetime.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//! show up for normal completions, or they won't show completions other than lifetimes depending
99
//! on the fixture input.
1010
use hir::ScopeDef;
11+
use syntax::ast;
1112

1213
use crate::{completions::Completions, context::CompletionContext};
1314

@@ -17,17 +18,15 @@ pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext)
1718
return;
1819
}
1920
let lp_string;
20-
let param_lifetime = match (
21-
&ctx.lifetime_syntax,
22-
ctx.lifetime_param_syntax.as_ref().and_then(|lp| lp.lifetime()),
23-
) {
24-
(Some(lt), Some(lp)) if lp == lt.clone() => return,
25-
(Some(_), Some(lp)) => {
26-
lp_string = lp.to_string();
27-
Some(&*lp_string)
28-
}
29-
_ => None,
30-
};
21+
let param_lifetime =
22+
match (&ctx.name_syntax, ctx.lifetime_param_syntax.as_ref().and_then(|lp| lp.lifetime())) {
23+
(Some(ast::NameLike::Lifetime(lt)), Some(lp)) if lp == lt.clone() => return,
24+
(Some(_), Some(lp)) => {
25+
lp_string = lp.to_string();
26+
Some(&*lp_string)
27+
}
28+
_ => None,
29+
};
3130

3231
ctx.scope.process_all_names(&mut |name, res| {
3332
if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {

crates/ide_completion/src/completions/qualified_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
9292
for (name, def) in module_scope {
9393
if ctx.in_use_tree() {
9494
if let hir::ScopeDef::Unknown = def {
95-
if let Some(name_ref) = ctx.name_ref_syntax.as_ref() {
95+
if let Some(ast::NameLike::NameRef(name_ref)) = ctx.name_syntax.as_ref() {
9696
if name_ref.syntax().text() == name.to_string().as_str() {
9797
// for `use self::foo$0`, don't suggest `foo` as a completion
9898
cov_mark::hit!(dont_complete_current_use);

crates/ide_completion/src/context.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,9 @@ pub(crate) struct CompletionContext<'a> {
9393
pub(super) function_def: Option<ast::Fn>,
9494
/// The parent impl of the cursor position if it exists.
9595
pub(super) impl_def: Option<ast::Impl>,
96-
pub(super) name_ref_syntax: Option<ast::NameRef>,
96+
pub(super) name_syntax: Option<ast::NameLike>,
9797

9898
// potentially set if we are completing a lifetime
99-
pub(super) lifetime_syntax: Option<ast::Lifetime>,
10099
pub(super) lifetime_param_syntax: Option<ast::LifetimeParam>,
101100
pub(super) lifetime_allowed: bool,
102101
pub(super) is_label_ref: bool,
@@ -161,8 +160,7 @@ impl<'a> CompletionContext<'a> {
161160
expected_type: None,
162161
function_def: None,
163162
impl_def: None,
164-
name_ref_syntax: None,
165-
lifetime_syntax: None,
163+
name_syntax: None,
166164
lifetime_param_syntax: None,
167165
lifetime_allowed: false,
168166
is_label_ref: false,
@@ -601,6 +599,8 @@ impl<'a> CompletionContext<'a> {
601599
self.completion_location =
602600
determine_location(&self.sema, original_file, offset, &name_like);
603601
self.prev_sibling = determine_prev_sibling(&name_like);
602+
self.name_syntax =
603+
find_node_at_offset(original_file, name_like.syntax().text_range().start());
604604
match name_like {
605605
ast::NameLike::Lifetime(lifetime) => {
606606
self.classify_lifetime(original_file, lifetime, offset);
@@ -620,8 +620,6 @@ impl<'a> CompletionContext<'a> {
620620
lifetime: ast::Lifetime,
621621
offset: TextSize,
622622
) {
623-
self.lifetime_syntax =
624-
find_node_at_offset(original_file, lifetime.syntax().text_range().start());
625623
if let Some(parent) = lifetime.syntax().parent() {
626624
if parent.kind() == ERROR {
627625
return;
@@ -695,9 +693,6 @@ impl<'a> CompletionContext<'a> {
695693
fn classify_name_ref(&mut self, original_file: &SyntaxNode, name_ref: ast::NameRef) {
696694
self.fill_impl_def();
697695

698-
self.name_ref_syntax =
699-
find_node_at_offset(original_file, name_ref.syntax().text_range().start());
700-
701696
self.function_def = self
702697
.sema
703698
.token_ancestors_with_macros(self.token.clone())

0 commit comments

Comments
 (0)