From 3ab1519cb27b927074ed7fbbb18a856e6e7fabb8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 23 Jan 2019 23:14:13 +0300 Subject: Change ids strategy this is a part of larghish hir refactoring which aims to * replace per-source-root module trees with per crate trees * switch from a monotyped DedId to type-specific ids --- crates/ra_ide_api/src/completion/complete_path.rs | 37 +++++++------ .../ra_ide_api/src/completion/completion_item.rs | 61 ++++++++-------------- 2 files changed, 43 insertions(+), 55 deletions(-) (limited to 'crates/ra_ide_api/src/completion') diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index e44b76c4a..0d7942496 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -13,8 +13,8 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { Some(it) => it, None => return, }; - match def_id.resolve(ctx.db) { - hir::Def::Module(module) => { + match def_id { + hir::ModuleDef::Module(module) => { let module_scope = module.scope(ctx.db); for (name, res) in module_scope.entries() { CompletionItem::new( @@ -26,21 +26,24 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { .add_to(acc); } } - hir::Def::Enum(e) => { - e.variants(ctx.db) - .into_iter() - .for_each(|(variant_name, variant)| { - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - variant_name.to_string(), - ) - .kind(CompletionItemKind::EnumVariant) - .set_documentation(variant.docs(ctx.db)) - .add_to(acc) - }); - } - _ => return, + + hir::ModuleDef::Def(def_id) => match def_id.resolve(ctx.db) { + hir::Def::Enum(e) => { + e.variants(ctx.db) + .into_iter() + .for_each(|(variant_name, variant)| { + CompletionItem::new( + CompletionKind::Reference, + ctx.source_range(), + variant_name.to_string(), + ) + .kind(CompletionItemKind::EnumVariant) + .set_documentation(variant.docs(ctx.db)) + .add_to(acc) + }); + } + _ => return, + }, }; } diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index 18c151932..5d6718a8d 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs @@ -1,6 +1,4 @@ -use hir::{Docs, Documentation, PerNs}; - -use crate::completion::completion_context::CompletionContext; +use hir::{Docs, Documentation}; use ra_syntax::{ ast::{self, AstNode}, TextRange, @@ -8,6 +6,8 @@ use ra_syntax::{ use ra_text_edit::TextEdit; use test_utils::tested_by; +use crate::completion::completion_context::CompletionContext; + /// `CompletionItem` describes a single completion variant in the editor pop-up. /// It is basically a POD with various properties. To construct a /// `CompletionItem`, use `new` method and the `Builder` struct. @@ -209,41 +209,26 @@ impl Builder { ctx: &CompletionContext, resolution: &hir::Resolution, ) -> Builder { - let resolved = resolution.def_id.map(|d| d.resolve(ctx.db)); - let (kind, docs) = match resolved { - PerNs { - types: Some(hir::Def::Module(..)), - .. - } => (CompletionItemKind::Module, None), - PerNs { - types: Some(hir::Def::Struct(s)), - .. - } => (CompletionItemKind::Struct, s.docs(ctx.db)), - PerNs { - types: Some(hir::Def::Enum(e)), - .. - } => (CompletionItemKind::Enum, e.docs(ctx.db)), - PerNs { - types: Some(hir::Def::Trait(t)), - .. - } => (CompletionItemKind::Trait, t.docs(ctx.db)), - PerNs { - types: Some(hir::Def::Type(t)), - .. - } => (CompletionItemKind::TypeAlias, t.docs(ctx.db)), - PerNs { - values: Some(hir::Def::Const(c)), - .. - } => (CompletionItemKind::Const, c.docs(ctx.db)), - PerNs { - values: Some(hir::Def::Static(s)), - .. - } => (CompletionItemKind::Static, s.docs(ctx.db)), - PerNs { - values: Some(hir::Def::Function(function)), - .. - } => return self.from_function(ctx, function), - _ => return self, + let def = resolution + .def_id + .take_types() + .or(resolution.def_id.take_values()); + let def = match def { + None => return self, + Some(it) => it, + }; + let (kind, docs) = match def { + hir::ModuleDef::Module(_) => (CompletionItemKind::Module, None), + hir::ModuleDef::Def(def_id) => match def_id.resolve(ctx.db) { + hir::Def::Struct(it) => (CompletionItemKind::Struct, it.docs(ctx.db)), + hir::Def::Enum(it) => (CompletionItemKind::Enum, it.docs(ctx.db)), + hir::Def::Trait(it) => (CompletionItemKind::Trait, it.docs(ctx.db)), + hir::Def::Type(it) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)), + hir::Def::Const(it) => (CompletionItemKind::Const, it.docs(ctx.db)), + hir::Def::Static(it) => (CompletionItemKind::Static, it.docs(ctx.db)), + hir::Def::Function(function) => return self.from_function(ctx, function), + _ => return self, + }, }; self.kind = Some(kind); self.documentation = docs; -- cgit v1.2.3