From 6dae5cbb1190cde6a20aa1758c7d87e84933378e Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sun, 2 Feb 2020 14:06:51 +0200 Subject: Require ModPath for importing --- crates/ra_ide/src/completion/complete_scope.rs | 71 +++++++++++++++++--------- 1 file changed, 47 insertions(+), 24 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/completion/complete_scope.rs b/crates/ra_ide/src/completion/complete_scope.rs index 458d7525e..fe0795984 100644 --- a/crates/ra_ide/src/completion/complete_scope.rs +++ b/crates/ra_ide/src/completion/complete_scope.rs @@ -6,6 +6,7 @@ use ra_text_edit::TextEditBuilder; use rustc_hash::FxHashMap; use crate::completion::{CompletionContext, CompletionItem, CompletionKind, Completions}; +use hir::{ModPath, PathKind}; pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { if !ctx.is_trivial_path { @@ -54,58 +55,80 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { } } -fn build_import_label(name: &str, path: &[SmolStr]) -> String { +fn build_import_label(name: &str, path: &ModPath) -> String { let mut buf = String::with_capacity(64); buf.push_str(name); buf.push_str(" ("); - fmt_import_path(path, &mut buf); + buf.push_str(&path.to_string()); buf.push_str(")"); buf } -fn fmt_import_path(path: &[SmolStr], buf: &mut String) { - let mut segments = path.iter(); - if let Some(s) = segments.next() { - buf.push_str(&s); - } - for s in segments { - buf.push_str("::"); - buf.push_str(&s); - } -} - #[derive(Debug, Clone, Default)] pub(crate) struct ImportResolver { // todo: use fst crate or something like that - dummy_names: Vec<(SmolStr, Vec)>, + dummy_names: Vec<(SmolStr, ModPath)>, } impl ImportResolver { pub(crate) fn new() -> Self { let dummy_names = vec![ - (SmolStr::new("fmt"), vec![SmolStr::new("std"), SmolStr::new("fmt")]), - (SmolStr::new("io"), vec![SmolStr::new("std"), SmolStr::new("io")]), - (SmolStr::new("iter"), vec![SmolStr::new("std"), SmolStr::new("iter")]), - (SmolStr::new("hash"), vec![SmolStr::new("std"), SmolStr::new("hash")]), + ( + SmolStr::new("fmt"), + ModPath { kind: PathKind::Plain, segments: vec![hir::known::std, hir::known::fmt] }, + ), + ( + SmolStr::new("io"), + ModPath { kind: PathKind::Plain, segments: vec![hir::known::std, hir::known::io] }, + ), + ( + SmolStr::new("iter"), + ModPath { + kind: PathKind::Plain, + segments: vec![hir::known::std, hir::known::iter], + }, + ), + ( + SmolStr::new("hash"), + ModPath { + kind: PathKind::Plain, + segments: vec![hir::known::std, hir::known::hash], + }, + ), ( SmolStr::new("Debug"), - vec![SmolStr::new("std"), SmolStr::new("fmt"), SmolStr::new("Debug")], + ModPath { + kind: PathKind::Plain, + segments: vec![hir::known::std, hir::known::fmt, hir::known::Debug], + }, ), ( SmolStr::new("Display"), - vec![SmolStr::new("std"), SmolStr::new("fmt"), SmolStr::new("Display")], + ModPath { + kind: PathKind::Plain, + segments: vec![hir::known::std, hir::known::fmt, hir::known::Display], + }, ), ( SmolStr::new("Hash"), - vec![SmolStr::new("std"), SmolStr::new("hash"), SmolStr::new("Hash")], + ModPath { + kind: PathKind::Plain, + segments: vec![hir::known::std, hir::known::hash, hir::known::Hash], + }, ), ( SmolStr::new("Hasher"), - vec![SmolStr::new("std"), SmolStr::new("hash"), SmolStr::new("Hasher")], + ModPath { + kind: PathKind::Plain, + segments: vec![hir::known::std, hir::known::hash, hir::known::Hasher], + }, ), ( SmolStr::new("Iterator"), - vec![SmolStr::new("std"), SmolStr::new("iter"), SmolStr::new("Iterator")], + ModPath { + kind: PathKind::Plain, + segments: vec![hir::known::std, hir::known::iter, hir::known::Iterator], + }, ), ]; @@ -115,7 +138,7 @@ impl ImportResolver { // Returns a map of importable items filtered by name. // The map associates item name with its full path. // todo: should return Resolutions - pub(crate) fn all_names(&self, name: &str) -> FxHashMap> { + pub(crate) fn all_names(&self, name: &str) -> FxHashMap { if name.len() > 1 { self.dummy_names.iter().filter(|(n, _)| n.contains(name)).cloned().collect() } else { -- cgit v1.2.3