mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
Properly resolve completion edits for empty input
This commit is contained in:
parent
09c11054a1
commit
d1ac3293f4
2 changed files with 46 additions and 35 deletions
|
@ -85,7 +85,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
|
||||||
let user_input_lowercased = potential_import_name.to_lowercase();
|
let user_input_lowercased = potential_import_name.to_lowercase();
|
||||||
let import_assets = import_assets(ctx, potential_import_name)?;
|
let import_assets = import_assets(ctx, potential_import_name)?;
|
||||||
let import_scope = ImportScope::find_insert_use_container(
|
let import_scope = ImportScope::find_insert_use_container(
|
||||||
position_for_import(ctx, import_assets.import_candidate())?,
|
position_for_import(ctx, Some(import_assets.import_candidate()))?,
|
||||||
&ctx.sema,
|
&ctx.sema,
|
||||||
)?;
|
)?;
|
||||||
let mut all_mod_paths = import_assets
|
let mut all_mod_paths = import_assets
|
||||||
|
@ -122,14 +122,20 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
|
||||||
Some(())
|
Some(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn position_for_import<'a>(
|
pub(crate) fn position_for_import<'a>(
|
||||||
ctx: &'a CompletionContext,
|
ctx: &'a CompletionContext,
|
||||||
import_candidate: &ImportCandidate,
|
import_candidate: Option<&ImportCandidate>,
|
||||||
) -> Option<&'a SyntaxNode> {
|
) -> Option<&'a SyntaxNode> {
|
||||||
Some(match import_candidate {
|
Some(match import_candidate {
|
||||||
ImportCandidate::Path(_) => ctx.name_ref_syntax.as_ref()?.syntax(),
|
Some(ImportCandidate::Path(_)) => ctx.name_ref_syntax.as_ref()?.syntax(),
|
||||||
ImportCandidate::TraitAssocItem(_) => ctx.path_qual.as_ref()?.syntax(),
|
Some(ImportCandidate::TraitAssocItem(_)) => ctx.path_qual.as_ref()?.syntax(),
|
||||||
ImportCandidate::TraitMethod(_) => ctx.dot_receiver.as_ref()?.syntax(),
|
Some(ImportCandidate::TraitMethod(_)) => ctx.dot_receiver.as_ref()?.syntax(),
|
||||||
|
None => ctx
|
||||||
|
.name_ref_syntax
|
||||||
|
.as_ref()
|
||||||
|
.map(|name_ref| name_ref.syntax())
|
||||||
|
.or_else(|| ctx.path_qual.as_ref().map(|path| path.syntax()))
|
||||||
|
.or_else(|| ctx.dot_receiver.as_ref().map(|expr| expr.syntax()))?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -565,7 +571,8 @@ fn main() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn blanket_trait_impl_import() {
|
fn blanket_trait_impl_import() {
|
||||||
check(
|
check_edit(
|
||||||
|
"another_function",
|
||||||
r#"
|
r#"
|
||||||
//- /lib.rs crate:dep
|
//- /lib.rs crate:dep
|
||||||
pub mod test_mod {
|
pub mod test_mod {
|
||||||
|
@ -583,9 +590,13 @@ fn main() {
|
||||||
dep::test_mod::TestStruct::ano$0
|
dep::test_mod::TestStruct::ano$0
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
r#"
|
||||||
fn another_function() (dep::test_mod::TestTrait) fn another_function()
|
use dep::test_mod::TestTrait;
|
||||||
"#]],
|
|
||||||
|
fn main() {
|
||||||
|
dep::test_mod::TestStruct::another_function()$0
|
||||||
|
}
|
||||||
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -593,8 +604,8 @@ fn main() {
|
||||||
fn zero_input_assoc_item_completion() {
|
fn zero_input_assoc_item_completion() {
|
||||||
check(
|
check(
|
||||||
r#"
|
r#"
|
||||||
//- /lib.rs crate:dep
|
//- /lib.rs crate:dep
|
||||||
pub mod test_mod {
|
pub mod test_mod {
|
||||||
pub trait TestTrait {
|
pub trait TestTrait {
|
||||||
const SPECIAL_CONST: u8;
|
const SPECIAL_CONST: u8;
|
||||||
type HumbleType;
|
type HumbleType;
|
||||||
|
@ -608,13 +619,13 @@ fn main() {
|
||||||
fn weird_function() {}
|
fn weird_function() {}
|
||||||
fn random_method(&self) {}
|
fn random_method(&self) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//- /main.rs crate:main deps:dep
|
//- /main.rs crate:main deps:dep
|
||||||
fn main() {
|
fn main() {
|
||||||
let test_struct = dep::test_mod::TestStruct {};
|
let test_struct = dep::test_mod::TestStruct {};
|
||||||
test_struct.$0
|
test_struct.$0
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
me random_method() (dep::test_mod::TestTrait) fn random_method(&self)
|
me random_method() (dep::test_mod::TestTrait) fn random_method(&self)
|
||||||
|
|
|
@ -11,10 +11,10 @@ mod render;
|
||||||
|
|
||||||
mod completions;
|
mod completions;
|
||||||
|
|
||||||
|
use completions::flyimport::position_for_import;
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
base_db::FilePosition, helpers::insert_use::ImportScope, imports_locator, RootDatabase,
|
base_db::FilePosition, helpers::insert_use::ImportScope, imports_locator, RootDatabase,
|
||||||
};
|
};
|
||||||
use syntax::AstNode;
|
|
||||||
use text_edit::TextEdit;
|
use text_edit::TextEdit;
|
||||||
|
|
||||||
use crate::{completions::Completions, context::CompletionContext, item::CompletionKind};
|
use crate::{completions::Completions, context::CompletionContext, item::CompletionKind};
|
||||||
|
@ -142,10 +142,10 @@ pub fn resolve_completion_edits(
|
||||||
import_for_trait_assoc_item: bool,
|
import_for_trait_assoc_item: bool,
|
||||||
) -> Option<Vec<TextEdit>> {
|
) -> Option<Vec<TextEdit>> {
|
||||||
let ctx = CompletionContext::new(db, position, config)?;
|
let ctx = CompletionContext::new(db, position, config)?;
|
||||||
let anchor = ctx.name_ref_syntax.as_ref()?;
|
let position_for_import = position_for_import(&ctx, None)?;
|
||||||
let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?;
|
let import_scope = ImportScope::find_insert_use_container(position_for_import, &ctx.sema)?;
|
||||||
|
|
||||||
let current_module = ctx.sema.scope(anchor.syntax()).module()?;
|
let current_module = ctx.sema.scope(position_for_import).module()?;
|
||||||
let current_crate = current_module.krate();
|
let current_crate = current_module.krate();
|
||||||
|
|
||||||
let import_path = imports_locator::find_exact_imports(&ctx.sema, current_crate, imported_name)
|
let import_path = imports_locator::find_exact_imports(&ctx.sema, current_crate, imported_name)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue