Use the new Resolver API in completion

This commit is contained in:
Florian Diebold 2019-01-27 20:50:57 +01:00
parent 6b076f1931
commit 33ff7b56ff
11 changed files with 190 additions and 106 deletions

View file

@ -7,13 +7,13 @@ use crate::{
use hir::Docs;
pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
let (path, module) = match (&ctx.path_prefix, &ctx.module) {
(Some(path), Some(module)) => (path.clone(), module),
let path = match &ctx.path_prefix {
Some(path) => path.clone(),
_ => return,
};
let def_id = match module.resolve_path(ctx.db, &path).take_types() {
Some(it) => it,
None => return,
let def = match ctx.resolver.resolve_path(ctx.db, &path).take_types() {
Some(Resolution::Def { def }) => def,
_ => return,
};
match def_id {
hir::ModuleDef::Module(module) => {
@ -24,7 +24,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
ctx.source_range(),
name.to_string(),
)
.from_resolution(ctx, res)
.from_resolution(ctx, &res.def.map(|def| hir::Resolution::Def { def }))
.add_to(acc);
}
}

View file

@ -1,61 +1,32 @@
use rustc_hash::FxHashSet;
use ra_syntax::ast::AstNode;
use crate::completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext};
use crate::completion::{CompletionItem, Completions, CompletionKind, CompletionContext};
pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
if !ctx.is_trivial_path {
return;
}
let module = match &ctx.module {
Some(it) => it,
None => return,
};
if let Some(function) = &ctx.function {
let scopes = function.scopes(ctx.db);
complete_fn(acc, &scopes, ctx);
}
let names = ctx.resolver.all_names();
let module_scope = module.scope(ctx.db);
module_scope
.entries()
.filter(|(_name, res)| {
// For cases like `use self::foo<|>` don't suggest foo itself.
match res.import {
None => true,
Some(import) => {
let source = module.import_source(ctx.db, import);
!source.syntax().range().is_subrange(&ctx.leaf.range())
}
}
})
// let module_scope = module.scope(ctx.db);
names
.into_iter()
// FIXME check tests
// .filter(|(_name, res)| {
// // For cases like `use self::foo<|>` don't suggest foo itself.
// match res.import {
// None => true,
// Some(import) => {
// let source = module.import_source(ctx.db, import);
// !source.syntax().range().is_subrange(&ctx.leaf.range())
// }
// }
// })
.for_each(|(name, res)| {
CompletionItem::new(
CompletionKind::Reference,
ctx.source_range(),
name.to_string(),
)
.from_resolution(ctx, res)
.add_to(acc)
});
}
fn complete_fn(
acc: &mut Completions,
scopes: &hir::ScopesWithSyntaxMapping,
ctx: &CompletionContext,
) {
let mut shadowed = FxHashSet::default();
scopes
.scope_chain_for_offset(ctx.offset)
.flat_map(|scope| scopes.scopes.entries(scope).iter())
.filter(|entry| shadowed.insert(entry.name()))
.for_each(|entry| {
CompletionItem::new(
CompletionKind::Reference,
ctx.source_range(),
entry.name().to_string(),
)
.kind(CompletionItemKind::Binding)
.from_resolution(ctx, &res)
.add_to(acc)
});
}

View file

@ -5,7 +5,7 @@ use ra_syntax::{
algo::{find_leaf_at_offset, find_covering_node, find_node_at_offset},
SyntaxKind::*,
};
use hir::source_binder;
use hir::{source_binder, Resolver};
use crate::{db, FilePosition};
@ -16,6 +16,7 @@ pub(crate) struct CompletionContext<'a> {
pub(super) db: &'a db::RootDatabase,
pub(super) offset: TextUnit,
pub(super) leaf: &'a SyntaxNode,
pub(super) resolver: Resolver<'static>,
pub(super) module: Option<hir::Module>,
pub(super) function: Option<hir::Function>,
pub(super) function_syntax: Option<&'a ast::FnDef>,
@ -42,12 +43,14 @@ impl<'a> CompletionContext<'a> {
original_file: &'a SourceFile,
position: FilePosition,
) -> Option<CompletionContext<'a>> {
let resolver = source_binder::resolver_for_position(db, position);
let module = source_binder::module_from_position(db, position);
let leaf = find_leaf_at_offset(original_file.syntax(), position.offset).left_biased()?;
let mut ctx = CompletionContext {
db,
leaf,
offset: position.offset,
resolver,
module,
function: None,
function_syntax: None,

View file

@ -1,5 +1,7 @@
use hir::{Docs, Documentation};
use ra_syntax::TextRange;
use hir::{Docs, Documentation, PerNs, Resolution};
use ra_syntax::{
TextRange,
};
use ra_text_edit::TextEdit;
use test_utils::tested_by;
@ -48,6 +50,7 @@ pub enum CompletionItemKind {
Trait,
TypeAlias,
Method,
TypeParam,
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
@ -207,23 +210,38 @@ impl Builder {
pub(super) fn from_resolution(
mut self,
ctx: &CompletionContext,
resolution: &hir::Resolution,
resolution: &PerNs<Resolution>,
) -> Builder {
let def = resolution.def.take_types().or(resolution.def.take_values());
use hir::ModuleDef::*;
let def = resolution
.as_ref()
.take_types()
.or(resolution.as_ref().take_values());
let def = match def {
None => return self,
Some(it) => it,
};
let (kind, docs) = match def {
hir::ModuleDef::Module(it) => (CompletionItemKind::Module, it.docs(ctx.db)),
hir::ModuleDef::Function(func) => return self.from_function(ctx, func),
hir::ModuleDef::Struct(it) => (CompletionItemKind::Struct, it.docs(ctx.db)),
hir::ModuleDef::Enum(it) => (CompletionItemKind::Enum, it.docs(ctx.db)),
hir::ModuleDef::EnumVariant(it) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)),
hir::ModuleDef::Const(it) => (CompletionItemKind::Const, it.docs(ctx.db)),
hir::ModuleDef::Static(it) => (CompletionItemKind::Static, it.docs(ctx.db)),
hir::ModuleDef::Trait(it) => (CompletionItemKind::Trait, it.docs(ctx.db)),
hir::ModuleDef::Type(it) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)),
Resolution::Def { def: Module(it) } => (CompletionItemKind::Module, it.docs(ctx.db)),
Resolution::Def {
def: Function(func),
} => return self.from_function(ctx, *func),
Resolution::Def { def: Struct(it) } => (CompletionItemKind::Struct, it.docs(ctx.db)),
Resolution::Def { def: Enum(it) } => (CompletionItemKind::Enum, it.docs(ctx.db)),
Resolution::Def {
def: EnumVariant(it),
} => (CompletionItemKind::EnumVariant, it.docs(ctx.db)),
Resolution::Def { def: Const(it) } => (CompletionItemKind::Const, it.docs(ctx.db)),
Resolution::Def { def: Static(it) } => (CompletionItemKind::Static, it.docs(ctx.db)),
Resolution::Def { def: Trait(it) } => (CompletionItemKind::Trait, it.docs(ctx.db)),
Resolution::Def { def: Type(it) } => (CompletionItemKind::TypeAlias, it.docs(ctx.db)),
Resolution::GenericParam { .. } => (CompletionItemKind::TypeParam, None),
Resolution::LocalBinding { .. } => (CompletionItemKind::Binding, None),
Resolution::SelfType { .. } => (
CompletionItemKind::TypeParam, // (does this need its own kind?)
None,
),
};
self.kind = Some(kind);
self.documentation = docs;

View file

@ -1,10 +1,24 @@
---
created: "2019-01-23T05:27:32.422259+00:00"
creator: insta@0.4.0
created: "2019-01-27T20:17:10.051725945+00:00"
creator: insta@0.5.2
expression: kind_completions
source: crates/ra_ide_api/src/completion/completion_item.rs
---
[
CompletionItem {
completion_kind: Reference,
label: "Self",
kind: Some(
TypeParam
),
detail: None,
documentation: None,
lookup: None,
insert_text: None,
insert_text_format: PlainText,
source_range: [25; 25),
text_edit: None
},
CompletionItem {
completion_kind: Reference,
label: "self",