From 766813898f7901736d82bfc103874474177e7aca Mon Sep 17 00:00:00 2001 From: Andrea Pretto Date: Tue, 19 Feb 2019 22:32:00 +0100 Subject: auto_import: make auto import working with target as a list of SmolStr instead of ast::Path --- crates/ra_hir/src/name.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs index 283f37845..331da6027 100644 --- a/crates/ra_hir/src/name.rs +++ b/crates/ra_hir/src/name.rs @@ -46,6 +46,10 @@ impl Name { Name::new(idx.to_string().into()) } + pub fn to_smolstr(&self) -> SmolStr { + self.text.clone() + } + pub(crate) fn as_known_name(&self) -> Option { let name = match self.text.as_str() { "isize" => KnownName::Isize, -- cgit v1.2.3 From 914421495835380b96e0016763fda6eff31a8179 Mon Sep 17 00:00:00 2001 From: Andrea Pretto Date: Mon, 15 Apr 2019 16:11:32 +0200 Subject: complete_import: add new import resolver infrastructure with some hardcoded importable name. Changes complete_scope to support that. --- crates/ra_hir/src/lib.rs | 2 +- crates/ra_hir/src/resolve.rs | 65 ++++++++++++++++++++++++++++++++++++++ crates/ra_hir/src/source_binder.rs | 19 ++++++++++- 3 files changed, 84 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 4411715de..f156e3f07 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -52,7 +52,7 @@ use crate::{ db::{HirDatabase, DefDatabase}, name::{AsName, KnownName}, source_id::{FileAstId, AstId}, - resolve::Resolver, + resolve::Resolver, resolve::ImportResolver, }; pub use self::{ diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index f2c85eb66..0f866e6c2 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -3,6 +3,8 @@ use std::sync::Arc; use rustc_hash::FxHashMap; +use ra_syntax::SmolStr; + use crate::{ ModuleDef, code_model_api::Crate, @@ -12,8 +14,12 @@ use crate::{ generics::GenericParams, expr::{scope::{ExprScopes, ScopeId}, PatId}, impl_block::ImplBlock, +<<<<<<< HEAD path::Path, Trait +======= + path::Path, Trait, +>>>>>>> complete_import: add new import resolver infrastructure with some hardcoded importable name. }; #[derive(Debug, Clone, Default)] @@ -21,6 +27,12 @@ pub(crate) struct Resolver { scopes: Vec, } +#[derive(Debug, Clone, Default)] +pub(crate) struct ImportResolver { + // todo: use fst crate or something like that + dummy_names: Vec<(SmolStr, Vec)>, +} + // FIXME how to store these best #[derive(Debug, Clone)] pub(crate) struct ModuleItemMap { @@ -309,3 +321,56 @@ impl Scope { } } } + +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("Debug"), + vec![SmolStr::new("std"), SmolStr::new("fmt"), SmolStr::new("Debug")], + ), + ( + SmolStr::new("Display"), + vec![SmolStr::new("std"), SmolStr::new("fmt"), SmolStr::new("Display")], + ), + ( + SmolStr::new("Hash"), + vec![SmolStr::new("std"), SmolStr::new("hash"), SmolStr::new("Hash")], + ), + ( + SmolStr::new("Hasher"), + vec![SmolStr::new("std"), SmolStr::new("hash"), SmolStr::new("Hasher")], + ), + ( + SmolStr::new("Iterator"), + vec![SmolStr::new("std"), SmolStr::new("iter"), SmolStr::new("Iterator")], + ), + ]; + + ImportResolver { dummy_names } + } + + // 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, + _db: &impl HirDatabase, + name: &Name, + ) -> FxHashMap> { + let name = name.to_smolstr(); + if name.len() > 1 { + self.dummy_names + .iter() + .filter(|(n, _)| n.as_str().contains(name.as_str())) + .cloned() + .collect() + } else { + FxHashMap::default() + } + } +} diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index f1bb13bc6..a6f0ab289 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -14,14 +14,19 @@ use ra_syntax::{ ast::{self, AstNode, NameOwner}, algo::find_node_at_offset, SyntaxKind::*, + SmolStr, }; use crate::{ HirDatabase, Function, Struct, Enum, Const, Static, Either, DefWithBody, PerNs, Name, +<<<<<<< HEAD AsName, Module, HirFileId, Crate, Trait, Resolver, Ty, +======= + AsName, Module, HirFileId, Crate, Trait, Resolver, ImportResolver, +>>>>>>> complete_import: add new import resolver infrastructure with some hardcoded importable name. expr::{BodySourceMap, scope::{ScopeId, ExprScopes}}, ids::LocationCtx, - expr, AstId + expr, AstId, }; /// Locates the module by `FileId`. Picks topmost module in the file. @@ -170,6 +175,7 @@ fn def_with_body_from_child_node( #[derive(Debug)] pub struct SourceAnalyzer { resolver: Resolver, + import_resolver: ImportResolver, body_source_map: Option>, infer: Option>, scopes: Option>, @@ -217,6 +223,7 @@ impl SourceAnalyzer { offset: Option, ) -> SourceAnalyzer { let def_with_body = def_with_body_from_child_node(db, file_id, node); + let import_resolver = ImportResolver::new(); if let Some(def) = def_with_body { let source_map = def.body_source_map(db); let scopes = db.expr_scopes(def); @@ -227,6 +234,7 @@ impl SourceAnalyzer { let resolver = expr::resolver_for_scope(def.body(db), db, scope); SourceAnalyzer { resolver, + import_resolver, body_source_map: Some(source_map), infer: Some(def.infer(db)), scopes: Some(scopes), @@ -237,6 +245,7 @@ impl SourceAnalyzer { .ancestors() .find_map(|node| try_get_resolver_for_node(db, file_id, node)) .unwrap_or_default(), + import_resolver, body_source_map: None, infer: None, scopes: None, @@ -323,6 +332,14 @@ impl SourceAnalyzer { self.resolver.all_names(db) } + pub fn all_import_names( + &self, + db: &impl HirDatabase, + name: &Name, + ) -> FxHashMap> { + self.import_resolver.all_names(db, name) + } + pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec { // FIXME: at least, this should work with any DefWithBody, but ideally // this should be hir-based altogether -- cgit v1.2.3 From 200032852be0c66b978c875a8edf0eca1f08b901 Mon Sep 17 00:00:00 2001 From: Andrea Pretto Date: Sun, 21 Apr 2019 23:55:47 +0200 Subject: complete_import: prevent panic when the anchor is the completion source range (fix rebase mess) Please enter the commit message for your changes. Lines starting --- crates/ra_hir/src/resolve.rs | 4 ---- crates/ra_hir/src/source_binder.rs | 6 +----- 2 files changed, 1 insertion(+), 9 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index 0f866e6c2..ce80be17f 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -14,12 +14,8 @@ use crate::{ generics::GenericParams, expr::{scope::{ExprScopes, ScopeId}, PatId}, impl_block::ImplBlock, -<<<<<<< HEAD path::Path, Trait -======= - path::Path, Trait, ->>>>>>> complete_import: add new import resolver infrastructure with some hardcoded importable name. }; #[derive(Debug, Clone, Default)] diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index a6f0ab289..42399622a 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -19,11 +19,7 @@ use ra_syntax::{ use crate::{ HirDatabase, Function, Struct, Enum, Const, Static, Either, DefWithBody, PerNs, Name, -<<<<<<< HEAD - AsName, Module, HirFileId, Crate, Trait, Resolver, Ty, -======= - AsName, Module, HirFileId, Crate, Trait, Resolver, ImportResolver, ->>>>>>> complete_import: add new import resolver infrastructure with some hardcoded importable name. + AsName, Module, HirFileId, Crate, Trait, Resolver, Ty, ImportResolver, expr::{BodySourceMap, scope::{ScopeId, ExprScopes}}, ids::LocationCtx, expr, AstId, -- cgit v1.2.3 From e01052d1f0b8bf70a418a10538528923c5f400d5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 22 Apr 2019 15:56:28 +0300 Subject: move auto-imoprter into IDE auto-import is purely an IDE concern, so it should be done outside of HIR --- crates/ra_hir/src/lib.rs | 2 +- crates/ra_hir/src/name.rs | 13 +++++++-- crates/ra_hir/src/resolve.rs | 59 -------------------------------------- crates/ra_hir/src/source_binder.rs | 15 +--------- 4 files changed, 13 insertions(+), 76 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index f156e3f07..4411715de 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -52,7 +52,7 @@ use crate::{ db::{HirDatabase, DefDatabase}, name::{AsName, KnownName}, source_id::{FileAstId, AstId}, - resolve::Resolver, resolve::ImportResolver, + resolve::Resolver, }; pub use self::{ diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs index 331da6027..9a999e66c 100644 --- a/crates/ra_hir/src/name.rs +++ b/crates/ra_hir/src/name.rs @@ -46,8 +46,17 @@ impl Name { Name::new(idx.to_string().into()) } - pub fn to_smolstr(&self) -> SmolStr { - self.text.clone() + // There's should be no way to extract a string out of `Name`: `Name` in the + // future, `Name` will include hygiene information, and you can't encode + // hygiene into a String. + // + // If you need to compare something with `Name`, compare `Name`s directly. + // + // If you need to render `Name` for the user, use the `Display` impl, but be + // aware that it strips hygiene info. + #[deprecated(note = "use to_string instead")] + pub fn as_smolstr(&self) -> &SmolStr { + &self.text } pub(crate) fn as_known_name(&self) -> Option { diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index ce80be17f..bd0f074c1 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -23,12 +23,6 @@ pub(crate) struct Resolver { scopes: Vec, } -#[derive(Debug, Clone, Default)] -pub(crate) struct ImportResolver { - // todo: use fst crate or something like that - dummy_names: Vec<(SmolStr, Vec)>, -} - // FIXME how to store these best #[derive(Debug, Clone)] pub(crate) struct ModuleItemMap { @@ -317,56 +311,3 @@ impl Scope { } } } - -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("Debug"), - vec![SmolStr::new("std"), SmolStr::new("fmt"), SmolStr::new("Debug")], - ), - ( - SmolStr::new("Display"), - vec![SmolStr::new("std"), SmolStr::new("fmt"), SmolStr::new("Display")], - ), - ( - SmolStr::new("Hash"), - vec![SmolStr::new("std"), SmolStr::new("hash"), SmolStr::new("Hash")], - ), - ( - SmolStr::new("Hasher"), - vec![SmolStr::new("std"), SmolStr::new("hash"), SmolStr::new("Hasher")], - ), - ( - SmolStr::new("Iterator"), - vec![SmolStr::new("std"), SmolStr::new("iter"), SmolStr::new("Iterator")], - ), - ]; - - ImportResolver { dummy_names } - } - - // 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, - _db: &impl HirDatabase, - name: &Name, - ) -> FxHashMap> { - let name = name.to_smolstr(); - if name.len() > 1 { - self.dummy_names - .iter() - .filter(|(n, _)| n.as_str().contains(name.as_str())) - .cloned() - .collect() - } else { - FxHashMap::default() - } - } -} diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 42399622a..2959e3eca 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -14,12 +14,11 @@ use ra_syntax::{ ast::{self, AstNode, NameOwner}, algo::find_node_at_offset, SyntaxKind::*, - SmolStr, }; use crate::{ HirDatabase, Function, Struct, Enum, Const, Static, Either, DefWithBody, PerNs, Name, - AsName, Module, HirFileId, Crate, Trait, Resolver, Ty, ImportResolver, + AsName, Module, HirFileId, Crate, Trait, Resolver, Ty, expr::{BodySourceMap, scope::{ScopeId, ExprScopes}}, ids::LocationCtx, expr, AstId, @@ -171,7 +170,6 @@ fn def_with_body_from_child_node( #[derive(Debug)] pub struct SourceAnalyzer { resolver: Resolver, - import_resolver: ImportResolver, body_source_map: Option>, infer: Option>, scopes: Option>, @@ -219,7 +217,6 @@ impl SourceAnalyzer { offset: Option, ) -> SourceAnalyzer { let def_with_body = def_with_body_from_child_node(db, file_id, node); - let import_resolver = ImportResolver::new(); if let Some(def) = def_with_body { let source_map = def.body_source_map(db); let scopes = db.expr_scopes(def); @@ -230,7 +227,6 @@ impl SourceAnalyzer { let resolver = expr::resolver_for_scope(def.body(db), db, scope); SourceAnalyzer { resolver, - import_resolver, body_source_map: Some(source_map), infer: Some(def.infer(db)), scopes: Some(scopes), @@ -241,7 +237,6 @@ impl SourceAnalyzer { .ancestors() .find_map(|node| try_get_resolver_for_node(db, file_id, node)) .unwrap_or_default(), - import_resolver, body_source_map: None, infer: None, scopes: None, @@ -328,14 +323,6 @@ impl SourceAnalyzer { self.resolver.all_names(db) } - pub fn all_import_names( - &self, - db: &impl HirDatabase, - name: &Name, - ) -> FxHashMap> { - self.import_resolver.all_names(db, name) - } - pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec { // FIXME: at least, this should work with any DefWithBody, but ideally // this should be hir-based altogether -- cgit v1.2.3 From aa1ef6ae9a9a61ab6ddc42d7987753df3aa67263 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 22 Apr 2019 16:18:14 +0300 Subject: unused import --- crates/ra_hir/src/resolve.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index bd0f074c1..f2c85eb66 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -3,8 +3,6 @@ use std::sync::Arc; use rustc_hash::FxHashMap; -use ra_syntax::SmolStr; - use crate::{ ModuleDef, code_model_api::Crate, -- cgit v1.2.3