mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
Expand target for autocompletion
This commit is contained in:
parent
a946970e2d
commit
0b838e3e23
14 changed files with 450 additions and 466 deletions
|
@ -17,7 +17,7 @@ use ide_db::{
|
|||
imports::import_assets::LocatedImport,
|
||||
RootDatabase, SnippetCap, SymbolKind,
|
||||
};
|
||||
use syntax::{ast, AstNode, SmolStr, SyntaxKind, TextRange};
|
||||
use syntax::{ast, format_smolstr, AstNode, SmolStr, SyntaxKind, TextRange};
|
||||
use text_edit::TextEdit;
|
||||
|
||||
use crate::{
|
||||
|
@ -272,9 +272,9 @@ pub(crate) fn render_resolution_with_import_pat(
|
|||
Some(render_resolution_pat(ctx, pattern_ctx, local_name, Some(import_edit), resolution))
|
||||
}
|
||||
|
||||
pub(crate) fn render_type_tree(
|
||||
pub(crate) fn render_expr(
|
||||
ctx: &CompletionContext<'_>,
|
||||
expr: &hir::term_search::TypeTree,
|
||||
expr: &hir::term_search::Expr,
|
||||
) -> Option<Builder> {
|
||||
let mut i = 1;
|
||||
let mut snippet_formatter = |ty: &hir::Type| {
|
||||
|
@ -292,31 +292,42 @@ pub(crate) fn render_type_tree(
|
|||
ty.as_adt()
|
||||
.and_then(|adt| adt.name(ctx.db).as_text())
|
||||
.map(|s| stdx::to_lower_snake_case(s.as_str()))
|
||||
.unwrap_or_else(|| String::from("_"))
|
||||
.unwrap_or_else(|| String::from("..."))
|
||||
};
|
||||
|
||||
let label = expr.gen_source_code(&ctx.scope, &mut label_formatter);
|
||||
|
||||
let source_range = match &ctx.expected_name {
|
||||
Some(name_or_ref) => name_or_ref.syntax().text_range(),
|
||||
None => match ctx.original_token.parent() {
|
||||
Some(node) => match node.ancestors().find_map(|n| ast::Path::cast(n)) {
|
||||
Some(path) => path.syntax().text_range(),
|
||||
None => node.text_range(),
|
||||
},
|
||||
None => ctx.source_range(),
|
||||
let source_range = match ctx.original_token.parent() {
|
||||
Some(node) => match node.ancestors().find_map(|n| ast::Path::cast(n)) {
|
||||
Some(path) => path.syntax().text_range(),
|
||||
None => node.text_range(),
|
||||
},
|
||||
None => ctx.source_range(),
|
||||
};
|
||||
|
||||
let mut item = CompletionItem::new(CompletionItemKind::Snippet, source_range, label);
|
||||
let mut item = CompletionItem::new(CompletionItemKind::Snippet, source_range, label.clone());
|
||||
|
||||
let snippet = format!("{}$0", expr.gen_source_code(&ctx.scope, &mut snippet_formatter));
|
||||
let edit = TextEdit::replace(source_range, snippet);
|
||||
item.snippet_edit(ctx.config.snippet_cap?, edit);
|
||||
item.documentation(Documentation::new(String::from("Autogenerated expression by term search")));
|
||||
item.set_relevance(crate::CompletionRelevance {
|
||||
type_match: Some(crate::item::CompletionRelevanceTypeMatch::CouldUnify),
|
||||
type_match: compute_type_match(ctx, &expr.ty(ctx.db)),
|
||||
..Default::default()
|
||||
});
|
||||
for trait_ in expr.traits_used(ctx.db) {
|
||||
let trait_item = hir::ItemInNs::from(hir::ModuleDef::from(trait_));
|
||||
let Some(path) = ctx.module.find_use_path(
|
||||
ctx.db,
|
||||
trait_item,
|
||||
ctx.config.prefer_no_std,
|
||||
ctx.config.prefer_prelude,
|
||||
) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
item.add_import(LocatedImport::new(path, trait_item, trait_item));
|
||||
}
|
||||
|
||||
Some(item)
|
||||
}
|
||||
|
@ -2243,6 +2254,8 @@ fn main() {
|
|||
&[CompletionItemKind::Snippet, CompletionItemKind::Method],
|
||||
expect![[r#"
|
||||
sn not [snippet]
|
||||
sn true [type]
|
||||
sn false [type]
|
||||
me not() (use ops::Not) [type_could_unify+requires_import]
|
||||
sn if []
|
||||
sn while []
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue