From d659b7a2f03788eb0f4f15e3730bbf65a18ed818 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 27 Nov 2018 21:45:42 +0300 Subject: start descriptors -> hir rename --- crates/ra_analysis/src/completion/mod.rs | 2 +- .../src/completion/reference_completion.rs | 2 +- crates/ra_analysis/src/db.rs | 20 +- crates/ra_analysis/src/descriptors/function/imp.rs | 21 - crates/ra_analysis/src/descriptors/function/mod.rs | 137 ----- .../ra_analysis/src/descriptors/function/scope.rs | 424 ---------------- crates/ra_analysis/src/descriptors/mod.rs | 137 ----- crates/ra_analysis/src/descriptors/module/imp.rs | 229 --------- crates/ra_analysis/src/descriptors/module/mod.rs | 378 -------------- .../ra_analysis/src/descriptors/module/nameres.rs | 549 --------------------- crates/ra_analysis/src/descriptors/path.rs | 148 ------ crates/ra_analysis/src/hir/function/imp.rs | 21 + crates/ra_analysis/src/hir/function/mod.rs | 137 +++++ crates/ra_analysis/src/hir/function/scope.rs | 424 ++++++++++++++++ crates/ra_analysis/src/hir/mod.rs | 144 ++++++ crates/ra_analysis/src/hir/module/imp.rs | 229 +++++++++ crates/ra_analysis/src/hir/module/mod.rs | 378 ++++++++++++++ crates/ra_analysis/src/hir/module/nameres.rs | 549 +++++++++++++++++++++ crates/ra_analysis/src/hir/path.rs | 148 ++++++ crates/ra_analysis/src/imp.rs | 4 +- crates/ra_analysis/src/lib.rs | 8 +- crates/ra_analysis/src/loc2id.rs | 4 +- 22 files changed, 2050 insertions(+), 2043 deletions(-) delete mode 100644 crates/ra_analysis/src/descriptors/function/imp.rs delete mode 100644 crates/ra_analysis/src/descriptors/function/mod.rs delete mode 100644 crates/ra_analysis/src/descriptors/function/scope.rs delete mode 100644 crates/ra_analysis/src/descriptors/mod.rs delete mode 100644 crates/ra_analysis/src/descriptors/module/imp.rs delete mode 100644 crates/ra_analysis/src/descriptors/module/mod.rs delete mode 100644 crates/ra_analysis/src/descriptors/module/nameres.rs delete mode 100644 crates/ra_analysis/src/descriptors/path.rs create mode 100644 crates/ra_analysis/src/hir/function/imp.rs create mode 100644 crates/ra_analysis/src/hir/function/mod.rs create mode 100644 crates/ra_analysis/src/hir/function/scope.rs create mode 100644 crates/ra_analysis/src/hir/mod.rs create mode 100644 crates/ra_analysis/src/hir/module/imp.rs create mode 100644 crates/ra_analysis/src/hir/module/mod.rs create mode 100644 crates/ra_analysis/src/hir/module/nameres.rs create mode 100644 crates/ra_analysis/src/hir/path.rs (limited to 'crates') diff --git a/crates/ra_analysis/src/completion/mod.rs b/crates/ra_analysis/src/completion/mod.rs index 5ef278127..1eb804c85 100644 --- a/crates/ra_analysis/src/completion/mod.rs +++ b/crates/ra_analysis/src/completion/mod.rs @@ -11,7 +11,7 @@ use rustc_hash::{FxHashMap}; use crate::{ db::{self, SyntaxDatabase}, - descriptors::{ + hir::{ module::{ModuleDescriptor} }, Cancelable, FilePosition diff --git a/crates/ra_analysis/src/completion/reference_completion.rs b/crates/ra_analysis/src/completion/reference_completion.rs index 858b52e76..ac6ef1d4a 100644 --- a/crates/ra_analysis/src/completion/reference_completion.rs +++ b/crates/ra_analysis/src/completion/reference_completion.rs @@ -10,7 +10,7 @@ use ra_syntax::{ use crate::{ db::RootDatabase, completion::CompletionItem, - descriptors::{ + hir::{ module::{ModuleDescriptor}, function::FnScopes, Def, diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index 4e34265fb..fa59b8b3e 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -7,7 +7,7 @@ use salsa::{self, Database}; use crate::{ db, - descriptors, + hir, symbol_index::SymbolIndex, syntax_ptr::SyntaxPtr, loc2id::{IdMaps, IdDatabase}, @@ -122,15 +122,15 @@ salsa::database_storage! { fn file_symbols() for FileSymbolsQuery; fn resolve_syntax_ptr() for ResolveSyntaxPtrQuery; } - impl descriptors::DescriptorDatabase { - fn module_tree() for descriptors::ModuleTreeQuery; - fn fn_scopes() for descriptors::FnScopesQuery; - fn _file_items() for descriptors::FileItemsQuery; - fn _file_item() for descriptors::FileItemQuery; - fn _input_module_items() for descriptors::InputModuleItemsQuery; - fn _item_map() for descriptors::ItemMapQuery; - fn _fn_syntax() for descriptors::FnSyntaxQuery; - fn _submodules() for descriptors::SubmodulesQuery; + impl hir::DescriptorDatabase { + fn module_tree() for hir::ModuleTreeQuery; + fn fn_scopes() for hir::FnScopesQuery; + fn _file_items() for hir::FileItemsQuery; + fn _file_item() for hir::FileItemQuery; + fn _input_module_items() for hir::InputModuleItemsQuery; + fn _item_map() for hir::ItemMapQuery; + fn _fn_syntax() for hir::FnSyntaxQuery; + fn _submodules() for hir::SubmodulesQuery; } } } diff --git a/crates/ra_analysis/src/descriptors/function/imp.rs b/crates/ra_analysis/src/descriptors/function/imp.rs deleted file mode 100644 index e09deba0f..000000000 --- a/crates/ra_analysis/src/descriptors/function/imp.rs +++ /dev/null @@ -1,21 +0,0 @@ -use std::sync::Arc; - -use ra_syntax::ast::{AstNode, FnDef, FnDefNode}; - -use crate::descriptors::{ - function::{FnId, FnScopes}, - DescriptorDatabase, -}; - -/// Resolve `FnId` to the corresponding `SyntaxNode` -pub(crate) fn fn_syntax(db: &impl DescriptorDatabase, fn_id: FnId) -> FnDefNode { - let ptr = db.id_maps().fn_ptr(fn_id); - let syntax = db.resolve_syntax_ptr(ptr); - FnDef::cast(syntax.borrowed()).unwrap().owned() -} - -pub(crate) fn fn_scopes(db: &impl DescriptorDatabase, fn_id: FnId) -> Arc { - let syntax = db._fn_syntax(fn_id); - let res = FnScopes::new(syntax.borrowed()); - Arc::new(res) -} diff --git a/crates/ra_analysis/src/descriptors/function/mod.rs b/crates/ra_analysis/src/descriptors/function/mod.rs deleted file mode 100644 index 86eee5e93..000000000 --- a/crates/ra_analysis/src/descriptors/function/mod.rs +++ /dev/null @@ -1,137 +0,0 @@ -pub(super) mod imp; -mod scope; - -use std::cmp::{max, min}; - -use ra_syntax::{ - ast::{self, AstNode, DocCommentsOwner, NameOwner}, - TextRange, TextUnit, -}; - -use crate::{ - syntax_ptr::SyntaxPtr, FileId, - loc2id::IdDatabase, -}; - -pub(crate) use self::scope::{resolve_local_name, FnScopes}; -pub(crate) use crate::loc2id::FnId; - -impl FnId { - pub(crate) fn get(db: &impl IdDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId { - let ptr = SyntaxPtr::new(file_id, fn_def.syntax()); - db.id_maps().fn_id(ptr) - } -} - -#[derive(Debug, Clone)] -pub struct FnDescriptor { - pub name: String, - pub label: String, - pub ret_type: Option, - pub params: Vec, - pub doc: Option, -} - -impl FnDescriptor { - pub fn new(node: ast::FnDef) -> Option { - let name = node.name()?.text().to_string(); - - let mut doc = None; - - // Strip the body out for the label. - let mut label: String = if let Some(body) = node.body() { - let body_range = body.syntax().range(); - let label: String = node - .syntax() - .children() - .filter(|child| !child.range().is_subrange(&body_range)) - .map(|node| node.text().to_string()) - .collect(); - label - } else { - node.syntax().text().to_string() - }; - - if let Some((comment_range, docs)) = FnDescriptor::extract_doc_comments(node) { - let comment_range = comment_range - .checked_sub(node.syntax().range().start()) - .unwrap(); - let start = comment_range.start().to_usize(); - let end = comment_range.end().to_usize(); - - // Remove the comment from the label - label.replace_range(start..end, ""); - - // Massage markdown - let mut processed_lines = Vec::new(); - let mut in_code_block = false; - for line in docs.lines() { - if line.starts_with("```") { - in_code_block = !in_code_block; - } - - let line = if in_code_block && line.starts_with("```") && !line.contains("rust") { - "```rust".into() - } else { - line.to_string() - }; - - processed_lines.push(line); - } - - if !processed_lines.is_empty() { - doc = Some(processed_lines.join("\n")); - } - } - - let params = FnDescriptor::param_list(node); - let ret_type = node.ret_type().map(|r| r.syntax().text().to_string()); - - Some(FnDescriptor { - name, - ret_type, - params, - label: label.trim().to_owned(), - doc, - }) - } - - fn extract_doc_comments(node: ast::FnDef) -> Option<(TextRange, String)> { - if node.doc_comments().count() == 0 { - return None; - } - - let comment_text = node.doc_comment_text(); - - let (begin, end) = node - .doc_comments() - .map(|comment| comment.syntax().range()) - .map(|range| (range.start().to_usize(), range.end().to_usize())) - .fold((std::usize::MAX, std::usize::MIN), |acc, range| { - (min(acc.0, range.0), max(acc.1, range.1)) - }); - - let range = TextRange::from_to(TextUnit::from_usize(begin), TextUnit::from_usize(end)); - - Some((range, comment_text)) - } - - fn param_list(node: ast::FnDef) -> Vec { - let mut res = vec![]; - if let Some(param_list) = node.param_list() { - if let Some(self_param) = param_list.self_param() { - res.push(self_param.syntax().text().to_string()) - } - - // Maybe use param.pat here? See if we can just extract the name? - //res.extend(param_list.params().map(|p| p.syntax().text().to_string())); - res.extend( - param_list - .params() - .filter_map(|p| p.pat()) - .map(|pat| pat.syntax().text().to_string()), - ); - } - res - } -} diff --git a/crates/ra_analysis/src/descriptors/function/scope.rs b/crates/ra_analysis/src/descriptors/function/scope.rs deleted file mode 100644 index 5307a0a8e..000000000 --- a/crates/ra_analysis/src/descriptors/function/scope.rs +++ /dev/null @@ -1,424 +0,0 @@ -use rustc_hash::{FxHashMap, FxHashSet}; - -use ra_syntax::{ - algo::generate, - ast::{self, ArgListOwner, LoopBodyOwner, NameOwner}, - AstNode, SmolStr, SyntaxNodeRef, -}; - -use crate::{ - syntax_ptr::LocalSyntaxPtr, - arena::{Arena, Id}, -}; - -pub(crate) type ScopeId = Id; - -#[derive(Debug, PartialEq, Eq)] -pub struct FnScopes { - pub(crate) self_param: Option, - scopes: Arena, - scope_for: FxHashMap, -} - -#[derive(Debug, PartialEq, Eq)] -pub struct ScopeEntry { - name: SmolStr, - ptr: LocalSyntaxPtr, -} - -#[derive(Debug, PartialEq, Eq)] -pub(crate) struct ScopeData { - parent: Option, - entries: Vec, -} - -impl FnScopes { - pub(crate) fn new(fn_def: ast::FnDef) -> FnScopes { - let mut scopes = FnScopes { - self_param: fn_def - .param_list() - .and_then(|it| it.self_param()) - .map(|it| LocalSyntaxPtr::new(it.syntax())), - scopes: Arena::default(), - scope_for: FxHashMap::default(), - }; - let root = scopes.root_scope(); - scopes.add_params_bindings(root, fn_def.param_list()); - if let Some(body) = fn_def.body() { - compute_block_scopes(body, &mut scopes, root) - } - scopes - } - pub(crate) fn entries(&self, scope: ScopeId) -> &[ScopeEntry] { - &self.scopes[scope].entries - } - pub fn scope_chain<'a>(&'a self, node: SyntaxNodeRef) -> impl Iterator + 'a { - generate(self.scope_for(node), move |&scope| { - self.scopes[scope].parent - }) - } - fn root_scope(&mut self) -> ScopeId { - self.scopes.alloc(ScopeData { - parent: None, - entries: vec![], - }) - } - fn new_scope(&mut self, parent: ScopeId) -> ScopeId { - self.scopes.alloc(ScopeData { - parent: Some(parent), - entries: vec![], - }) - } - fn add_bindings(&mut self, scope: ScopeId, pat: ast::Pat) { - let entries = pat - .syntax() - .descendants() - .filter_map(ast::BindPat::cast) - .filter_map(ScopeEntry::new); - self.scopes[scope].entries.extend(entries); - } - fn add_params_bindings(&mut self, scope: ScopeId, params: Option) { - params - .into_iter() - .flat_map(|it| it.params()) - .filter_map(|it| it.pat()) - .for_each(|it| self.add_bindings(scope, it)); - } - fn set_scope(&mut self, node: SyntaxNodeRef, scope: ScopeId) { - self.scope_for.insert(LocalSyntaxPtr::new(node), scope); - } - fn scope_for(&self, node: SyntaxNodeRef) -> Option { - node.ancestors() - .map(LocalSyntaxPtr::new) - .filter_map(|it| self.scope_for.get(&it).map(|&scope| scope)) - .next() - } -} - -impl ScopeEntry { - fn new(pat: ast::BindPat) -> Option { - let name = pat.name()?; - let res = ScopeEntry { - name: name.text(), - ptr: LocalSyntaxPtr::new(pat.syntax()), - }; - Some(res) - } - pub(crate) fn name(&self) -> &SmolStr { - &self.name - } - pub(crate) fn ptr(&self) -> LocalSyntaxPtr { - self.ptr - } -} - -fn compute_block_scopes(block: ast::Block, scopes: &mut FnScopes, mut scope: ScopeId) { - for stmt in block.statements() { - match stmt { - ast::Stmt::LetStmt(stmt) => { - if let Some(expr) = stmt.initializer() { - scopes.set_scope(expr.syntax(), scope); - compute_expr_scopes(expr, scopes, scope); - } - scope = scopes.new_scope(scope); - if let Some(pat) = stmt.pat() { - scopes.add_bindings(scope, pat); - } - } - ast::Stmt::ExprStmt(expr_stmt) => { - if let Some(expr) = expr_stmt.expr() { - scopes.set_scope(expr.syntax(), scope); - compute_expr_scopes(expr, scopes, scope); - } - } - } - } - if let Some(expr) = block.expr() { - scopes.set_scope(expr.syntax(), scope); - compute_expr_scopes(expr, scopes, scope); - } -} - -fn compute_expr_scopes(expr: ast::Expr, scopes: &mut FnScopes, scope: ScopeId) { - match expr { - ast::Expr::IfExpr(e) => { - let cond_scope = e - .condition() - .and_then(|cond| compute_cond_scopes(cond, scopes, scope)); - if let Some(block) = e.then_branch() { - compute_block_scopes(block, scopes, cond_scope.unwrap_or(scope)); - } - if let Some(block) = e.else_branch() { - compute_block_scopes(block, scopes, scope); - } - } - ast::Expr::BlockExpr(e) => { - if let Some(block) = e.block() { - compute_block_scopes(block, scopes, scope); - } - } - ast::Expr::LoopExpr(e) => { - if let Some(block) = e.loop_body() { - compute_block_scopes(block, scopes, scope); - } - } - ast::Expr::WhileExpr(e) => { - let cond_scope = e - .condition() - .and_then(|cond| compute_cond_scopes(cond, scopes, scope)); - if let Some(block) = e.loop_body() { - compute_block_scopes(block, scopes, cond_scope.unwrap_or(scope)); - } - } - ast::Expr::ForExpr(e) => { - if let Some(expr) = e.iterable() { - compute_expr_scopes(expr, scopes, scope); - } - let mut scope = scope; - if let Some(pat) = e.pat() { - scope = scopes.new_scope(scope); - scopes.add_bindings(scope, pat); - } - if let Some(block) = e.loop_body() { - compute_block_scopes(block, scopes, scope); - } - } - ast::Expr::LambdaExpr(e) => { - let scope = scopes.new_scope(scope); - scopes.add_params_bindings(scope, e.param_list()); - if let Some(body) = e.body() { - scopes.set_scope(body.syntax(), scope); - compute_expr_scopes(body, scopes, scope); - } - } - ast::Expr::CallExpr(e) => { - compute_call_scopes(e.expr(), e.arg_list(), scopes, scope); - } - ast::Expr::MethodCallExpr(e) => { - compute_call_scopes(e.expr(), e.arg_list(), scopes, scope); - } - ast::Expr::MatchExpr(e) => { - if let Some(expr) = e.expr() { - compute_expr_scopes(expr, scopes, scope); - } - for arm in e.match_arm_list().into_iter().flat_map(|it| it.arms()) { - let scope = scopes.new_scope(scope); - for pat in arm.pats() { - scopes.add_bindings(scope, pat); - } - if let Some(expr) = arm.expr() { - compute_expr_scopes(expr, scopes, scope); - } - } - } - _ => expr - .syntax() - .children() - .filter_map(ast::Expr::cast) - .for_each(|expr| compute_expr_scopes(expr, scopes, scope)), - }; - - fn compute_call_scopes( - receiver: Option, - arg_list: Option, - scopes: &mut FnScopes, - scope: ScopeId, - ) { - arg_list - .into_iter() - .flat_map(|it| it.args()) - .chain(receiver) - .for_each(|expr| compute_expr_scopes(expr, scopes, scope)); - } - - fn compute_cond_scopes( - cond: ast::Condition, - scopes: &mut FnScopes, - scope: ScopeId, - ) -> Option { - if let Some(expr) = cond.expr() { - compute_expr_scopes(expr, scopes, scope); - } - if let Some(pat) = cond.pat() { - let s = scopes.new_scope(scope); - scopes.add_bindings(s, pat); - Some(s) - } else { - None - } - } -} - -pub fn resolve_local_name<'a>( - name_ref: ast::NameRef, - scopes: &'a FnScopes, -) -> Option<&'a ScopeEntry> { - let mut shadowed = FxHashSet::default(); - let ret = scopes - .scope_chain(name_ref.syntax()) - .flat_map(|scope| scopes.entries(scope).iter()) - .filter(|entry| shadowed.insert(entry.name())) - .filter(|entry| entry.name() == &name_ref.text()) - .nth(0); - ret -} - -#[cfg(test)] -mod tests { - use ra_editor::find_node_at_offset; - use ra_syntax::SourceFileNode; - use test_utils::extract_offset; - - use super::*; - - fn do_check(code: &str, expected: &[&str]) { - let (off, code) = extract_offset(code); - let code = { - let mut buf = String::new(); - let off = u32::from(off) as usize; - buf.push_str(&code[..off]); - buf.push_str("marker"); - buf.push_str(&code[off..]); - buf - }; - let file = SourceFileNode::parse(&code); - let marker: ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); - let fn_def: ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); - let scopes = FnScopes::new(fn_def); - let actual = scopes - .scope_chain(marker.syntax()) - .flat_map(|scope| scopes.entries(scope)) - .map(|it| it.name()) - .collect::>(); - assert_eq!(actual.as_slice(), expected); - } - - #[test] - fn test_lambda_scope() { - do_check( - r" - fn quux(foo: i32) { - let f = |bar, baz: i32| { - <|> - }; - }", - &["bar", "baz", "foo"], - ); - } - - #[test] - fn test_call_scope() { - do_check( - r" - fn quux() { - f(|x| <|> ); - }", - &["x"], - ); - } - - #[test] - fn test_metod_call_scope() { - do_check( - r" - fn quux() { - z.f(|x| <|> ); - }", - &["x"], - ); - } - - #[test] - fn test_loop_scope() { - do_check( - r" - fn quux() { - loop { - let x = (); - <|> - }; - }", - &["x"], - ); - } - - #[test] - fn test_match() { - do_check( - r" - fn quux() { - match () { - Some(x) => { - <|> - } - }; - }", - &["x"], - ); - } - - #[test] - fn test_shadow_variable() { - do_check( - r" - fn foo(x: String) { - let x : &str = &x<|>; - }", - &["x"], - ); - } - - fn do_check_local_name(code: &str, expected_offset: u32) { - let (off, code) = extract_offset(code); - let file = SourceFileNode::parse(&code); - let fn_def: ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); - let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap(); - - let scopes = FnScopes::new(fn_def); - - let local_name_entry = resolve_local_name(name_ref, &scopes).unwrap(); - let local_name = local_name_entry.ptr().resolve(&file); - let expected_name = - find_node_at_offset::(file.syntax(), expected_offset.into()).unwrap(); - assert_eq!(local_name.range(), expected_name.syntax().range()); - } - - #[test] - fn test_resolve_local_name() { - do_check_local_name( - r#" - fn foo(x: i32, y: u32) { - { - let z = x * 2; - } - { - let t = x<|> * 3; - } - }"#, - 21, - ); - } - - #[test] - fn test_resolve_local_name_declaration() { - do_check_local_name( - r#" - fn foo(x: String) { - let x : &str = &x<|>; - }"#, - 21, - ); - } - - #[test] - fn test_resolve_local_name_shadow() { - do_check_local_name( - r" - fn foo(x: String) { - let x : &str = &x; - x<|> - }", - 46, - ); - } -} diff --git a/crates/ra_analysis/src/descriptors/mod.rs b/crates/ra_analysis/src/descriptors/mod.rs deleted file mode 100644 index 7a1bcf447..000000000 --- a/crates/ra_analysis/src/descriptors/mod.rs +++ /dev/null @@ -1,137 +0,0 @@ -pub(crate) mod function; -pub(crate) mod module; -mod path; - -use std::sync::Arc; - -use ra_syntax::{ - ast::{self, FnDefNode, AstNode}, - TextRange, SyntaxNode, -}; - -use crate::{ - FileId, - db::SyntaxDatabase, - descriptors::function::{resolve_local_name, FnId, FnScopes}, - descriptors::module::{ - ModuleId, ModuleTree, ModuleSource, ModuleDescriptor, - nameres::{ItemMap, InputModuleItems, FileItems} - }, - input::SourceRootId, - loc2id::{IdDatabase, DefId, DefLoc}, - syntax_ptr::LocalSyntaxPtr, - Cancelable, -}; - -pub(crate) use self::path::{Path, PathKind}; -pub(crate) use self::module::nameres::FileItemId; - -salsa::query_group! { - pub(crate) trait DescriptorDatabase: SyntaxDatabase + IdDatabase { - fn fn_scopes(fn_id: FnId) -> Arc { - type FnScopesQuery; - use fn function::imp::fn_scopes; - } - - fn _file_items(file_id: FileId) -> Arc { - type FileItemsQuery; - storage dependencies; - use fn module::nameres::file_items; - } - - fn _file_item(file_id: FileId, file_item_id: FileItemId) -> SyntaxNode { - type FileItemQuery; - storage dependencies; - use fn module::nameres::file_item; - } - - fn _input_module_items(source_root_id: SourceRootId, module_id: ModuleId) -> Cancelable> { - type InputModuleItemsQuery; - use fn module::nameres::input_module_items; - } - fn _item_map(source_root_id: SourceRootId) -> Cancelable> { - type ItemMapQuery; - use fn module::nameres::item_map; - } - fn _module_tree(source_root_id: SourceRootId) -> Cancelable> { - type ModuleTreeQuery; - use fn module::imp::module_tree; - } - fn _fn_syntax(fn_id: FnId) -> FnDefNode { - type FnSyntaxQuery; - // Don't retain syntax trees in memory - storage dependencies; - use fn function::imp::fn_syntax; - } - fn _submodules(source: ModuleSource) -> Cancelable>> { - type SubmodulesQuery; - use fn module::imp::submodules; - } - } -} - -pub(crate) enum Def { - Module(ModuleDescriptor), - Item, -} - -impl DefId { - pub(crate) fn resolve(self, db: &impl DescriptorDatabase) -> Cancelable { - let loc = db.id_maps().def_loc(self); - let res = match loc { - DefLoc::Module { id, source_root } => { - let descr = ModuleDescriptor::new(db, source_root, id)?; - Def::Module(descr) - } - DefLoc::Item { .. } => Def::Item, - }; - Ok(res) - } -} - -#[derive(Debug)] -pub struct ReferenceDescriptor { - pub range: TextRange, - pub name: String, -} - -#[derive(Debug)] -pub struct DeclarationDescriptor<'a> { - pat: ast::BindPat<'a>, - pub range: TextRange, -} - -impl<'a> DeclarationDescriptor<'a> { - pub fn new(pat: ast::BindPat) -> DeclarationDescriptor { - let range = pat.syntax().range(); - - DeclarationDescriptor { pat, range } - } - - pub fn find_all_refs(&self) -> Vec { - let name_ptr = LocalSyntaxPtr::new(self.pat.syntax()); - - let fn_def = match self.pat.syntax().ancestors().find_map(ast::FnDef::cast) { - Some(def) => def, - None => return Default::default(), - }; - - let fn_scopes = FnScopes::new(fn_def); - - let refs: Vec<_> = fn_def - .syntax() - .descendants() - .filter_map(ast::NameRef::cast) - .filter(|name_ref| match resolve_local_name(*name_ref, &fn_scopes) { - None => false, - Some(entry) => entry.ptr() == name_ptr, - }) - .map(|name_ref| ReferenceDescriptor { - name: name_ref.syntax().text().to_string(), - range: name_ref.syntax().range(), - }) - .collect(); - - refs - } -} diff --git a/crates/ra_analysis/src/descriptors/module/imp.rs b/crates/ra_analysis/src/descriptors/module/imp.rs deleted file mode 100644 index 80892acb7..000000000 --- a/crates/ra_analysis/src/descriptors/module/imp.rs +++ /dev/null @@ -1,229 +0,0 @@ -use std::sync::Arc; - -use ra_syntax::{ - ast::{self, NameOwner}, - SmolStr, -}; -use relative_path::RelativePathBuf; -use rustc_hash::{FxHashMap, FxHashSet}; - -use crate::{ - db, - descriptors::DescriptorDatabase, - input::{SourceRoot, SourceRootId}, - Cancelable, FileId, FileResolverImp, -}; - -use super::{ - LinkData, LinkId, ModuleData, ModuleId, ModuleSource, ModuleSourceNode, - ModuleTree, Problem, -}; - -#[derive(Clone, Hash, PartialEq, Eq, Debug)] -pub(crate) enum Submodule { - Declaration(SmolStr), - Definition(SmolStr, ModuleSource), -} - -impl Submodule { - fn name(&self) -> &SmolStr { - match self { - Submodule::Declaration(name) => name, - Submodule::Definition(name, _) => name, - } - } -} - -pub(crate) fn submodules( - db: &impl DescriptorDatabase, - source: ModuleSource, -) -> Cancelable>> { - db::check_canceled(db)?; - let file_id = source.file_id(); - let submodules = match source.resolve(db) { - ModuleSourceNode::SourceFile(it) => collect_submodules(file_id, it.borrowed()), - ModuleSourceNode::Module(it) => it - .borrowed() - .item_list() - .map(|it| collect_submodules(file_id, it)) - .unwrap_or_else(Vec::new), - }; - return Ok(Arc::new(submodules)); - - fn collect_submodules<'a>( - file_id: FileId, - root: impl ast::ModuleItemOwner<'a>, - ) -> Vec { - modules(root) - .map(|(name, m)| { - if m.has_semi() { - Submodule::Declaration(name) - } else { - let src = ModuleSource::new_inline(file_id, m); - Submodule::Definition(name, src) - } - }) - .collect() - } -} - -pub(crate) fn modules<'a>( - root: impl ast::ModuleItemOwner<'a>, -) -> impl Iterator)> { - root.items() - .filter_map(|item| match item { - ast::ModuleItem::Module(m) => Some(m), - _ => None, - }) - .filter_map(|module| { - let name = module.name()?.text(); - Some((name, module)) - }) -} - -pub(crate) fn module_tree( - db: &impl DescriptorDatabase, - source_root: SourceRootId, -) -> Cancelable> { - db::check_canceled(db)?; - let res = create_module_tree(db, source_root)?; - Ok(Arc::new(res)) -} - -fn create_module_tree<'a>( - db: &impl DescriptorDatabase, - source_root: SourceRootId, -) -> Cancelable { - let mut tree = ModuleTree::default(); - - let mut roots = FxHashMap::default(); - let mut visited = FxHashSet::default(); - - let source_root = db.source_root(source_root); - for &file_id in source_root.files.iter() { - let source = ModuleSource::SourceFile(file_id); - if visited.contains(&source) { - continue; // TODO: use explicit crate_roots here - } - assert!(!roots.contains_key(&file_id)); - let module_id = build_subtree( - db, - &source_root, - &mut tree, - &mut visited, - &mut roots, - None, - source, - )?; - roots.insert(file_id, module_id); - } - Ok(tree) -} - -fn build_subtree( - db: &impl DescriptorDatabase, - source_root: &SourceRoot, - tree: &mut ModuleTree, - visited: &mut FxHashSet, - roots: &mut FxHashMap, - parent: Option, - source: ModuleSource, -) -> Cancelable { - visited.insert(source); - let id = tree.push_mod(ModuleData { - source, - parent, - children: Vec::new(), - }); - for sub in db._submodules(source)?.iter() { - let link = tree.push_link(LinkData { - name: sub.name().clone(), - owner: id, - points_to: Vec::new(), - problem: None, - }); - - let (points_to, problem) = match sub { - Submodule::Declaration(name) => { - let (points_to, problem) = - resolve_submodule(source, &name, &source_root.file_resolver); - let points_to = points_to - .into_iter() - .map(|file_id| match roots.remove(&file_id) { - Some(module_id) => { - tree.mods[module_id].parent = Some(link); - Ok(module_id) - } - None => build_subtree( - db, - source_root, - tree, - visited, - roots, - Some(link), - ModuleSource::SourceFile(file_id), - ), - }) - .collect::>>()?; - (points_to, problem) - } - Submodule::Definition(_name, submodule_source) => { - let points_to = build_subtree( - db, - source_root, - tree, - visited, - roots, - Some(link), - *submodule_source, - )?; - (vec![points_to], None) - } - }; - - tree.links[link].points_to = points_to; - tree.links[link].problem = problem; - } - Ok(id) -} - -fn resolve_submodule( - source: ModuleSource, - name: &SmolStr, - file_resolver: &FileResolverImp, -) -> (Vec, Option) { - let file_id = match source { - ModuleSource::SourceFile(it) => it, - ModuleSource::Module(..) => { - // TODO - return (Vec::new(), None); - } - }; - let mod_name = file_resolver.file_stem(file_id); - let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main"; - - let file_mod = RelativePathBuf::from(format!("../{}.rs", name)); - let dir_mod = RelativePathBuf::from(format!("../{}/mod.rs", name)); - let points_to: Vec; - let problem: Option; - if is_dir_owner { - points_to = [&file_mod, &dir_mod] - .iter() - .filter_map(|path| file_resolver.resolve(file_id, path)) - .collect(); - problem = if points_to.is_empty() { - Some(Problem::UnresolvedModule { - candidate: file_mod, - }) - } else { - None - } - } else { - points_to = Vec::new(); - problem = Some(Problem::NotDirOwner { - move_to: RelativePathBuf::from(format!("../{}/mod.rs", mod_name)), - candidate: file_mod, - }); - } - (points_to, problem) -} diff --git a/crates/ra_analysis/src/descriptors/module/mod.rs b/crates/ra_analysis/src/descriptors/module/mod.rs deleted file mode 100644 index 78911d5d9..000000000 --- a/crates/ra_analysis/src/descriptors/module/mod.rs +++ /dev/null @@ -1,378 +0,0 @@ -pub(super) mod imp; -pub(super) mod nameres; - -use std::sync::Arc; - -use ra_editor::find_node_at_offset; - -use ra_syntax::{ - algo::generate, - ast::{self, AstNode, NameOwner}, - SmolStr, SyntaxNode, -}; -use relative_path::RelativePathBuf; - -use crate::{ - db::SyntaxDatabase, syntax_ptr::SyntaxPtr, FileId, FilePosition, Cancelable, - descriptors::{Path, PathKind, DescriptorDatabase}, - input::SourceRootId, - arena::{Arena, Id}, - loc2id::{DefLoc, DefId}, -}; - -pub(crate) use self::nameres::ModuleScope; - -/// `ModuleDescriptor` is API entry point to get all the information -/// about a particular module. -#[derive(Debug, Clone)] -pub(crate) struct ModuleDescriptor { - tree: Arc, - source_root_id: SourceRootId, - module_id: ModuleId, -} - -impl ModuleDescriptor { - /// Lookup `ModuleDescriptor` by `FileId`. Note that this is inherently - /// lossy transformation: in general, a single source might correspond to - /// several modules. - pub fn guess_from_file_id( - db: &impl DescriptorDatabase, - file_id: FileId, - ) -> Cancelable> { - ModuleDescriptor::guess_from_source(db, file_id, ModuleSource::SourceFile(file_id)) - } - - /// Lookup `ModuleDescriptor` by position in the source code. Note that this - /// is inherently lossy transformation: in general, a single source might - /// correspond to several modules. - pub fn guess_from_position( - db: &impl DescriptorDatabase, - position: FilePosition, - ) -> Cancelable> { - let file = db.file_syntax(position.file_id); - let module_source = match find_node_at_offset::(file.syntax(), position.offset) - { - Some(m) if !m.has_semi() => ModuleSource::new_inline(position.file_id, m), - _ => ModuleSource::SourceFile(position.file_id), - }; - ModuleDescriptor::guess_from_source(db, position.file_id, module_source) - } - - fn guess_from_source( - db: &impl DescriptorDatabase, - file_id: FileId, - module_source: ModuleSource, - ) -> Cancelable> { - let source_root_id = db.file_source_root(file_id); - let module_tree = db._module_tree(source_root_id)?; - - let res = match module_tree.any_module_for_source(module_source) { - None => None, - Some(module_id) => Some(ModuleDescriptor { - tree: module_tree, - source_root_id, - module_id, - }), - }; - Ok(res) - } - - pub(super) fn new( - db: &impl DescriptorDatabase, - source_root_id: SourceRootId, - module_id: ModuleId, - ) -> Cancelable { - let module_tree = db._module_tree(source_root_id)?; - let res = ModuleDescriptor { - tree: module_tree, - source_root_id, - module_id, - }; - Ok(res) - } - - /// Returns `mod foo;` or `mod foo {}` node whihc declared this module. - /// Returns `None` for the root module - pub fn parent_link_source( - &self, - db: &impl DescriptorDatabase, - ) -> Option<(FileId, ast::ModuleNode)> { - let link = self.module_id.parent_link(&self.tree)?; - let file_id = link.owner(&self.tree).source(&self.tree).file_id(); - let src = link.bind_source(&self.tree, db); - Some((file_id, src)) - } - - pub fn source(&self) -> ModuleSource { - self.module_id.source(&self.tree) - } - - /// Parent module. Returns `None` if this is a root module. - pub fn parent(&self) -> Option { - let parent_id = self.module_id.parent(&self.tree)?; - Some(ModuleDescriptor { - module_id: parent_id, - ..self.clone() - }) - } - - /// The root of the tree this module is part of - pub fn crate_root(&self) -> ModuleDescriptor { - let root_id = self.module_id.crate_root(&self.tree); - ModuleDescriptor { - module_id: root_id, - ..self.clone() - } - } - - /// `name` is `None` for the crate's root module - #[allow(unused)] - pub fn name(&self) -> Option { - let link = self.module_id.parent_link(&self.tree)?; - Some(link.name(&self.tree)) - } - - pub fn def_id(&self, db: &impl DescriptorDatabase) -> DefId { - let def_loc = DefLoc::Module { - id: self.module_id, - source_root: self.source_root_id, - }; - db.id_maps().def_id(def_loc) - } - - /// Finds a child module with the specified name. - pub fn child(&self, name: &str) -> Option { - let child_id = self.module_id.child(&self.tree, name)?; - Some(ModuleDescriptor { - module_id: child_id, - ..self.clone() - }) - } - - /// Returns a `ModuleScope`: a set of items, visible in this module. - pub(crate) fn scope(&self, db: &impl DescriptorDatabase) -> Cancelable { - let item_map = db._item_map(self.source_root_id)?; - let res = item_map.per_module[&self.module_id].clone(); - Ok(res) - } - - pub(crate) fn resolve_path( - &self, - db: &impl DescriptorDatabase, - path: Path, - ) -> Cancelable> { - let mut curr = match path.kind { - PathKind::Crate => self.crate_root(), - PathKind::Self_ | PathKind::Plain => self.clone(), - PathKind::Super => ctry!(self.parent()), - } - .def_id(db); - - let segments = path.segments; - for name in segments.iter() { - let module = match db.id_maps().def_loc(curr) { - DefLoc::Module { id, source_root } => ModuleDescriptor::new(db, source_root, id)?, - _ => return Ok(None), - }; - let scope = module.scope(db)?; - curr = ctry!(ctry!(scope.get(&name)).def_id); - } - Ok(Some(curr)) - } - - pub fn problems(&self, db: &impl DescriptorDatabase) -> Vec<(SyntaxNode, Problem)> { - self.module_id.problems(&self.tree, db) - } -} - -/// Phisically, rust source is organized as a set of files, but logically it is -/// organized as a tree of modules. Usually, a single file corresponds to a -/// single module, but it is not nessary the case. -/// -/// Module encapsulate the logic of transitioning from the fuzzy world of files -/// (which can have multiple parents) to the precise world of modules (which -/// always have one parent). -#[derive(Default, Debug, PartialEq, Eq)] -pub(crate) struct ModuleTree { - mods: Arena, - links: Arena, -} - -impl ModuleTree { - fn modules<'a>(&'a self) -> impl Iterator + 'a { - self.mods.iter().map(|(id, _)| id) - } - - fn modules_for_source(&self, source: ModuleSource) -> Vec { - self.mods - .iter() - .filter(|(_idx, it)| it.source == source) - .map(|(idx, _)| idx) - .collect() - } - - fn any_module_for_source(&self, source: ModuleSource) -> Option { - self.modules_for_source(source).pop() - } -} - -/// `ModuleSource` is the syntax tree element that produced this module: -/// either a file, or an inlinde module. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub(crate) enum ModuleSource { - SourceFile(FileId), - #[allow(dead_code)] - Module(SyntaxPtr), -} - -/// An owned syntax node for a module. Unlike `ModuleSource`, -/// this holds onto the AST for the whole file. -enum ModuleSourceNode { - SourceFile(ast::SourceFileNode), - Module(ast::ModuleNode), -} - -pub(crate) type ModuleId = Id; -type LinkId = Id; - -#[derive(Clone, Debug, Hash, PartialEq, Eq)] -pub enum Problem { - UnresolvedModule { - candidate: RelativePathBuf, - }, - NotDirOwner { - move_to: RelativePathBuf, - candidate: RelativePathBuf, - }, -} - -impl ModuleId { - fn source(self, tree: &ModuleTree) -> ModuleSource { - tree.mods[self].source - } - fn parent_link(self, tree: &ModuleTree) -> Option { - tree.mods[self].parent - } - fn parent(self, tree: &ModuleTree) -> Option { - let link = self.parent_link(tree)?; - Some(tree.links[link].owner) - } - fn crate_root(self, tree: &ModuleTree) -> ModuleId { - generate(Some(self), move |it| it.parent(tree)) - .last() - .unwrap() - } - fn child(self, tree: &ModuleTree, name: &str) -> Option { - let link = tree.mods[self] - .children - .iter() - .map(|&it| &tree.links[it]) - .find(|it| it.name == name)?; - Some(*link.points_to.first()?) - } - fn children<'a>(self, tree: &'a ModuleTree) -> impl Iterator + 'a { - tree.mods[self].children.iter().filter_map(move |&it| { - let link = &tree.links[it]; - let module = *link.points_to.first()?; - Some((link.name.clone(), module)) - }) - } - fn problems(self, tree: &ModuleTree, db: &impl SyntaxDatabase) -> Vec<(SyntaxNode, Problem)> { - tree.mods[self] - .children - .iter() - .filter_map(|&it| { - let p = tree.links[it].problem.clone()?; - let s = it.bind_source(tree, db); - let s = s.borrowed().name().unwrap().syntax().owned(); - Some((s, p)) - }) - .collect() - } -} - -impl LinkId { - fn owner(self, tree: &ModuleTree) -> ModuleId { - tree.links[self].owner - } - fn name(self, tree: &ModuleTree) -> SmolStr { - tree.links[self].name.clone() - } - fn bind_source<'a>(self, tree: &ModuleTree, db: &impl SyntaxDatabase) -> ast::ModuleNode { - let owner = self.owner(tree); - match owner.source(tree).resolve(db) { - ModuleSourceNode::SourceFile(root) => { - let ast = imp::modules(root.borrowed()) - .find(|(name, _)| name == &tree.links[self].name) - .unwrap() - .1; - ast.owned() - } - ModuleSourceNode::Module(it) => it, - } - } -} - -#[derive(Debug, PartialEq, Eq, Hash)] -pub(crate) struct ModuleData { - source: ModuleSource, - parent: Option, - children: Vec, -} - -impl ModuleSource { - fn new_inline(file_id: FileId, module: ast::Module) -> ModuleSource { - assert!(!module.has_semi()); - let ptr = SyntaxPtr::new(file_id, module.syntax()); - ModuleSource::Module(ptr) - } - - pub(crate) fn as_file(self) -> Option { - match self { - ModuleSource::SourceFile(f) => Some(f), - ModuleSource::Module(..) => None, - } - } - - pub(crate) fn file_id(self) -> FileId { - match self { - ModuleSource::SourceFile(f) => f, - ModuleSource::Module(ptr) => ptr.file_id(), - } - } - - fn resolve(self, db: &impl SyntaxDatabase) -> ModuleSourceNode { - match self { - ModuleSource::SourceFile(file_id) => { - let syntax = db.file_syntax(file_id); - ModuleSourceNode::SourceFile(syntax.ast().owned()) - } - ModuleSource::Module(ptr) => { - let syntax = db.resolve_syntax_ptr(ptr); - let syntax = syntax.borrowed(); - let module = ast::Module::cast(syntax).unwrap(); - ModuleSourceNode::Module(module.owned()) - } - } - } -} - -#[derive(Hash, Debug, PartialEq, Eq)] -struct LinkData { - owner: ModuleId, - name: SmolStr, - points_to: Vec, - problem: Option, -} - -impl ModuleTree { - fn push_mod(&mut self, data: ModuleData) -> ModuleId { - self.mods.alloc(data) - } - fn push_link(&mut self, data: LinkData) -> LinkId { - let owner = data.owner; - let id = self.links.alloc(data); - self.mods[owner].children.push(id); - id - } -} diff --git a/crates/ra_analysis/src/descriptors/module/nameres.rs b/crates/ra_analysis/src/descriptors/module/nameres.rs deleted file mode 100644 index d2964f67f..000000000 --- a/crates/ra_analysis/src/descriptors/module/nameres.rs +++ /dev/null @@ -1,549 +0,0 @@ -//! Name resolution algorithm. The end result of the algorithm is `ItemMap`: a -//! map with maps each module to it's scope: the set of items, visible in the -//! module. That is, we only resolve imports here, name resolution of item -//! bodies will be done in a separate step. -//! -//! Like Rustc, we use an interative per-crate algorithm: we start with scopes -//! containing only directly defined items, and then iteratively resolve -//! imports. -//! -//! To make this work nicely in the IDE scenarios, we place `InputModuleItems` -//! in between raw syntax and name resolution. `InputModuleItems` are computed -//! using only the module's syntax, and it is all directly defined items plus -//! imports. The plain is to make `InputModuleItems` independent of local -//! modifications (that is, typing inside a function shold not change IMIs), -//! such that the results of name resolution can be preserved unless the module -//! structure itself is modified. -use std::{ - sync::Arc, - time::Instant, - ops::Index, -}; - -use rustc_hash::FxHashMap; - -use ra_syntax::{ - SyntaxNode, SyntaxNodeRef, TextRange, - SmolStr, SyntaxKind::{self, *}, - ast::{self, ModuleItemOwner, AstNode} -}; - -use crate::{ - Cancelable, FileId, - loc2id::{DefId, DefLoc}, - descriptors::{ - Path, PathKind, - DescriptorDatabase, - module::{ModuleId, ModuleTree, ModuleSourceNode}, - }, - input::SourceRootId, - arena::{Arena, Id} -}; - -/// Identifier of item within a specific file. This is stable over reparses, so -/// it's OK to use it as a salsa key/value. -pub(crate) type FileItemId = Id; - -/// Maps item's `SyntaxNode`s to `FileItemId` and back. -#[derive(Debug, PartialEq, Eq, Default)] -pub(crate) struct FileItems { - arena: Arena, -} - -impl FileItems { - fn alloc(&mut self, item: SyntaxNode) -> FileItemId { - self.arena.alloc(item) - } - fn id_of(&self, item: SyntaxNodeRef) -> FileItemId { - let (id, _item) = self - .arena - .iter() - .find(|(_id, i)| i.borrowed() == item) - .unwrap(); - id - } -} - -impl Index for FileItems { - type Output = SyntaxNode; - fn index(&self, idx: FileItemId) -> &SyntaxNode { - &self.arena[idx] - } -} - -pub(crate) fn file_items(db: &impl DescriptorDatabase, file_id: FileId) -> Arc { - let source_file = db.file_syntax(file_id); - let source_file = source_file.borrowed(); - let mut res = FileItems::default(); - source_file - .syntax() - .descendants() - .filter_map(ast::ModuleItem::cast) - .map(|it| it.syntax().owned()) - .for_each(|it| { - res.alloc(it); - }); - Arc::new(res) -} - -pub(crate) fn file_item( - db: &impl DescriptorDatabase, - file_id: FileId, - file_item_id: FileItemId, -) -> SyntaxNode { - db._file_items(file_id)[file_item_id].clone() -} - -/// Item map is the result of the name resolution. Item map contains, for each -/// module, the set of visible items. -#[derive(Default, Debug, PartialEq, Eq)] -pub(crate) struct ItemMap { - pub(crate) per_module: FxHashMap, -} - -#[derive(Debug, Default, PartialEq, Eq, Clone)] -pub(crate) struct ModuleScope { - items: FxHashMap, -} - -impl ModuleScope { - pub(crate) fn entries<'a>(&'a self) -> impl Iterator + 'a { - self.items.iter() - } - pub(crate) fn get(&self, name: &SmolStr) -> Option<&Resolution> { - self.items.get(name) - } -} - -/// A set of items and imports declared inside a module, without relation to -/// other modules. -/// -/// This stands in-between raw syntax and name resolution and alow us to avoid -/// recomputing name res: if `InputModuleItems` are the same, we can avoid -/// running name resolution. -#[derive(Debug, Default, PartialEq, Eq)] -pub(crate) struct InputModuleItems { - items: Vec, - imports: Vec, -} - -#[derive(Debug, PartialEq, Eq)] -struct ModuleItem { - id: FileItemId, - name: SmolStr, - kind: SyntaxKind, - vis: Vis, -} - -#[derive(Debug, PartialEq, Eq)] -enum Vis { - // Priv, - Other, -} - -#[derive(Debug, Clone, PartialEq, Eq)] -struct Import { - path: Path, - kind: ImportKind, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) struct NamedImport { - file_item_id: FileItemId, - relative_range: TextRange, -} - -impl NamedImport { - pub(crate) fn range(&self, db: &impl DescriptorDatabase, file_id: FileId) -> TextRange { - let syntax = db._file_item(file_id, self.file_item_id); - let offset = syntax.borrowed().range().start(); - self.relative_range + offset - } -} - -#[derive(Debug, Clone, PartialEq, Eq)] -enum ImportKind { - Glob, - Named(NamedImport), -} - -pub(crate) fn input_module_items( - db: &impl DescriptorDatabase, - source_root: SourceRootId, - module_id: ModuleId, -) -> Cancelable> { - let module_tree = db._module_tree(source_root)?; - let source = module_id.source(&module_tree); - let file_items = db._file_items(source.file_id()); - let res = match source.resolve(db) { - ModuleSourceNode::SourceFile(it) => { - let items = it.borrowed().items(); - InputModuleItems::new(&file_items, items) - } - ModuleSourceNode::Module(it) => { - let items = it - .borrowed() - .item_list() - .into_iter() - .flat_map(|it| it.items()); - InputModuleItems::new(&file_items, items) - } - }; - Ok(Arc::new(res)) -} - -pub(crate) fn item_map( - db: &impl DescriptorDatabase, - source_root: SourceRootId, -) -> Cancelable> { - let start = Instant::now(); - let module_tree = db._module_tree(source_root)?; - let input = module_tree - .modules() - .map(|id| { - let items = db._input_module_items(source_root, id)?; - Ok((id, items)) - }) - .collect::>>()?; - let mut resolver = Resolver { - db: db, - input: &input, - source_root, - module_tree, - result: ItemMap::default(), - }; - resolver.resolve()?; - let res = resolver.result; - let elapsed = start.elapsed(); - log::info!("item_map: {:?}", elapsed); - Ok(Arc::new(res)) -} - -/// Resolution is basically `DefId` atm, but it should account for stuff like -/// multiple namespaces, ambiguity and errors. -#[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct Resolution { - /// None for unresolved - pub(crate) def_id: Option, - /// ident by whitch this is imported into local scope. - pub(crate) import: Option, -} - -// #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -// enum Namespace { -// Types, -// Values, -// } - -// #[derive(Debug)] -// struct PerNs { -// types: Option, -// values: Option, -// } - -impl InputModuleItems { - fn new<'a>( - file_items: &FileItems, - items: impl Iterator>, - ) -> InputModuleItems { - let mut res = InputModuleItems::default(); - for item in items { - res.add_item(file_items, item); - } - res - } - - fn add_item(&mut self, file_items: &FileItems, item: ast::ModuleItem) -> Option<()> { - match item { - ast::ModuleItem::StructDef(it) => self.items.push(ModuleItem::new(file_items, it)?), - ast::ModuleItem::EnumDef(it) => self.items.push(ModuleItem::new(file_items, it)?), - ast::ModuleItem::FnDef(it) => self.items.push(ModuleItem::new(file_items, it)?), - ast::ModuleItem::TraitDef(it) => self.items.push(ModuleItem::new(file_items, it)?), - ast::ModuleItem::TypeDef(it) => self.items.push(ModuleItem::new(file_items, it)?), - ast::ModuleItem::ImplItem(_) => { - // impls don't define items - } - ast::ModuleItem::UseItem(it) => self.add_use_item(file_items, it), - ast::ModuleItem::ExternCrateItem(_) => { - // TODO - } - ast::ModuleItem::ConstDef(it) => self.items.push(ModuleItem::new(file_items, it)?), - ast::ModuleItem::StaticDef(it) => self.items.push(ModuleItem::new(file_items, it)?), - ast::ModuleItem::Module(it) => self.items.push(ModuleItem::new(file_items, it)?), - } - Some(()) - } - - fn add_use_item(&mut self, file_items: &FileItems, item: ast::UseItem) { - let file_item_id = file_items.id_of(item.syntax()); - let start_offset = item.syntax().range().start(); - Path::expand_use_item(item, |path, range| { - let kind = match range { - None => ImportKind::Glob, - Some(range) => ImportKind::Named(NamedImport { - file_item_id, - relative_range: range - start_offset, - }), - }; - self.imports.push(Import { kind, path }) - }) - } -} - -impl ModuleItem { - fn new<'a>(file_items: &FileItems, item: impl ast::NameOwner<'a>) -> Option { - let name = item.name()?.text(); - let kind = item.syntax().kind(); - let vis = Vis::Other; - let id = file_items.id_of(item.syntax()); - let res = ModuleItem { - id, - name, - kind, - vis, - }; - Some(res) - } -} - -struct Resolver<'a, DB> { - db: &'a DB, - input: &'a FxHashMap>, - source_root: SourceRootId, - module_tree: Arc, - result: ItemMap, -} - -impl<'a, DB> Resolver<'a, DB> -where - DB: DescriptorDatabase, -{ - fn resolve(&mut self) -> Cancelable<()> { - for (&module_id, items) in self.input.iter() { - self.populate_module(module_id, items) - } - - for &module_id in self.input.keys() { - crate::db::check_canceled(self.db)?; - self.resolve_imports(module_id); - } - Ok(()) - } - - fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) { - let file_id = module_id.source(&self.module_tree).file_id(); - - let mut module_items = ModuleScope::default(); - - for import in input.imports.iter() { - if let Some(name) = import.path.segments.iter().last() { - if let ImportKind::Named(import) = import.kind { - module_items.items.insert( - name.clone(), - Resolution { - def_id: None, - import: Some(import), - }, - ); - } - } - } - - for item in input.items.iter() { - if item.kind == MODULE { - // handle submodules separatelly - continue; - } - let def_loc = DefLoc::Item { - file_id, - id: item.id, - }; - let def_id = self.db.id_maps().def_id(def_loc); - let resolution = Resolution { - def_id: Some(def_id), - import: None, - }; - module_items.items.insert(item.name.clone(), resolution); - } - - for (name, mod_id) in module_id.children(&self.module_tree) { - let def_loc = DefLoc::Module { - id: mod_id, - source_root: self.source_root, - }; - let def_id = self.db.id_maps().def_id(def_loc); - let resolution = Resolution { - def_id: Some(def_id), - import: None, - }; - module_items.items.insert(name, resolution); - } - - self.result.per_module.insert(module_id, module_items); - } - - fn resolve_imports(&mut self, module_id: ModuleId) { - for import in self.input[&module_id].imports.iter() { - self.resolve_import(module_id, import); - } - } - - fn resolve_import(&mut self, module_id: ModuleId, import: &Import) { - let ptr = match import.kind { - ImportKind::Glob => return, - ImportKind::Named(ptr) => ptr, - }; - - let mut curr = match import.path.kind { - // TODO: handle extern crates - PathKind::Plain => return, - PathKind::Self_ => module_id, - PathKind::Super => { - match module_id.parent(&self.module_tree) { - Some(it) => it, - // TODO: error - None => return, - } - } - PathKind::Crate => module_id.crate_root(&self.module_tree), - }; - - for (i, name) in import.path.segments.iter().enumerate() { - let is_last = i == import.path.segments.len() - 1; - - let def_id = match self.result.per_module[&curr].items.get(name) { - None => return, - Some(res) => match res.def_id { - Some(it) => it, - None => return, - }, - }; - - if !is_last { - curr = match self.db.id_maps().def_loc(def_id) { - DefLoc::Module { id, .. } => id, - _ => return, - } - } else { - self.update(module_id, |items| { - let res = Resolution { - def_id: Some(def_id), - import: Some(ptr), - }; - items.items.insert(name.clone(), res); - }) - } - } - } - - fn update(&mut self, module_id: ModuleId, f: impl FnOnce(&mut ModuleScope)) { - let module_items = self.result.per_module.get_mut(&module_id).unwrap(); - f(module_items) - } -} - -#[cfg(test)] -mod tests { - use crate::{ - AnalysisChange, - mock_analysis::{MockAnalysis, analysis_and_position}, - descriptors::{DescriptorDatabase, module::ModuleDescriptor}, - input::FilesDatabase, -}; - use super::*; - - fn item_map(fixture: &str) -> (Arc, ModuleId) { - let (analysis, pos) = analysis_and_position(fixture); - let db = analysis.imp.db; - let source_root = db.file_source_root(pos.file_id); - let descr = ModuleDescriptor::guess_from_position(&*db, pos) - .unwrap() - .unwrap(); - let module_id = descr.module_id; - (db._item_map(source_root).unwrap(), module_id) - } - - #[test] - fn test_item_map() { - let (item_map, module_id) = item_map( - " - //- /lib.rs - mod foo; - - use crate::foo::bar::Baz; - <|> - - //- /foo/mod.rs - pub mod bar; - - //- /foo/bar.rs - pub struct Baz; - ", - ); - let name = SmolStr::from("Baz"); - let resolution = &item_map.per_module[&module_id].items[&name]; - assert!(resolution.def_id.is_some()); - } - - #[test] - fn typing_inside_a_function_should_not_invalidate_item_map() { - let mock_analysis = MockAnalysis::with_files( - " - //- /lib.rs - mod foo; - - use crate::foo::bar::Baz; - - fn foo() -> i32 { - 1 + 1 - } - //- /foo/mod.rs - pub mod bar; - - //- /foo/bar.rs - pub struct Baz; - ", - ); - - let file_id = mock_analysis.id_of("/lib.rs"); - let mut host = mock_analysis.analysis_host(); - - let source_root = host.analysis().imp.db.file_source_root(file_id); - - { - let db = host.analysis().imp.db; - let events = db.log_executed(|| { - db._item_map(source_root).unwrap(); - }); - assert!(format!("{:?}", events).contains("_item_map")) - } - - let mut change = AnalysisChange::new(); - - change.change_file( - file_id, - " - mod foo; - - use crate::foo::bar::Baz; - - fn foo() -> i32 { 92 } - " - .to_string(), - ); - - host.apply_change(change); - - { - let db = host.analysis().imp.db; - let events = db.log_executed(|| { - db._item_map(source_root).unwrap(); - }); - assert!( - !format!("{:?}", events).contains("_item_map"), - "{:#?}", - events - ) - } - } -} diff --git a/crates/ra_analysis/src/descriptors/path.rs b/crates/ra_analysis/src/descriptors/path.rs deleted file mode 100644 index 8279daf4b..000000000 --- a/crates/ra_analysis/src/descriptors/path.rs +++ /dev/null @@ -1,148 +0,0 @@ -use ra_syntax::{SmolStr, ast, AstNode, TextRange}; - -#[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct Path { - pub(crate) kind: PathKind, - pub(crate) segments: Vec, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) enum PathKind { - Plain, - Self_, - Super, - Crate, -} - -impl Path { - /// Calls `cb` with all paths, represented by this use item. - pub(crate) fn expand_use_item(item: ast::UseItem, mut cb: impl FnMut(Path, Option)) { - if let Some(tree) = item.use_tree() { - expand_use_tree(None, tree, &mut cb); - } - } - - /// Converts an `ast::Path` to `Path`. Works with use trees. - pub(crate) fn from_ast(mut path: ast::Path) -> Option { - let mut kind = PathKind::Plain; - let mut segments = Vec::new(); - loop { - let segment = path.segment()?; - match segment.kind()? { - ast::PathSegmentKind::Name(name) => segments.push(name.text()), - ast::PathSegmentKind::CrateKw => { - kind = PathKind::Crate; - break; - } - ast::PathSegmentKind::SelfKw => { - kind = PathKind::Self_; - break; - } - ast::PathSegmentKind::SuperKw => { - kind = PathKind::Super; - break; - } - } - path = match qualifier(path) { - Some(it) => it, - None => break, - }; - } - segments.reverse(); - return Some(Path { kind, segments }); - - fn qualifier(path: ast::Path) -> Option { - if let Some(q) = path.qualifier() { - return Some(q); - } - // TODO: this bottom up traversal is not too precise. - // Should we handle do a top-down analysiss, recording results? - let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?; - let use_tree = use_tree_list.parent_use_tree(); - use_tree.path() - } - } - - /// `true` is this path is a single identifier, like `foo` - pub(crate) fn is_ident(&self) -> bool { - self.kind == PathKind::Plain && self.segments.len() == 1 - } -} - -fn expand_use_tree( - prefix: Option, - tree: ast::UseTree, - cb: &mut impl FnMut(Path, Option), -) { - if let Some(use_tree_list) = tree.use_tree_list() { - let prefix = match tree.path() { - None => prefix, - Some(path) => match convert_path(prefix, path) { - Some(it) => Some(it), - None => return, // TODO: report errors somewhere - }, - }; - for tree in use_tree_list.use_trees() { - expand_use_tree(prefix.clone(), tree, cb); - } - } else { - if let Some(ast_path) = tree.path() { - if let Some(path) = convert_path(prefix, ast_path) { - let range = if tree.has_star() { - None - } else { - let range = ast_path.segment().unwrap().syntax().range(); - Some(range) - }; - cb(path, range) - } - } - } -} - -fn convert_path(prefix: Option, path: ast::Path) -> Option { - let prefix = if let Some(qual) = path.qualifier() { - Some(convert_path(prefix, qual)?) - } else { - None - }; - let segment = path.segment()?; - let res = match segment.kind()? { - ast::PathSegmentKind::Name(name) => { - let mut res = prefix.unwrap_or_else(|| Path { - kind: PathKind::Plain, - segments: Vec::with_capacity(1), - }); - res.segments.push(name.text()); - res - } - ast::PathSegmentKind::CrateKw => { - if prefix.is_some() { - return None; - } - Path { - kind: PathKind::Crate, - segments: Vec::new(), - } - } - ast::PathSegmentKind::SelfKw => { - if prefix.is_some() { - return None; - } - Path { - kind: PathKind::Self_, - segments: Vec::new(), - } - } - ast::PathSegmentKind::SuperKw => { - if prefix.is_some() { - return None; - } - Path { - kind: PathKind::Super, - segments: Vec::new(), - } - } - }; - Some(res) -} diff --git a/crates/ra_analysis/src/hir/function/imp.rs b/crates/ra_analysis/src/hir/function/imp.rs new file mode 100644 index 000000000..cd573a47f --- /dev/null +++ b/crates/ra_analysis/src/hir/function/imp.rs @@ -0,0 +1,21 @@ +use std::sync::Arc; + +use ra_syntax::ast::{AstNode, FnDef, FnDefNode}; + +use crate::hir::{ + function::{FnId, FnScopes}, + DescriptorDatabase, +}; + +/// Resolve `FnId` to the corresponding `SyntaxNode` +pub(crate) fn fn_syntax(db: &impl DescriptorDatabase, fn_id: FnId) -> FnDefNode { + let ptr = db.id_maps().fn_ptr(fn_id); + let syntax = db.resolve_syntax_ptr(ptr); + FnDef::cast(syntax.borrowed()).unwrap().owned() +} + +pub(crate) fn fn_scopes(db: &impl DescriptorDatabase, fn_id: FnId) -> Arc { + let syntax = db._fn_syntax(fn_id); + let res = FnScopes::new(syntax.borrowed()); + Arc::new(res) +} diff --git a/crates/ra_analysis/src/hir/function/mod.rs b/crates/ra_analysis/src/hir/function/mod.rs new file mode 100644 index 000000000..86eee5e93 --- /dev/null +++ b/crates/ra_analysis/src/hir/function/mod.rs @@ -0,0 +1,137 @@ +pub(super) mod imp; +mod scope; + +use std::cmp::{max, min}; + +use ra_syntax::{ + ast::{self, AstNode, DocCommentsOwner, NameOwner}, + TextRange, TextUnit, +}; + +use crate::{ + syntax_ptr::SyntaxPtr, FileId, + loc2id::IdDatabase, +}; + +pub(crate) use self::scope::{resolve_local_name, FnScopes}; +pub(crate) use crate::loc2id::FnId; + +impl FnId { + pub(crate) fn get(db: &impl IdDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId { + let ptr = SyntaxPtr::new(file_id, fn_def.syntax()); + db.id_maps().fn_id(ptr) + } +} + +#[derive(Debug, Clone)] +pub struct FnDescriptor { + pub name: String, + pub label: String, + pub ret_type: Option, + pub params: Vec, + pub doc: Option, +} + +impl FnDescriptor { + pub fn new(node: ast::FnDef) -> Option { + let name = node.name()?.text().to_string(); + + let mut doc = None; + + // Strip the body out for the label. + let mut label: String = if let Some(body) = node.body() { + let body_range = body.syntax().range(); + let label: String = node + .syntax() + .children() + .filter(|child| !child.range().is_subrange(&body_range)) + .map(|node| node.text().to_string()) + .collect(); + label + } else { + node.syntax().text().to_string() + }; + + if let Some((comment_range, docs)) = FnDescriptor::extract_doc_comments(node) { + let comment_range = comment_range + .checked_sub(node.syntax().range().start()) + .unwrap(); + let start = comment_range.start().to_usize(); + let end = comment_range.end().to_usize(); + + // Remove the comment from the label + label.replace_range(start..end, ""); + + // Massage markdown + let mut processed_lines = Vec::new(); + let mut in_code_block = false; + for line in docs.lines() { + if line.starts_with("```") { + in_code_block = !in_code_block; + } + + let line = if in_code_block && line.starts_with("```") && !line.contains("rust") { + "```rust".into() + } else { + line.to_string() + }; + + processed_lines.push(line); + } + + if !processed_lines.is_empty() { + doc = Some(processed_lines.join("\n")); + } + } + + let params = FnDescriptor::param_list(node); + let ret_type = node.ret_type().map(|r| r.syntax().text().to_string()); + + Some(FnDescriptor { + name, + ret_type, + params, + label: label.trim().to_owned(), + doc, + }) + } + + fn extract_doc_comments(node: ast::FnDef) -> Option<(TextRange, String)> { + if node.doc_comments().count() == 0 { + return None; + } + + let comment_text = node.doc_comment_text(); + + let (begin, end) = node + .doc_comments() + .map(|comment| comment.syntax().range()) + .map(|range| (range.start().to_usize(), range.end().to_usize())) + .fold((std::usize::MAX, std::usize::MIN), |acc, range| { + (min(acc.0, range.0), max(acc.1, range.1)) + }); + + let range = TextRange::from_to(TextUnit::from_usize(begin), TextUnit::from_usize(end)); + + Some((range, comment_text)) + } + + fn param_list(node: ast::FnDef) -> Vec { + let mut res = vec![]; + if let Some(param_list) = node.param_list() { + if let Some(self_param) = param_list.self_param() { + res.push(self_param.syntax().text().to_string()) + } + + // Maybe use param.pat here? See if we can just extract the name? + //res.extend(param_list.params().map(|p| p.syntax().text().to_string())); + res.extend( + param_list + .params() + .filter_map(|p| p.pat()) + .map(|pat| pat.syntax().text().to_string()), + ); + } + res + } +} diff --git a/crates/ra_analysis/src/hir/function/scope.rs b/crates/ra_analysis/src/hir/function/scope.rs new file mode 100644 index 000000000..5307a0a8e --- /dev/null +++ b/crates/ra_analysis/src/hir/function/scope.rs @@ -0,0 +1,424 @@ +use rustc_hash::{FxHashMap, FxHashSet}; + +use ra_syntax::{ + algo::generate, + ast::{self, ArgListOwner, LoopBodyOwner, NameOwner}, + AstNode, SmolStr, SyntaxNodeRef, +}; + +use crate::{ + syntax_ptr::LocalSyntaxPtr, + arena::{Arena, Id}, +}; + +pub(crate) type ScopeId = Id; + +#[derive(Debug, PartialEq, Eq)] +pub struct FnScopes { + pub(crate) self_param: Option, + scopes: Arena, + scope_for: FxHashMap, +} + +#[derive(Debug, PartialEq, Eq)] +pub struct ScopeEntry { + name: SmolStr, + ptr: LocalSyntaxPtr, +} + +#[derive(Debug, PartialEq, Eq)] +pub(crate) struct ScopeData { + parent: Option, + entries: Vec, +} + +impl FnScopes { + pub(crate) fn new(fn_def: ast::FnDef) -> FnScopes { + let mut scopes = FnScopes { + self_param: fn_def + .param_list() + .and_then(|it| it.self_param()) + .map(|it| LocalSyntaxPtr::new(it.syntax())), + scopes: Arena::default(), + scope_for: FxHashMap::default(), + }; + let root = scopes.root_scope(); + scopes.add_params_bindings(root, fn_def.param_list()); + if let Some(body) = fn_def.body() { + compute_block_scopes(body, &mut scopes, root) + } + scopes + } + pub(crate) fn entries(&self, scope: ScopeId) -> &[ScopeEntry] { + &self.scopes[scope].entries + } + pub fn scope_chain<'a>(&'a self, node: SyntaxNodeRef) -> impl Iterator + 'a { + generate(self.scope_for(node), move |&scope| { + self.scopes[scope].parent + }) + } + fn root_scope(&mut self) -> ScopeId { + self.scopes.alloc(ScopeData { + parent: None, + entries: vec![], + }) + } + fn new_scope(&mut self, parent: ScopeId) -> ScopeId { + self.scopes.alloc(ScopeData { + parent: Some(parent), + entries: vec![], + }) + } + fn add_bindings(&mut self, scope: ScopeId, pat: ast::Pat) { + let entries = pat + .syntax() + .descendants() + .filter_map(ast::BindPat::cast) + .filter_map(ScopeEntry::new); + self.scopes[scope].entries.extend(entries); + } + fn add_params_bindings(&mut self, scope: ScopeId, params: Option) { + params + .into_iter() + .flat_map(|it| it.params()) + .filter_map(|it| it.pat()) + .for_each(|it| self.add_bindings(scope, it)); + } + fn set_scope(&mut self, node: SyntaxNodeRef, scope: ScopeId) { + self.scope_for.insert(LocalSyntaxPtr::new(node), scope); + } + fn scope_for(&self, node: SyntaxNodeRef) -> Option { + node.ancestors() + .map(LocalSyntaxPtr::new) + .filter_map(|it| self.scope_for.get(&it).map(|&scope| scope)) + .next() + } +} + +impl ScopeEntry { + fn new(pat: ast::BindPat) -> Option { + let name = pat.name()?; + let res = ScopeEntry { + name: name.text(), + ptr: LocalSyntaxPtr::new(pat.syntax()), + }; + Some(res) + } + pub(crate) fn name(&self) -> &SmolStr { + &self.name + } + pub(crate) fn ptr(&self) -> LocalSyntaxPtr { + self.ptr + } +} + +fn compute_block_scopes(block: ast::Block, scopes: &mut FnScopes, mut scope: ScopeId) { + for stmt in block.statements() { + match stmt { + ast::Stmt::LetStmt(stmt) => { + if let Some(expr) = stmt.initializer() { + scopes.set_scope(expr.syntax(), scope); + compute_expr_scopes(expr, scopes, scope); + } + scope = scopes.new_scope(scope); + if let Some(pat) = stmt.pat() { + scopes.add_bindings(scope, pat); + } + } + ast::Stmt::ExprStmt(expr_stmt) => { + if let Some(expr) = expr_stmt.expr() { + scopes.set_scope(expr.syntax(), scope); + compute_expr_scopes(expr, scopes, scope); + } + } + } + } + if let Some(expr) = block.expr() { + scopes.set_scope(expr.syntax(), scope); + compute_expr_scopes(expr, scopes, scope); + } +} + +fn compute_expr_scopes(expr: ast::Expr, scopes: &mut FnScopes, scope: ScopeId) { + match expr { + ast::Expr::IfExpr(e) => { + let cond_scope = e + .condition() + .and_then(|cond| compute_cond_scopes(cond, scopes, scope)); + if let Some(block) = e.then_branch() { + compute_block_scopes(block, scopes, cond_scope.unwrap_or(scope)); + } + if let Some(block) = e.else_branch() { + compute_block_scopes(block, scopes, scope); + } + } + ast::Expr::BlockExpr(e) => { + if let Some(block) = e.block() { + compute_block_scopes(block, scopes, scope); + } + } + ast::Expr::LoopExpr(e) => { + if let Some(block) = e.loop_body() { + compute_block_scopes(block, scopes, scope); + } + } + ast::Expr::WhileExpr(e) => { + let cond_scope = e + .condition() + .and_then(|cond| compute_cond_scopes(cond, scopes, scope)); + if let Some(block) = e.loop_body() { + compute_block_scopes(block, scopes, cond_scope.unwrap_or(scope)); + } + } + ast::Expr::ForExpr(e) => { + if let Some(expr) = e.iterable() { + compute_expr_scopes(expr, scopes, scope); + } + let mut scope = scope; + if let Some(pat) = e.pat() { + scope = scopes.new_scope(scope); + scopes.add_bindings(scope, pat); + } + if let Some(block) = e.loop_body() { + compute_block_scopes(block, scopes, scope); + } + } + ast::Expr::LambdaExpr(e) => { + let scope = scopes.new_scope(scope); + scopes.add_params_bindings(scope, e.param_list()); + if let Some(body) = e.body() { + scopes.set_scope(body.syntax(), scope); + compute_expr_scopes(body, scopes, scope); + } + } + ast::Expr::CallExpr(e) => { + compute_call_scopes(e.expr(), e.arg_list(), scopes, scope); + } + ast::Expr::MethodCallExpr(e) => { + compute_call_scopes(e.expr(), e.arg_list(), scopes, scope); + } + ast::Expr::MatchExpr(e) => { + if let Some(expr) = e.expr() { + compute_expr_scopes(expr, scopes, scope); + } + for arm in e.match_arm_list().into_iter().flat_map(|it| it.arms()) { + let scope = scopes.new_scope(scope); + for pat in arm.pats() { + scopes.add_bindings(scope, pat); + } + if let Some(expr) = arm.expr() { + compute_expr_scopes(expr, scopes, scope); + } + } + } + _ => expr + .syntax() + .children() + .filter_map(ast::Expr::cast) + .for_each(|expr| compute_expr_scopes(expr, scopes, scope)), + }; + + fn compute_call_scopes( + receiver: Option, + arg_list: Option, + scopes: &mut FnScopes, + scope: ScopeId, + ) { + arg_list + .into_iter() + .flat_map(|it| it.args()) + .chain(receiver) + .for_each(|expr| compute_expr_scopes(expr, scopes, scope)); + } + + fn compute_cond_scopes( + cond: ast::Condition, + scopes: &mut FnScopes, + scope: ScopeId, + ) -> Option { + if let Some(expr) = cond.expr() { + compute_expr_scopes(expr, scopes, scope); + } + if let Some(pat) = cond.pat() { + let s = scopes.new_scope(scope); + scopes.add_bindings(s, pat); + Some(s) + } else { + None + } + } +} + +pub fn resolve_local_name<'a>( + name_ref: ast::NameRef, + scopes: &'a FnScopes, +) -> Option<&'a ScopeEntry> { + let mut shadowed = FxHashSet::default(); + let ret = scopes + .scope_chain(name_ref.syntax()) + .flat_map(|scope| scopes.entries(scope).iter()) + .filter(|entry| shadowed.insert(entry.name())) + .filter(|entry| entry.name() == &name_ref.text()) + .nth(0); + ret +} + +#[cfg(test)] +mod tests { + use ra_editor::find_node_at_offset; + use ra_syntax::SourceFileNode; + use test_utils::extract_offset; + + use super::*; + + fn do_check(code: &str, expected: &[&str]) { + let (off, code) = extract_offset(code); + let code = { + let mut buf = String::new(); + let off = u32::from(off) as usize; + buf.push_str(&code[..off]); + buf.push_str("marker"); + buf.push_str(&code[off..]); + buf + }; + let file = SourceFileNode::parse(&code); + let marker: ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); + let fn_def: ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); + let scopes = FnScopes::new(fn_def); + let actual = scopes + .scope_chain(marker.syntax()) + .flat_map(|scope| scopes.entries(scope)) + .map(|it| it.name()) + .collect::>(); + assert_eq!(actual.as_slice(), expected); + } + + #[test] + fn test_lambda_scope() { + do_check( + r" + fn quux(foo: i32) { + let f = |bar, baz: i32| { + <|> + }; + }", + &["bar", "baz", "foo"], + ); + } + + #[test] + fn test_call_scope() { + do_check( + r" + fn quux() { + f(|x| <|> ); + }", + &["x"], + ); + } + + #[test] + fn test_metod_call_scope() { + do_check( + r" + fn quux() { + z.f(|x| <|> ); + }", + &["x"], + ); + } + + #[test] + fn test_loop_scope() { + do_check( + r" + fn quux() { + loop { + let x = (); + <|> + }; + }", + &["x"], + ); + } + + #[test] + fn test_match() { + do_check( + r" + fn quux() { + match () { + Some(x) => { + <|> + } + }; + }", + &["x"], + ); + } + + #[test] + fn test_shadow_variable() { + do_check( + r" + fn foo(x: String) { + let x : &str = &x<|>; + }", + &["x"], + ); + } + + fn do_check_local_name(code: &str, expected_offset: u32) { + let (off, code) = extract_offset(code); + let file = SourceFileNode::parse(&code); + let fn_def: ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); + let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap(); + + let scopes = FnScopes::new(fn_def); + + let local_name_entry = resolve_local_name(name_ref, &scopes).unwrap(); + let local_name = local_name_entry.ptr().resolve(&file); + let expected_name = + find_node_at_offset::(file.syntax(), expected_offset.into()).unwrap(); + assert_eq!(local_name.range(), expected_name.syntax().range()); + } + + #[test] + fn test_resolve_local_name() { + do_check_local_name( + r#" + fn foo(x: i32, y: u32) { + { + let z = x * 2; + } + { + let t = x<|> * 3; + } + }"#, + 21, + ); + } + + #[test] + fn test_resolve_local_name_declaration() { + do_check_local_name( + r#" + fn foo(x: String) { + let x : &str = &x<|>; + }"#, + 21, + ); + } + + #[test] + fn test_resolve_local_name_shadow() { + do_check_local_name( + r" + fn foo(x: String) { + let x : &str = &x; + x<|> + }", + 46, + ); + } +} diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs new file mode 100644 index 000000000..6bee2b5c4 --- /dev/null +++ b/crates/ra_analysis/src/hir/mod.rs @@ -0,0 +1,144 @@ +//! HIR (previsouly known as descriptors) provides a high-level OO acess to Rust +//! code. +//! +//! The principal difference between HIR and syntax trees is that HIR is bound +//! to a particular crate instance. That is, it has cfg flags and features +//! applied. So, there relation between syntax and HIR is many-to-one. + +pub(crate) mod function; +pub(crate) mod module; +mod path; + +use std::sync::Arc; + +use ra_syntax::{ + ast::{self, FnDefNode, AstNode}, + TextRange, SyntaxNode, +}; + +use crate::{ + FileId, + db::SyntaxDatabase, + hir::function::{resolve_local_name, FnId, FnScopes}, + hir::module::{ + ModuleId, ModuleTree, ModuleSource, ModuleDescriptor, + nameres::{ItemMap, InputModuleItems, FileItems} + }, + input::SourceRootId, + loc2id::{IdDatabase, DefId, DefLoc}, + syntax_ptr::LocalSyntaxPtr, + Cancelable, +}; + +pub(crate) use self::path::{Path, PathKind}; +pub(crate) use self::module::nameres::FileItemId; + +salsa::query_group! { + pub(crate) trait DescriptorDatabase: SyntaxDatabase + IdDatabase { + fn fn_scopes(fn_id: FnId) -> Arc { + type FnScopesQuery; + use fn function::imp::fn_scopes; + } + + fn _file_items(file_id: FileId) -> Arc { + type FileItemsQuery; + storage dependencies; + use fn module::nameres::file_items; + } + + fn _file_item(file_id: FileId, file_item_id: FileItemId) -> SyntaxNode { + type FileItemQuery; + storage dependencies; + use fn module::nameres::file_item; + } + + fn _input_module_items(source_root_id: SourceRootId, module_id: ModuleId) -> Cancelable> { + type InputModuleItemsQuery; + use fn module::nameres::input_module_items; + } + fn _item_map(source_root_id: SourceRootId) -> Cancelable> { + type ItemMapQuery; + use fn module::nameres::item_map; + } + fn _module_tree(source_root_id: SourceRootId) -> Cancelable> { + type ModuleTreeQuery; + use fn module::imp::module_tree; + } + fn _fn_syntax(fn_id: FnId) -> FnDefNode { + type FnSyntaxQuery; + // Don't retain syntax trees in memory + storage dependencies; + use fn function::imp::fn_syntax; + } + fn _submodules(source: ModuleSource) -> Cancelable>> { + type SubmodulesQuery; + use fn module::imp::submodules; + } + } +} + +pub(crate) enum Def { + Module(ModuleDescriptor), + Item, +} + +impl DefId { + pub(crate) fn resolve(self, db: &impl DescriptorDatabase) -> Cancelable { + let loc = db.id_maps().def_loc(self); + let res = match loc { + DefLoc::Module { id, source_root } => { + let descr = ModuleDescriptor::new(db, source_root, id)?; + Def::Module(descr) + } + DefLoc::Item { .. } => Def::Item, + }; + Ok(res) + } +} + +#[derive(Debug)] +pub struct ReferenceDescriptor { + pub range: TextRange, + pub name: String, +} + +#[derive(Debug)] +pub struct DeclarationDescriptor<'a> { + pat: ast::BindPat<'a>, + pub range: TextRange, +} + +impl<'a> DeclarationDescriptor<'a> { + pub fn new(pat: ast::BindPat) -> DeclarationDescriptor { + let range = pat.syntax().range(); + + DeclarationDescriptor { pat, range } + } + + pub fn find_all_refs(&self) -> Vec { + let name_ptr = LocalSyntaxPtr::new(self.pat.syntax()); + + let fn_def = match self.pat.syntax().ancestors().find_map(ast::FnDef::cast) { + Some(def) => def, + None => return Default::default(), + }; + + let fn_scopes = FnScopes::new(fn_def); + + let refs: Vec<_> = fn_def + .syntax() + .descendants() + .filter_map(ast::NameRef::cast) + .filter(|name_ref| match resolve_local_name(*name_ref, &fn_scopes) { + None => false, + Some(entry) => entry.ptr() == name_ptr, + }) + .map(|name_ref| ReferenceDescriptor { + name: name_ref.syntax().text().to_string(), + range: name_ref.syntax().range(), + }) + .collect(); + + refs + } +} diff --git a/crates/ra_analysis/src/hir/module/imp.rs b/crates/ra_analysis/src/hir/module/imp.rs new file mode 100644 index 000000000..d8539ed85 --- /dev/null +++ b/crates/ra_analysis/src/hir/module/imp.rs @@ -0,0 +1,229 @@ +use std::sync::Arc; + +use ra_syntax::{ + ast::{self, NameOwner}, + SmolStr, +}; +use relative_path::RelativePathBuf; +use rustc_hash::{FxHashMap, FxHashSet}; + +use crate::{ + db, + hir::DescriptorDatabase, + input::{SourceRoot, SourceRootId}, + Cancelable, FileId, FileResolverImp, +}; + +use super::{ + LinkData, LinkId, ModuleData, ModuleId, ModuleSource, ModuleSourceNode, + ModuleTree, Problem, +}; + +#[derive(Clone, Hash, PartialEq, Eq, Debug)] +pub(crate) enum Submodule { + Declaration(SmolStr), + Definition(SmolStr, ModuleSource), +} + +impl Submodule { + fn name(&self) -> &SmolStr { + match self { + Submodule::Declaration(name) => name, + Submodule::Definition(name, _) => name, + } + } +} + +pub(crate) fn submodules( + db: &impl DescriptorDatabase, + source: ModuleSource, +) -> Cancelable>> { + db::check_canceled(db)?; + let file_id = source.file_id(); + let submodules = match source.resolve(db) { + ModuleSourceNode::SourceFile(it) => collect_submodules(file_id, it.borrowed()), + ModuleSourceNode::Module(it) => it + .borrowed() + .item_list() + .map(|it| collect_submodules(file_id, it)) + .unwrap_or_else(Vec::new), + }; + return Ok(Arc::new(submodules)); + + fn collect_submodules<'a>( + file_id: FileId, + root: impl ast::ModuleItemOwner<'a>, + ) -> Vec { + modules(root) + .map(|(name, m)| { + if m.has_semi() { + Submodule::Declaration(name) + } else { + let src = ModuleSource::new_inline(file_id, m); + Submodule::Definition(name, src) + } + }) + .collect() + } +} + +pub(crate) fn modules<'a>( + root: impl ast::ModuleItemOwner<'a>, +) -> impl Iterator)> { + root.items() + .filter_map(|item| match item { + ast::ModuleItem::Module(m) => Some(m), + _ => None, + }) + .filter_map(|module| { + let name = module.name()?.text(); + Some((name, module)) + }) +} + +pub(crate) fn module_tree( + db: &impl DescriptorDatabase, + source_root: SourceRootId, +) -> Cancelable> { + db::check_canceled(db)?; + let res = create_module_tree(db, source_root)?; + Ok(Arc::new(res)) +} + +fn create_module_tree<'a>( + db: &impl DescriptorDatabase, + source_root: SourceRootId, +) -> Cancelable { + let mut tree = ModuleTree::default(); + + let mut roots = FxHashMap::default(); + let mut visited = FxHashSet::default(); + + let source_root = db.source_root(source_root); + for &file_id in source_root.files.iter() { + let source = ModuleSource::SourceFile(file_id); + if visited.contains(&source) { + continue; // TODO: use explicit crate_roots here + } + assert!(!roots.contains_key(&file_id)); + let module_id = build_subtree( + db, + &source_root, + &mut tree, + &mut visited, + &mut roots, + None, + source, + )?; + roots.insert(file_id, module_id); + } + Ok(tree) +} + +fn build_subtree( + db: &impl DescriptorDatabase, + source_root: &SourceRoot, + tree: &mut ModuleTree, + visited: &mut FxHashSet, + roots: &mut FxHashMap, + parent: Option, + source: ModuleSource, +) -> Cancelable { + visited.insert(source); + let id = tree.push_mod(ModuleData { + source, + parent, + children: Vec::new(), + }); + for sub in db._submodules(source)?.iter() { + let link = tree.push_link(LinkData { + name: sub.name().clone(), + owner: id, + points_to: Vec::new(), + problem: None, + }); + + let (points_to, problem) = match sub { + Submodule::Declaration(name) => { + let (points_to, problem) = + resolve_submodule(source, &name, &source_root.file_resolver); + let points_to = points_to + .into_iter() + .map(|file_id| match roots.remove(&file_id) { + Some(module_id) => { + tree.mods[module_id].parent = Some(link); + Ok(module_id) + } + None => build_subtree( + db, + source_root, + tree, + visited, + roots, + Some(link), + ModuleSource::SourceFile(file_id), + ), + }) + .collect::>>()?; + (points_to, problem) + } + Submodule::Definition(_name, submodule_source) => { + let points_to = build_subtree( + db, + source_root, + tree, + visited, + roots, + Some(link), + *submodule_source, + )?; + (vec![points_to], None) + } + }; + + tree.links[link].points_to = points_to; + tree.links[link].problem = problem; + } + Ok(id) +} + +fn resolve_submodule( + source: ModuleSource, + name: &SmolStr, + file_resolver: &FileResolverImp, +) -> (Vec, Option) { + let file_id = match source { + ModuleSource::SourceFile(it) => it, + ModuleSource::Module(..) => { + // TODO + return (Vec::new(), None); + } + }; + let mod_name = file_resolver.file_stem(file_id); + let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main"; + + let file_mod = RelativePathBuf::from(format!("../{}.rs", name)); + let dir_mod = RelativePathBuf::from(format!("../{}/mod.rs", name)); + let points_to: Vec; + let problem: Option; + if is_dir_owner { + points_to = [&file_mod, &dir_mod] + .iter() + .filter_map(|path| file_resolver.resolve(file_id, path)) + .collect(); + problem = if points_to.is_empty() { + Some(Problem::UnresolvedModule { + candidate: file_mod, + }) + } else { + None + } + } else { + points_to = Vec::new(); + problem = Some(Problem::NotDirOwner { + move_to: RelativePathBuf::from(format!("../{}/mod.rs", mod_name)), + candidate: file_mod, + }); + } + (points_to, problem) +} diff --git a/crates/ra_analysis/src/hir/module/mod.rs b/crates/ra_analysis/src/hir/module/mod.rs new file mode 100644 index 000000000..f374a079f --- /dev/null +++ b/crates/ra_analysis/src/hir/module/mod.rs @@ -0,0 +1,378 @@ +pub(super) mod imp; +pub(super) mod nameres; + +use std::sync::Arc; + +use ra_editor::find_node_at_offset; + +use ra_syntax::{ + algo::generate, + ast::{self, AstNode, NameOwner}, + SmolStr, SyntaxNode, +}; +use relative_path::RelativePathBuf; + +use crate::{ + db::SyntaxDatabase, syntax_ptr::SyntaxPtr, FileId, FilePosition, Cancelable, + hir::{Path, PathKind, DescriptorDatabase}, + input::SourceRootId, + arena::{Arena, Id}, + loc2id::{DefLoc, DefId}, +}; + +pub(crate) use self::nameres::ModuleScope; + +/// `ModuleDescriptor` is API entry point to get all the information +/// about a particular module. +#[derive(Debug, Clone)] +pub(crate) struct ModuleDescriptor { + tree: Arc, + source_root_id: SourceRootId, + module_id: ModuleId, +} + +impl ModuleDescriptor { + /// Lookup `ModuleDescriptor` by `FileId`. Note that this is inherently + /// lossy transformation: in general, a single source might correspond to + /// several modules. + pub fn guess_from_file_id( + db: &impl DescriptorDatabase, + file_id: FileId, + ) -> Cancelable> { + ModuleDescriptor::guess_from_source(db, file_id, ModuleSource::SourceFile(file_id)) + } + + /// Lookup `ModuleDescriptor` by position in the source code. Note that this + /// is inherently lossy transformation: in general, a single source might + /// correspond to several modules. + pub fn guess_from_position( + db: &impl DescriptorDatabase, + position: FilePosition, + ) -> Cancelable> { + let file = db.file_syntax(position.file_id); + let module_source = match find_node_at_offset::(file.syntax(), position.offset) + { + Some(m) if !m.has_semi() => ModuleSource::new_inline(position.file_id, m), + _ => ModuleSource::SourceFile(position.file_id), + }; + ModuleDescriptor::guess_from_source(db, position.file_id, module_source) + } + + fn guess_from_source( + db: &impl DescriptorDatabase, + file_id: FileId, + module_source: ModuleSource, + ) -> Cancelable> { + let source_root_id = db.file_source_root(file_id); + let module_tree = db._module_tree(source_root_id)?; + + let res = match module_tree.any_module_for_source(module_source) { + None => None, + Some(module_id) => Some(ModuleDescriptor { + tree: module_tree, + source_root_id, + module_id, + }), + }; + Ok(res) + } + + pub(super) fn new( + db: &impl DescriptorDatabase, + source_root_id: SourceRootId, + module_id: ModuleId, + ) -> Cancelable { + let module_tree = db._module_tree(source_root_id)?; + let res = ModuleDescriptor { + tree: module_tree, + source_root_id, + module_id, + }; + Ok(res) + } + + /// Returns `mod foo;` or `mod foo {}` node whihc declared this module. + /// Returns `None` for the root module + pub fn parent_link_source( + &self, + db: &impl DescriptorDatabase, + ) -> Option<(FileId, ast::ModuleNode)> { + let link = self.module_id.parent_link(&self.tree)?; + let file_id = link.owner(&self.tree).source(&self.tree).file_id(); + let src = link.bind_source(&self.tree, db); + Some((file_id, src)) + } + + pub fn source(&self) -> ModuleSource { + self.module_id.source(&self.tree) + } + + /// Parent module. Returns `None` if this is a root module. + pub fn parent(&self) -> Option { + let parent_id = self.module_id.parent(&self.tree)?; + Some(ModuleDescriptor { + module_id: parent_id, + ..self.clone() + }) + } + + /// The root of the tree this module is part of + pub fn crate_root(&self) -> ModuleDescriptor { + let root_id = self.module_id.crate_root(&self.tree); + ModuleDescriptor { + module_id: root_id, + ..self.clone() + } + } + + /// `name` is `None` for the crate's root module + #[allow(unused)] + pub fn name(&self) -> Option { + let link = self.module_id.parent_link(&self.tree)?; + Some(link.name(&self.tree)) + } + + pub fn def_id(&self, db: &impl DescriptorDatabase) -> DefId { + let def_loc = DefLoc::Module { + id: self.module_id, + source_root: self.source_root_id, + }; + db.id_maps().def_id(def_loc) + } + + /// Finds a child module with the specified name. + pub fn child(&self, name: &str) -> Option { + let child_id = self.module_id.child(&self.tree, name)?; + Some(ModuleDescriptor { + module_id: child_id, + ..self.clone() + }) + } + + /// Returns a `ModuleScope`: a set of items, visible in this module. + pub(crate) fn scope(&self, db: &impl DescriptorDatabase) -> Cancelable { + let item_map = db._item_map(self.source_root_id)?; + let res = item_map.per_module[&self.module_id].clone(); + Ok(res) + } + + pub(crate) fn resolve_path( + &self, + db: &impl DescriptorDatabase, + path: Path, + ) -> Cancelable> { + let mut curr = match path.kind { + PathKind::Crate => self.crate_root(), + PathKind::Self_ | PathKind::Plain => self.clone(), + PathKind::Super => ctry!(self.parent()), + } + .def_id(db); + + let segments = path.segments; + for name in segments.iter() { + let module = match db.id_maps().def_loc(curr) { + DefLoc::Module { id, source_root } => ModuleDescriptor::new(db, source_root, id)?, + _ => return Ok(None), + }; + let scope = module.scope(db)?; + curr = ctry!(ctry!(scope.get(&name)).def_id); + } + Ok(Some(curr)) + } + + pub fn problems(&self, db: &impl DescriptorDatabase) -> Vec<(SyntaxNode, Problem)> { + self.module_id.problems(&self.tree, db) + } +} + +/// Phisically, rust source is organized as a set of files, but logically it is +/// organized as a tree of modules. Usually, a single file corresponds to a +/// single module, but it is not nessary the case. +/// +/// Module encapsulate the logic of transitioning from the fuzzy world of files +/// (which can have multiple parents) to the precise world of modules (which +/// always have one parent). +#[derive(Default, Debug, PartialEq, Eq)] +pub(crate) struct ModuleTree { + mods: Arena, + links: Arena, +} + +impl ModuleTree { + fn modules<'a>(&'a self) -> impl Iterator + 'a { + self.mods.iter().map(|(id, _)| id) + } + + fn modules_for_source(&self, source: ModuleSource) -> Vec { + self.mods + .iter() + .filter(|(_idx, it)| it.source == source) + .map(|(idx, _)| idx) + .collect() + } + + fn any_module_for_source(&self, source: ModuleSource) -> Option { + self.modules_for_source(source).pop() + } +} + +/// `ModuleSource` is the syntax tree element that produced this module: +/// either a file, or an inlinde module. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub(crate) enum ModuleSource { + SourceFile(FileId), + #[allow(dead_code)] + Module(SyntaxPtr), +} + +/// An owned syntax node for a module. Unlike `ModuleSource`, +/// this holds onto the AST for the whole file. +enum ModuleSourceNode { + SourceFile(ast::SourceFileNode), + Module(ast::ModuleNode), +} + +pub(crate) type ModuleId = Id; +type LinkId = Id; + +#[derive(Clone, Debug, Hash, PartialEq, Eq)] +pub enum Problem { + UnresolvedModule { + candidate: RelativePathBuf, + }, + NotDirOwner { + move_to: RelativePathBuf, + candidate: RelativePathBuf, + }, +} + +impl ModuleId { + fn source(self, tree: &ModuleTree) -> ModuleSource { + tree.mods[self].source + } + fn parent_link(self, tree: &ModuleTree) -> Option { + tree.mods[self].parent + } + fn parent(self, tree: &ModuleTree) -> Option { + let link = self.parent_link(tree)?; + Some(tree.links[link].owner) + } + fn crate_root(self, tree: &ModuleTree) -> ModuleId { + generate(Some(self), move |it| it.parent(tree)) + .last() + .unwrap() + } + fn child(self, tree: &ModuleTree, name: &str) -> Option { + let link = tree.mods[self] + .children + .iter() + .map(|&it| &tree.links[it]) + .find(|it| it.name == name)?; + Some(*link.points_to.first()?) + } + fn children<'a>(self, tree: &'a ModuleTree) -> impl Iterator + 'a { + tree.mods[self].children.iter().filter_map(move |&it| { + let link = &tree.links[it]; + let module = *link.points_to.first()?; + Some((link.name.clone(), module)) + }) + } + fn problems(self, tree: &ModuleTree, db: &impl SyntaxDatabase) -> Vec<(SyntaxNode, Problem)> { + tree.mods[self] + .children + .iter() + .filter_map(|&it| { + let p = tree.links[it].problem.clone()?; + let s = it.bind_source(tree, db); + let s = s.borrowed().name().unwrap().syntax().owned(); + Some((s, p)) + }) + .collect() + } +} + +impl LinkId { + fn owner(self, tree: &ModuleTree) -> ModuleId { + tree.links[self].owner + } + fn name(self, tree: &ModuleTree) -> SmolStr { + tree.links[self].name.clone() + } + fn bind_source<'a>(self, tree: &ModuleTree, db: &impl SyntaxDatabase) -> ast::ModuleNode { + let owner = self.owner(tree); + match owner.source(tree).resolve(db) { + ModuleSourceNode::SourceFile(root) => { + let ast = imp::modules(root.borrowed()) + .find(|(name, _)| name == &tree.links[self].name) + .unwrap() + .1; + ast.owned() + } + ModuleSourceNode::Module(it) => it, + } + } +} + +#[derive(Debug, PartialEq, Eq, Hash)] +pub(crate) struct ModuleData { + source: ModuleSource, + parent: Option, + children: Vec, +} + +impl ModuleSource { + fn new_inline(file_id: FileId, module: ast::Module) -> ModuleSource { + assert!(!module.has_semi()); + let ptr = SyntaxPtr::new(file_id, module.syntax()); + ModuleSource::Module(ptr) + } + + pub(crate) fn as_file(self) -> Option { + match self { + ModuleSource::SourceFile(f) => Some(f), + ModuleSource::Module(..) => None, + } + } + + pub(crate) fn file_id(self) -> FileId { + match self { + ModuleSource::SourceFile(f) => f, + ModuleSource::Module(ptr) => ptr.file_id(), + } + } + + fn resolve(self, db: &impl SyntaxDatabase) -> ModuleSourceNode { + match self { + ModuleSource::SourceFile(file_id) => { + let syntax = db.file_syntax(file_id); + ModuleSourceNode::SourceFile(syntax.ast().owned()) + } + ModuleSource::Module(ptr) => { + let syntax = db.resolve_syntax_ptr(ptr); + let syntax = syntax.borrowed(); + let module = ast::Module::cast(syntax).unwrap(); + ModuleSourceNode::Module(module.owned()) + } + } + } +} + +#[derive(Hash, Debug, PartialEq, Eq)] +struct LinkData { + owner: ModuleId, + name: SmolStr, + points_to: Vec, + problem: Option, +} + +impl ModuleTree { + fn push_mod(&mut self, data: ModuleData) -> ModuleId { + self.mods.alloc(data) + } + fn push_link(&mut self, data: LinkData) -> LinkId { + let owner = data.owner; + let id = self.links.alloc(data); + self.mods[owner].children.push(id); + id + } +} diff --git a/crates/ra_analysis/src/hir/module/nameres.rs b/crates/ra_analysis/src/hir/module/nameres.rs new file mode 100644 index 000000000..1ec85fbf2 --- /dev/null +++ b/crates/ra_analysis/src/hir/module/nameres.rs @@ -0,0 +1,549 @@ +//! Name resolution algorithm. The end result of the algorithm is `ItemMap`: a +//! map with maps each module to it's scope: the set of items, visible in the +//! module. That is, we only resolve imports here, name resolution of item +//! bodies will be done in a separate step. +//! +//! Like Rustc, we use an interative per-crate algorithm: we start with scopes +//! containing only directly defined items, and then iteratively resolve +//! imports. +//! +//! To make this work nicely in the IDE scenarios, we place `InputModuleItems` +//! in between raw syntax and name resolution. `InputModuleItems` are computed +//! using only the module's syntax, and it is all directly defined items plus +//! imports. The plain is to make `InputModuleItems` independent of local +//! modifications (that is, typing inside a function shold not change IMIs), +//! such that the results of name resolution can be preserved unless the module +//! structure itself is modified. +use std::{ + sync::Arc, + time::Instant, + ops::Index, +}; + +use rustc_hash::FxHashMap; + +use ra_syntax::{ + SyntaxNode, SyntaxNodeRef, TextRange, + SmolStr, SyntaxKind::{self, *}, + ast::{self, ModuleItemOwner, AstNode} +}; + +use crate::{ + Cancelable, FileId, + loc2id::{DefId, DefLoc}, + hir::{ + Path, PathKind, + DescriptorDatabase, + module::{ModuleId, ModuleTree, ModuleSourceNode}, + }, + input::SourceRootId, + arena::{Arena, Id} +}; + +/// Identifier of item within a specific file. This is stable over reparses, so +/// it's OK to use it as a salsa key/value. +pub(crate) type FileItemId = Id; + +/// Maps item's `SyntaxNode`s to `FileItemId` and back. +#[derive(Debug, PartialEq, Eq, Default)] +pub(crate) struct FileItems { + arena: Arena, +} + +impl FileItems { + fn alloc(&mut self, item: SyntaxNode) -> FileItemId { + self.arena.alloc(item) + } + fn id_of(&self, item: SyntaxNodeRef) -> FileItemId { + let (id, _item) = self + .arena + .iter() + .find(|(_id, i)| i.borrowed() == item) + .unwrap(); + id + } +} + +impl Index for FileItems { + type Output = SyntaxNode; + fn index(&self, idx: FileItemId) -> &SyntaxNode { + &self.arena[idx] + } +} + +pub(crate) fn file_items(db: &impl DescriptorDatabase, file_id: FileId) -> Arc { + let source_file = db.file_syntax(file_id); + let source_file = source_file.borrowed(); + let mut res = FileItems::default(); + source_file + .syntax() + .descendants() + .filter_map(ast::ModuleItem::cast) + .map(|it| it.syntax().owned()) + .for_each(|it| { + res.alloc(it); + }); + Arc::new(res) +} + +pub(crate) fn file_item( + db: &impl DescriptorDatabase, + file_id: FileId, + file_item_id: FileItemId, +) -> SyntaxNode { + db._file_items(file_id)[file_item_id].clone() +} + +/// Item map is the result of the name resolution. Item map contains, for each +/// module, the set of visible items. +#[derive(Default, Debug, PartialEq, Eq)] +pub(crate) struct ItemMap { + pub(crate) per_module: FxHashMap, +} + +#[derive(Debug, Default, PartialEq, Eq, Clone)] +pub(crate) struct ModuleScope { + items: FxHashMap, +} + +impl ModuleScope { + pub(crate) fn entries<'a>(&'a self) -> impl Iterator + 'a { + self.items.iter() + } + pub(crate) fn get(&self, name: &SmolStr) -> Option<&Resolution> { + self.items.get(name) + } +} + +/// A set of items and imports declared inside a module, without relation to +/// other modules. +/// +/// This stands in-between raw syntax and name resolution and alow us to avoid +/// recomputing name res: if `InputModuleItems` are the same, we can avoid +/// running name resolution. +#[derive(Debug, Default, PartialEq, Eq)] +pub(crate) struct InputModuleItems { + items: Vec, + imports: Vec, +} + +#[derive(Debug, PartialEq, Eq)] +struct ModuleItem { + id: FileItemId, + name: SmolStr, + kind: SyntaxKind, + vis: Vis, +} + +#[derive(Debug, PartialEq, Eq)] +enum Vis { + // Priv, + Other, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct Import { + path: Path, + kind: ImportKind, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct NamedImport { + file_item_id: FileItemId, + relative_range: TextRange, +} + +impl NamedImport { + pub(crate) fn range(&self, db: &impl DescriptorDatabase, file_id: FileId) -> TextRange { + let syntax = db._file_item(file_id, self.file_item_id); + let offset = syntax.borrowed().range().start(); + self.relative_range + offset + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +enum ImportKind { + Glob, + Named(NamedImport), +} + +pub(crate) fn input_module_items( + db: &impl DescriptorDatabase, + source_root: SourceRootId, + module_id: ModuleId, +) -> Cancelable> { + let module_tree = db._module_tree(source_root)?; + let source = module_id.source(&module_tree); + let file_items = db._file_items(source.file_id()); + let res = match source.resolve(db) { + ModuleSourceNode::SourceFile(it) => { + let items = it.borrowed().items(); + InputModuleItems::new(&file_items, items) + } + ModuleSourceNode::Module(it) => { + let items = it + .borrowed() + .item_list() + .into_iter() + .flat_map(|it| it.items()); + InputModuleItems::new(&file_items, items) + } + }; + Ok(Arc::new(res)) +} + +pub(crate) fn item_map( + db: &impl DescriptorDatabase, + source_root: SourceRootId, +) -> Cancelable> { + let start = Instant::now(); + let module_tree = db._module_tree(source_root)?; + let input = module_tree + .modules() + .map(|id| { + let items = db._input_module_items(source_root, id)?; + Ok((id, items)) + }) + .collect::>>()?; + let mut resolver = Resolver { + db: db, + input: &input, + source_root, + module_tree, + result: ItemMap::default(), + }; + resolver.resolve()?; + let res = resolver.result; + let elapsed = start.elapsed(); + log::info!("item_map: {:?}", elapsed); + Ok(Arc::new(res)) +} + +/// Resolution is basically `DefId` atm, but it should account for stuff like +/// multiple namespaces, ambiguity and errors. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct Resolution { + /// None for unresolved + pub(crate) def_id: Option, + /// ident by whitch this is imported into local scope. + pub(crate) import: Option, +} + +// #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +// enum Namespace { +// Types, +// Values, +// } + +// #[derive(Debug)] +// struct PerNs { +// types: Option, +// values: Option, +// } + +impl InputModuleItems { + fn new<'a>( + file_items: &FileItems, + items: impl Iterator>, + ) -> InputModuleItems { + let mut res = InputModuleItems::default(); + for item in items { + res.add_item(file_items, item); + } + res + } + + fn add_item(&mut self, file_items: &FileItems, item: ast::ModuleItem) -> Option<()> { + match item { + ast::ModuleItem::StructDef(it) => self.items.push(ModuleItem::new(file_items, it)?), + ast::ModuleItem::EnumDef(it) => self.items.push(ModuleItem::new(file_items, it)?), + ast::ModuleItem::FnDef(it) => self.items.push(ModuleItem::new(file_items, it)?), + ast::ModuleItem::TraitDef(it) => self.items.push(ModuleItem::new(file_items, it)?), + ast::ModuleItem::TypeDef(it) => self.items.push(ModuleItem::new(file_items, it)?), + ast::ModuleItem::ImplItem(_) => { + // impls don't define items + } + ast::ModuleItem::UseItem(it) => self.add_use_item(file_items, it), + ast::ModuleItem::ExternCrateItem(_) => { + // TODO + } + ast::ModuleItem::ConstDef(it) => self.items.push(ModuleItem::new(file_items, it)?), + ast::ModuleItem::StaticDef(it) => self.items.push(ModuleItem::new(file_items, it)?), + ast::ModuleItem::Module(it) => self.items.push(ModuleItem::new(file_items, it)?), + } + Some(()) + } + + fn add_use_item(&mut self, file_items: &FileItems, item: ast::UseItem) { + let file_item_id = file_items.id_of(item.syntax()); + let start_offset = item.syntax().range().start(); + Path::expand_use_item(item, |path, range| { + let kind = match range { + None => ImportKind::Glob, + Some(range) => ImportKind::Named(NamedImport { + file_item_id, + relative_range: range - start_offset, + }), + }; + self.imports.push(Import { kind, path }) + }) + } +} + +impl ModuleItem { + fn new<'a>(file_items: &FileItems, item: impl ast::NameOwner<'a>) -> Option { + let name = item.name()?.text(); + let kind = item.syntax().kind(); + let vis = Vis::Other; + let id = file_items.id_of(item.syntax()); + let res = ModuleItem { + id, + name, + kind, + vis, + }; + Some(res) + } +} + +struct Resolver<'a, DB> { + db: &'a DB, + input: &'a FxHashMap>, + source_root: SourceRootId, + module_tree: Arc, + result: ItemMap, +} + +impl<'a, DB> Resolver<'a, DB> +where + DB: DescriptorDatabase, +{ + fn resolve(&mut self) -> Cancelable<()> { + for (&module_id, items) in self.input.iter() { + self.populate_module(module_id, items) + } + + for &module_id in self.input.keys() { + crate::db::check_canceled(self.db)?; + self.resolve_imports(module_id); + } + Ok(()) + } + + fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) { + let file_id = module_id.source(&self.module_tree).file_id(); + + let mut module_items = ModuleScope::default(); + + for import in input.imports.iter() { + if let Some(name) = import.path.segments.iter().last() { + if let ImportKind::Named(import) = import.kind { + module_items.items.insert( + name.clone(), + Resolution { + def_id: None, + import: Some(import), + }, + ); + } + } + } + + for item in input.items.iter() { + if item.kind == MODULE { + // handle submodules separatelly + continue; + } + let def_loc = DefLoc::Item { + file_id, + id: item.id, + }; + let def_id = self.db.id_maps().def_id(def_loc); + let resolution = Resolution { + def_id: Some(def_id), + import: None, + }; + module_items.items.insert(item.name.clone(), resolution); + } + + for (name, mod_id) in module_id.children(&self.module_tree) { + let def_loc = DefLoc::Module { + id: mod_id, + source_root: self.source_root, + }; + let def_id = self.db.id_maps().def_id(def_loc); + let resolution = Resolution { + def_id: Some(def_id), + import: None, + }; + module_items.items.insert(name, resolution); + } + + self.result.per_module.insert(module_id, module_items); + } + + fn resolve_imports(&mut self, module_id: ModuleId) { + for import in self.input[&module_id].imports.iter() { + self.resolve_import(module_id, import); + } + } + + fn resolve_import(&mut self, module_id: ModuleId, import: &Import) { + let ptr = match import.kind { + ImportKind::Glob => return, + ImportKind::Named(ptr) => ptr, + }; + + let mut curr = match import.path.kind { + // TODO: handle extern crates + PathKind::Plain => return, + PathKind::Self_ => module_id, + PathKind::Super => { + match module_id.parent(&self.module_tree) { + Some(it) => it, + // TODO: error + None => return, + } + } + PathKind::Crate => module_id.crate_root(&self.module_tree), + }; + + for (i, name) in import.path.segments.iter().enumerate() { + let is_last = i == import.path.segments.len() - 1; + + let def_id = match self.result.per_module[&curr].items.get(name) { + None => return, + Some(res) => match res.def_id { + Some(it) => it, + None => return, + }, + }; + + if !is_last { + curr = match self.db.id_maps().def_loc(def_id) { + DefLoc::Module { id, .. } => id, + _ => return, + } + } else { + self.update(module_id, |items| { + let res = Resolution { + def_id: Some(def_id), + import: Some(ptr), + }; + items.items.insert(name.clone(), res); + }) + } + } + } + + fn update(&mut self, module_id: ModuleId, f: impl FnOnce(&mut ModuleScope)) { + let module_items = self.result.per_module.get_mut(&module_id).unwrap(); + f(module_items) + } +} + +#[cfg(test)] +mod tests { + use crate::{ + AnalysisChange, + mock_analysis::{MockAnalysis, analysis_and_position}, + hir::{DescriptorDatabase, module::ModuleDescriptor}, + input::FilesDatabase, +}; + use super::*; + + fn item_map(fixture: &str) -> (Arc, ModuleId) { + let (analysis, pos) = analysis_and_position(fixture); + let db = analysis.imp.db; + let source_root = db.file_source_root(pos.file_id); + let descr = ModuleDescriptor::guess_from_position(&*db, pos) + .unwrap() + .unwrap(); + let module_id = descr.module_id; + (db._item_map(source_root).unwrap(), module_id) + } + + #[test] + fn test_item_map() { + let (item_map, module_id) = item_map( + " + //- /lib.rs + mod foo; + + use crate::foo::bar::Baz; + <|> + + //- /foo/mod.rs + pub mod bar; + + //- /foo/bar.rs + pub struct Baz; + ", + ); + let name = SmolStr::from("Baz"); + let resolution = &item_map.per_module[&module_id].items[&name]; + assert!(resolution.def_id.is_some()); + } + + #[test] + fn typing_inside_a_function_should_not_invalidate_item_map() { + let mock_analysis = MockAnalysis::with_files( + " + //- /lib.rs + mod foo; + + use crate::foo::bar::Baz; + + fn foo() -> i32 { + 1 + 1 + } + //- /foo/mod.rs + pub mod bar; + + //- /foo/bar.rs + pub struct Baz; + ", + ); + + let file_id = mock_analysis.id_of("/lib.rs"); + let mut host = mock_analysis.analysis_host(); + + let source_root = host.analysis().imp.db.file_source_root(file_id); + + { + let db = host.analysis().imp.db; + let events = db.log_executed(|| { + db._item_map(source_root).unwrap(); + }); + assert!(format!("{:?}", events).contains("_item_map")) + } + + let mut change = AnalysisChange::new(); + + change.change_file( + file_id, + " + mod foo; + + use crate::foo::bar::Baz; + + fn foo() -> i32 { 92 } + " + .to_string(), + ); + + host.apply_change(change); + + { + let db = host.analysis().imp.db; + let events = db.log_executed(|| { + db._item_map(source_root).unwrap(); + }); + assert!( + !format!("{:?}", events).contains("_item_map"), + "{:#?}", + events + ) + } + } +} diff --git a/crates/ra_analysis/src/hir/path.rs b/crates/ra_analysis/src/hir/path.rs new file mode 100644 index 000000000..8279daf4b --- /dev/null +++ b/crates/ra_analysis/src/hir/path.rs @@ -0,0 +1,148 @@ +use ra_syntax::{SmolStr, ast, AstNode, TextRange}; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct Path { + pub(crate) kind: PathKind, + pub(crate) segments: Vec, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum PathKind { + Plain, + Self_, + Super, + Crate, +} + +impl Path { + /// Calls `cb` with all paths, represented by this use item. + pub(crate) fn expand_use_item(item: ast::UseItem, mut cb: impl FnMut(Path, Option)) { + if let Some(tree) = item.use_tree() { + expand_use_tree(None, tree, &mut cb); + } + } + + /// Converts an `ast::Path` to `Path`. Works with use trees. + pub(crate) fn from_ast(mut path: ast::Path) -> Option { + let mut kind = PathKind::Plain; + let mut segments = Vec::new(); + loop { + let segment = path.segment()?; + match segment.kind()? { + ast::PathSegmentKind::Name(name) => segments.push(name.text()), + ast::PathSegmentKind::CrateKw => { + kind = PathKind::Crate; + break; + } + ast::PathSegmentKind::SelfKw => { + kind = PathKind::Self_; + break; + } + ast::PathSegmentKind::SuperKw => { + kind = PathKind::Super; + break; + } + } + path = match qualifier(path) { + Some(it) => it, + None => break, + }; + } + segments.reverse(); + return Some(Path { kind, segments }); + + fn qualifier(path: ast::Path) -> Option { + if let Some(q) = path.qualifier() { + return Some(q); + } + // TODO: this bottom up traversal is not too precise. + // Should we handle do a top-down analysiss, recording results? + let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?; + let use_tree = use_tree_list.parent_use_tree(); + use_tree.path() + } + } + + /// `true` is this path is a single identifier, like `foo` + pub(crate) fn is_ident(&self) -> bool { + self.kind == PathKind::Plain && self.segments.len() == 1 + } +} + +fn expand_use_tree( + prefix: Option, + tree: ast::UseTree, + cb: &mut impl FnMut(Path, Option), +) { + if let Some(use_tree_list) = tree.use_tree_list() { + let prefix = match tree.path() { + None => prefix, + Some(path) => match convert_path(prefix, path) { + Some(it) => Some(it), + None => return, // TODO: report errors somewhere + }, + }; + for tree in use_tree_list.use_trees() { + expand_use_tree(prefix.clone(), tree, cb); + } + } else { + if let Some(ast_path) = tree.path() { + if let Some(path) = convert_path(prefix, ast_path) { + let range = if tree.has_star() { + None + } else { + let range = ast_path.segment().unwrap().syntax().range(); + Some(range) + }; + cb(path, range) + } + } + } +} + +fn convert_path(prefix: Option, path: ast::Path) -> Option { + let prefix = if let Some(qual) = path.qualifier() { + Some(convert_path(prefix, qual)?) + } else { + None + }; + let segment = path.segment()?; + let res = match segment.kind()? { + ast::PathSegmentKind::Name(name) => { + let mut res = prefix.unwrap_or_else(|| Path { + kind: PathKind::Plain, + segments: Vec::with_capacity(1), + }); + res.segments.push(name.text()); + res + } + ast::PathSegmentKind::CrateKw => { + if prefix.is_some() { + return None; + } + Path { + kind: PathKind::Crate, + segments: Vec::new(), + } + } + ast::PathSegmentKind::SelfKw => { + if prefix.is_some() { + return None; + } + Path { + kind: PathKind::Self_, + segments: Vec::new(), + } + } + ast::PathSegmentKind::SuperKw => { + if prefix.is_some() { + return None; + } + Path { + kind: PathKind::Super, + segments: Vec::new(), + } + } + }; + Some(res) +} diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 8a41b3152..8d60f9af7 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -19,7 +19,7 @@ use salsa::{Database, ParallelDatabase}; use crate::{ completion::{completions, CompletionItem}, db::{self, FileSyntaxQuery, SyntaxDatabase}, - descriptors::{ + hir::{ function::{FnDescriptor, FnId}, module::{ModuleDescriptor, Problem}, DeclarationDescriptor, DescriptorDatabase, @@ -589,7 +589,7 @@ fn resolve_local_name( let fn_def = name_ref.syntax().ancestors().find_map(ast::FnDef::cast)?; let fn_id = FnId::get(db, file_id, fn_def); let scopes = db.fn_scopes(fn_id); - let scope_entry = crate::descriptors::function::resolve_local_name(name_ref, &scopes)?; + let scope_entry = crate::hir::function::resolve_local_name(name_ref, &scopes)?; let syntax = db.resolve_syntax_ptr(scope_entry.ptr().into_global(file_id)); Some((scope_entry.name().clone(), syntax.range())) } diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 0fbfd8a40..4659eb523 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -19,14 +19,14 @@ macro_rules! ctry { } mod arena; +mod syntax_ptr; +mod input; mod db; mod loc2id; -mod input; mod imp; mod completion; -mod descriptors; +mod hir; mod symbol_index; -mod syntax_ptr; pub mod mock_analysis; use std::{fmt, sync::Arc}; @@ -42,7 +42,7 @@ use crate::{ pub use crate::{ completion::CompletionItem, - descriptors::function::FnDescriptor, + hir::function::FnDescriptor, input::{CrateGraph, CrateId, FileId, FileResolver}, }; pub use ra_editor::{ diff --git a/crates/ra_analysis/src/loc2id.rs b/crates/ra_analysis/src/loc2id.rs index c7c799a91..d42af4e0a 100644 --- a/crates/ra_analysis/src/loc2id.rs +++ b/crates/ra_analysis/src/loc2id.rs @@ -9,8 +9,8 @@ use rustc_hash::FxHashMap; use crate::{ FileId, - descriptors::FileItemId, - descriptors::module::ModuleId, + hir::FileItemId, + hir::module::ModuleId, syntax_ptr::SyntaxPtr, input::SourceRootId, }; -- cgit v1.2.3 From f4d0cb64fc8b3010bdc4b168a2fa6d96a6cf90b1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 27 Nov 2018 21:58:44 +0300 Subject: rename descriptorsdb -> hirdb --- crates/ra_analysis/src/db.rs | 2 +- crates/ra_analysis/src/hir/function/imp.rs | 6 +++--- crates/ra_analysis/src/hir/mod.rs | 4 ++-- crates/ra_analysis/src/hir/module/imp.rs | 10 +++++----- crates/ra_analysis/src/hir/module/mod.rs | 23 ++++++++++------------- crates/ra_analysis/src/hir/module/nameres.rs | 16 ++++++++-------- crates/ra_analysis/src/imp.rs | 2 +- 7 files changed, 30 insertions(+), 33 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index fa59b8b3e..b4fbbe7ad 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -122,7 +122,7 @@ salsa::database_storage! { fn file_symbols() for FileSymbolsQuery; fn resolve_syntax_ptr() for ResolveSyntaxPtrQuery; } - impl hir::DescriptorDatabase { + impl hir::HirDatabase { fn module_tree() for hir::ModuleTreeQuery; fn fn_scopes() for hir::FnScopesQuery; fn _file_items() for hir::FileItemsQuery; diff --git a/crates/ra_analysis/src/hir/function/imp.rs b/crates/ra_analysis/src/hir/function/imp.rs index cd573a47f..0d487d024 100644 --- a/crates/ra_analysis/src/hir/function/imp.rs +++ b/crates/ra_analysis/src/hir/function/imp.rs @@ -4,17 +4,17 @@ use ra_syntax::ast::{AstNode, FnDef, FnDefNode}; use crate::hir::{ function::{FnId, FnScopes}, - DescriptorDatabase, + HirDatabase, }; /// Resolve `FnId` to the corresponding `SyntaxNode` -pub(crate) fn fn_syntax(db: &impl DescriptorDatabase, fn_id: FnId) -> FnDefNode { +pub(crate) fn fn_syntax(db: &impl HirDatabase, fn_id: FnId) -> FnDefNode { let ptr = db.id_maps().fn_ptr(fn_id); let syntax = db.resolve_syntax_ptr(ptr); FnDef::cast(syntax.borrowed()).unwrap().owned() } -pub(crate) fn fn_scopes(db: &impl DescriptorDatabase, fn_id: FnId) -> Arc { +pub(crate) fn fn_scopes(db: &impl HirDatabase, fn_id: FnId) -> Arc { let syntax = db._fn_syntax(fn_id); let res = FnScopes::new(syntax.borrowed()); Arc::new(res) diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs index 6bee2b5c4..232a8558b 100644 --- a/crates/ra_analysis/src/hir/mod.rs +++ b/crates/ra_analysis/src/hir/mod.rs @@ -34,7 +34,7 @@ pub(crate) use self::path::{Path, PathKind}; pub(crate) use self::module::nameres::FileItemId; salsa::query_group! { - pub(crate) trait DescriptorDatabase: SyntaxDatabase + IdDatabase { +pub(crate) trait HirDatabase: SyntaxDatabase + IdDatabase { fn fn_scopes(fn_id: FnId) -> Arc { type FnScopesQuery; use fn function::imp::fn_scopes; @@ -83,7 +83,7 @@ pub(crate) enum Def { } impl DefId { - pub(crate) fn resolve(self, db: &impl DescriptorDatabase) -> Cancelable { + pub(crate) fn resolve(self, db: &impl HirDatabase) -> Cancelable { let loc = db.id_maps().def_loc(self); let res = match loc { DefLoc::Module { id, source_root } => { diff --git a/crates/ra_analysis/src/hir/module/imp.rs b/crates/ra_analysis/src/hir/module/imp.rs index d8539ed85..062457ae0 100644 --- a/crates/ra_analysis/src/hir/module/imp.rs +++ b/crates/ra_analysis/src/hir/module/imp.rs @@ -9,7 +9,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; use crate::{ db, - hir::DescriptorDatabase, + hir::HirDatabase, input::{SourceRoot, SourceRootId}, Cancelable, FileId, FileResolverImp, }; @@ -35,7 +35,7 @@ impl Submodule { } pub(crate) fn submodules( - db: &impl DescriptorDatabase, + db: &impl HirDatabase, source: ModuleSource, ) -> Cancelable>> { db::check_canceled(db)?; @@ -82,7 +82,7 @@ pub(crate) fn modules<'a>( } pub(crate) fn module_tree( - db: &impl DescriptorDatabase, + db: &impl HirDatabase, source_root: SourceRootId, ) -> Cancelable> { db::check_canceled(db)?; @@ -91,7 +91,7 @@ pub(crate) fn module_tree( } fn create_module_tree<'a>( - db: &impl DescriptorDatabase, + db: &impl HirDatabase, source_root: SourceRootId, ) -> Cancelable { let mut tree = ModuleTree::default(); @@ -121,7 +121,7 @@ fn create_module_tree<'a>( } fn build_subtree( - db: &impl DescriptorDatabase, + db: &impl HirDatabase, source_root: &SourceRoot, tree: &mut ModuleTree, visited: &mut FxHashSet, diff --git a/crates/ra_analysis/src/hir/module/mod.rs b/crates/ra_analysis/src/hir/module/mod.rs index f374a079f..5bff11c90 100644 --- a/crates/ra_analysis/src/hir/module/mod.rs +++ b/crates/ra_analysis/src/hir/module/mod.rs @@ -14,7 +14,7 @@ use relative_path::RelativePathBuf; use crate::{ db::SyntaxDatabase, syntax_ptr::SyntaxPtr, FileId, FilePosition, Cancelable, - hir::{Path, PathKind, DescriptorDatabase}, + hir::{Path, PathKind, HirDatabase}, input::SourceRootId, arena::{Arena, Id}, loc2id::{DefLoc, DefId}, @@ -36,7 +36,7 @@ impl ModuleDescriptor { /// lossy transformation: in general, a single source might correspond to /// several modules. pub fn guess_from_file_id( - db: &impl DescriptorDatabase, + db: &impl HirDatabase, file_id: FileId, ) -> Cancelable> { ModuleDescriptor::guess_from_source(db, file_id, ModuleSource::SourceFile(file_id)) @@ -46,7 +46,7 @@ impl ModuleDescriptor { /// is inherently lossy transformation: in general, a single source might /// correspond to several modules. pub fn guess_from_position( - db: &impl DescriptorDatabase, + db: &impl HirDatabase, position: FilePosition, ) -> Cancelable> { let file = db.file_syntax(position.file_id); @@ -59,7 +59,7 @@ impl ModuleDescriptor { } fn guess_from_source( - db: &impl DescriptorDatabase, + db: &impl HirDatabase, file_id: FileId, module_source: ModuleSource, ) -> Cancelable> { @@ -78,7 +78,7 @@ impl ModuleDescriptor { } pub(super) fn new( - db: &impl DescriptorDatabase, + db: &impl HirDatabase, source_root_id: SourceRootId, module_id: ModuleId, ) -> Cancelable { @@ -93,10 +93,7 @@ impl ModuleDescriptor { /// Returns `mod foo;` or `mod foo {}` node whihc declared this module. /// Returns `None` for the root module - pub fn parent_link_source( - &self, - db: &impl DescriptorDatabase, - ) -> Option<(FileId, ast::ModuleNode)> { + pub fn parent_link_source(&self, db: &impl HirDatabase) -> Option<(FileId, ast::ModuleNode)> { let link = self.module_id.parent_link(&self.tree)?; let file_id = link.owner(&self.tree).source(&self.tree).file_id(); let src = link.bind_source(&self.tree, db); @@ -132,7 +129,7 @@ impl ModuleDescriptor { Some(link.name(&self.tree)) } - pub fn def_id(&self, db: &impl DescriptorDatabase) -> DefId { + pub fn def_id(&self, db: &impl HirDatabase) -> DefId { let def_loc = DefLoc::Module { id: self.module_id, source_root: self.source_root_id, @@ -150,7 +147,7 @@ impl ModuleDescriptor { } /// Returns a `ModuleScope`: a set of items, visible in this module. - pub(crate) fn scope(&self, db: &impl DescriptorDatabase) -> Cancelable { + pub(crate) fn scope(&self, db: &impl HirDatabase) -> Cancelable { let item_map = db._item_map(self.source_root_id)?; let res = item_map.per_module[&self.module_id].clone(); Ok(res) @@ -158,7 +155,7 @@ impl ModuleDescriptor { pub(crate) fn resolve_path( &self, - db: &impl DescriptorDatabase, + db: &impl HirDatabase, path: Path, ) -> Cancelable> { let mut curr = match path.kind { @@ -180,7 +177,7 @@ impl ModuleDescriptor { Ok(Some(curr)) } - pub fn problems(&self, db: &impl DescriptorDatabase) -> Vec<(SyntaxNode, Problem)> { + pub fn problems(&self, db: &impl HirDatabase) -> Vec<(SyntaxNode, Problem)> { self.module_id.problems(&self.tree, db) } } diff --git a/crates/ra_analysis/src/hir/module/nameres.rs b/crates/ra_analysis/src/hir/module/nameres.rs index 1ec85fbf2..bb5a888c8 100644 --- a/crates/ra_analysis/src/hir/module/nameres.rs +++ b/crates/ra_analysis/src/hir/module/nameres.rs @@ -33,7 +33,7 @@ use crate::{ loc2id::{DefId, DefLoc}, hir::{ Path, PathKind, - DescriptorDatabase, + HirDatabase, module::{ModuleId, ModuleTree, ModuleSourceNode}, }, input::SourceRootId, @@ -71,7 +71,7 @@ impl Index for FileItems { } } -pub(crate) fn file_items(db: &impl DescriptorDatabase, file_id: FileId) -> Arc { +pub(crate) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc { let source_file = db.file_syntax(file_id); let source_file = source_file.borrowed(); let mut res = FileItems::default(); @@ -87,7 +87,7 @@ pub(crate) fn file_items(db: &impl DescriptorDatabase, file_id: FileId) -> Arc SyntaxNode { @@ -154,7 +154,7 @@ pub(crate) struct NamedImport { } impl NamedImport { - pub(crate) fn range(&self, db: &impl DescriptorDatabase, file_id: FileId) -> TextRange { + pub(crate) fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange { let syntax = db._file_item(file_id, self.file_item_id); let offset = syntax.borrowed().range().start(); self.relative_range + offset @@ -168,7 +168,7 @@ enum ImportKind { } pub(crate) fn input_module_items( - db: &impl DescriptorDatabase, + db: &impl HirDatabase, source_root: SourceRootId, module_id: ModuleId, ) -> Cancelable> { @@ -193,7 +193,7 @@ pub(crate) fn input_module_items( } pub(crate) fn item_map( - db: &impl DescriptorDatabase, + db: &impl HirDatabase, source_root: SourceRootId, ) -> Cancelable> { let start = Instant::now(); @@ -316,7 +316,7 @@ struct Resolver<'a, DB> { impl<'a, DB> Resolver<'a, DB> where - DB: DescriptorDatabase, + DB: HirDatabase, { fn resolve(&mut self) -> Cancelable<()> { for (&module_id, items) in self.input.iter() { @@ -447,7 +447,7 @@ mod tests { use crate::{ AnalysisChange, mock_analysis::{MockAnalysis, analysis_and_position}, - hir::{DescriptorDatabase, module::ModuleDescriptor}, + hir::{HirDatabase, module::ModuleDescriptor}, input::FilesDatabase, }; use super::*; diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 8d60f9af7..6dadc059f 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -22,7 +22,7 @@ use crate::{ hir::{ function::{FnDescriptor, FnId}, module::{ModuleDescriptor, Problem}, - DeclarationDescriptor, DescriptorDatabase, + DeclarationDescriptor, HirDatabase, }, input::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE}, symbol_index::SymbolIndex, -- cgit v1.2.3 From 109a7f3717612c58c73c5c153b632385b922fc9d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 27 Nov 2018 22:58:09 +0300 Subject: itroduce FunctionDescriptor --- crates/ra_analysis/src/hir/function/mod.rs | 25 ++++++++++++++++++++++++- crates/ra_analysis/src/hir/mod.rs | 9 ++++++--- crates/ra_analysis/src/imp.rs | 11 ++++++----- 3 files changed, 36 insertions(+), 9 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/hir/function/mod.rs b/crates/ra_analysis/src/hir/function/mod.rs index 86eee5e93..c8af6bc21 100644 --- a/crates/ra_analysis/src/hir/function/mod.rs +++ b/crates/ra_analysis/src/hir/function/mod.rs @@ -1,7 +1,10 @@ pub(super) mod imp; mod scope; -use std::cmp::{max, min}; +use std::{ + cmp::{max, min}, + sync::Arc, +}; use ra_syntax::{ ast::{self, AstNode, DocCommentsOwner, NameOwner}, @@ -9,6 +12,7 @@ use ra_syntax::{ }; use crate::{ + hir::HirDatabase, syntax_ptr::SyntaxPtr, FileId, loc2id::IdDatabase, }; @@ -23,6 +27,25 @@ impl FnId { } } +pub(crate) struct FunctionDescriptor { + fn_id: FnId, +} + +impl FunctionDescriptor { + pub(crate) fn guess_from_source( + db: &impl HirDatabase, + file_id: FileId, + fn_def: ast::FnDef, + ) -> FunctionDescriptor { + let fn_id = FnId::get(db, file_id, fn_def); + FunctionDescriptor { fn_id } + } + + pub(crate) fn scope(&self, db: &impl HirDatabase) -> Arc { + db.fn_scopes(self.fn_id) + } +} + #[derive(Debug, Clone)] pub struct FnDescriptor { pub name: String, diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs index 232a8558b..edeaeb8e6 100644 --- a/crates/ra_analysis/src/hir/mod.rs +++ b/crates/ra_analysis/src/hir/mod.rs @@ -21,7 +21,7 @@ use crate::{ db::SyntaxDatabase, hir::function::{resolve_local_name, FnId, FnScopes}, hir::module::{ - ModuleId, ModuleTree, ModuleSource, ModuleDescriptor, + ModuleId, ModuleTree, ModuleSource, nameres::{ItemMap, InputModuleItems, FileItems} }, input::SourceRootId, @@ -30,8 +30,11 @@ use crate::{ Cancelable, }; -pub(crate) use self::path::{Path, PathKind}; -pub(crate) use self::module::nameres::FileItemId; +pub(crate) use self::{ + path::{Path, PathKind}, + module::{ModuleDescriptor, nameres::FileItemId}, + function::FunctionDescriptor, +}; salsa::query_group! { pub(crate) trait HirDatabase: SyntaxDatabase + IdDatabase { diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 6dadc059f..ad4b40c58 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -20,9 +20,10 @@ use crate::{ completion::{completions, CompletionItem}, db::{self, FileSyntaxQuery, SyntaxDatabase}, hir::{ - function::{FnDescriptor, FnId}, - module::{ModuleDescriptor, Problem}, - DeclarationDescriptor, HirDatabase, + FunctionDescriptor, ModuleDescriptor, + function::FnDescriptor, + module::{Problem}, + DeclarationDescriptor, }, input::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE}, symbol_index::SymbolIndex, @@ -587,8 +588,8 @@ fn resolve_local_name( name_ref: ast::NameRef, ) -> Option<(SmolStr, TextRange)> { let fn_def = name_ref.syntax().ancestors().find_map(ast::FnDef::cast)?; - let fn_id = FnId::get(db, file_id, fn_def); - let scopes = db.fn_scopes(fn_id); + let function = FunctionDescriptor::guess_from_source(db, file_id, fn_def); + let scopes = function.scope(db); let scope_entry = crate::hir::function::resolve_local_name(name_ref, &scopes)?; let syntax = db.resolve_syntax_ptr(scope_entry.ptr().into_global(file_id)); Some((scope_entry.name().clone(), syntax.range())) -- cgit v1.2.3 From f14902f67bbc5f68cb700b6b929b269d3d51a4a9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 27 Nov 2018 23:03:08 +0300 Subject: move hir db --- crates/ra_analysis/src/db.rs | 18 +++++------ crates/ra_analysis/src/hir/db.rs | 63 +++++++++++++++++++++++++++++++++++++++ crates/ra_analysis/src/hir/mod.rs | 63 ++++----------------------------------- 3 files changed, 78 insertions(+), 66 deletions(-) create mode 100644 crates/ra_analysis/src/hir/db.rs (limited to 'crates') diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index b4fbbe7ad..08aa9053b 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -122,15 +122,15 @@ salsa::database_storage! { fn file_symbols() for FileSymbolsQuery; fn resolve_syntax_ptr() for ResolveSyntaxPtrQuery; } - impl hir::HirDatabase { - fn module_tree() for hir::ModuleTreeQuery; - fn fn_scopes() for hir::FnScopesQuery; - fn _file_items() for hir::FileItemsQuery; - fn _file_item() for hir::FileItemQuery; - fn _input_module_items() for hir::InputModuleItemsQuery; - fn _item_map() for hir::ItemMapQuery; - fn _fn_syntax() for hir::FnSyntaxQuery; - fn _submodules() for hir::SubmodulesQuery; + impl hir::db::HirDatabase { + fn module_tree() for hir::db::ModuleTreeQuery; + fn fn_scopes() for hir::db::FnScopesQuery; + fn _file_items() for hir::db::FileItemsQuery; + fn _file_item() for hir::db::FileItemQuery; + fn _input_module_items() for hir::db::InputModuleItemsQuery; + fn _item_map() for hir::db::ItemMapQuery; + fn _fn_syntax() for hir::db::FnSyntaxQuery; + fn _submodules() for hir::db::SubmodulesQuery; } } } diff --git a/crates/ra_analysis/src/hir/db.rs b/crates/ra_analysis/src/hir/db.rs new file mode 100644 index 000000000..3cdf8e6d6 --- /dev/null +++ b/crates/ra_analysis/src/hir/db.rs @@ -0,0 +1,63 @@ +use std::sync::Arc; + +use ra_syntax::{ + SyntaxNode, + ast::FnDefNode, +}; + +use crate::{ + FileId, + db::SyntaxDatabase, + hir::function::{FnId, FnScopes}, + hir::module::{ + ModuleId, ModuleTree, ModuleSource, + nameres::{ItemMap, InputModuleItems, FileItems, FileItemId} + }, + input::SourceRootId, + loc2id::{IdDatabase}, + Cancelable, +}; + +salsa::query_group! { +pub(crate) trait HirDatabase: SyntaxDatabase + IdDatabase { + fn fn_scopes(fn_id: FnId) -> Arc { + type FnScopesQuery; + use fn crate::hir::function::imp::fn_scopes; + } + + fn _file_items(file_id: FileId) -> Arc { + type FileItemsQuery; + storage dependencies; + use fn crate::hir::module::nameres::file_items; + } + + fn _file_item(file_id: FileId, file_item_id: FileItemId) -> SyntaxNode { + type FileItemQuery; + storage dependencies; + use fn crate::hir::module::nameres::file_item; + } + + fn _input_module_items(source_root_id: SourceRootId, module_id: ModuleId) -> Cancelable> { + type InputModuleItemsQuery; + use fn crate::hir::module::nameres::input_module_items; + } + fn _item_map(source_root_id: SourceRootId) -> Cancelable> { + type ItemMapQuery; + use fn crate::hir::module::nameres::item_map; + } + fn _module_tree(source_root_id: SourceRootId) -> Cancelable> { + type ModuleTreeQuery; + use fn crate::hir::module::imp::module_tree; + } + fn _fn_syntax(fn_id: FnId) -> FnDefNode { + type FnSyntaxQuery; + // Don't retain syntax trees in memory + storage dependencies; + use fn crate::hir::function::imp::fn_syntax; + } + fn _submodules(source: ModuleSource) -> Cancelable>> { + type SubmodulesQuery; + use fn crate::hir::module::imp::submodules; + } + } +} diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs index edeaeb8e6..27ac71a26 100644 --- a/crates/ra_analysis/src/hir/mod.rs +++ b/crates/ra_analysis/src/hir/mod.rs @@ -7,25 +7,18 @@ pub(crate) mod function; pub(crate) mod module; +pub(crate) mod db; mod path; -use std::sync::Arc; - use ra_syntax::{ - ast::{self, FnDefNode, AstNode}, - TextRange, SyntaxNode, + ast::{self, AstNode}, + TextRange, }; use crate::{ - FileId, - db::SyntaxDatabase, - hir::function::{resolve_local_name, FnId, FnScopes}, - hir::module::{ - ModuleId, ModuleTree, ModuleSource, - nameres::{ItemMap, InputModuleItems, FileItems} - }, - input::SourceRootId, - loc2id::{IdDatabase, DefId, DefLoc}, + hir::db::HirDatabase, + hir::function::{resolve_local_name, FnScopes}, + loc2id::{DefId, DefLoc}, syntax_ptr::LocalSyntaxPtr, Cancelable, }; @@ -36,50 +29,6 @@ pub(crate) use self::{ function::FunctionDescriptor, }; -salsa::query_group! { -pub(crate) trait HirDatabase: SyntaxDatabase + IdDatabase { - fn fn_scopes(fn_id: FnId) -> Arc { - type FnScopesQuery; - use fn function::imp::fn_scopes; - } - - fn _file_items(file_id: FileId) -> Arc { - type FileItemsQuery; - storage dependencies; - use fn module::nameres::file_items; - } - - fn _file_item(file_id: FileId, file_item_id: FileItemId) -> SyntaxNode { - type FileItemQuery; - storage dependencies; - use fn module::nameres::file_item; - } - - fn _input_module_items(source_root_id: SourceRootId, module_id: ModuleId) -> Cancelable> { - type InputModuleItemsQuery; - use fn module::nameres::input_module_items; - } - fn _item_map(source_root_id: SourceRootId) -> Cancelable> { - type ItemMapQuery; - use fn module::nameres::item_map; - } - fn _module_tree(source_root_id: SourceRootId) -> Cancelable> { - type ModuleTreeQuery; - use fn module::imp::module_tree; - } - fn _fn_syntax(fn_id: FnId) -> FnDefNode { - type FnSyntaxQuery; - // Don't retain syntax trees in memory - storage dependencies; - use fn function::imp::fn_syntax; - } - fn _submodules(source: ModuleSource) -> Cancelable>> { - type SubmodulesQuery; - use fn module::imp::submodules; - } - } -} - pub(crate) enum Def { Module(ModuleDescriptor), Item, -- cgit v1.2.3 From 90bc832b22aefb2a382b3465793df7e528351aa1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 27 Nov 2018 23:05:33 +0300 Subject: remove underscores --- crates/ra_analysis/src/hir/db.rs | 14 +++++++------- crates/ra_analysis/src/hir/function/imp.rs | 2 +- crates/ra_analysis/src/hir/module/imp.rs | 2 +- crates/ra_analysis/src/hir/module/mod.rs | 6 +++--- crates/ra_analysis/src/hir/module/nameres.rs | 20 ++++++++++---------- 5 files changed, 22 insertions(+), 22 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/hir/db.rs b/crates/ra_analysis/src/hir/db.rs index 3cdf8e6d6..498a5c2dd 100644 --- a/crates/ra_analysis/src/hir/db.rs +++ b/crates/ra_analysis/src/hir/db.rs @@ -25,37 +25,37 @@ pub(crate) trait HirDatabase: SyntaxDatabase + IdDatabase { use fn crate::hir::function::imp::fn_scopes; } - fn _file_items(file_id: FileId) -> Arc { + fn file_items(file_id: FileId) -> Arc { type FileItemsQuery; storage dependencies; use fn crate::hir::module::nameres::file_items; } - fn _file_item(file_id: FileId, file_item_id: FileItemId) -> SyntaxNode { + fn file_item(file_id: FileId, file_item_id: FileItemId) -> SyntaxNode { type FileItemQuery; storage dependencies; use fn crate::hir::module::nameres::file_item; } - fn _input_module_items(source_root_id: SourceRootId, module_id: ModuleId) -> Cancelable> { + fn input_module_items(source_root_id: SourceRootId, module_id: ModuleId) -> Cancelable> { type InputModuleItemsQuery; use fn crate::hir::module::nameres::input_module_items; } - fn _item_map(source_root_id: SourceRootId) -> Cancelable> { + fn item_map(source_root_id: SourceRootId) -> Cancelable> { type ItemMapQuery; use fn crate::hir::module::nameres::item_map; } - fn _module_tree(source_root_id: SourceRootId) -> Cancelable> { + fn module_tree(source_root_id: SourceRootId) -> Cancelable> { type ModuleTreeQuery; use fn crate::hir::module::imp::module_tree; } - fn _fn_syntax(fn_id: FnId) -> FnDefNode { + fn fn_syntax(fn_id: FnId) -> FnDefNode { type FnSyntaxQuery; // Don't retain syntax trees in memory storage dependencies; use fn crate::hir::function::imp::fn_syntax; } - fn _submodules(source: ModuleSource) -> Cancelable>> { + fn submodules(source: ModuleSource) -> Cancelable>> { type SubmodulesQuery; use fn crate::hir::module::imp::submodules; } diff --git a/crates/ra_analysis/src/hir/function/imp.rs b/crates/ra_analysis/src/hir/function/imp.rs index 0d487d024..5f5f68ac5 100644 --- a/crates/ra_analysis/src/hir/function/imp.rs +++ b/crates/ra_analysis/src/hir/function/imp.rs @@ -15,7 +15,7 @@ pub(crate) fn fn_syntax(db: &impl HirDatabase, fn_id: FnId) -> FnDefNode { } pub(crate) fn fn_scopes(db: &impl HirDatabase, fn_id: FnId) -> Arc { - let syntax = db._fn_syntax(fn_id); + let syntax = db.fn_syntax(fn_id); let res = FnScopes::new(syntax.borrowed()); Arc::new(res) } diff --git a/crates/ra_analysis/src/hir/module/imp.rs b/crates/ra_analysis/src/hir/module/imp.rs index 062457ae0..f9853584e 100644 --- a/crates/ra_analysis/src/hir/module/imp.rs +++ b/crates/ra_analysis/src/hir/module/imp.rs @@ -135,7 +135,7 @@ fn build_subtree( parent, children: Vec::new(), }); - for sub in db._submodules(source)?.iter() { + for sub in db.submodules(source)?.iter() { let link = tree.push_link(LinkData { name: sub.name().clone(), owner: id, diff --git a/crates/ra_analysis/src/hir/module/mod.rs b/crates/ra_analysis/src/hir/module/mod.rs index 5bff11c90..55b6639be 100644 --- a/crates/ra_analysis/src/hir/module/mod.rs +++ b/crates/ra_analysis/src/hir/module/mod.rs @@ -64,7 +64,7 @@ impl ModuleDescriptor { module_source: ModuleSource, ) -> Cancelable> { let source_root_id = db.file_source_root(file_id); - let module_tree = db._module_tree(source_root_id)?; + let module_tree = db.module_tree(source_root_id)?; let res = match module_tree.any_module_for_source(module_source) { None => None, @@ -82,7 +82,7 @@ impl ModuleDescriptor { source_root_id: SourceRootId, module_id: ModuleId, ) -> Cancelable { - let module_tree = db._module_tree(source_root_id)?; + let module_tree = db.module_tree(source_root_id)?; let res = ModuleDescriptor { tree: module_tree, source_root_id, @@ -148,7 +148,7 @@ impl ModuleDescriptor { /// Returns a `ModuleScope`: a set of items, visible in this module. pub(crate) fn scope(&self, db: &impl HirDatabase) -> Cancelable { - let item_map = db._item_map(self.source_root_id)?; + let item_map = db.item_map(self.source_root_id)?; let res = item_map.per_module[&self.module_id].clone(); Ok(res) } diff --git a/crates/ra_analysis/src/hir/module/nameres.rs b/crates/ra_analysis/src/hir/module/nameres.rs index bb5a888c8..eaf9f9373 100644 --- a/crates/ra_analysis/src/hir/module/nameres.rs +++ b/crates/ra_analysis/src/hir/module/nameres.rs @@ -91,7 +91,7 @@ pub(crate) fn file_item( file_id: FileId, file_item_id: FileItemId, ) -> SyntaxNode { - db._file_items(file_id)[file_item_id].clone() + db.file_items(file_id)[file_item_id].clone() } /// Item map is the result of the name resolution. Item map contains, for each @@ -155,7 +155,7 @@ pub(crate) struct NamedImport { impl NamedImport { pub(crate) fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange { - let syntax = db._file_item(file_id, self.file_item_id); + let syntax = db.file_item(file_id, self.file_item_id); let offset = syntax.borrowed().range().start(); self.relative_range + offset } @@ -172,9 +172,9 @@ pub(crate) fn input_module_items( source_root: SourceRootId, module_id: ModuleId, ) -> Cancelable> { - let module_tree = db._module_tree(source_root)?; + let module_tree = db.module_tree(source_root)?; let source = module_id.source(&module_tree); - let file_items = db._file_items(source.file_id()); + let file_items = db.file_items(source.file_id()); let res = match source.resolve(db) { ModuleSourceNode::SourceFile(it) => { let items = it.borrowed().items(); @@ -197,11 +197,11 @@ pub(crate) fn item_map( source_root: SourceRootId, ) -> Cancelable> { let start = Instant::now(); - let module_tree = db._module_tree(source_root)?; + let module_tree = db.module_tree(source_root)?; let input = module_tree .modules() .map(|id| { - let items = db._input_module_items(source_root, id)?; + let items = db.input_module_items(source_root, id)?; Ok((id, items)) }) .collect::>>()?; @@ -460,7 +460,7 @@ mod tests { .unwrap() .unwrap(); let module_id = descr.module_id; - (db._item_map(source_root).unwrap(), module_id) + (db.item_map(source_root).unwrap(), module_id) } #[test] @@ -513,9 +513,9 @@ mod tests { { let db = host.analysis().imp.db; let events = db.log_executed(|| { - db._item_map(source_root).unwrap(); + db.item_map(source_root).unwrap(); }); - assert!(format!("{:?}", events).contains("_item_map")) + assert!(format!("{:?}", events).contains("item_map")) } let mut change = AnalysisChange::new(); @@ -537,7 +537,7 @@ mod tests { { let db = host.analysis().imp.db; let events = db.log_executed(|| { - db._item_map(source_root).unwrap(); + db.item_map(source_root).unwrap(); }); assert!( !format!("{:?}", events).contains("_item_map"), -- cgit v1.2.3 From b7049ea543d5ea9a965dfa51d9da923739f2420d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 27 Nov 2018 23:33:36 +0300 Subject: move query definitions --- crates/ra_analysis/src/db.rs | 13 +- crates/ra_analysis/src/hir/db.rs | 85 +++++++------ crates/ra_analysis/src/hir/function/imp.rs | 21 ---- crates/ra_analysis/src/hir/function/mod.rs | 4 +- crates/ra_analysis/src/hir/mod.rs | 1 + crates/ra_analysis/src/hir/module/imp.rs | 35 +----- crates/ra_analysis/src/hir/module/mod.rs | 10 +- crates/ra_analysis/src/hir/module/nameres.rs | 100 ++------------- crates/ra_analysis/src/hir/query_definitions.rs | 158 ++++++++++++++++++++++++ crates/ra_analysis/src/loc2id.rs | 4 - 10 files changed, 232 insertions(+), 199 deletions(-) delete mode 100644 crates/ra_analysis/src/hir/function/imp.rs create mode 100644 crates/ra_analysis/src/hir/query_definitions.rs (limited to 'crates') diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index 08aa9053b..78bbfcf2d 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -10,7 +10,7 @@ use crate::{ hir, symbol_index::SymbolIndex, syntax_ptr::SyntaxPtr, - loc2id::{IdMaps, IdDatabase}, + loc2id::{IdMaps}, Cancelable, Canceled, FileId, }; @@ -77,7 +77,14 @@ impl salsa::ParallelDatabase for RootDatabase { } } -impl IdDatabase for RootDatabase { +pub(crate) trait BaseDatabase: salsa::Database { + fn id_maps(&self) -> &IdMaps; + fn check_canceled(&self) -> Cancelable<()> { + check_canceled(self) + } +} + +impl BaseDatabase for RootDatabase { fn id_maps(&self) -> &IdMaps { &self.id_maps } @@ -136,7 +143,7 @@ salsa::database_storage! { } salsa::query_group! { - pub(crate) trait SyntaxDatabase: crate::input::FilesDatabase { + pub(crate) trait SyntaxDatabase: crate::input::FilesDatabase + BaseDatabase { fn file_syntax(file_id: FileId) -> SourceFileNode { type FileSyntaxQuery; } diff --git a/crates/ra_analysis/src/hir/db.rs b/crates/ra_analysis/src/hir/db.rs index 498a5c2dd..c6dbde79b 100644 --- a/crates/ra_analysis/src/hir/db.rs +++ b/crates/ra_analysis/src/hir/db.rs @@ -8,56 +8,59 @@ use ra_syntax::{ use crate::{ FileId, db::SyntaxDatabase, + hir::query_definitions, hir::function::{FnId, FnScopes}, hir::module::{ ModuleId, ModuleTree, ModuleSource, nameres::{ItemMap, InputModuleItems, FileItems, FileItemId} }, input::SourceRootId, - loc2id::{IdDatabase}, Cancelable, }; salsa::query_group! { -pub(crate) trait HirDatabase: SyntaxDatabase + IdDatabase { - fn fn_scopes(fn_id: FnId) -> Arc { - type FnScopesQuery; - use fn crate::hir::function::imp::fn_scopes; - } - - fn file_items(file_id: FileId) -> Arc { - type FileItemsQuery; - storage dependencies; - use fn crate::hir::module::nameres::file_items; - } - - fn file_item(file_id: FileId, file_item_id: FileItemId) -> SyntaxNode { - type FileItemQuery; - storage dependencies; - use fn crate::hir::module::nameres::file_item; - } - - fn input_module_items(source_root_id: SourceRootId, module_id: ModuleId) -> Cancelable> { - type InputModuleItemsQuery; - use fn crate::hir::module::nameres::input_module_items; - } - fn item_map(source_root_id: SourceRootId) -> Cancelable> { - type ItemMapQuery; - use fn crate::hir::module::nameres::item_map; - } - fn module_tree(source_root_id: SourceRootId) -> Cancelable> { - type ModuleTreeQuery; - use fn crate::hir::module::imp::module_tree; - } - fn fn_syntax(fn_id: FnId) -> FnDefNode { - type FnSyntaxQuery; - // Don't retain syntax trees in memory - storage dependencies; - use fn crate::hir::function::imp::fn_syntax; - } - fn submodules(source: ModuleSource) -> Cancelable>> { - type SubmodulesQuery; - use fn crate::hir::module::imp::submodules; - } + +pub(crate) trait HirDatabase: SyntaxDatabase { + fn fn_scopes(fn_id: FnId) -> Arc { + type FnScopesQuery; + use fn query_definitions::fn_scopes; + } + fn fn_syntax(fn_id: FnId) -> FnDefNode { + type FnSyntaxQuery; + // Don't retain syntax trees in memory + storage dependencies; + use fn query_definitions::fn_syntax; + } + + fn file_items(file_id: FileId) -> Arc { + type FileItemsQuery; + storage dependencies; + use fn query_definitions::file_items; + } + + fn file_item(file_id: FileId, file_item_id: FileItemId) -> SyntaxNode { + type FileItemQuery; + storage dependencies; + use fn query_definitions::file_item; + } + + fn submodules(source: ModuleSource) -> Cancelable>> { + type SubmodulesQuery; + use fn query_definitions::submodules; + } + + fn input_module_items(source_root_id: SourceRootId, module_id: ModuleId) -> Cancelable> { + type InputModuleItemsQuery; + use fn query_definitions::input_module_items; + } + fn item_map(source_root_id: SourceRootId) -> Cancelable> { + type ItemMapQuery; + use fn query_definitions::item_map; } + fn module_tree(source_root_id: SourceRootId) -> Cancelable> { + type ModuleTreeQuery; + use fn crate::hir::module::imp::module_tree; + } +} + } diff --git a/crates/ra_analysis/src/hir/function/imp.rs b/crates/ra_analysis/src/hir/function/imp.rs deleted file mode 100644 index 5f5f68ac5..000000000 --- a/crates/ra_analysis/src/hir/function/imp.rs +++ /dev/null @@ -1,21 +0,0 @@ -use std::sync::Arc; - -use ra_syntax::ast::{AstNode, FnDef, FnDefNode}; - -use crate::hir::{ - function::{FnId, FnScopes}, - HirDatabase, -}; - -/// Resolve `FnId` to the corresponding `SyntaxNode` -pub(crate) fn fn_syntax(db: &impl HirDatabase, fn_id: FnId) -> FnDefNode { - let ptr = db.id_maps().fn_ptr(fn_id); - let syntax = db.resolve_syntax_ptr(ptr); - FnDef::cast(syntax.borrowed()).unwrap().owned() -} - -pub(crate) fn fn_scopes(db: &impl HirDatabase, fn_id: FnId) -> Arc { - let syntax = db.fn_syntax(fn_id); - let res = FnScopes::new(syntax.borrowed()); - Arc::new(res) -} diff --git a/crates/ra_analysis/src/hir/function/mod.rs b/crates/ra_analysis/src/hir/function/mod.rs index c8af6bc21..5de9806e3 100644 --- a/crates/ra_analysis/src/hir/function/mod.rs +++ b/crates/ra_analysis/src/hir/function/mod.rs @@ -1,4 +1,3 @@ -pub(super) mod imp; mod scope; use std::{ @@ -14,14 +13,13 @@ use ra_syntax::{ use crate::{ hir::HirDatabase, syntax_ptr::SyntaxPtr, FileId, - loc2id::IdDatabase, }; pub(crate) use self::scope::{resolve_local_name, FnScopes}; pub(crate) use crate::loc2id::FnId; impl FnId { - pub(crate) fn get(db: &impl IdDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId { + pub(crate) fn get(db: &impl HirDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId { let ptr = SyntaxPtr::new(file_id, fn_def.syntax()); db.id_maps().fn_id(ptr) } diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs index 27ac71a26..dc52fa4ef 100644 --- a/crates/ra_analysis/src/hir/mod.rs +++ b/crates/ra_analysis/src/hir/mod.rs @@ -9,6 +9,7 @@ pub(crate) mod function; pub(crate) mod module; pub(crate) mod db; mod path; +mod query_definitions; use ra_syntax::{ ast::{self, AstNode}, diff --git a/crates/ra_analysis/src/hir/module/imp.rs b/crates/ra_analysis/src/hir/module/imp.rs index f9853584e..3b1baff76 100644 --- a/crates/ra_analysis/src/hir/module/imp.rs +++ b/crates/ra_analysis/src/hir/module/imp.rs @@ -15,7 +15,7 @@ use crate::{ }; use super::{ - LinkData, LinkId, ModuleData, ModuleId, ModuleSource, ModuleSourceNode, + LinkData, LinkId, ModuleData, ModuleId, ModuleSource, ModuleTree, Problem, }; @@ -34,39 +34,6 @@ impl Submodule { } } -pub(crate) fn submodules( - db: &impl HirDatabase, - source: ModuleSource, -) -> Cancelable>> { - db::check_canceled(db)?; - let file_id = source.file_id(); - let submodules = match source.resolve(db) { - ModuleSourceNode::SourceFile(it) => collect_submodules(file_id, it.borrowed()), - ModuleSourceNode::Module(it) => it - .borrowed() - .item_list() - .map(|it| collect_submodules(file_id, it)) - .unwrap_or_else(Vec::new), - }; - return Ok(Arc::new(submodules)); - - fn collect_submodules<'a>( - file_id: FileId, - root: impl ast::ModuleItemOwner<'a>, - ) -> Vec { - modules(root) - .map(|(name, m)| { - if m.has_semi() { - Submodule::Declaration(name) - } else { - let src = ModuleSource::new_inline(file_id, m); - Submodule::Definition(name, src) - } - }) - .collect() - } -} - pub(crate) fn modules<'a>( root: impl ast::ModuleItemOwner<'a>, ) -> impl Iterator)> { diff --git a/crates/ra_analysis/src/hir/module/mod.rs b/crates/ra_analysis/src/hir/module/mod.rs index 55b6639be..4d5945b1a 100644 --- a/crates/ra_analysis/src/hir/module/mod.rs +++ b/crates/ra_analysis/src/hir/module/mod.rs @@ -196,7 +196,7 @@ pub(crate) struct ModuleTree { } impl ModuleTree { - fn modules<'a>(&'a self) -> impl Iterator + 'a { + pub(in crate::hir) fn modules<'a>(&'a self) -> impl Iterator + 'a { self.mods.iter().map(|(id, _)| id) } @@ -224,7 +224,7 @@ pub(crate) enum ModuleSource { /// An owned syntax node for a module. Unlike `ModuleSource`, /// this holds onto the AST for the whole file. -enum ModuleSourceNode { +pub(crate) enum ModuleSourceNode { SourceFile(ast::SourceFileNode), Module(ast::ModuleNode), } @@ -244,7 +244,7 @@ pub enum Problem { } impl ModuleId { - fn source(self, tree: &ModuleTree) -> ModuleSource { + pub(in crate::hir) fn source(self, tree: &ModuleTree) -> ModuleSource { tree.mods[self].source } fn parent_link(self, tree: &ModuleTree) -> Option { @@ -318,7 +318,7 @@ pub(crate) struct ModuleData { } impl ModuleSource { - fn new_inline(file_id: FileId, module: ast::Module) -> ModuleSource { + pub(crate) fn new_inline(file_id: FileId, module: ast::Module) -> ModuleSource { assert!(!module.has_semi()); let ptr = SyntaxPtr::new(file_id, module.syntax()); ModuleSource::Module(ptr) @@ -338,7 +338,7 @@ impl ModuleSource { } } - fn resolve(self, db: &impl SyntaxDatabase) -> ModuleSourceNode { + pub(crate) fn resolve(self, db: &impl SyntaxDatabase) -> ModuleSourceNode { match self { ModuleSource::SourceFile(file_id) => { let syntax = db.file_syntax(file_id); diff --git a/crates/ra_analysis/src/hir/module/nameres.rs b/crates/ra_analysis/src/hir/module/nameres.rs index eaf9f9373..db5d6d9c0 100644 --- a/crates/ra_analysis/src/hir/module/nameres.rs +++ b/crates/ra_analysis/src/hir/module/nameres.rs @@ -16,7 +16,6 @@ //! structure itself is modified. use std::{ sync::Arc, - time::Instant, ops::Index, }; @@ -25,7 +24,7 @@ use rustc_hash::FxHashMap; use ra_syntax::{ SyntaxNode, SyntaxNodeRef, TextRange, SmolStr, SyntaxKind::{self, *}, - ast::{self, ModuleItemOwner, AstNode} + ast::{self, AstNode} }; use crate::{ @@ -34,7 +33,7 @@ use crate::{ hir::{ Path, PathKind, HirDatabase, - module::{ModuleId, ModuleTree, ModuleSourceNode}, + module::{ModuleId, ModuleTree}, }, input::SourceRootId, arena::{Arena, Id} @@ -51,7 +50,7 @@ pub(crate) struct FileItems { } impl FileItems { - fn alloc(&mut self, item: SyntaxNode) -> FileItemId { + pub(crate) fn alloc(&mut self, item: SyntaxNode) -> FileItemId { self.arena.alloc(item) } fn id_of(&self, item: SyntaxNodeRef) -> FileItemId { @@ -71,29 +70,6 @@ impl Index for FileItems { } } -pub(crate) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc { - let source_file = db.file_syntax(file_id); - let source_file = source_file.borrowed(); - let mut res = FileItems::default(); - source_file - .syntax() - .descendants() - .filter_map(ast::ModuleItem::cast) - .map(|it| it.syntax().owned()) - .for_each(|it| { - res.alloc(it); - }); - Arc::new(res) -} - -pub(crate) fn file_item( - db: &impl HirDatabase, - file_id: FileId, - file_item_id: FileItemId, -) -> SyntaxNode { - db.file_items(file_id)[file_item_id].clone() -} - /// Item map is the result of the name resolution. Item map contains, for each /// module, the set of visible items. #[derive(Default, Debug, PartialEq, Eq)] @@ -167,58 +143,6 @@ enum ImportKind { Named(NamedImport), } -pub(crate) fn input_module_items( - db: &impl HirDatabase, - source_root: SourceRootId, - module_id: ModuleId, -) -> Cancelable> { - let module_tree = db.module_tree(source_root)?; - let source = module_id.source(&module_tree); - let file_items = db.file_items(source.file_id()); - let res = match source.resolve(db) { - ModuleSourceNode::SourceFile(it) => { - let items = it.borrowed().items(); - InputModuleItems::new(&file_items, items) - } - ModuleSourceNode::Module(it) => { - let items = it - .borrowed() - .item_list() - .into_iter() - .flat_map(|it| it.items()); - InputModuleItems::new(&file_items, items) - } - }; - Ok(Arc::new(res)) -} - -pub(crate) fn item_map( - db: &impl HirDatabase, - source_root: SourceRootId, -) -> Cancelable> { - let start = Instant::now(); - let module_tree = db.module_tree(source_root)?; - let input = module_tree - .modules() - .map(|id| { - let items = db.input_module_items(source_root, id)?; - Ok((id, items)) - }) - .collect::>>()?; - let mut resolver = Resolver { - db: db, - input: &input, - source_root, - module_tree, - result: ItemMap::default(), - }; - resolver.resolve()?; - let res = resolver.result; - let elapsed = start.elapsed(); - log::info!("item_map: {:?}", elapsed); - Ok(Arc::new(res)) -} - /// Resolution is basically `DefId` atm, but it should account for stuff like /// multiple namespaces, ambiguity and errors. #[derive(Debug, Clone, PartialEq, Eq)] @@ -242,7 +166,7 @@ pub(crate) struct Resolution { // } impl InputModuleItems { - fn new<'a>( + pub(in crate::hir) fn new<'a>( file_items: &FileItems, items: impl Iterator>, ) -> InputModuleItems { @@ -306,19 +230,19 @@ impl ModuleItem { } } -struct Resolver<'a, DB> { - db: &'a DB, - input: &'a FxHashMap>, - source_root: SourceRootId, - module_tree: Arc, - result: ItemMap, +pub(in crate::hir) struct Resolver<'a, DB> { + pub db: &'a DB, + pub input: &'a FxHashMap>, + pub source_root: SourceRootId, + pub module_tree: Arc, + pub result: ItemMap, } impl<'a, DB> Resolver<'a, DB> where DB: HirDatabase, { - fn resolve(&mut self) -> Cancelable<()> { + pub(in crate::hir) fn resolve(mut self) -> Cancelable { for (&module_id, items) in self.input.iter() { self.populate_module(module_id, items) } @@ -327,7 +251,7 @@ where crate::db::check_canceled(self.db)?; self.resolve_imports(module_id); } - Ok(()) + Ok(self.result) } fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) { diff --git a/crates/ra_analysis/src/hir/query_definitions.rs b/crates/ra_analysis/src/hir/query_definitions.rs new file mode 100644 index 000000000..8584a8d64 --- /dev/null +++ b/crates/ra_analysis/src/hir/query_definitions.rs @@ -0,0 +1,158 @@ +use std::{ + sync::Arc, + time::Instant, +}; + +use rustc_hash::FxHashMap; +use ra_syntax::{ + AstNode, SyntaxNode, SmolStr, + ast::{self, FnDef, FnDefNode, NameOwner, ModuleItemOwner} +}; + +use crate::{ + FileId, Cancelable, + hir::{ + db::HirDatabase, + function::{FnId, FnScopes}, + module::{ + ModuleSource, ModuleSourceNode, ModuleId, + imp::Submodule, + nameres::{FileItems, FileItemId, InputModuleItems, ItemMap, Resolver}, + }, + }, + input::SourceRootId, +}; + +/// Resolve `FnId` to the corresponding `SyntaxNode` +pub(super) fn fn_syntax(db: &impl HirDatabase, fn_id: FnId) -> FnDefNode { + let ptr = db.id_maps().fn_ptr(fn_id); + let syntax = db.resolve_syntax_ptr(ptr); + FnDef::cast(syntax.borrowed()).unwrap().owned() +} + +pub(super) fn fn_scopes(db: &impl HirDatabase, fn_id: FnId) -> Arc { + let syntax = db.fn_syntax(fn_id); + let res = FnScopes::new(syntax.borrowed()); + Arc::new(res) +} + +pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc { + let source_file = db.file_syntax(file_id); + let source_file = source_file.borrowed(); + let mut res = FileItems::default(); + source_file + .syntax() + .descendants() + .filter_map(ast::ModuleItem::cast) + .map(|it| it.syntax().owned()) + .for_each(|it| { + res.alloc(it); + }); + Arc::new(res) +} + +pub(super) fn file_item( + db: &impl HirDatabase, + file_id: FileId, + file_item_id: FileItemId, +) -> SyntaxNode { + db.file_items(file_id)[file_item_id].clone() +} + +pub(crate) fn submodules( + db: &impl HirDatabase, + source: ModuleSource, +) -> Cancelable>> { + db.check_canceled()?; + let file_id = source.file_id(); + let submodules = match source.resolve(db) { + ModuleSourceNode::SourceFile(it) => collect_submodules(file_id, it.borrowed()), + ModuleSourceNode::Module(it) => it + .borrowed() + .item_list() + .map(|it| collect_submodules(file_id, it)) + .unwrap_or_else(Vec::new), + }; + return Ok(Arc::new(submodules)); + + fn collect_submodules<'a>( + file_id: FileId, + root: impl ast::ModuleItemOwner<'a>, + ) -> Vec { + modules(root) + .map(|(name, m)| { + if m.has_semi() { + Submodule::Declaration(name) + } else { + let src = ModuleSource::new_inline(file_id, m); + Submodule::Definition(name, src) + } + }) + .collect() + } +} + +pub(crate) fn modules<'a>( + root: impl ast::ModuleItemOwner<'a>, +) -> impl Iterator)> { + root.items() + .filter_map(|item| match item { + ast::ModuleItem::Module(m) => Some(m), + _ => None, + }) + .filter_map(|module| { + let name = module.name()?.text(); + Some((name, module)) + }) +} + +pub(super) fn input_module_items( + db: &impl HirDatabase, + source_root: SourceRootId, + module_id: ModuleId, +) -> Cancelable> { + let module_tree = db.module_tree(source_root)?; + let source = module_id.source(&module_tree); + let file_items = db.file_items(source.file_id()); + let res = match source.resolve(db) { + ModuleSourceNode::SourceFile(it) => { + let items = it.borrowed().items(); + InputModuleItems::new(&file_items, items) + } + ModuleSourceNode::Module(it) => { + let items = it + .borrowed() + .item_list() + .into_iter() + .flat_map(|it| it.items()); + InputModuleItems::new(&file_items, items) + } + }; + Ok(Arc::new(res)) +} + +pub(super) fn item_map( + db: &impl HirDatabase, + source_root: SourceRootId, +) -> Cancelable> { + let start = Instant::now(); + let module_tree = db.module_tree(source_root)?; + let input = module_tree + .modules() + .map(|id| { + let items = db.input_module_items(source_root, id)?; + Ok((id, items)) + }) + .collect::>>()?; + let resolver = Resolver { + db: db, + input: &input, + source_root, + module_tree, + result: ItemMap::default(), + }; + let res = resolver.resolve()?; + let elapsed = start.elapsed(); + log::info!("item_map: {:?}", elapsed); + Ok(Arc::new(res)) +} diff --git a/crates/ra_analysis/src/loc2id.rs b/crates/ra_analysis/src/loc2id.rs index d42af4e0a..40da83665 100644 --- a/crates/ra_analysis/src/loc2id.rs +++ b/crates/ra_analysis/src/loc2id.rs @@ -109,10 +109,6 @@ pub(crate) enum DefLoc { }, } -pub(crate) trait IdDatabase: salsa::Database { - fn id_maps(&self) -> &IdMaps; -} - #[derive(Debug, Default, Clone)] pub(crate) struct IdMaps { inner: Arc, -- cgit v1.2.3 From 93fae0826194b1590013c9bb0d977e60bd135918 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 27 Nov 2018 23:35:53 +0300 Subject: make module module private --- crates/ra_analysis/src/completion/mod.rs | 6 +++--- crates/ra_analysis/src/completion/reference_completion.rs | 2 +- crates/ra_analysis/src/hir/mod.rs | 4 ++-- crates/ra_analysis/src/imp.rs | 2 +- crates/ra_analysis/src/loc2id.rs | 3 +-- 5 files changed, 8 insertions(+), 9 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/completion/mod.rs b/crates/ra_analysis/src/completion/mod.rs index 1eb804c85..08fb149af 100644 --- a/crates/ra_analysis/src/completion/mod.rs +++ b/crates/ra_analysis/src/completion/mod.rs @@ -11,9 +11,9 @@ use rustc_hash::{FxHashMap}; use crate::{ db::{self, SyntaxDatabase}, - hir::{ - module::{ModuleDescriptor} - }, + hir:: + ModuleDescriptor + , Cancelable, FilePosition }; diff --git a/crates/ra_analysis/src/completion/reference_completion.rs b/crates/ra_analysis/src/completion/reference_completion.rs index ac6ef1d4a..1bf210685 100644 --- a/crates/ra_analysis/src/completion/reference_completion.rs +++ b/crates/ra_analysis/src/completion/reference_completion.rs @@ -11,7 +11,7 @@ use crate::{ db::RootDatabase, completion::CompletionItem, hir::{ - module::{ModuleDescriptor}, + ModuleDescriptor, function::FnScopes, Def, Path, diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs index dc52fa4ef..2035c2d23 100644 --- a/crates/ra_analysis/src/hir/mod.rs +++ b/crates/ra_analysis/src/hir/mod.rs @@ -6,7 +6,7 @@ //! applied. So, there relation between syntax and HIR is many-to-one. pub(crate) mod function; -pub(crate) mod module; +mod module; pub(crate) mod db; mod path; mod query_definitions; @@ -26,7 +26,7 @@ use crate::{ pub(crate) use self::{ path::{Path, PathKind}, - module::{ModuleDescriptor, nameres::FileItemId}, + module::{ModuleDescriptor, ModuleId, Problem, nameres::FileItemId}, function::FunctionDescriptor, }; diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index ad4b40c58..1b7e16ff4 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -22,7 +22,7 @@ use crate::{ hir::{ FunctionDescriptor, ModuleDescriptor, function::FnDescriptor, - module::{Problem}, + Problem, DeclarationDescriptor, }, input::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE}, diff --git a/crates/ra_analysis/src/loc2id.rs b/crates/ra_analysis/src/loc2id.rs index 40da83665..8af7b642b 100644 --- a/crates/ra_analysis/src/loc2id.rs +++ b/crates/ra_analysis/src/loc2id.rs @@ -9,8 +9,7 @@ use rustc_hash::FxHashMap; use crate::{ FileId, - hir::FileItemId, - hir::module::ModuleId, + hir::{FileItemId, ModuleId}, syntax_ptr::SyntaxPtr, input::SourceRootId, }; -- cgit v1.2.3 From 67de38ec7d5c235dec209fe859d6bf6b0dbe497a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 27 Nov 2018 23:40:23 +0300 Subject: move resolve_local_scope --- .../src/completion/reference_completion.rs | 2 +- crates/ra_analysis/src/hir/function/mod.rs | 2 +- crates/ra_analysis/src/hir/function/scope.rs | 29 +++++++++++----------- crates/ra_analysis/src/hir/mod.rs | 14 ++++++----- crates/ra_analysis/src/imp.rs | 5 ++-- crates/ra_analysis/src/lib.rs | 2 +- 6 files changed, 27 insertions(+), 27 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/completion/reference_completion.rs b/crates/ra_analysis/src/completion/reference_completion.rs index 1bf210685..5bf8c3725 100644 --- a/crates/ra_analysis/src/completion/reference_completion.rs +++ b/crates/ra_analysis/src/completion/reference_completion.rs @@ -12,7 +12,7 @@ use crate::{ completion::CompletionItem, hir::{ ModuleDescriptor, - function::FnScopes, + FnScopes, Def, Path, }, diff --git a/crates/ra_analysis/src/hir/function/mod.rs b/crates/ra_analysis/src/hir/function/mod.rs index 5de9806e3..8161a604f 100644 --- a/crates/ra_analysis/src/hir/function/mod.rs +++ b/crates/ra_analysis/src/hir/function/mod.rs @@ -15,7 +15,7 @@ use crate::{ syntax_ptr::SyntaxPtr, FileId, }; -pub(crate) use self::scope::{resolve_local_name, FnScopes}; +pub(crate) use self::scope::FnScopes; pub(crate) use crate::loc2id::FnId; impl FnId { diff --git a/crates/ra_analysis/src/hir/function/scope.rs b/crates/ra_analysis/src/hir/function/scope.rs index 5307a0a8e..b8bdebe47 100644 --- a/crates/ra_analysis/src/hir/function/scope.rs +++ b/crates/ra_analysis/src/hir/function/scope.rs @@ -57,6 +57,19 @@ impl FnScopes { self.scopes[scope].parent }) } + pub(crate) fn resolve_local_name<'a>( + &'a self, + name_ref: ast::NameRef, + ) -> Option<&'a ScopeEntry> { + let mut shadowed = FxHashSet::default(); + let ret = self + .scope_chain(name_ref.syntax()) + .flat_map(|scope| self.entries(scope).iter()) + .filter(|entry| shadowed.insert(entry.name())) + .filter(|entry| entry.name() == &name_ref.text()) + .nth(0); + ret + } fn root_scope(&mut self) -> ScopeId { self.scopes.alloc(ScopeData { parent: None, @@ -249,20 +262,6 @@ fn compute_expr_scopes(expr: ast::Expr, scopes: &mut FnScopes, scope: ScopeId) { } } -pub fn resolve_local_name<'a>( - name_ref: ast::NameRef, - scopes: &'a FnScopes, -) -> Option<&'a ScopeEntry> { - let mut shadowed = FxHashSet::default(); - let ret = scopes - .scope_chain(name_ref.syntax()) - .flat_map(|scope| scopes.entries(scope).iter()) - .filter(|entry| shadowed.insert(entry.name())) - .filter(|entry| entry.name() == &name_ref.text()) - .nth(0); - ret -} - #[cfg(test)] mod tests { use ra_editor::find_node_at_offset; @@ -376,7 +375,7 @@ mod tests { let scopes = FnScopes::new(fn_def); - let local_name_entry = resolve_local_name(name_ref, &scopes).unwrap(); + let local_name_entry = scopes.resolve_local_name(name_ref).unwrap(); let local_name = local_name_entry.ptr().resolve(&file); let expected_name = find_node_at_offset::(file.syntax(), expected_offset.into()).unwrap(); diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs index 2035c2d23..1d37fae32 100644 --- a/crates/ra_analysis/src/hir/mod.rs +++ b/crates/ra_analysis/src/hir/mod.rs @@ -5,11 +5,11 @@ //! to a particular crate instance. That is, it has cfg flags and features //! applied. So, there relation between syntax and HIR is many-to-one. -pub(crate) mod function; -mod module; pub(crate) mod db; -mod path; mod query_definitions; +mod function; +mod module; +mod path; use ra_syntax::{ ast::{self, AstNode}, @@ -18,7 +18,6 @@ use ra_syntax::{ use crate::{ hir::db::HirDatabase, - hir::function::{resolve_local_name, FnScopes}, loc2id::{DefId, DefLoc}, syntax_ptr::LocalSyntaxPtr, Cancelable, @@ -27,9 +26,12 @@ use crate::{ pub(crate) use self::{ path::{Path, PathKind}, module::{ModuleDescriptor, ModuleId, Problem, nameres::FileItemId}, - function::FunctionDescriptor, + function::{FunctionDescriptor, FnScopes}, }; +//TODO: FIXME +pub use self::function::FnDescriptor; + pub(crate) enum Def { Module(ModuleDescriptor), Item, @@ -82,7 +84,7 @@ impl<'a> DeclarationDescriptor<'a> { .syntax() .descendants() .filter_map(ast::NameRef::cast) - .filter(|name_ref| match resolve_local_name(*name_ref, &fn_scopes) { + .filter(|name_ref| match fn_scopes.resolve_local_name(*name_ref) { None => false, Some(entry) => entry.ptr() == name_ptr, }) diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 1b7e16ff4..347d44638 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -20,8 +20,7 @@ use crate::{ completion::{completions, CompletionItem}, db::{self, FileSyntaxQuery, SyntaxDatabase}, hir::{ - FunctionDescriptor, ModuleDescriptor, - function::FnDescriptor, + FnDescriptor, FunctionDescriptor, ModuleDescriptor, Problem, DeclarationDescriptor, }, @@ -590,7 +589,7 @@ fn resolve_local_name( let fn_def = name_ref.syntax().ancestors().find_map(ast::FnDef::cast)?; let function = FunctionDescriptor::guess_from_source(db, file_id, fn_def); let scopes = function.scope(db); - let scope_entry = crate::hir::function::resolve_local_name(name_ref, &scopes)?; + let scope_entry = scopes.resolve_local_name(name_ref)?; let syntax = db.resolve_syntax_ptr(scope_entry.ptr().into_global(file_id)); Some((scope_entry.name().clone(), syntax.range())) } diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 4659eb523..a3088c5ad 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -42,7 +42,7 @@ use crate::{ pub use crate::{ completion::CompletionItem, - hir::function::FnDescriptor, + hir::FnDescriptor, input::{CrateGraph, CrateId, FileId, FileResolver}, }; pub use ra_editor::{ -- cgit v1.2.3 From 7207eef716c0970df1b3523f8f4bb685518f8f73 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 27 Nov 2018 23:48:24 +0300 Subject: rename FnDescriptior -> FnSignatureInfo --- crates/ra_analysis/src/hir/function/mod.rs | 17 +++++++++++------ crates/ra_analysis/src/hir/mod.rs | 3 +-- crates/ra_analysis/src/imp.rs | 12 +++++++----- crates/ra_analysis/src/lib.rs | 4 ++-- 4 files changed, 21 insertions(+), 15 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/hir/function/mod.rs b/crates/ra_analysis/src/hir/function/mod.rs index 8161a604f..5e44a88a7 100644 --- a/crates/ra_analysis/src/hir/function/mod.rs +++ b/crates/ra_analysis/src/hir/function/mod.rs @@ -42,10 +42,15 @@ impl FunctionDescriptor { pub(crate) fn scope(&self, db: &impl HirDatabase) -> Arc { db.fn_scopes(self.fn_id) } + + pub(crate) fn signature_info(&self, db: &impl HirDatabase) -> Option { + let syntax = db.fn_syntax(self.fn_id); + FnSignatureInfo::new(syntax.borrowed()) + } } #[derive(Debug, Clone)] -pub struct FnDescriptor { +pub struct FnSignatureInfo { pub name: String, pub label: String, pub ret_type: Option, @@ -53,8 +58,8 @@ pub struct FnDescriptor { pub doc: Option, } -impl FnDescriptor { - pub fn new(node: ast::FnDef) -> Option { +impl FnSignatureInfo { + fn new(node: ast::FnDef) -> Option { let name = node.name()?.text().to_string(); let mut doc = None; @@ -73,7 +78,7 @@ impl FnDescriptor { node.syntax().text().to_string() }; - if let Some((comment_range, docs)) = FnDescriptor::extract_doc_comments(node) { + if let Some((comment_range, docs)) = FnSignatureInfo::extract_doc_comments(node) { let comment_range = comment_range .checked_sub(node.syntax().range().start()) .unwrap(); @@ -105,10 +110,10 @@ impl FnDescriptor { } } - let params = FnDescriptor::param_list(node); + let params = FnSignatureInfo::param_list(node); let ret_type = node.ret_type().map(|r| r.syntax().text().to_string()); - Some(FnDescriptor { + Some(FnSignatureInfo { name, ret_type, params, diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs index 1d37fae32..5a9086cef 100644 --- a/crates/ra_analysis/src/hir/mod.rs +++ b/crates/ra_analysis/src/hir/mod.rs @@ -29,8 +29,7 @@ pub(crate) use self::{ function::{FunctionDescriptor, FnScopes}, }; -//TODO: FIXME -pub use self::function::FnDescriptor; +pub use self::function::FnSignatureInfo; pub(crate) enum Def { Module(ModuleDescriptor), diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 347d44638..b16edb969 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -20,7 +20,7 @@ use crate::{ completion::{completions, CompletionItem}, db::{self, FileSyntaxQuery, SyntaxDatabase}, hir::{ - FnDescriptor, FunctionDescriptor, ModuleDescriptor, + FunctionDescriptor, FnSignatureInfo, ModuleDescriptor, Problem, DeclarationDescriptor, }, @@ -445,7 +445,7 @@ impl AnalysisImpl { pub fn resolve_callable( &self, position: FilePosition, - ) -> Cancelable)>> { + ) -> Cancelable)>> { let file = self.db.file_syntax(position.file_id); let syntax = file.syntax(); @@ -455,11 +455,13 @@ impl AnalysisImpl { // Resolve the function's NameRef (NOTE: this isn't entirely accurate). let file_symbols = self.index_resolve(name_ref)?; - for (fn_fiel_id, fs) in file_symbols { + for (fn_file_id, fs) in file_symbols { if fs.kind == FN_DEF { - let fn_file = self.db.file_syntax(fn_fiel_id); + let fn_file = self.db.file_syntax(fn_file_id); if let Some(fn_def) = find_node_at_offset(fn_file.syntax(), fs.node_range.start()) { - if let Some(descriptor) = FnDescriptor::new(fn_def) { + let descr = + FunctionDescriptor::guess_from_source(&*self.db, fn_file_id, fn_def); + if let Some(descriptor) = descr.signature_info(&*self.db) { // If we have a calling expression let's find which argument we are on let mut current_parameter = None; diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index a3088c5ad..c0e43544e 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -42,7 +42,7 @@ use crate::{ pub use crate::{ completion::CompletionItem, - hir::FnDescriptor, + hir::FnSignatureInfo, input::{CrateGraph, CrateId, FileId, FileResolver}, }; pub use ra_editor::{ @@ -305,7 +305,7 @@ impl Analysis { pub fn resolve_callable( &self, position: FilePosition, - ) -> Cancelable)>> { + ) -> Cancelable)>> { self.imp.resolve_callable(position) } } -- cgit v1.2.3 From 16f67ee384d5b49358de167069535af727b87dba Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 01:11:29 +0300 Subject: move resolve_local to Scopes --- crates/ra_analysis/src/hir/function/mod.rs | 28 ++++++++- crates/ra_analysis/src/hir/function/scope.rs | 29 ++++++++- crates/ra_analysis/src/hir/mod.rs | 53 ---------------- crates/ra_analysis/src/imp.rs | 93 ++++++++++++++-------------- crates/ra_analysis/src/syntax_ptr.rs | 7 +-- crates/ra_analysis/tests/tests.rs | 4 +- 6 files changed, 105 insertions(+), 109 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/hir/function/mod.rs b/crates/ra_analysis/src/hir/function/mod.rs index 5e44a88a7..d171b6a8d 100644 --- a/crates/ra_analysis/src/hir/function/mod.rs +++ b/crates/ra_analysis/src/hir/function/mod.rs @@ -6,8 +6,8 @@ use std::{ }; use ra_syntax::{ + TextRange, TextUnit, SyntaxNodeRef, ast::{self, AstNode, DocCommentsOwner, NameOwner}, - TextRange, TextUnit, }; use crate::{ @@ -39,6 +39,32 @@ impl FunctionDescriptor { FunctionDescriptor { fn_id } } + pub(crate) fn guess_for_name_ref( + db: &impl HirDatabase, + file_id: FileId, + name_ref: ast::NameRef, + ) -> Option { + FunctionDescriptor::guess_for_node(db, file_id, name_ref.syntax()) + } + + pub(crate) fn guess_for_bind_pat( + db: &impl HirDatabase, + file_id: FileId, + bind_pat: ast::BindPat, + ) -> Option { + FunctionDescriptor::guess_for_node(db, file_id, bind_pat.syntax()) + } + + fn guess_for_node( + db: &impl HirDatabase, + file_id: FileId, + node: SyntaxNodeRef, + ) -> Option { + let fn_def = node.ancestors().find_map(ast::FnDef::cast)?; + let res = FunctionDescriptor::guess_from_source(db, file_id, fn_def); + Some(res) + } + pub(crate) fn scope(&self, db: &impl HirDatabase) -> Arc { db.fn_scopes(self.fn_id) } diff --git a/crates/ra_analysis/src/hir/function/scope.rs b/crates/ra_analysis/src/hir/function/scope.rs index b8bdebe47..76b2fea68 100644 --- a/crates/ra_analysis/src/hir/function/scope.rs +++ b/crates/ra_analysis/src/hir/function/scope.rs @@ -1,9 +1,9 @@ use rustc_hash::{FxHashMap, FxHashSet}; use ra_syntax::{ + AstNode, SmolStr, SyntaxNodeRef, TextRange, algo::generate, ast::{self, ArgListOwner, LoopBodyOwner, NameOwner}, - AstNode, SmolStr, SyntaxNodeRef, }; use crate::{ @@ -70,6 +70,27 @@ impl FnScopes { .nth(0); ret } + + pub fn find_all_refs(&self, pat: ast::BindPat) -> Vec { + let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); + let name_ptr = LocalSyntaxPtr::new(pat.syntax()); + let refs: Vec<_> = fn_def + .syntax() + .descendants() + .filter_map(ast::NameRef::cast) + .filter(|name_ref| match self.resolve_local_name(*name_ref) { + None => false, + Some(entry) => entry.ptr() == name_ptr, + }) + .map(|name_ref| ReferenceDescriptor { + name: name_ref.syntax().text().to_string(), + range: name_ref.syntax().range(), + }) + .collect(); + + refs + } + fn root_scope(&mut self) -> ScopeId { self.scopes.alloc(ScopeData { parent: None, @@ -262,6 +283,12 @@ fn compute_expr_scopes(expr: ast::Expr, scopes: &mut FnScopes, scope: ScopeId) { } } +#[derive(Debug)] +pub struct ReferenceDescriptor { + pub range: TextRange, + pub name: String, +} + #[cfg(test)] mod tests { use ra_editor::find_node_at_offset; diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs index 5a9086cef..e234173a9 100644 --- a/crates/ra_analysis/src/hir/mod.rs +++ b/crates/ra_analysis/src/hir/mod.rs @@ -11,15 +11,9 @@ mod function; mod module; mod path; -use ra_syntax::{ - ast::{self, AstNode}, - TextRange, -}; - use crate::{ hir::db::HirDatabase, loc2id::{DefId, DefLoc}, - syntax_ptr::LocalSyntaxPtr, Cancelable, }; @@ -49,50 +43,3 @@ impl DefId { Ok(res) } } - -#[derive(Debug)] -pub struct ReferenceDescriptor { - pub range: TextRange, - pub name: String, -} - -#[derive(Debug)] -pub struct DeclarationDescriptor<'a> { - pat: ast::BindPat<'a>, - pub range: TextRange, -} - -impl<'a> DeclarationDescriptor<'a> { - pub fn new(pat: ast::BindPat) -> DeclarationDescriptor { - let range = pat.syntax().range(); - - DeclarationDescriptor { pat, range } - } - - pub fn find_all_refs(&self) -> Vec { - let name_ptr = LocalSyntaxPtr::new(self.pat.syntax()); - - let fn_def = match self.pat.syntax().ancestors().find_map(ast::FnDef::cast) { - Some(def) => def, - None => return Default::default(), - }; - - let fn_scopes = FnScopes::new(fn_def); - - let refs: Vec<_> = fn_def - .syntax() - .descendants() - .filter_map(ast::NameRef::cast) - .filter(|name_ref| match fn_scopes.resolve_local_name(*name_ref) { - None => false, - Some(entry) => entry.ptr() == name_ptr, - }) - .map(|name_ref| ReferenceDescriptor { - name: name_ref.syntax().text().to_string(), - range: name_ref.syntax().range(), - }) - .collect(); - - refs - } -} diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index b16edb969..377f7420f 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -7,7 +7,7 @@ use std::{ use ra_editor::{self, find_node_at_offset, FileSymbol, LineIndex, LocalEdit}; use ra_syntax::{ ast::{self, ArgListOwner, Expr, NameOwner}, - AstNode, SourceFileNode, SmolStr, + AstNode, SourceFileNode, SyntaxKind::*, SyntaxNodeRef, TextRange, TextUnit, }; @@ -22,7 +22,6 @@ use crate::{ hir::{ FunctionDescriptor, FnSignatureInfo, ModuleDescriptor, Problem, - DeclarationDescriptor, }, input::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE}, symbol_index::SymbolIndex, @@ -273,24 +272,27 @@ impl AnalysisImpl { let file = self.db.file_syntax(position.file_id); let syntax = file.syntax(); if let Some(name_ref) = find_node_at_offset::(syntax, position.offset) { - // First try to resolve the symbol locally - return if let Some((name, range)) = - resolve_local_name(&self.db, position.file_id, name_ref) + if let Some(fn_descr) = + FunctionDescriptor::guess_for_name_ref(&*self.db, position.file_id, name_ref) { - let mut vec = vec![]; - vec.push(( - position.file_id, - FileSymbol { - name, - node_range: range, - kind: NAME, - }, - )); - Ok(vec) - } else { - // If that fails try the index based approach. - self.index_resolve(name_ref) - }; + let scope = fn_descr.scope(&*self.db); + // First try to resolve the symbol locally + return if let Some(entry) = scope.resolve_local_name(name_ref) { + let mut vec = vec![]; + vec.push(( + position.file_id, + FileSymbol { + name: entry.name().clone(), + node_range: entry.ptr().range(), + kind: NAME, + }, + )); + Ok(vec) + } else { + // If that fails try the index based approach. + self.index_resolve(name_ref) + }; + } } if let Some(name) = find_node_at_offset::(syntax, position.offset) { if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) { @@ -320,31 +322,41 @@ impl AnalysisImpl { pub fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> { let file = self.db.file_syntax(position.file_id); - let syntax = file.syntax(); - // Find the binding associated with the offset - let maybe_binding = - find_node_at_offset::(syntax, position.offset).or_else(|| { - let name_ref = find_node_at_offset::(syntax, position.offset)?; - let resolved = resolve_local_name(&self.db, position.file_id, name_ref)?; - find_node_at_offset::(syntax, resolved.1.end()) - }); - - let binding = match maybe_binding { + let (binding, descr) = match find_binding(&self.db, &file, position) { None => return Vec::new(), Some(it) => it, }; - let decl = DeclarationDescriptor::new(binding); - - let mut ret = vec![(position.file_id, decl.range)]; + let mut ret = vec![(position.file_id, binding.syntax().range())]; ret.extend( - decl.find_all_refs() + descr + .scope(&*self.db) + .find_all_refs(binding) .into_iter() .map(|ref_desc| (position.file_id, ref_desc.range)), ); - ret + return ret; + + fn find_binding<'a>( + db: &db::RootDatabase, + source_file: &'a SourceFileNode, + position: FilePosition, + ) -> Option<(ast::BindPat<'a>, FunctionDescriptor)> { + let syntax = source_file.syntax(); + if let Some(binding) = find_node_at_offset::(syntax, position.offset) { + let descr = FunctionDescriptor::guess_for_bind_pat(db, position.file_id, binding)?; + return Some((binding, descr)); + }; + let name_ref = find_node_at_offset::(syntax, position.offset)?; + let descr = FunctionDescriptor::guess_for_name_ref(db, position.file_id, name_ref)?; + let scope = descr.scope(db); + let resolved = scope.resolve_local_name(name_ref)?; + let resolved = resolved.ptr().resolve(source_file); + let binding = find_node_at_offset::(syntax, resolved.range().end())?; + Some((binding, descr)) + } } pub fn doc_comment_for( @@ -582,16 +594,3 @@ impl<'a> FnCallNode<'a> { } } } - -fn resolve_local_name( - db: &db::RootDatabase, - file_id: FileId, - name_ref: ast::NameRef, -) -> Option<(SmolStr, TextRange)> { - let fn_def = name_ref.syntax().ancestors().find_map(ast::FnDef::cast)?; - let function = FunctionDescriptor::guess_from_source(db, file_id, fn_def); - let scopes = function.scope(db); - let scope_entry = scopes.resolve_local_name(name_ref)?; - let syntax = db.resolve_syntax_ptr(scope_entry.ptr().into_global(file_id)); - Some((scope_entry.name().clone(), syntax.range())) -} diff --git a/crates/ra_analysis/src/syntax_ptr.rs b/crates/ra_analysis/src/syntax_ptr.rs index e45934ce0..3168e82a7 100644 --- a/crates/ra_analysis/src/syntax_ptr.rs +++ b/crates/ra_analysis/src/syntax_ptr.rs @@ -56,11 +56,8 @@ impl LocalSyntaxPtr { } } - pub(crate) fn into_global(self, file_id: FileId) -> SyntaxPtr { - SyntaxPtr { - file_id, - local: self, - } + pub(crate) fn range(self) -> TextRange { + self.range } } diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs index 72b2ebf97..8e7856027 100644 --- a/crates/ra_analysis/tests/tests.rs +++ b/crates/ra_analysis/tests/tests.rs @@ -10,10 +10,10 @@ use test_utils::assert_eq_dbg; use ra_analysis::{ mock_analysis::{analysis_and_position, single_file, single_file_with_position, MockAnalysis}, - AnalysisChange, CrateGraph, FileId, FnDescriptor, + AnalysisChange, CrateGraph, FileId, FnSignatureInfo, }; -fn get_signature(text: &str) -> (FnDescriptor, Option) { +fn get_signature(text: &str) -> (FnSignatureInfo, Option) { let (analysis, position) = single_file_with_position(text); analysis.resolve_callable(position).unwrap().unwrap() } -- cgit v1.2.3 From 36b1d20c1661877c0c5a55ccd07522bc97bfc254 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 01:19:55 +0300 Subject: rename ModuleDescriptor -> Module --- crates/ra_analysis/src/completion/mod.rs | 6 ++-- .../src/completion/reference_completion.rs | 6 ++-- crates/ra_analysis/src/hir/mod.rs | 6 ++-- crates/ra_analysis/src/hir/module/mod.rs | 40 +++++++++++----------- crates/ra_analysis/src/imp.rs | 11 +++--- 5 files changed, 34 insertions(+), 35 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/completion/mod.rs b/crates/ra_analysis/src/completion/mod.rs index 08fb149af..67ec9a735 100644 --- a/crates/ra_analysis/src/completion/mod.rs +++ b/crates/ra_analysis/src/completion/mod.rs @@ -11,9 +11,7 @@ use rustc_hash::{FxHashMap}; use crate::{ db::{self, SyntaxDatabase}, - hir:: - ModuleDescriptor - , + hir, Cancelable, FilePosition }; @@ -38,7 +36,7 @@ pub(crate) fn completions( original_file.reparse(&edit) }; - let module = ctry!(ModuleDescriptor::guess_from_position(db, position)?); + let module = ctry!(hir::Module::guess_from_position(db, position)?); let mut res = Vec::new(); let mut has_completions = false; diff --git a/crates/ra_analysis/src/completion/reference_completion.rs b/crates/ra_analysis/src/completion/reference_completion.rs index 5bf8c3725..881d29916 100644 --- a/crates/ra_analysis/src/completion/reference_completion.rs +++ b/crates/ra_analysis/src/completion/reference_completion.rs @@ -11,7 +11,7 @@ use crate::{ db::RootDatabase, completion::CompletionItem, hir::{ - ModuleDescriptor, + self, FnScopes, Def, Path, @@ -22,7 +22,7 @@ use crate::{ pub(super) fn completions( acc: &mut Vec, db: &RootDatabase, - module: &ModuleDescriptor, + module: &hir::Module, file: &SourceFileNode, name_ref: ast::NameRef, ) -> Cancelable<()> { @@ -150,7 +150,7 @@ fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec, db: &RootDatabase, - module: &ModuleDescriptor, + module: &hir::Module, mut path: Path, ) -> Cancelable<()> { if path.segments.is_empty() { diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs index e234173a9..1de8fadcf 100644 --- a/crates/ra_analysis/src/hir/mod.rs +++ b/crates/ra_analysis/src/hir/mod.rs @@ -19,14 +19,14 @@ use crate::{ pub(crate) use self::{ path::{Path, PathKind}, - module::{ModuleDescriptor, ModuleId, Problem, nameres::FileItemId}, + module::{Module, ModuleId, Problem, nameres::FileItemId}, function::{FunctionDescriptor, FnScopes}, }; pub use self::function::FnSignatureInfo; pub(crate) enum Def { - Module(ModuleDescriptor), + Module(Module), Item, } @@ -35,7 +35,7 @@ impl DefId { let loc = db.id_maps().def_loc(self); let res = match loc { DefLoc::Module { id, source_root } => { - let descr = ModuleDescriptor::new(db, source_root, id)?; + let descr = Module::new(db, source_root, id)?; Def::Module(descr) } DefLoc::Item { .. } => Def::Item, diff --git a/crates/ra_analysis/src/hir/module/mod.rs b/crates/ra_analysis/src/hir/module/mod.rs index 4d5945b1a..33d2b95ab 100644 --- a/crates/ra_analysis/src/hir/module/mod.rs +++ b/crates/ra_analysis/src/hir/module/mod.rs @@ -22,53 +22,53 @@ use crate::{ pub(crate) use self::nameres::ModuleScope; -/// `ModuleDescriptor` is API entry point to get all the information +/// `Module` is API entry point to get all the information /// about a particular module. #[derive(Debug, Clone)] -pub(crate) struct ModuleDescriptor { +pub(crate) struct Module { tree: Arc, source_root_id: SourceRootId, module_id: ModuleId, } -impl ModuleDescriptor { - /// Lookup `ModuleDescriptor` by `FileId`. Note that this is inherently +impl Module { + /// Lookup `Module` by `FileId`. Note that this is inherently /// lossy transformation: in general, a single source might correspond to /// several modules. pub fn guess_from_file_id( db: &impl HirDatabase, file_id: FileId, - ) -> Cancelable> { - ModuleDescriptor::guess_from_source(db, file_id, ModuleSource::SourceFile(file_id)) + ) -> Cancelable> { + Module::guess_from_source(db, file_id, ModuleSource::SourceFile(file_id)) } - /// Lookup `ModuleDescriptor` by position in the source code. Note that this + /// Lookup `Module` by position in the source code. Note that this /// is inherently lossy transformation: in general, a single source might /// correspond to several modules. pub fn guess_from_position( db: &impl HirDatabase, position: FilePosition, - ) -> Cancelable> { + ) -> Cancelable> { let file = db.file_syntax(position.file_id); let module_source = match find_node_at_offset::(file.syntax(), position.offset) { Some(m) if !m.has_semi() => ModuleSource::new_inline(position.file_id, m), _ => ModuleSource::SourceFile(position.file_id), }; - ModuleDescriptor::guess_from_source(db, position.file_id, module_source) + Module::guess_from_source(db, position.file_id, module_source) } fn guess_from_source( db: &impl HirDatabase, file_id: FileId, module_source: ModuleSource, - ) -> Cancelable> { + ) -> Cancelable> { let source_root_id = db.file_source_root(file_id); let module_tree = db.module_tree(source_root_id)?; let res = match module_tree.any_module_for_source(module_source) { None => None, - Some(module_id) => Some(ModuleDescriptor { + Some(module_id) => Some(Module { tree: module_tree, source_root_id, module_id, @@ -81,9 +81,9 @@ impl ModuleDescriptor { db: &impl HirDatabase, source_root_id: SourceRootId, module_id: ModuleId, - ) -> Cancelable { + ) -> Cancelable { let module_tree = db.module_tree(source_root_id)?; - let res = ModuleDescriptor { + let res = Module { tree: module_tree, source_root_id, module_id, @@ -105,18 +105,18 @@ impl ModuleDescriptor { } /// Parent module. Returns `None` if this is a root module. - pub fn parent(&self) -> Option { + pub fn parent(&self) -> Option { let parent_id = self.module_id.parent(&self.tree)?; - Some(ModuleDescriptor { + Some(Module { module_id: parent_id, ..self.clone() }) } /// The root of the tree this module is part of - pub fn crate_root(&self) -> ModuleDescriptor { + pub fn crate_root(&self) -> Module { let root_id = self.module_id.crate_root(&self.tree); - ModuleDescriptor { + Module { module_id: root_id, ..self.clone() } @@ -138,9 +138,9 @@ impl ModuleDescriptor { } /// Finds a child module with the specified name. - pub fn child(&self, name: &str) -> Option { + pub fn child(&self, name: &str) -> Option { let child_id = self.module_id.child(&self.tree, name)?; - Some(ModuleDescriptor { + Some(Module { module_id: child_id, ..self.clone() }) @@ -168,7 +168,7 @@ impl ModuleDescriptor { let segments = path.segments; for name in segments.iter() { let module = match db.id_maps().def_loc(curr) { - DefLoc::Module { id, source_root } => ModuleDescriptor::new(db, source_root, id)?, + DefLoc::Module { id, source_root } => Module::new(db, source_root, id)?, _ => return Ok(None), }; let scope = module.scope(db)?; diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 377f7420f..9118ed7d4 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -20,7 +20,8 @@ use crate::{ completion::{completions, CompletionItem}, db::{self, FileSyntaxQuery, SyntaxDatabase}, hir::{ - FunctionDescriptor, FnSignatureInfo, ModuleDescriptor, + self, + FunctionDescriptor, FnSignatureInfo, Problem, }, input::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE}, @@ -226,7 +227,7 @@ impl AnalysisImpl { /// This return `Vec`: a module may be included from several places. We /// don't handle this case yet though, so the Vec has length at most one. pub fn parent_module(&self, position: FilePosition) -> Cancelable> { - let descr = match ModuleDescriptor::guess_from_position(&*self.db, position)? { + let descr = match hir::Module::guess_from_position(&*self.db, position)? { None => return Ok(Vec::new()), Some(it) => it, }; @@ -245,7 +246,7 @@ impl AnalysisImpl { } /// Returns `Vec` for the same reason as `parent_module` pub fn crate_for(&self, file_id: FileId) -> Cancelable> { - let descr = match ModuleDescriptor::guess_from_file_id(&*self.db, file_id)? { + let descr = match hir::Module::guess_from_file_id(&*self.db, file_id)? { None => return Ok(Vec::new()), Some(it) => it, }; @@ -298,7 +299,7 @@ impl AnalysisImpl { if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) { if module.has_semi() { let parent_module = - ModuleDescriptor::guess_from_file_id(&*self.db, position.file_id)?; + hir::Module::guess_from_file_id(&*self.db, position.file_id)?; let child_name = module.name(); match (parent_module, child_name) { (Some(parent_module), Some(child_name)) => { @@ -380,7 +381,7 @@ impl AnalysisImpl { fix: None, }) .collect::>(); - if let Some(m) = ModuleDescriptor::guess_from_file_id(&*self.db, file_id)? { + if let Some(m) = hir::Module::guess_from_file_id(&*self.db, file_id)? { for (name_node, problem) in m.problems(&*self.db) { let diag = match problem { Problem::UnresolvedModule { candidate } => { -- cgit v1.2.3 From 806ea03b64455a0c10fd8a8d6d98ed4e7ac15058 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 01:22:17 +0300 Subject: drop descriptor suffix, use hir:: instead --- crates/ra_analysis/src/hir/function/mod.rs | 20 ++++++++++---------- crates/ra_analysis/src/hir/mod.rs | 2 +- crates/ra_analysis/src/hir/module/nameres.rs | 4 ++-- crates/ra_analysis/src/imp.rs | 13 ++++++------- 4 files changed, 19 insertions(+), 20 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/hir/function/mod.rs b/crates/ra_analysis/src/hir/function/mod.rs index d171b6a8d..9ca8f4e7c 100644 --- a/crates/ra_analysis/src/hir/function/mod.rs +++ b/crates/ra_analysis/src/hir/function/mod.rs @@ -25,43 +25,43 @@ impl FnId { } } -pub(crate) struct FunctionDescriptor { +pub(crate) struct Function { fn_id: FnId, } -impl FunctionDescriptor { +impl Function { pub(crate) fn guess_from_source( db: &impl HirDatabase, file_id: FileId, fn_def: ast::FnDef, - ) -> FunctionDescriptor { + ) -> Function { let fn_id = FnId::get(db, file_id, fn_def); - FunctionDescriptor { fn_id } + Function { fn_id } } pub(crate) fn guess_for_name_ref( db: &impl HirDatabase, file_id: FileId, name_ref: ast::NameRef, - ) -> Option { - FunctionDescriptor::guess_for_node(db, file_id, name_ref.syntax()) + ) -> Option { + Function::guess_for_node(db, file_id, name_ref.syntax()) } pub(crate) fn guess_for_bind_pat( db: &impl HirDatabase, file_id: FileId, bind_pat: ast::BindPat, - ) -> Option { - FunctionDescriptor::guess_for_node(db, file_id, bind_pat.syntax()) + ) -> Option { + Function::guess_for_node(db, file_id, bind_pat.syntax()) } fn guess_for_node( db: &impl HirDatabase, file_id: FileId, node: SyntaxNodeRef, - ) -> Option { + ) -> Option { let fn_def = node.ancestors().find_map(ast::FnDef::cast)?; - let res = FunctionDescriptor::guess_from_source(db, file_id, fn_def); + let res = Function::guess_from_source(db, file_id, fn_def); Some(res) } diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs index 1de8fadcf..863ffd919 100644 --- a/crates/ra_analysis/src/hir/mod.rs +++ b/crates/ra_analysis/src/hir/mod.rs @@ -20,7 +20,7 @@ use crate::{ pub(crate) use self::{ path::{Path, PathKind}, module::{Module, ModuleId, Problem, nameres::FileItemId}, - function::{FunctionDescriptor, FnScopes}, + function::{Function, FnScopes}, }; pub use self::function::FnSignatureInfo; diff --git a/crates/ra_analysis/src/hir/module/nameres.rs b/crates/ra_analysis/src/hir/module/nameres.rs index db5d6d9c0..2ff5e0537 100644 --- a/crates/ra_analysis/src/hir/module/nameres.rs +++ b/crates/ra_analysis/src/hir/module/nameres.rs @@ -371,7 +371,7 @@ mod tests { use crate::{ AnalysisChange, mock_analysis::{MockAnalysis, analysis_and_position}, - hir::{HirDatabase, module::ModuleDescriptor}, + hir::{self, HirDatabase}, input::FilesDatabase, }; use super::*; @@ -380,7 +380,7 @@ mod tests { let (analysis, pos) = analysis_and_position(fixture); let db = analysis.imp.db; let source_root = db.file_source_root(pos.file_id); - let descr = ModuleDescriptor::guess_from_position(&*db, pos) + let descr = hir::Module::guess_from_position(&*db, pos) .unwrap() .unwrap(); let module_id = descr.module_id; diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 9118ed7d4..2e5853949 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -21,7 +21,7 @@ use crate::{ db::{self, FileSyntaxQuery, SyntaxDatabase}, hir::{ self, - FunctionDescriptor, FnSignatureInfo, + FnSignatureInfo, Problem, }, input::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE}, @@ -274,7 +274,7 @@ impl AnalysisImpl { let syntax = file.syntax(); if let Some(name_ref) = find_node_at_offset::(syntax, position.offset) { if let Some(fn_descr) = - FunctionDescriptor::guess_for_name_ref(&*self.db, position.file_id, name_ref) + hir::Function::guess_for_name_ref(&*self.db, position.file_id, name_ref) { let scope = fn_descr.scope(&*self.db); // First try to resolve the symbol locally @@ -344,14 +344,14 @@ impl AnalysisImpl { db: &db::RootDatabase, source_file: &'a SourceFileNode, position: FilePosition, - ) -> Option<(ast::BindPat<'a>, FunctionDescriptor)> { + ) -> Option<(ast::BindPat<'a>, hir::Function)> { let syntax = source_file.syntax(); if let Some(binding) = find_node_at_offset::(syntax, position.offset) { - let descr = FunctionDescriptor::guess_for_bind_pat(db, position.file_id, binding)?; + let descr = hir::Function::guess_for_bind_pat(db, position.file_id, binding)?; return Some((binding, descr)); }; let name_ref = find_node_at_offset::(syntax, position.offset)?; - let descr = FunctionDescriptor::guess_for_name_ref(db, position.file_id, name_ref)?; + let descr = hir::Function::guess_for_name_ref(db, position.file_id, name_ref)?; let scope = descr.scope(db); let resolved = scope.resolve_local_name(name_ref)?; let resolved = resolved.ptr().resolve(source_file); @@ -472,8 +472,7 @@ impl AnalysisImpl { if fs.kind == FN_DEF { let fn_file = self.db.file_syntax(fn_file_id); if let Some(fn_def) = find_node_at_offset(fn_file.syntax(), fs.node_range.start()) { - let descr = - FunctionDescriptor::guess_from_source(&*self.db, fn_file_id, fn_def); + let descr = hir::Function::guess_from_source(&*self.db, fn_file_id, fn_def); if let Some(descriptor) = descr.signature_info(&*self.db) { // If we have a calling expression let's find which argument we are on let mut current_parameter = None; -- cgit v1.2.3 From 4c9933c01657349438f9170c2ef7d6352144b224 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 01:38:39 +0300 Subject: check_canceled is a method --- crates/ra_analysis/src/db.rs | 17 ++++++----------- crates/ra_analysis/src/hir/module/imp.rs | 3 +-- crates/ra_analysis/src/hir/module/nameres.rs | 2 +- crates/ra_analysis/src/input.rs | 2 +- 4 files changed, 9 insertions(+), 15 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index 78bbfcf2d..d7fffc188 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -6,7 +6,6 @@ use ra_syntax::{SourceFileNode, SyntaxNode}; use salsa::{self, Database}; use crate::{ - db, hir, symbol_index::SymbolIndex, syntax_ptr::SyntaxPtr, @@ -59,14 +58,6 @@ impl Default for RootDatabase { } } -pub(crate) fn check_canceled(db: &impl salsa::Database) -> Cancelable<()> { - if db.salsa_runtime().is_current_revision_canceled() { - Err(Canceled) - } else { - Ok(()) - } -} - impl salsa::ParallelDatabase for RootDatabase { fn snapshot(&self) -> salsa::Snapshot { salsa::Snapshot::new(RootDatabase { @@ -80,7 +71,11 @@ impl salsa::ParallelDatabase for RootDatabase { pub(crate) trait BaseDatabase: salsa::Database { fn id_maps(&self) -> &IdMaps; fn check_canceled(&self) -> Cancelable<()> { - check_canceled(self) + if self.salsa_runtime().is_current_revision_canceled() { + Err(Canceled) + } else { + Ok(()) + } } } @@ -171,7 +166,7 @@ fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc { Arc::new(LineIndex::new(&*text)) } fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Cancelable> { - db::check_canceled(db)?; + db.check_canceled()?; let syntax = db.file_syntax(file_id); Ok(Arc::new(SymbolIndex::for_file(file_id, syntax))) } diff --git a/crates/ra_analysis/src/hir/module/imp.rs b/crates/ra_analysis/src/hir/module/imp.rs index 3b1baff76..d51ca2d59 100644 --- a/crates/ra_analysis/src/hir/module/imp.rs +++ b/crates/ra_analysis/src/hir/module/imp.rs @@ -8,7 +8,6 @@ use relative_path::RelativePathBuf; use rustc_hash::{FxHashMap, FxHashSet}; use crate::{ - db, hir::HirDatabase, input::{SourceRoot, SourceRootId}, Cancelable, FileId, FileResolverImp, @@ -52,7 +51,7 @@ pub(crate) fn module_tree( db: &impl HirDatabase, source_root: SourceRootId, ) -> Cancelable> { - db::check_canceled(db)?; + db.check_canceled()?; let res = create_module_tree(db, source_root)?; Ok(Arc::new(res)) } diff --git a/crates/ra_analysis/src/hir/module/nameres.rs b/crates/ra_analysis/src/hir/module/nameres.rs index 2ff5e0537..d38940085 100644 --- a/crates/ra_analysis/src/hir/module/nameres.rs +++ b/crates/ra_analysis/src/hir/module/nameres.rs @@ -248,7 +248,7 @@ where } for &module_id in self.input.keys() { - crate::db::check_canceled(self.db)?; + self.db.check_canceled()?; self.resolve_imports(module_id); } Ok(self.result) diff --git a/crates/ra_analysis/src/input.rs b/crates/ra_analysis/src/input.rs index a78b6e397..60086d1ae 100644 --- a/crates/ra_analysis/src/input.rs +++ b/crates/ra_analysis/src/input.rs @@ -33,7 +33,7 @@ impl CrateGraph { pub trait FileResolver: fmt::Debug + Send + Sync + 'static { fn file_stem(&self, file_id: FileId) -> String; fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option; - fn debug_path(&self, _file_id: FileId) -> Option { + fn debug_path(&self, _1file_id: FileId) -> Option { None } } -- cgit v1.2.3 From b9100d769a043c55f83b709de2714dac935e333f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 01:41:10 +0300 Subject: Remove unused dead code --- crates/ra_analysis/src/hir/module/mod.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/hir/module/mod.rs b/crates/ra_analysis/src/hir/module/mod.rs index 33d2b95ab..4213bc39f 100644 --- a/crates/ra_analysis/src/hir/module/mod.rs +++ b/crates/ra_analysis/src/hir/module/mod.rs @@ -218,7 +218,6 @@ impl ModuleTree { #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub(crate) enum ModuleSource { SourceFile(FileId), - #[allow(dead_code)] Module(SyntaxPtr), } -- cgit v1.2.3 From 5e7f4202cf4d64f565d6d035cd2e854acfc336ab Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 01:45:36 +0300 Subject: Move FileItems up --- crates/ra_analysis/src/hir/db.rs | 3 +- crates/ra_analysis/src/hir/mod.rs | 38 ++++++++++++++++++++++++- crates/ra_analysis/src/hir/module/nameres.rs | 36 ++--------------------- crates/ra_analysis/src/hir/query_definitions.rs | 3 +- 4 files changed, 43 insertions(+), 37 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/hir/db.rs b/crates/ra_analysis/src/hir/db.rs index c6dbde79b..a226e8205 100644 --- a/crates/ra_analysis/src/hir/db.rs +++ b/crates/ra_analysis/src/hir/db.rs @@ -8,11 +8,12 @@ use ra_syntax::{ use crate::{ FileId, db::SyntaxDatabase, + hir::{FileItems, FileItemId}, hir::query_definitions, hir::function::{FnId, FnScopes}, hir::module::{ ModuleId, ModuleTree, ModuleSource, - nameres::{ItemMap, InputModuleItems, FileItems, FileItemId} + nameres::{ItemMap, InputModuleItems} }, input::SourceRootId, Cancelable, diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs index 863ffd919..3d4a55ca4 100644 --- a/crates/ra_analysis/src/hir/mod.rs +++ b/crates/ra_analysis/src/hir/mod.rs @@ -11,15 +11,20 @@ mod function; mod module; mod path; +use std::ops::Index; + +use ra_syntax::{SyntaxNodeRef, SyntaxNode}; + use crate::{ hir::db::HirDatabase, loc2id::{DefId, DefLoc}, Cancelable, + arena::{Arena, Id}, }; pub(crate) use self::{ path::{Path, PathKind}, - module::{Module, ModuleId, Problem, nameres::FileItemId}, + module::{Module, ModuleId, Problem}, function::{Function, FnScopes}, }; @@ -43,3 +48,34 @@ impl DefId { Ok(res) } } + +/// Identifier of item within a specific file. This is stable over reparses, so +/// it's OK to use it as a salsa key/value. +pub(crate) type FileItemId = Id; + +/// Maps item's `SyntaxNode`s to `FileItemId` and back. +#[derive(Debug, PartialEq, Eq, Default)] +pub(crate) struct FileItems { + arena: Arena, +} + +impl FileItems { + fn alloc(&mut self, item: SyntaxNode) -> FileItemId { + self.arena.alloc(item) + } + fn id_of(&self, item: SyntaxNodeRef) -> FileItemId { + let (id, _item) = self + .arena + .iter() + .find(|(_id, i)| i.borrowed() == item) + .unwrap(); + id + } +} + +impl Index for FileItems { + type Output = SyntaxNode; + fn index(&self, idx: FileItemId) -> &SyntaxNode { + &self.arena[idx] + } +} diff --git a/crates/ra_analysis/src/hir/module/nameres.rs b/crates/ra_analysis/src/hir/module/nameres.rs index d38940085..f7d8c8e8c 100644 --- a/crates/ra_analysis/src/hir/module/nameres.rs +++ b/crates/ra_analysis/src/hir/module/nameres.rs @@ -16,13 +16,12 @@ //! structure itself is modified. use std::{ sync::Arc, - ops::Index, }; use rustc_hash::FxHashMap; use ra_syntax::{ - SyntaxNode, SyntaxNodeRef, TextRange, + TextRange, SmolStr, SyntaxKind::{self, *}, ast::{self, AstNode} }; @@ -31,45 +30,14 @@ use crate::{ Cancelable, FileId, loc2id::{DefId, DefLoc}, hir::{ + FileItemId, FileItems, Path, PathKind, HirDatabase, module::{ModuleId, ModuleTree}, }, input::SourceRootId, - arena::{Arena, Id} }; -/// Identifier of item within a specific file. This is stable over reparses, so -/// it's OK to use it as a salsa key/value. -pub(crate) type FileItemId = Id; - -/// Maps item's `SyntaxNode`s to `FileItemId` and back. -#[derive(Debug, PartialEq, Eq, Default)] -pub(crate) struct FileItems { - arena: Arena, -} - -impl FileItems { - pub(crate) fn alloc(&mut self, item: SyntaxNode) -> FileItemId { - self.arena.alloc(item) - } - fn id_of(&self, item: SyntaxNodeRef) -> FileItemId { - let (id, _item) = self - .arena - .iter() - .find(|(_id, i)| i.borrowed() == item) - .unwrap(); - id - } -} - -impl Index for FileItems { - type Output = SyntaxNode; - fn index(&self, idx: FileItemId) -> &SyntaxNode { - &self.arena[idx] - } -} - /// Item map is the result of the name resolution. Item map contains, for each /// module, the set of visible items. #[derive(Default, Debug, PartialEq, Eq)] diff --git a/crates/ra_analysis/src/hir/query_definitions.rs b/crates/ra_analysis/src/hir/query_definitions.rs index 8584a8d64..e6bfbc6cf 100644 --- a/crates/ra_analysis/src/hir/query_definitions.rs +++ b/crates/ra_analysis/src/hir/query_definitions.rs @@ -12,12 +12,13 @@ use ra_syntax::{ use crate::{ FileId, Cancelable, hir::{ + FileItems, FileItemId, db::HirDatabase, function::{FnId, FnScopes}, module::{ ModuleSource, ModuleSourceNode, ModuleId, imp::Submodule, - nameres::{FileItems, FileItemId, InputModuleItems, ItemMap, Resolver}, + nameres::{InputModuleItems, ItemMap, Resolver}, }, }, input::SourceRootId, -- cgit v1.2.3 From 00df339c419fc75af6a939b75be8bef5b5a69732 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 01:48:43 +0300 Subject: rename --- crates/ra_analysis/src/db.rs | 2 +- crates/ra_analysis/src/hir/db.rs | 8 ++++---- crates/ra_analysis/src/hir/mod.rs | 16 ++++++++-------- crates/ra_analysis/src/hir/module/nameres.rs | 14 +++++++------- crates/ra_analysis/src/hir/query_definitions.rs | 8 ++++---- crates/ra_analysis/src/loc2id.rs | 4 ++-- 6 files changed, 26 insertions(+), 26 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index d7fffc188..97f170473 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -127,7 +127,7 @@ salsa::database_storage! { impl hir::db::HirDatabase { fn module_tree() for hir::db::ModuleTreeQuery; fn fn_scopes() for hir::db::FnScopesQuery; - fn _file_items() for hir::db::FileItemsQuery; + fn _file_items() for hir::db::SourceFileItemsQuery; fn _file_item() for hir::db::FileItemQuery; fn _input_module_items() for hir::db::InputModuleItemsQuery; fn _item_map() for hir::db::ItemMapQuery; diff --git a/crates/ra_analysis/src/hir/db.rs b/crates/ra_analysis/src/hir/db.rs index a226e8205..e74fcc8ad 100644 --- a/crates/ra_analysis/src/hir/db.rs +++ b/crates/ra_analysis/src/hir/db.rs @@ -8,7 +8,7 @@ use ra_syntax::{ use crate::{ FileId, db::SyntaxDatabase, - hir::{FileItems, FileItemId}, + hir::{SourceFileItems, SourceFileItemId}, hir::query_definitions, hir::function::{FnId, FnScopes}, hir::module::{ @@ -33,13 +33,13 @@ pub(crate) trait HirDatabase: SyntaxDatabase { use fn query_definitions::fn_syntax; } - fn file_items(file_id: FileId) -> Arc { - type FileItemsQuery; + fn file_items(file_id: FileId) -> Arc { + type SourceFileItemsQuery; storage dependencies; use fn query_definitions::file_items; } - fn file_item(file_id: FileId, file_item_id: FileItemId) -> SyntaxNode { + fn file_item(file_id: FileId, file_item_id: SourceFileItemId) -> SyntaxNode { type FileItemQuery; storage dependencies; use fn query_definitions::file_item; diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs index 3d4a55ca4..aa416df20 100644 --- a/crates/ra_analysis/src/hir/mod.rs +++ b/crates/ra_analysis/src/hir/mod.rs @@ -51,19 +51,19 @@ impl DefId { /// Identifier of item within a specific file. This is stable over reparses, so /// it's OK to use it as a salsa key/value. -pub(crate) type FileItemId = Id; +pub(crate) type SourceFileItemId = Id; -/// Maps item's `SyntaxNode`s to `FileItemId` and back. +/// Maps item's `SyntaxNode`s to `SourceFileItemId` and back. #[derive(Debug, PartialEq, Eq, Default)] -pub(crate) struct FileItems { +pub(crate) struct SourceFileItems { arena: Arena, } -impl FileItems { - fn alloc(&mut self, item: SyntaxNode) -> FileItemId { +impl SourceFileItems { + fn alloc(&mut self, item: SyntaxNode) -> SourceFileItemId { self.arena.alloc(item) } - fn id_of(&self, item: SyntaxNodeRef) -> FileItemId { + fn id_of(&self, item: SyntaxNodeRef) -> SourceFileItemId { let (id, _item) = self .arena .iter() @@ -73,9 +73,9 @@ impl FileItems { } } -impl Index for FileItems { +impl Index for SourceFileItems { type Output = SyntaxNode; - fn index(&self, idx: FileItemId) -> &SyntaxNode { + fn index(&self, idx: SourceFileItemId) -> &SyntaxNode { &self.arena[idx] } } diff --git a/crates/ra_analysis/src/hir/module/nameres.rs b/crates/ra_analysis/src/hir/module/nameres.rs index f7d8c8e8c..9dc54f6c0 100644 --- a/crates/ra_analysis/src/hir/module/nameres.rs +++ b/crates/ra_analysis/src/hir/module/nameres.rs @@ -30,7 +30,7 @@ use crate::{ Cancelable, FileId, loc2id::{DefId, DefLoc}, hir::{ - FileItemId, FileItems, + SourceFileItemId, SourceFileItems, Path, PathKind, HirDatabase, module::{ModuleId, ModuleTree}, @@ -73,7 +73,7 @@ pub(crate) struct InputModuleItems { #[derive(Debug, PartialEq, Eq)] struct ModuleItem { - id: FileItemId, + id: SourceFileItemId, name: SmolStr, kind: SyntaxKind, vis: Vis, @@ -93,7 +93,7 @@ struct Import { #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) struct NamedImport { - file_item_id: FileItemId, + file_item_id: SourceFileItemId, relative_range: TextRange, } @@ -135,7 +135,7 @@ pub(crate) struct Resolution { impl InputModuleItems { pub(in crate::hir) fn new<'a>( - file_items: &FileItems, + file_items: &SourceFileItems, items: impl Iterator>, ) -> InputModuleItems { let mut res = InputModuleItems::default(); @@ -145,7 +145,7 @@ impl InputModuleItems { res } - fn add_item(&mut self, file_items: &FileItems, item: ast::ModuleItem) -> Option<()> { + fn add_item(&mut self, file_items: &SourceFileItems, item: ast::ModuleItem) -> Option<()> { match item { ast::ModuleItem::StructDef(it) => self.items.push(ModuleItem::new(file_items, it)?), ast::ModuleItem::EnumDef(it) => self.items.push(ModuleItem::new(file_items, it)?), @@ -166,7 +166,7 @@ impl InputModuleItems { Some(()) } - fn add_use_item(&mut self, file_items: &FileItems, item: ast::UseItem) { + fn add_use_item(&mut self, file_items: &SourceFileItems, item: ast::UseItem) { let file_item_id = file_items.id_of(item.syntax()); let start_offset = item.syntax().range().start(); Path::expand_use_item(item, |path, range| { @@ -183,7 +183,7 @@ impl InputModuleItems { } impl ModuleItem { - fn new<'a>(file_items: &FileItems, item: impl ast::NameOwner<'a>) -> Option { + fn new<'a>(file_items: &SourceFileItems, item: impl ast::NameOwner<'a>) -> Option { let name = item.name()?.text(); let kind = item.syntax().kind(); let vis = Vis::Other; diff --git a/crates/ra_analysis/src/hir/query_definitions.rs b/crates/ra_analysis/src/hir/query_definitions.rs index e6bfbc6cf..53926cf16 100644 --- a/crates/ra_analysis/src/hir/query_definitions.rs +++ b/crates/ra_analysis/src/hir/query_definitions.rs @@ -12,7 +12,7 @@ use ra_syntax::{ use crate::{ FileId, Cancelable, hir::{ - FileItems, FileItemId, + SourceFileItems, SourceFileItemId, db::HirDatabase, function::{FnId, FnScopes}, module::{ @@ -37,10 +37,10 @@ pub(super) fn fn_scopes(db: &impl HirDatabase, fn_id: FnId) -> Arc { Arc::new(res) } -pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc { +pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc { let source_file = db.file_syntax(file_id); let source_file = source_file.borrowed(); - let mut res = FileItems::default(); + let mut res = SourceFileItems::default(); source_file .syntax() .descendants() @@ -55,7 +55,7 @@ pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc SyntaxNode { db.file_items(file_id)[file_item_id].clone() } diff --git a/crates/ra_analysis/src/loc2id.rs b/crates/ra_analysis/src/loc2id.rs index 8af7b642b..49af19c75 100644 --- a/crates/ra_analysis/src/loc2id.rs +++ b/crates/ra_analysis/src/loc2id.rs @@ -9,7 +9,7 @@ use rustc_hash::FxHashMap; use crate::{ FileId, - hir::{FileItemId, ModuleId}, + hir::{SourceFileItemId, ModuleId}, syntax_ptr::SyntaxPtr, input::SourceRootId, }; @@ -104,7 +104,7 @@ pub(crate) enum DefLoc { }, Item { file_id: FileId, - id: FileItemId, + id: SourceFileItemId, }, } -- cgit v1.2.3 From 9027a21f9a1c7fcee0a59a1e28928fed29781dd8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 01:53:54 +0300 Subject: Introduce SourceItemId --- crates/ra_analysis/src/hir/db.rs | 4 ++-- crates/ra_analysis/src/hir/mod.rs | 7 +++++++ crates/ra_analysis/src/hir/module/nameres.rs | 14 ++++++++++---- crates/ra_analysis/src/hir/query_definitions.rs | 10 +++------- crates/ra_analysis/src/loc2id.rs | 6 ++---- 5 files changed, 24 insertions(+), 17 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/hir/db.rs b/crates/ra_analysis/src/hir/db.rs index e74fcc8ad..0998295f5 100644 --- a/crates/ra_analysis/src/hir/db.rs +++ b/crates/ra_analysis/src/hir/db.rs @@ -8,7 +8,7 @@ use ra_syntax::{ use crate::{ FileId, db::SyntaxDatabase, - hir::{SourceFileItems, SourceFileItemId}, + hir::{SourceFileItems, SourceItemId}, hir::query_definitions, hir::function::{FnId, FnScopes}, hir::module::{ @@ -39,7 +39,7 @@ pub(crate) trait HirDatabase: SyntaxDatabase { use fn query_definitions::file_items; } - fn file_item(file_id: FileId, file_item_id: SourceFileItemId) -> SyntaxNode { + fn file_item(source_item_id: SourceItemId) -> SyntaxNode { type FileItemQuery; storage dependencies; use fn query_definitions::file_item; diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs index aa416df20..9527cc33f 100644 --- a/crates/ra_analysis/src/hir/mod.rs +++ b/crates/ra_analysis/src/hir/mod.rs @@ -16,6 +16,7 @@ use std::ops::Index; use ra_syntax::{SyntaxNodeRef, SyntaxNode}; use crate::{ + FileId, hir::db::HirDatabase, loc2id::{DefId, DefLoc}, Cancelable, @@ -53,6 +54,12 @@ impl DefId { /// it's OK to use it as a salsa key/value. pub(crate) type SourceFileItemId = Id; +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub(crate) struct SourceItemId { + file_id: FileId, + item_id: SourceFileItemId, +} + /// Maps item's `SyntaxNode`s to `SourceFileItemId` and back. #[derive(Debug, PartialEq, Eq, Default)] pub(crate) struct SourceFileItems { diff --git a/crates/ra_analysis/src/hir/module/nameres.rs b/crates/ra_analysis/src/hir/module/nameres.rs index 9dc54f6c0..f22832eda 100644 --- a/crates/ra_analysis/src/hir/module/nameres.rs +++ b/crates/ra_analysis/src/hir/module/nameres.rs @@ -30,7 +30,7 @@ use crate::{ Cancelable, FileId, loc2id::{DefId, DefLoc}, hir::{ - SourceFileItemId, SourceFileItems, + SourceItemId, SourceFileItemId, SourceFileItems, Path, PathKind, HirDatabase, module::{ModuleId, ModuleTree}, @@ -99,7 +99,11 @@ pub(crate) struct NamedImport { impl NamedImport { pub(crate) fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange { - let syntax = db.file_item(file_id, self.file_item_id); + let source_item_id = SourceItemId { + file_id, + item_id: self.file_item_id, + }; + let syntax = db.file_item(source_item_id); let offset = syntax.borrowed().range().start(); self.relative_range + offset } @@ -247,8 +251,10 @@ where continue; } let def_loc = DefLoc::Item { - file_id, - id: item.id, + source_item_id: SourceItemId { + file_id, + item_id: item.id, + }, }; let def_id = self.db.id_maps().def_id(def_loc); let resolution = Resolution { diff --git a/crates/ra_analysis/src/hir/query_definitions.rs b/crates/ra_analysis/src/hir/query_definitions.rs index 53926cf16..ae292e964 100644 --- a/crates/ra_analysis/src/hir/query_definitions.rs +++ b/crates/ra_analysis/src/hir/query_definitions.rs @@ -12,7 +12,7 @@ use ra_syntax::{ use crate::{ FileId, Cancelable, hir::{ - SourceFileItems, SourceFileItemId, + SourceFileItems, SourceItemId, db::HirDatabase, function::{FnId, FnScopes}, module::{ @@ -52,12 +52,8 @@ pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc SyntaxNode { - db.file_items(file_id)[file_item_id].clone() +pub(super) fn file_item(db: &impl HirDatabase, source_item_id: SourceItemId) -> SyntaxNode { + db.file_items(source_item_id.file_id)[source_item_id.item_id].clone() } pub(crate) fn submodules( diff --git a/crates/ra_analysis/src/loc2id.rs b/crates/ra_analysis/src/loc2id.rs index 49af19c75..5b2c0f615 100644 --- a/crates/ra_analysis/src/loc2id.rs +++ b/crates/ra_analysis/src/loc2id.rs @@ -8,8 +8,7 @@ use std::{ use rustc_hash::FxHashMap; use crate::{ - FileId, - hir::{SourceFileItemId, ModuleId}, + hir::{SourceItemId, ModuleId}, syntax_ptr::SyntaxPtr, input::SourceRootId, }; @@ -103,8 +102,7 @@ pub(crate) enum DefLoc { source_root: SourceRootId, }, Item { - file_id: FileId, - id: SourceFileItemId, + source_item_id: SourceItemId, }, } -- cgit v1.2.3 From 3922503205e2798e21273a22112f584951f25623 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 02:09:09 +0300 Subject: ItemId based module source --- crates/ra_analysis/src/hir/module/mod.rs | 32 +++++++++++++++---------- crates/ra_analysis/src/hir/query_definitions.rs | 7 +++--- crates/ra_analysis/src/syntax_ptr.rs | 4 ---- 3 files changed, 23 insertions(+), 20 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/hir/module/mod.rs b/crates/ra_analysis/src/hir/module/mod.rs index 4213bc39f..a6b7a5466 100644 --- a/crates/ra_analysis/src/hir/module/mod.rs +++ b/crates/ra_analysis/src/hir/module/mod.rs @@ -13,8 +13,8 @@ use ra_syntax::{ use relative_path::RelativePathBuf; use crate::{ - db::SyntaxDatabase, syntax_ptr::SyntaxPtr, FileId, FilePosition, Cancelable, - hir::{Path, PathKind, HirDatabase}, + FileId, FilePosition, Cancelable, + hir::{Path, PathKind, HirDatabase, SourceItemId}, input::SourceRootId, arena::{Arena, Id}, loc2id::{DefLoc, DefId}, @@ -52,7 +52,7 @@ impl Module { let file = db.file_syntax(position.file_id); let module_source = match find_node_at_offset::(file.syntax(), position.offset) { - Some(m) if !m.has_semi() => ModuleSource::new_inline(position.file_id, m), + Some(m) if !m.has_semi() => ModuleSource::new_inline(db, position.file_id, m), _ => ModuleSource::SourceFile(position.file_id), }; Module::guess_from_source(db, position.file_id, module_source) @@ -218,7 +218,7 @@ impl ModuleTree { #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub(crate) enum ModuleSource { SourceFile(FileId), - Module(SyntaxPtr), + Module(SourceItemId), } /// An owned syntax node for a module. Unlike `ModuleSource`, @@ -273,7 +273,7 @@ impl ModuleId { Some((link.name.clone(), module)) }) } - fn problems(self, tree: &ModuleTree, db: &impl SyntaxDatabase) -> Vec<(SyntaxNode, Problem)> { + fn problems(self, tree: &ModuleTree, db: &impl HirDatabase) -> Vec<(SyntaxNode, Problem)> { tree.mods[self] .children .iter() @@ -294,7 +294,7 @@ impl LinkId { fn name(self, tree: &ModuleTree) -> SmolStr { tree.links[self].name.clone() } - fn bind_source<'a>(self, tree: &ModuleTree, db: &impl SyntaxDatabase) -> ast::ModuleNode { + fn bind_source<'a>(self, tree: &ModuleTree, db: &impl HirDatabase) -> ast::ModuleNode { let owner = self.owner(tree); match owner.source(tree).resolve(db) { ModuleSourceNode::SourceFile(root) => { @@ -317,10 +317,16 @@ pub(crate) struct ModuleData { } impl ModuleSource { - pub(crate) fn new_inline(file_id: FileId, module: ast::Module) -> ModuleSource { + pub(crate) fn new_inline( + db: &impl HirDatabase, + file_id: FileId, + module: ast::Module, + ) -> ModuleSource { assert!(!module.has_semi()); - let ptr = SyntaxPtr::new(file_id, module.syntax()); - ModuleSource::Module(ptr) + let items = db.file_items(file_id); + let item_id = items.id_of(module.syntax()); + let id = SourceItemId { file_id, item_id }; + ModuleSource::Module(id) } pub(crate) fn as_file(self) -> Option { @@ -333,18 +339,18 @@ impl ModuleSource { pub(crate) fn file_id(self) -> FileId { match self { ModuleSource::SourceFile(f) => f, - ModuleSource::Module(ptr) => ptr.file_id(), + ModuleSource::Module(source_item_id) => source_item_id.file_id, } } - pub(crate) fn resolve(self, db: &impl SyntaxDatabase) -> ModuleSourceNode { + pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { match self { ModuleSource::SourceFile(file_id) => { let syntax = db.file_syntax(file_id); ModuleSourceNode::SourceFile(syntax.ast().owned()) } - ModuleSource::Module(ptr) => { - let syntax = db.resolve_syntax_ptr(ptr); + ModuleSource::Module(item_id) => { + let syntax = db.file_item(item_id); let syntax = syntax.borrowed(); let module = ast::Module::cast(syntax).unwrap(); ModuleSourceNode::Module(module.owned()) diff --git a/crates/ra_analysis/src/hir/query_definitions.rs b/crates/ra_analysis/src/hir/query_definitions.rs index ae292e964..cdd986ce4 100644 --- a/crates/ra_analysis/src/hir/query_definitions.rs +++ b/crates/ra_analysis/src/hir/query_definitions.rs @@ -63,16 +63,17 @@ pub(crate) fn submodules( db.check_canceled()?; let file_id = source.file_id(); let submodules = match source.resolve(db) { - ModuleSourceNode::SourceFile(it) => collect_submodules(file_id, it.borrowed()), + ModuleSourceNode::SourceFile(it) => collect_submodules(db, file_id, it.borrowed()), ModuleSourceNode::Module(it) => it .borrowed() .item_list() - .map(|it| collect_submodules(file_id, it)) + .map(|it| collect_submodules(db, file_id, it)) .unwrap_or_else(Vec::new), }; return Ok(Arc::new(submodules)); fn collect_submodules<'a>( + db: &impl HirDatabase, file_id: FileId, root: impl ast::ModuleItemOwner<'a>, ) -> Vec { @@ -81,7 +82,7 @@ pub(crate) fn submodules( if m.has_semi() { Submodule::Declaration(name) } else { - let src = ModuleSource::new_inline(file_id, m); + let src = ModuleSource::new_inline(db, file_id, m); Submodule::Definition(name, src) } }) diff --git a/crates/ra_analysis/src/syntax_ptr.rs b/crates/ra_analysis/src/syntax_ptr.rs index 3168e82a7..ca8efc9b2 100644 --- a/crates/ra_analysis/src/syntax_ptr.rs +++ b/crates/ra_analysis/src/syntax_ptr.rs @@ -22,10 +22,6 @@ impl SyntaxPtr { let local = LocalSyntaxPtr::new(node); SyntaxPtr { file_id, local } } - - pub(crate) fn file_id(self) -> FileId { - self.file_id - } } /// A pionter to a syntax node inside a file. -- cgit v1.2.3 From c2abd17f57fa14960e8a175bdabe49eb365a585f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 02:13:52 +0300 Subject: Use ItemPtr for id --- crates/ra_analysis/src/hir/function/mod.rs | 10 ++++++---- crates/ra_analysis/src/hir/query_definitions.rs | 4 ++-- crates/ra_analysis/src/loc2id.rs | 9 ++++----- crates/ra_analysis/src/syntax_ptr.rs | 7 ------- 4 files changed, 12 insertions(+), 18 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/hir/function/mod.rs b/crates/ra_analysis/src/hir/function/mod.rs index 9ca8f4e7c..280218fd4 100644 --- a/crates/ra_analysis/src/hir/function/mod.rs +++ b/crates/ra_analysis/src/hir/function/mod.rs @@ -11,8 +11,8 @@ use ra_syntax::{ }; use crate::{ - hir::HirDatabase, - syntax_ptr::SyntaxPtr, FileId, + hir::{HirDatabase, SourceItemId}, + FileId, }; pub(crate) use self::scope::FnScopes; @@ -20,8 +20,10 @@ pub(crate) use crate::loc2id::FnId; impl FnId { pub(crate) fn get(db: &impl HirDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId { - let ptr = SyntaxPtr::new(file_id, fn_def.syntax()); - db.id_maps().fn_id(ptr) + let file_items = db.file_items(file_id); + let item_id = file_items.id_of(fn_def.syntax()); + let item_id = SourceItemId { file_id, item_id }; + db.id_maps().fn_id(item_id) } } diff --git a/crates/ra_analysis/src/hir/query_definitions.rs b/crates/ra_analysis/src/hir/query_definitions.rs index cdd986ce4..6c633e9ab 100644 --- a/crates/ra_analysis/src/hir/query_definitions.rs +++ b/crates/ra_analysis/src/hir/query_definitions.rs @@ -26,8 +26,8 @@ use crate::{ /// Resolve `FnId` to the corresponding `SyntaxNode` pub(super) fn fn_syntax(db: &impl HirDatabase, fn_id: FnId) -> FnDefNode { - let ptr = db.id_maps().fn_ptr(fn_id); - let syntax = db.resolve_syntax_ptr(ptr); + let item_id = db.id_maps().fn_item_id(fn_id); + let syntax = db.file_item(item_id); FnDef::cast(syntax.borrowed()).unwrap().owned() } diff --git a/crates/ra_analysis/src/loc2id.rs b/crates/ra_analysis/src/loc2id.rs index 5b2c0f615..204708942 100644 --- a/crates/ra_analysis/src/loc2id.rs +++ b/crates/ra_analysis/src/loc2id.rs @@ -9,7 +9,6 @@ use rustc_hash::FxHashMap; use crate::{ hir::{SourceItemId, ModuleId}, - syntax_ptr::SyntaxPtr, input::SourceRootId, }; @@ -112,10 +111,10 @@ pub(crate) struct IdMaps { } impl IdMaps { - pub(crate) fn fn_id(&self, ptr: SyntaxPtr) -> FnId { - self.inner.fns.lock().loc2id(&ptr) + pub(crate) fn fn_id(&self, item_id: SourceItemId) -> FnId { + self.inner.fns.lock().loc2id(&item_id) } - pub(crate) fn fn_ptr(&self, fn_id: FnId) -> SyntaxPtr { + pub(crate) fn fn_item_id(&self, fn_id: FnId) -> SourceItemId { self.inner.fns.lock().id2loc(fn_id) } @@ -129,6 +128,6 @@ impl IdMaps { #[derive(Debug, Default)] struct IdMapsInner { - fns: Mutex>, + fns: Mutex>, defs: Mutex>, } diff --git a/crates/ra_analysis/src/syntax_ptr.rs b/crates/ra_analysis/src/syntax_ptr.rs index ca8efc9b2..7bbf1fc33 100644 --- a/crates/ra_analysis/src/syntax_ptr.rs +++ b/crates/ra_analysis/src/syntax_ptr.rs @@ -17,13 +17,6 @@ pub(crate) struct SyntaxPtr { local: LocalSyntaxPtr, } -impl SyntaxPtr { - pub(crate) fn new(file_id: FileId, node: SyntaxNodeRef) -> SyntaxPtr { - let local = LocalSyntaxPtr::new(node); - SyntaxPtr { file_id, local } - } -} - /// A pionter to a syntax node inside a file. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub(crate) struct LocalSyntaxPtr { -- cgit v1.2.3 From 201aa7ea2ae75f9599dc74ad2f4d3f41c1540e73 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 02:15:21 +0300 Subject: remove syntax ptr --- crates/ra_analysis/src/db.rs | 10 +--------- crates/ra_analysis/src/syntax_ptr.rs | 17 ----------------- 2 files changed, 1 insertion(+), 26 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index 97f170473..4c9c84e4f 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -2,13 +2,12 @@ use std::sync::Arc; #[cfg(test)] use parking_lot::Mutex; use ra_editor::LineIndex; -use ra_syntax::{SourceFileNode, SyntaxNode}; +use ra_syntax::{SourceFileNode}; use salsa::{self, Database}; use crate::{ hir, symbol_index::SymbolIndex, - syntax_ptr::SyntaxPtr, loc2id::{IdMaps}, Cancelable, Canceled, FileId, }; @@ -122,7 +121,6 @@ salsa::database_storage! { fn file_syntax() for FileSyntaxQuery; fn file_lines() for FileLinesQuery; fn file_symbols() for FileSymbolsQuery; - fn resolve_syntax_ptr() for ResolveSyntaxPtrQuery; } impl hir::db::HirDatabase { fn module_tree() for hir::db::ModuleTreeQuery; @@ -148,12 +146,6 @@ salsa::query_group! { fn file_symbols(file_id: FileId) -> Cancelable> { type FileSymbolsQuery; } - fn resolve_syntax_ptr(ptr: SyntaxPtr) -> SyntaxNode { - type ResolveSyntaxPtrQuery; - // Don't retain syntax trees in memory - storage dependencies; - use fn crate::syntax_ptr::resolve_syntax_ptr; - } } } diff --git a/crates/ra_analysis/src/syntax_ptr.rs b/crates/ra_analysis/src/syntax_ptr.rs index 7bbf1fc33..f4b05fc19 100644 --- a/crates/ra_analysis/src/syntax_ptr.rs +++ b/crates/ra_analysis/src/syntax_ptr.rs @@ -1,22 +1,5 @@ use ra_syntax::{SourceFileNode, SyntaxKind, SyntaxNode, SyntaxNodeRef, TextRange}; -use crate::db::SyntaxDatabase; -use crate::FileId; - -pub(crate) fn resolve_syntax_ptr(db: &impl SyntaxDatabase, ptr: SyntaxPtr) -> SyntaxNode { - let syntax = db.file_syntax(ptr.file_id); - ptr.local.resolve(&syntax) -} - -/// SyntaxPtr is a cheap `Copy` id which identifies a particular syntax node, -/// without retaining syntax tree in memory. You need to explicitly `resolve` -/// `SyntaxPtr` to get a `SyntaxNode` -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(crate) struct SyntaxPtr { - file_id: FileId, - local: LocalSyntaxPtr, -} - /// A pionter to a syntax node inside a file. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub(crate) struct LocalSyntaxPtr { -- cgit v1.2.3 From 65c064b2a99fb9c0589672ae6c9848aaa9e0efcf Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 02:22:25 +0300 Subject: introduce SymbolsDatabase --- crates/ra_analysis/src/db.rs | 28 +++++++++++----------------- crates/ra_analysis/src/imp.rs | 4 ++-- crates/ra_analysis/src/input.rs | 6 +----- crates/ra_analysis/src/symbol_index.rs | 29 ++++++++++++++++++++++++++--- 4 files changed, 40 insertions(+), 27 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index 4c9c84e4f..0901c2bba 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -7,7 +7,7 @@ use salsa::{self, Database}; use crate::{ hir, - symbol_index::SymbolIndex, + symbol_index, loc2id::{IdMaps}, Cancelable, Canceled, FileId, }; @@ -114,23 +114,25 @@ salsa::database_storage! { fn file_source_root() for crate::input::FileSourceRootQuery; fn source_root() for crate::input::SourceRootQuery; fn libraries() for crate::input::LibrariesQuery; - fn library_symbols() for crate::input::LibrarySymbolsQuery; fn crate_graph() for crate::input::CrateGraphQuery; } impl SyntaxDatabase { fn file_syntax() for FileSyntaxQuery; fn file_lines() for FileLinesQuery; - fn file_symbols() for FileSymbolsQuery; + } + impl symbol_index::SymbolsDatabase { + fn file_symbols() for symbol_index::FileSymbolsQuery; + fn library_symbols() for symbol_index::LibrarySymbolsQuery; } impl hir::db::HirDatabase { fn module_tree() for hir::db::ModuleTreeQuery; fn fn_scopes() for hir::db::FnScopesQuery; - fn _file_items() for hir::db::SourceFileItemsQuery; - fn _file_item() for hir::db::FileItemQuery; - fn _input_module_items() for hir::db::InputModuleItemsQuery; - fn _item_map() for hir::db::ItemMapQuery; - fn _fn_syntax() for hir::db::FnSyntaxQuery; - fn _submodules() for hir::db::SubmodulesQuery; + fn file_items() for hir::db::SourceFileItemsQuery; + fn file_item() for hir::db::FileItemQuery; + fn input_module_items() for hir::db::InputModuleItemsQuery; + fn item_map() for hir::db::ItemMapQuery; + fn fn_syntax() for hir::db::FnSyntaxQuery; + fn submodules() for hir::db::SubmodulesQuery; } } } @@ -143,9 +145,6 @@ salsa::query_group! { fn file_lines(file_id: FileId) -> Arc { type FileLinesQuery; } - fn file_symbols(file_id: FileId) -> Cancelable> { - type FileSymbolsQuery; - } } } @@ -157,8 +156,3 @@ fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc { let text = db.file_text(file_id); Arc::new(LineIndex::new(&*text)) } -fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Cancelable> { - db.check_canceled()?; - let syntax = db.file_syntax(file_id); - Ok(Arc::new(SymbolIndex::for_file(file_id, syntax))) -} diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 2e5853949..e3b78bb1a 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -25,7 +25,7 @@ use crate::{ Problem, }, input::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE}, - symbol_index::SymbolIndex, + symbol_index::{SymbolIndex, SymbolsDatabase}, AnalysisChange, Cancelable, CrateGraph, CrateId, Diagnostic, FileId, FileResolver, FileSystemEdit, FilePosition, Query, SourceChange, SourceFileNodeEdit, }; @@ -161,7 +161,7 @@ impl AnalysisHostImpl { .query_mut(crate::input::SourceRootQuery) .set(source_root_id, Arc::new(source_root)); self.db - .query_mut(crate::input::LibrarySymbolsQuery) + .query_mut(crate::symbol_index::LibrarySymbolsQuery) .set(source_root_id, Arc::new(library.symbol_index)); } self.db diff --git a/crates/ra_analysis/src/input.rs b/crates/ra_analysis/src/input.rs index 60086d1ae..e601cd58a 100644 --- a/crates/ra_analysis/src/input.rs +++ b/crates/ra_analysis/src/input.rs @@ -5,7 +5,7 @@ use rustc_hash::FxHashMap; use rustc_hash::FxHashSet; use salsa; -use crate::{symbol_index::SymbolIndex, FileResolverImp}; +use crate::FileResolverImp; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct FileId(pub u32); @@ -56,10 +56,6 @@ salsa::query_group! { type LibrariesQuery; storage input; } - fn library_symbols(id: SourceRootId) -> Arc { - type LibrarySymbolsQuery; - storage input; - } fn crate_graph() -> Arc { type CrateGraphQuery; storage input; diff --git a/crates/ra_analysis/src/symbol_index.rs b/crates/ra_analysis/src/symbol_index.rs index 3a0667ecd..a6937d7f2 100644 --- a/crates/ra_analysis/src/symbol_index.rs +++ b/crates/ra_analysis/src/symbol_index.rs @@ -4,14 +4,37 @@ use std::{ }; use fst::{self, Streamer}; -use ra_editor::{file_symbols, FileSymbol}; +use ra_editor::{self, FileSymbol}; use ra_syntax::{ SourceFileNode, SyntaxKind::{self, *}, }; use rayon::prelude::*; -use crate::{FileId, Query}; +use crate::{ + Cancelable, + FileId, Query, + db::SyntaxDatabase, + input::SourceRootId, +}; + +salsa::query_group! { + pub(crate) trait SymbolsDatabase: SyntaxDatabase { + fn file_symbols(file_id: FileId) -> Cancelable> { + type FileSymbolsQuery; + } + fn library_symbols(id: SourceRootId) -> Arc { + type LibrarySymbolsQuery; + storage input; + } + } +} + +fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Cancelable> { + db.check_canceled()?; + let syntax = db.file_syntax(file_id); + Ok(Arc::new(SymbolIndex::for_file(file_id, syntax))) +} #[derive(Default, Debug)] pub(crate) struct SymbolIndex { @@ -39,7 +62,7 @@ impl SymbolIndex { ) -> SymbolIndex { let mut symbols = files .flat_map(|(file_id, file)| { - file_symbols(&file) + ra_editor::file_symbols(&file) .into_iter() .map(move |symbol| (symbol.name.as_str().to_lowercase(), (file_id, symbol))) .collect::>() -- cgit v1.2.3 From ec45dfea1e37ba40ea3e2c8c6df0991a3d49213f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 02:25:03 +0300 Subject: rename file_syntax -> source_file --- crates/ra_analysis/src/completion/mod.rs | 2 +- crates/ra_analysis/src/db.rs | 8 ++++---- crates/ra_analysis/src/hir/module/mod.rs | 4 ++-- crates/ra_analysis/src/hir/query_definitions.rs | 2 +- crates/ra_analysis/src/imp.rs | 18 +++++++++--------- crates/ra_analysis/src/symbol_index.rs | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/completion/mod.rs b/crates/ra_analysis/src/completion/mod.rs index 67ec9a735..844dabb19 100644 --- a/crates/ra_analysis/src/completion/mod.rs +++ b/crates/ra_analysis/src/completion/mod.rs @@ -29,7 +29,7 @@ pub(crate) fn completions( db: &db::RootDatabase, position: FilePosition, ) -> Cancelable>> { - let original_file = db.file_syntax(position.file_id); + let original_file = db.source_file(position.file_id); // Insert a fake ident to get a valid parse tree let file = { let edit = AtomEdit::insert(position.offset, "intellijRulezz".to_string()); diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index 0901c2bba..11154cc65 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -117,7 +117,7 @@ salsa::database_storage! { fn crate_graph() for crate::input::CrateGraphQuery; } impl SyntaxDatabase { - fn file_syntax() for FileSyntaxQuery; + fn source_file() for SourceFileQuery; fn file_lines() for FileLinesQuery; } impl symbol_index::SymbolsDatabase { @@ -139,8 +139,8 @@ salsa::database_storage! { salsa::query_group! { pub(crate) trait SyntaxDatabase: crate::input::FilesDatabase + BaseDatabase { - fn file_syntax(file_id: FileId) -> SourceFileNode { - type FileSyntaxQuery; + fn source_file(file_id: FileId) -> SourceFileNode { + type SourceFileQuery; } fn file_lines(file_id: FileId) -> Arc { type FileLinesQuery; @@ -148,7 +148,7 @@ salsa::query_group! { } } -fn file_syntax(db: &impl SyntaxDatabase, file_id: FileId) -> SourceFileNode { +fn source_file(db: &impl SyntaxDatabase, file_id: FileId) -> SourceFileNode { let text = db.file_text(file_id); SourceFileNode::parse(&*text) } diff --git a/crates/ra_analysis/src/hir/module/mod.rs b/crates/ra_analysis/src/hir/module/mod.rs index a6b7a5466..83f176b32 100644 --- a/crates/ra_analysis/src/hir/module/mod.rs +++ b/crates/ra_analysis/src/hir/module/mod.rs @@ -49,7 +49,7 @@ impl Module { db: &impl HirDatabase, position: FilePosition, ) -> Cancelable> { - let file = db.file_syntax(position.file_id); + let file = db.source_file(position.file_id); let module_source = match find_node_at_offset::(file.syntax(), position.offset) { Some(m) if !m.has_semi() => ModuleSource::new_inline(db, position.file_id, m), @@ -346,7 +346,7 @@ impl ModuleSource { pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { match self { ModuleSource::SourceFile(file_id) => { - let syntax = db.file_syntax(file_id); + let syntax = db.source_file(file_id); ModuleSourceNode::SourceFile(syntax.ast().owned()) } ModuleSource::Module(item_id) => { diff --git a/crates/ra_analysis/src/hir/query_definitions.rs b/crates/ra_analysis/src/hir/query_definitions.rs index 6c633e9ab..e7fba5d72 100644 --- a/crates/ra_analysis/src/hir/query_definitions.rs +++ b/crates/ra_analysis/src/hir/query_definitions.rs @@ -38,7 +38,7 @@ pub(super) fn fn_scopes(db: &impl HirDatabase, fn_id: FnId) -> Arc { } pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc { - let source_file = db.file_syntax(file_id); + let source_file = db.source_file(file_id); let source_file = source_file.borrowed(); let mut res = SourceFileItems::default(); source_file diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index e3b78bb1a..c86bc111a 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -18,7 +18,7 @@ use salsa::{Database, ParallelDatabase}; use crate::{ completion::{completions, CompletionItem}, - db::{self, FileSyntaxQuery, SyntaxDatabase}, + db::{self, SourceFileQuery, SyntaxDatabase}, hir::{ self, FnSignatureInfo, @@ -189,7 +189,7 @@ impl fmt::Debug for AnalysisImpl { impl AnalysisImpl { pub fn file_syntax(&self, file_id: FileId) -> SourceFileNode { - self.db.file_syntax(file_id) + self.db.source_file(file_id) } pub fn file_line_index(&self, file_id: FileId) -> Arc { self.db.file_lines(file_id) @@ -220,7 +220,7 @@ impl AnalysisImpl { .collect() }; self.db - .query(FileSyntaxQuery) + .query(SourceFileQuery) .sweep(salsa::SweepStrategy::default().discard_values()); Ok(query.search(&buf)) } @@ -270,7 +270,7 @@ impl AnalysisImpl { &self, position: FilePosition, ) -> Cancelable> { - let file = self.db.file_syntax(position.file_id); + let file = self.db.source_file(position.file_id); let syntax = file.syntax(); if let Some(name_ref) = find_node_at_offset::(syntax, position.offset) { if let Some(fn_descr) = @@ -322,7 +322,7 @@ impl AnalysisImpl { } pub fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> { - let file = self.db.file_syntax(position.file_id); + let file = self.db.source_file(position.file_id); // Find the binding associated with the offset let (binding, descr) = match find_binding(&self.db, &file, position) { None => return Vec::new(), @@ -365,13 +365,13 @@ impl AnalysisImpl { file_id: FileId, symbol: FileSymbol, ) -> Cancelable> { - let file = self.db.file_syntax(file_id); + let file = self.db.source_file(file_id); Ok(symbol.docs(&file)) } pub fn diagnostics(&self, file_id: FileId) -> Cancelable> { - let syntax = self.db.file_syntax(file_id); + let syntax = self.db.source_file(file_id); let mut res = ra_editor::diagnostics(&syntax) .into_iter() @@ -459,7 +459,7 @@ impl AnalysisImpl { &self, position: FilePosition, ) -> Cancelable)>> { - let file = self.db.file_syntax(position.file_id); + let file = self.db.source_file(position.file_id); let syntax = file.syntax(); // Find the calling expression and it's NameRef @@ -470,7 +470,7 @@ impl AnalysisImpl { let file_symbols = self.index_resolve(name_ref)?; for (fn_file_id, fs) in file_symbols { if fs.kind == FN_DEF { - let fn_file = self.db.file_syntax(fn_file_id); + let fn_file = self.db.source_file(fn_file_id); if let Some(fn_def) = find_node_at_offset(fn_file.syntax(), fs.node_range.start()) { let descr = hir::Function::guess_from_source(&*self.db, fn_file_id, fn_def); if let Some(descriptor) = descr.signature_info(&*self.db) { diff --git a/crates/ra_analysis/src/symbol_index.rs b/crates/ra_analysis/src/symbol_index.rs index a6937d7f2..747b34e38 100644 --- a/crates/ra_analysis/src/symbol_index.rs +++ b/crates/ra_analysis/src/symbol_index.rs @@ -32,7 +32,7 @@ salsa::query_group! { fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Cancelable> { db.check_canceled()?; - let syntax = db.file_syntax(file_id); + let syntax = db.source_file(file_id); Ok(Arc::new(SymbolIndex::for_file(file_id, syntax))) } -- cgit v1.2.3 From b2de95879a8d48cc4077895376b0aaed1e972169 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 02:49:28 +0300 Subject: generalize location interner --- crates/ra_analysis/src/db.rs | 18 ++++- crates/ra_analysis/src/hir/db.rs | 18 +++-- crates/ra_analysis/src/hir/function/mod.rs | 2 +- crates/ra_analysis/src/hir/mod.rs | 2 +- crates/ra_analysis/src/hir/module/mod.rs | 4 +- crates/ra_analysis/src/hir/module/nameres.rs | 6 +- crates/ra_analysis/src/hir/query_definitions.rs | 2 +- crates/ra_analysis/src/loc2id.rs | 98 +++++++++++++++++-------- 8 files changed, 100 insertions(+), 50 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index 11154cc65..e0b7afac5 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -8,7 +8,7 @@ use salsa::{self, Database}; use crate::{ hir, symbol_index, - loc2id::{IdMaps}, + loc2id::{IdMaps, LocationIntener, DefId, DefLoc, FnId}, Cancelable, Canceled, FileId, }; @@ -20,7 +20,7 @@ pub(crate) struct RootDatabase { events: (), runtime: salsa::Runtime, - id_maps: IdMaps, + id_maps: Arc, } impl salsa::Database for RootDatabase { @@ -45,7 +45,7 @@ impl Default for RootDatabase { let mut db = RootDatabase { events: Default::default(), runtime: salsa::Runtime::default(), - id_maps: IdMaps::default(), + id_maps: Default::default(), }; db.query_mut(crate::input::SourceRootQuery) .set(crate::input::WORKSPACE, Default::default()); @@ -84,6 +84,18 @@ impl BaseDatabase for RootDatabase { } } +impl AsRef> for RootDatabase { + fn as_ref(&self) -> &LocationIntener { + &self.id_maps.defs + } +} + +impl AsRef> for RootDatabase { + fn as_ref(&self) -> &LocationIntener { + &self.id_maps.fns + } +} + #[cfg(test)] impl RootDatabase { pub(crate) fn log(&self, f: impl FnOnce()) -> Vec> { diff --git a/crates/ra_analysis/src/hir/db.rs b/crates/ra_analysis/src/hir/db.rs index 0998295f5..bf0dc393a 100644 --- a/crates/ra_analysis/src/hir/db.rs +++ b/crates/ra_analysis/src/hir/db.rs @@ -8,20 +8,24 @@ use ra_syntax::{ use crate::{ FileId, db::SyntaxDatabase, - hir::{SourceFileItems, SourceItemId}, - hir::query_definitions, - hir::function::{FnId, FnScopes}, - hir::module::{ - ModuleId, ModuleTree, ModuleSource, - nameres::{ItemMap, InputModuleItems} + hir::{ + SourceFileItems, SourceItemId, + query_definitions, + function::{FnScopes}, + module::{ModuleId, ModuleTree, ModuleSource, + nameres::{ItemMap, InputModuleItems}}, }, input::SourceRootId, + loc2id::{DefLoc, DefId, FnId, LocationIntener}, Cancelable, }; salsa::query_group! { -pub(crate) trait HirDatabase: SyntaxDatabase { +pub(crate) trait HirDatabase: SyntaxDatabase + + AsRef> + + AsRef> +{ fn fn_scopes(fn_id: FnId) -> Arc { type FnScopesQuery; use fn query_definitions::fn_scopes; diff --git a/crates/ra_analysis/src/hir/function/mod.rs b/crates/ra_analysis/src/hir/function/mod.rs index 280218fd4..e64a9f045 100644 --- a/crates/ra_analysis/src/hir/function/mod.rs +++ b/crates/ra_analysis/src/hir/function/mod.rs @@ -23,7 +23,7 @@ impl FnId { let file_items = db.file_items(file_id); let item_id = file_items.id_of(fn_def.syntax()); let item_id = SourceItemId { file_id, item_id }; - db.id_maps().fn_id(item_id) + FnId::from_loc(db, &item_id) } } diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs index 9527cc33f..61e6c9913 100644 --- a/crates/ra_analysis/src/hir/mod.rs +++ b/crates/ra_analysis/src/hir/mod.rs @@ -38,7 +38,7 @@ pub(crate) enum Def { impl DefId { pub(crate) fn resolve(self, db: &impl HirDatabase) -> Cancelable { - let loc = db.id_maps().def_loc(self); + let loc = self.loc(db); let res = match loc { DefLoc::Module { id, source_root } => { let descr = Module::new(db, source_root, id)?; diff --git a/crates/ra_analysis/src/hir/module/mod.rs b/crates/ra_analysis/src/hir/module/mod.rs index 83f176b32..893ec3a10 100644 --- a/crates/ra_analysis/src/hir/module/mod.rs +++ b/crates/ra_analysis/src/hir/module/mod.rs @@ -134,7 +134,7 @@ impl Module { id: self.module_id, source_root: self.source_root_id, }; - db.id_maps().def_id(def_loc) + def_loc.id(db) } /// Finds a child module with the specified name. @@ -167,7 +167,7 @@ impl Module { let segments = path.segments; for name in segments.iter() { - let module = match db.id_maps().def_loc(curr) { + let module = match curr.loc(db) { DefLoc::Module { id, source_root } => Module::new(db, source_root, id)?, _ => return Ok(None), }; diff --git a/crates/ra_analysis/src/hir/module/nameres.rs b/crates/ra_analysis/src/hir/module/nameres.rs index f22832eda..f48f51c8d 100644 --- a/crates/ra_analysis/src/hir/module/nameres.rs +++ b/crates/ra_analysis/src/hir/module/nameres.rs @@ -256,7 +256,7 @@ where item_id: item.id, }, }; - let def_id = self.db.id_maps().def_id(def_loc); + let def_id = def_loc.id(self.db); let resolution = Resolution { def_id: Some(def_id), import: None, @@ -269,7 +269,7 @@ where id: mod_id, source_root: self.source_root, }; - let def_id = self.db.id_maps().def_id(def_loc); + let def_id = def_loc.id(self.db); let resolution = Resolution { def_id: Some(def_id), import: None, @@ -318,7 +318,7 @@ where }; if !is_last { - curr = match self.db.id_maps().def_loc(def_id) { + curr = match def_id.loc(self.db) { DefLoc::Module { id, .. } => id, _ => return, } diff --git a/crates/ra_analysis/src/hir/query_definitions.rs b/crates/ra_analysis/src/hir/query_definitions.rs index e7fba5d72..6570ca994 100644 --- a/crates/ra_analysis/src/hir/query_definitions.rs +++ b/crates/ra_analysis/src/hir/query_definitions.rs @@ -26,7 +26,7 @@ use crate::{ /// Resolve `FnId` to the corresponding `SyntaxNode` pub(super) fn fn_syntax(db: &impl HirDatabase, fn_id: FnId) -> FnDefNode { - let item_id = db.id_maps().fn_item_id(fn_id); + let item_id = fn_id.loc(db); let syntax = db.file_item(item_id); FnDef::cast(syntax.borrowed()).unwrap().owned() } diff --git a/crates/ra_analysis/src/loc2id.rs b/crates/ra_analysis/src/loc2id.rs index 204708942..2aa141130 100644 --- a/crates/ra_analysis/src/loc2id.rs +++ b/crates/ra_analysis/src/loc2id.rs @@ -1,9 +1,6 @@ use parking_lot::Mutex; -use std::{ - hash::Hash, - sync::Arc, -}; +use std::hash::Hash; use rustc_hash::FxHashMap; @@ -23,19 +20,19 @@ use crate::{ /// bidirectional mapping between positional and numeric ids, we can use compact /// representation wich still allows us to get the actual item #[derive(Debug)] -pub(crate) struct Loc2IdMap +struct Loc2IdMap where ID: NumericId, - L: Clone + Eq + Hash, + LOC: Clone + Eq + Hash, { - loc2id: FxHashMap, - id2loc: FxHashMap, + loc2id: FxHashMap, + id2loc: FxHashMap, } -impl Default for Loc2IdMap +impl Default for Loc2IdMap where ID: NumericId, - L: Clone + Eq + Hash, + LOC: Clone + Eq + Hash, { fn default() -> Self { Loc2IdMap { @@ -45,12 +42,12 @@ where } } -impl Loc2IdMap +impl Loc2IdMap where ID: NumericId, - L: Clone + Eq + Hash, + LOC: Clone + Eq + Hash, { - pub fn loc2id(&mut self, loc: &L) -> ID { + pub fn loc2id(&mut self, loc: &LOC) -> ID { match self.loc2id.get(loc) { Some(id) => return id.clone(), None => (), @@ -63,7 +60,7 @@ where id } - pub fn id2loc(&self, id: ID) -> L { + pub fn id2loc(&self, id: ID) -> LOC { self.id2loc[&id].clone() } } @@ -90,6 +87,18 @@ macro_rules! impl_numeric_id { pub(crate) struct FnId(u32); impl_numeric_id!(FnId); +impl FnId { + pub(crate) fn from_loc( + db: &impl AsRef>, + loc: &SourceItemId, + ) -> FnId { + db.as_ref().loc2id(loc) + } + pub(crate) fn loc(self, db: &impl AsRef>) -> SourceItemId { + db.as_ref().id2loc(self) + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub(crate) struct DefId(u32); impl_numeric_id!(DefId); @@ -105,29 +114,54 @@ pub(crate) enum DefLoc { }, } -#[derive(Debug, Default, Clone)] -pub(crate) struct IdMaps { - inner: Arc, +impl DefId { + pub(crate) fn loc(self, db: &impl AsRef>) -> DefLoc { + db.as_ref().id2loc(self) + } } -impl IdMaps { - pub(crate) fn fn_id(&self, item_id: SourceItemId) -> FnId { - self.inner.fns.lock().loc2id(&item_id) - } - pub(crate) fn fn_item_id(&self, fn_id: FnId) -> SourceItemId { - self.inner.fns.lock().id2loc(fn_id) +impl DefLoc { + pub(crate) fn id(&self, db: &impl AsRef>) -> DefId { + db.as_ref().loc2id(&self) } +} - pub(crate) fn def_id(&self, loc: DefLoc) -> DefId { - self.inner.defs.lock().loc2id(&loc) - } - pub(crate) fn def_loc(&self, def_id: DefId) -> DefLoc { - self.inner.defs.lock().id2loc(def_id) +#[derive(Debug, Default)] +pub(crate) struct IdMaps { + pub(crate) fns: LocationIntener, + pub(crate) defs: LocationIntener, +} + +#[derive(Debug)] +pub(crate) struct LocationIntener +where + ID: NumericId, + LOC: Clone + Eq + Hash, +{ + map: Mutex>, +} + +impl Default for LocationIntener +where + ID: NumericId, + LOC: Clone + Eq + Hash, +{ + fn default() -> Self { + LocationIntener { + map: Default::default(), + } } } -#[derive(Debug, Default)] -struct IdMapsInner { - fns: Mutex>, - defs: Mutex>, +impl LocationIntener +where + ID: NumericId, + LOC: Clone + Eq + Hash, +{ + fn loc2id(&self, loc: &LOC) -> ID { + self.map.lock().loc2id(loc) + } + fn id2loc(&self, id: ID) -> LOC { + self.map.lock().id2loc(id) + } } -- cgit v1.2.3 From 11168c464cd962af3336a2cc68295496066edd6c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 03:25:20 +0300 Subject: move db basics to ra_db This should allow to move hir to a separate crate --- crates/ra_analysis/Cargo.toml | 1 + crates/ra_analysis/src/completion/mod.rs | 3 +- crates/ra_analysis/src/db.rs | 69 ++++------------ crates/ra_analysis/src/hir/db.rs | 5 +- crates/ra_analysis/src/hir/function/scope.rs | 3 +- crates/ra_analysis/src/hir/module/imp.rs | 4 +- crates/ra_analysis/src/hir/module/mod.rs | 2 +- crates/ra_analysis/src/hir/module/nameres.rs | 5 +- crates/ra_analysis/src/hir/query_definitions.rs | 2 +- crates/ra_analysis/src/imp.rs | 101 ++++------------------- crates/ra_analysis/src/input.rs | 75 ----------------- crates/ra_analysis/src/lib.rs | 22 ++--- crates/ra_analysis/src/loc2id.rs | 102 +----------------------- crates/ra_analysis/src/symbol_index.rs | 3 +- crates/ra_analysis/src/syntax_ptr.rs | 48 ----------- crates/ra_analysis/tests/tests.rs | 2 +- crates/ra_db/Cargo.toml | 16 ++++ crates/ra_db/src/file_resolver.rs | 76 ++++++++++++++++++ crates/ra_db/src/input.rs | 73 +++++++++++++++++ crates/ra_db/src/lib.rs | 69 ++++++++++++++++ crates/ra_db/src/loc2id.rs | 100 +++++++++++++++++++++++ crates/ra_db/src/syntax_ptr.rs | 48 +++++++++++ crates/ra_lsp_server/src/server_world.rs | 2 +- 23 files changed, 437 insertions(+), 394 deletions(-) delete mode 100644 crates/ra_analysis/src/input.rs delete mode 100644 crates/ra_analysis/src/syntax_ptr.rs create mode 100644 crates/ra_db/Cargo.toml create mode 100644 crates/ra_db/src/file_resolver.rs create mode 100644 crates/ra_db/src/input.rs create mode 100644 crates/ra_db/src/lib.rs create mode 100644 crates/ra_db/src/loc2id.rs create mode 100644 crates/ra_db/src/syntax_ptr.rs (limited to 'crates') diff --git a/crates/ra_analysis/Cargo.toml b/crates/ra_analysis/Cargo.toml index 5dae45857..48d8e56e3 100644 --- a/crates/ra_analysis/Cargo.toml +++ b/crates/ra_analysis/Cargo.toml @@ -15,4 +15,5 @@ parking_lot = "0.6.4" id-arena = { git = "https://github.com/fitzgen/id-arena/", rev = "43ecd67" } ra_syntax = { path = "../ra_syntax" } ra_editor = { path = "../ra_editor" } +ra_db = { path = "../ra_db" } test_utils = { path = "../test_utils" } diff --git a/crates/ra_analysis/src/completion/mod.rs b/crates/ra_analysis/src/completion/mod.rs index 844dabb19..538b51633 100644 --- a/crates/ra_analysis/src/completion/mod.rs +++ b/crates/ra_analysis/src/completion/mod.rs @@ -7,10 +7,11 @@ use ra_syntax::{ AstNode, AtomEdit, SyntaxNodeRef, }; +use ra_db::SyntaxDatabase; use rustc_hash::{FxHashMap}; use crate::{ - db::{self, SyntaxDatabase}, + db, hir, Cancelable, FilePosition }; diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index e0b7afac5..1b2dd4b3d 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -1,15 +1,13 @@ use std::sync::Arc; #[cfg(test)] use parking_lot::Mutex; -use ra_editor::LineIndex; -use ra_syntax::{SourceFileNode}; use salsa::{self, Database}; +use ra_db::{LocationIntener, BaseDatabase}; use crate::{ hir, symbol_index, - loc2id::{IdMaps, LocationIntener, DefId, DefLoc, FnId}, - Cancelable, Canceled, FileId, + loc2id::{IdMaps, DefId, DefLoc, FnId}, }; #[derive(Debug)] @@ -47,11 +45,11 @@ impl Default for RootDatabase { runtime: salsa::Runtime::default(), id_maps: Default::default(), }; - db.query_mut(crate::input::SourceRootQuery) - .set(crate::input::WORKSPACE, Default::default()); - db.query_mut(crate::input::CrateGraphQuery) + db.query_mut(ra_db::SourceRootQuery) + .set(ra_db::WORKSPACE, Default::default()); + db.query_mut(ra_db::CrateGraphQuery) .set((), Default::default()); - db.query_mut(crate::input::LibrariesQuery) + db.query_mut(ra_db::LibrariesQuery) .set((), Default::default()); db } @@ -67,22 +65,7 @@ impl salsa::ParallelDatabase for RootDatabase { } } -pub(crate) trait BaseDatabase: salsa::Database { - fn id_maps(&self) -> &IdMaps; - fn check_canceled(&self) -> Cancelable<()> { - if self.salsa_runtime().is_current_revision_canceled() { - Err(Canceled) - } else { - Ok(()) - } - } -} - -impl BaseDatabase for RootDatabase { - fn id_maps(&self) -> &IdMaps { - &self.id_maps - } -} +impl BaseDatabase for RootDatabase {} impl AsRef> for RootDatabase { fn as_ref(&self) -> &LocationIntener { @@ -121,16 +104,16 @@ impl RootDatabase { salsa::database_storage! { pub(crate) struct RootDatabaseStorage for RootDatabase { - impl crate::input::FilesDatabase { - fn file_text() for crate::input::FileTextQuery; - fn file_source_root() for crate::input::FileSourceRootQuery; - fn source_root() for crate::input::SourceRootQuery; - fn libraries() for crate::input::LibrariesQuery; - fn crate_graph() for crate::input::CrateGraphQuery; + impl ra_db::FilesDatabase { + fn file_text() for ra_db::FileTextQuery; + fn file_source_root() for ra_db::FileSourceRootQuery; + fn source_root() for ra_db::SourceRootQuery; + fn libraries() for ra_db::LibrariesQuery; + fn crate_graph() for ra_db::CrateGraphQuery; } - impl SyntaxDatabase { - fn source_file() for SourceFileQuery; - fn file_lines() for FileLinesQuery; + impl ra_db::SyntaxDatabase { + fn source_file() for ra_db::SourceFileQuery; + fn file_lines() for ra_db::FileLinesQuery; } impl symbol_index::SymbolsDatabase { fn file_symbols() for symbol_index::FileSymbolsQuery; @@ -148,23 +131,3 @@ salsa::database_storage! { } } } - -salsa::query_group! { - pub(crate) trait SyntaxDatabase: crate::input::FilesDatabase + BaseDatabase { - fn source_file(file_id: FileId) -> SourceFileNode { - type SourceFileQuery; - } - fn file_lines(file_id: FileId) -> Arc { - type FileLinesQuery; - } - } -} - -fn source_file(db: &impl SyntaxDatabase, file_id: FileId) -> SourceFileNode { - let text = db.file_text(file_id); - SourceFileNode::parse(&*text) -} -fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc { - let text = db.file_text(file_id); - Arc::new(LineIndex::new(&*text)) -} diff --git a/crates/ra_analysis/src/hir/db.rs b/crates/ra_analysis/src/hir/db.rs index bf0dc393a..0ae2086ff 100644 --- a/crates/ra_analysis/src/hir/db.rs +++ b/crates/ra_analysis/src/hir/db.rs @@ -4,10 +4,10 @@ use ra_syntax::{ SyntaxNode, ast::FnDefNode, }; +use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase}; use crate::{ FileId, - db::SyntaxDatabase, hir::{ SourceFileItems, SourceItemId, query_definitions, @@ -15,8 +15,7 @@ use crate::{ module::{ModuleId, ModuleTree, ModuleSource, nameres::{ItemMap, InputModuleItems}}, }, - input::SourceRootId, - loc2id::{DefLoc, DefId, FnId, LocationIntener}, + loc2id::{DefLoc, DefId, FnId}, Cancelable, }; diff --git a/crates/ra_analysis/src/hir/function/scope.rs b/crates/ra_analysis/src/hir/function/scope.rs index 76b2fea68..ed789fede 100644 --- a/crates/ra_analysis/src/hir/function/scope.rs +++ b/crates/ra_analysis/src/hir/function/scope.rs @@ -5,9 +5,10 @@ use ra_syntax::{ algo::generate, ast::{self, ArgListOwner, LoopBodyOwner, NameOwner}, }; +use ra_db::LocalSyntaxPtr; use crate::{ - syntax_ptr::LocalSyntaxPtr, + arena::{Arena, Id}, }; diff --git a/crates/ra_analysis/src/hir/module/imp.rs b/crates/ra_analysis/src/hir/module/imp.rs index d51ca2d59..c8f7ed58d 100644 --- a/crates/ra_analysis/src/hir/module/imp.rs +++ b/crates/ra_analysis/src/hir/module/imp.rs @@ -6,11 +6,11 @@ use ra_syntax::{ }; use relative_path::RelativePathBuf; use rustc_hash::{FxHashMap, FxHashSet}; +use ra_db::{SourceRoot, SourceRootId, FileResolverImp}; use crate::{ hir::HirDatabase, - input::{SourceRoot, SourceRootId}, - Cancelable, FileId, FileResolverImp, + Cancelable, FileId, }; use super::{ diff --git a/crates/ra_analysis/src/hir/module/mod.rs b/crates/ra_analysis/src/hir/module/mod.rs index 893ec3a10..683cb5d4c 100644 --- a/crates/ra_analysis/src/hir/module/mod.rs +++ b/crates/ra_analysis/src/hir/module/mod.rs @@ -10,12 +10,12 @@ use ra_syntax::{ ast::{self, AstNode, NameOwner}, SmolStr, SyntaxNode, }; +use ra_db::SourceRootId; use relative_path::RelativePathBuf; use crate::{ FileId, FilePosition, Cancelable, hir::{Path, PathKind, HirDatabase, SourceItemId}, - input::SourceRootId, arena::{Arena, Id}, loc2id::{DefLoc, DefId}, }; diff --git a/crates/ra_analysis/src/hir/module/nameres.rs b/crates/ra_analysis/src/hir/module/nameres.rs index f48f51c8d..5c87e7af2 100644 --- a/crates/ra_analysis/src/hir/module/nameres.rs +++ b/crates/ra_analysis/src/hir/module/nameres.rs @@ -19,12 +19,12 @@ use std::{ }; use rustc_hash::FxHashMap; - use ra_syntax::{ TextRange, SmolStr, SyntaxKind::{self, *}, ast::{self, AstNode} }; +use ra_db::SourceRootId; use crate::{ Cancelable, FileId, @@ -35,7 +35,6 @@ use crate::{ HirDatabase, module::{ModuleId, ModuleTree}, }, - input::SourceRootId, }; /// Item map is the result of the name resolution. Item map contains, for each @@ -342,11 +341,11 @@ where #[cfg(test)] mod tests { + use ra_db::FilesDatabase; use crate::{ AnalysisChange, mock_analysis::{MockAnalysis, analysis_and_position}, hir::{self, HirDatabase}, - input::FilesDatabase, }; use super::*; diff --git a/crates/ra_analysis/src/hir/query_definitions.rs b/crates/ra_analysis/src/hir/query_definitions.rs index 6570ca994..fbdf8eb67 100644 --- a/crates/ra_analysis/src/hir/query_definitions.rs +++ b/crates/ra_analysis/src/hir/query_definitions.rs @@ -8,6 +8,7 @@ use ra_syntax::{ AstNode, SyntaxNode, SmolStr, ast::{self, FnDef, FnDefNode, NameOwner, ModuleItemOwner} }; +use ra_db::SourceRootId; use crate::{ FileId, Cancelable, @@ -21,7 +22,6 @@ use crate::{ nameres::{InputModuleItems, ItemMap, Resolver}, }, }, - input::SourceRootId, }; /// Resolve `FnId` to the corresponding `SyntaxNode` diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index c86bc111a..9a8694221 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -1,6 +1,5 @@ use std::{ fmt, - hash::{Hash, Hasher}, sync::Arc, }; @@ -11,84 +10,24 @@ use ra_syntax::{ SyntaxKind::*, SyntaxNodeRef, TextRange, TextUnit, }; +use ra_db::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE, SyntaxDatabase, SourceFileQuery}; use rayon::prelude::*; -use relative_path::RelativePath; use rustc_hash::FxHashSet; use salsa::{Database, ParallelDatabase}; use crate::{ completion::{completions, CompletionItem}, - db::{self, SourceFileQuery, SyntaxDatabase}, + db, hir::{ self, FnSignatureInfo, Problem, }, - input::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE}, symbol_index::{SymbolIndex, SymbolsDatabase}, - AnalysisChange, Cancelable, CrateGraph, CrateId, Diagnostic, FileId, FileResolver, + AnalysisChange, Cancelable, CrateId, Diagnostic, FileId, FileSystemEdit, FilePosition, Query, SourceChange, SourceFileNodeEdit, }; -#[derive(Clone, Debug)] -pub(crate) struct FileResolverImp { - inner: Arc, -} - -impl PartialEq for FileResolverImp { - fn eq(&self, other: &FileResolverImp) -> bool { - self.inner() == other.inner() - } -} - -impl Eq for FileResolverImp {} - -impl Hash for FileResolverImp { - fn hash(&self, hasher: &mut H) { - self.inner().hash(hasher); - } -} - -impl FileResolverImp { - pub(crate) fn new(inner: Arc) -> FileResolverImp { - FileResolverImp { inner } - } - pub(crate) fn file_stem(&self, file_id: FileId) -> String { - self.inner.file_stem(file_id) - } - pub(crate) fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option { - self.inner.resolve(file_id, path) - } - pub(crate) fn debug_path(&self, file_id: FileId) -> Option { - self.inner.debug_path(file_id) - } - fn inner(&self) -> *const FileResolver { - &*self.inner - } -} - -impl Default for FileResolverImp { - fn default() -> FileResolverImp { - #[derive(Debug)] - struct DummyResolver; - impl FileResolver for DummyResolver { - fn file_stem(&self, _file_: FileId) -> String { - panic!("file resolver not set") - } - fn resolve( - &self, - _file_id: FileId, - _path: &::relative_path::RelativePath, - ) -> Option { - panic!("file resolver not set") - } - } - FileResolverImp { - inner: Arc::new(DummyResolver), - } - } -} - #[derive(Debug, Default)] pub(crate) struct AnalysisHostImpl { db: db::RootDatabase, @@ -105,7 +44,7 @@ impl AnalysisHostImpl { for (file_id, text) in change.files_changed { self.db - .query_mut(crate::input::FileTextQuery) + .query_mut(ra_db::FileTextQuery) .set(file_id, Arc::new(text)) } if !(change.files_added.is_empty() && change.files_removed.is_empty()) { @@ -115,22 +54,22 @@ impl AnalysisHostImpl { let mut source_root = SourceRoot::clone(&self.db.source_root(WORKSPACE)); for (file_id, text) in change.files_added { self.db - .query_mut(crate::input::FileTextQuery) + .query_mut(ra_db::FileTextQuery) .set(file_id, Arc::new(text)); self.db - .query_mut(crate::input::FileSourceRootQuery) - .set(file_id, crate::input::WORKSPACE); + .query_mut(ra_db::FileSourceRootQuery) + .set(file_id, ra_db::WORKSPACE); source_root.files.insert(file_id); } for file_id in change.files_removed { self.db - .query_mut(crate::input::FileTextQuery) + .query_mut(ra_db::FileTextQuery) .set(file_id, Arc::new(String::new())); source_root.files.remove(&file_id); } source_root.file_resolver = file_resolver; self.db - .query_mut(crate::input::SourceRootQuery) + .query_mut(ra_db::SourceRootQuery) .set(WORKSPACE, Arc::new(source_root)) } if !change.libraries_added.is_empty() { @@ -147,10 +86,10 @@ impl AnalysisHostImpl { library.file_resolver.debug_path(file_id) ); self.db - .query_mut(crate::input::FileSourceRootQuery) + .query_mut(ra_db::FileSourceRootQuery) .set_constant(file_id, source_root_id); self.db - .query_mut(crate::input::FileTextQuery) + .query_mut(ra_db::FileTextQuery) .set_constant(file_id, Arc::new(text)); } let source_root = SourceRoot { @@ -158,19 +97,19 @@ impl AnalysisHostImpl { file_resolver: library.file_resolver, }; self.db - .query_mut(crate::input::SourceRootQuery) + .query_mut(ra_db::SourceRootQuery) .set(source_root_id, Arc::new(source_root)); self.db .query_mut(crate::symbol_index::LibrarySymbolsQuery) .set(source_root_id, Arc::new(library.symbol_index)); } self.db - .query_mut(crate::input::LibrariesQuery) + .query_mut(ra_db::LibrariesQuery) .set((), Arc::new(libraries)); } if let Some(crate_graph) = change.crate_graph { self.db - .query_mut(crate::input::CrateGraphQuery) + .query_mut(ra_db::CrateGraphQuery) .set((), Arc::new(crate_graph)) } } @@ -261,7 +200,7 @@ impl AnalysisImpl { Ok(crate_id.into_iter().collect()) } pub fn crate_root(&self, crate_id: CrateId) -> FileId { - self.db.crate_graph().crate_roots[&crate_id] + self.db.crate_graph().crate_root(crate_id) } pub fn completions(&self, position: FilePosition) -> Cancelable>> { completions(&self.db, position) @@ -546,16 +485,6 @@ impl SourceChange { } } -impl CrateGraph { - fn crate_id_for_crate_root(&self, file_id: FileId) -> Option { - let (&crate_id, _) = self - .crate_roots - .iter() - .find(|(_crate_id, &root_id)| root_id == file_id)?; - Some(crate_id) - } -} - enum FnCallNode<'a> { CallExpr(ast::CallExpr<'a>), MethodCallExpr(ast::MethodCallExpr<'a>), diff --git a/crates/ra_analysis/src/input.rs b/crates/ra_analysis/src/input.rs deleted file mode 100644 index e601cd58a..000000000 --- a/crates/ra_analysis/src/input.rs +++ /dev/null @@ -1,75 +0,0 @@ -use std::{fmt, sync::Arc}; - -use relative_path::RelativePath; -use rustc_hash::FxHashMap; -use rustc_hash::FxHashSet; -use salsa; - -use crate::FileResolverImp; - -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct FileId(pub u32); - -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct CrateId(pub u32); - -#[derive(Debug, Clone, Default, PartialEq, Eq)] -pub struct CrateGraph { - pub(crate) crate_roots: FxHashMap, -} - -impl CrateGraph { - pub fn new() -> CrateGraph { - CrateGraph::default() - } - pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId { - let crate_id = CrateId(self.crate_roots.len() as u32); - let prev = self.crate_roots.insert(crate_id, file_id); - assert!(prev.is_none()); - crate_id - } -} - -pub trait FileResolver: fmt::Debug + Send + Sync + 'static { - fn file_stem(&self, file_id: FileId) -> String; - fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option; - fn debug_path(&self, _1file_id: FileId) -> Option { - None - } -} - -salsa::query_group! { - pub(crate) trait FilesDatabase: salsa::Database { - fn file_text(file_id: FileId) -> Arc { - type FileTextQuery; - storage input; - } - fn file_source_root(file_id: FileId) -> SourceRootId { - type FileSourceRootQuery; - storage input; - } - fn source_root(id: SourceRootId) -> Arc { - type SourceRootQuery; - storage input; - } - fn libraries() -> Arc> { - type LibrariesQuery; - storage input; - } - fn crate_graph() -> Arc { - type CrateGraphQuery; - storage input; - } - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub(crate) struct SourceRootId(pub(crate) u32); - -#[derive(Default, Clone, Debug, PartialEq, Eq)] -pub(crate) struct SourceRoot { - pub(crate) file_resolver: FileResolverImp, - pub(crate) files: FxHashSet, -} - -pub(crate) const WORKSPACE: SourceRootId = SourceRootId(0); diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index c0e43544e..012d36b8e 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -19,8 +19,6 @@ macro_rules! ctry { } mod arena; -mod syntax_ptr; -mod input; mod db; mod loc2id; mod imp; @@ -32,35 +30,27 @@ pub mod mock_analysis; use std::{fmt, sync::Arc}; use ra_syntax::{AtomEdit, SourceFileNode, TextRange, TextUnit}; +use ra_db::FileResolverImp; use rayon::prelude::*; use relative_path::RelativePathBuf; use crate::{ - imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp}, + imp::{AnalysisHostImpl, AnalysisImpl}, symbol_index::SymbolIndex, }; pub use crate::{ completion::CompletionItem, hir::FnSignatureInfo, - input::{CrateGraph, CrateId, FileId, FileResolver}, }; pub use ra_editor::{ FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode, }; -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub struct Canceled; - -pub type Cancelable = Result; - -impl std::fmt::Display for Canceled { - fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - fmt.write_str("Canceled") - } -} - -impl std::error::Error for Canceled {} +pub use ra_db::{ + Canceled, Cancelable, + CrateGraph, CrateId, FileId, FileResolver +}; #[derive(Default)] pub struct AnalysisChange { diff --git a/crates/ra_analysis/src/loc2id.rs b/crates/ra_analysis/src/loc2id.rs index 2aa141130..7956431ab 100644 --- a/crates/ra_analysis/src/loc2id.rs +++ b/crates/ra_analysis/src/loc2id.rs @@ -1,74 +1,10 @@ -use parking_lot::Mutex; - -use std::hash::Hash; - -use rustc_hash::FxHashMap; +use ra_db::SourceRootId; use crate::{ hir::{SourceItemId, ModuleId}, - input::SourceRootId, }; -/// There are two principle ways to refer to things: -/// - by their locatinon (module in foo/bar/baz.rs at line 42) -/// - by their numeric id (module `ModuleId(42)`) -/// -/// The first one is more powerful (you can actually find the thing in question -/// by id), but the second one is so much more compact. -/// -/// `Loc2IdMap` allows us to have a cake an eat it as well: by maintaining a -/// bidirectional mapping between positional and numeric ids, we can use compact -/// representation wich still allows us to get the actual item -#[derive(Debug)] -struct Loc2IdMap -where - ID: NumericId, - LOC: Clone + Eq + Hash, -{ - loc2id: FxHashMap, - id2loc: FxHashMap, -} - -impl Default for Loc2IdMap -where - ID: NumericId, - LOC: Clone + Eq + Hash, -{ - fn default() -> Self { - Loc2IdMap { - loc2id: FxHashMap::default(), - id2loc: FxHashMap::default(), - } - } -} - -impl Loc2IdMap -where - ID: NumericId, - LOC: Clone + Eq + Hash, -{ - pub fn loc2id(&mut self, loc: &LOC) -> ID { - match self.loc2id.get(loc) { - Some(id) => return id.clone(), - None => (), - } - let id = self.loc2id.len(); - assert!(id < u32::max_value() as usize); - let id = ID::from_u32(id as u32); - self.loc2id.insert(loc.clone(), id.clone()); - self.id2loc.insert(id.clone(), loc.clone()); - id - } - - pub fn id2loc(&self, id: ID) -> LOC { - self.id2loc[&id].clone() - } -} - -pub(crate) trait NumericId: Clone + Eq + Hash { - fn from_u32(id: u32) -> Self; - fn to_u32(self) -> u32; -} +use ra_db::{NumericId, LocationIntener}; macro_rules! impl_numeric_id { ($id:ident) => { @@ -131,37 +67,3 @@ pub(crate) struct IdMaps { pub(crate) fns: LocationIntener, pub(crate) defs: LocationIntener, } - -#[derive(Debug)] -pub(crate) struct LocationIntener -where - ID: NumericId, - LOC: Clone + Eq + Hash, -{ - map: Mutex>, -} - -impl Default for LocationIntener -where - ID: NumericId, - LOC: Clone + Eq + Hash, -{ - fn default() -> Self { - LocationIntener { - map: Default::default(), - } - } -} - -impl LocationIntener -where - ID: NumericId, - LOC: Clone + Eq + Hash, -{ - fn loc2id(&self, loc: &LOC) -> ID { - self.map.lock().loc2id(loc) - } - fn id2loc(&self, id: ID) -> LOC { - self.map.lock().id2loc(id) - } -} diff --git a/crates/ra_analysis/src/symbol_index.rs b/crates/ra_analysis/src/symbol_index.rs index 747b34e38..b48a37229 100644 --- a/crates/ra_analysis/src/symbol_index.rs +++ b/crates/ra_analysis/src/symbol_index.rs @@ -9,13 +9,12 @@ use ra_syntax::{ SourceFileNode, SyntaxKind::{self, *}, }; +use ra_db::{SyntaxDatabase, SourceRootId}; use rayon::prelude::*; use crate::{ Cancelable, FileId, Query, - db::SyntaxDatabase, - input::SourceRootId, }; salsa::query_group! { diff --git a/crates/ra_analysis/src/syntax_ptr.rs b/crates/ra_analysis/src/syntax_ptr.rs deleted file mode 100644 index f4b05fc19..000000000 --- a/crates/ra_analysis/src/syntax_ptr.rs +++ /dev/null @@ -1,48 +0,0 @@ -use ra_syntax::{SourceFileNode, SyntaxKind, SyntaxNode, SyntaxNodeRef, TextRange}; - -/// A pionter to a syntax node inside a file. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(crate) struct LocalSyntaxPtr { - range: TextRange, - kind: SyntaxKind, -} - -impl LocalSyntaxPtr { - pub(crate) fn new(node: SyntaxNodeRef) -> LocalSyntaxPtr { - LocalSyntaxPtr { - range: node.range(), - kind: node.kind(), - } - } - - pub(crate) fn resolve(self, file: &SourceFileNode) -> SyntaxNode { - let mut curr = file.syntax(); - loop { - if curr.range() == self.range && curr.kind() == self.kind { - return curr.owned(); - } - curr = curr - .children() - .find(|it| self.range.is_subrange(&it.range())) - .unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self)) - } - } - - pub(crate) fn range(self) -> TextRange { - self.range - } -} - -#[test] -fn test_local_syntax_ptr() { - use ra_syntax::{ast, AstNode}; - let file = SourceFileNode::parse("struct Foo { f: u32, }"); - let field = file - .syntax() - .descendants() - .find_map(ast::NamedFieldDef::cast) - .unwrap(); - let ptr = LocalSyntaxPtr::new(field.syntax()); - let field_syntax = ptr.resolve(&file); - assert_eq!(field.syntax(), field_syntax); -} diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs index 8e7856027..fbe89f444 100644 --- a/crates/ra_analysis/tests/tests.rs +++ b/crates/ra_analysis/tests/tests.rs @@ -126,7 +126,7 @@ fn test_resolve_crate_root() { let mut host = mock.analysis_host(); assert!(host.analysis().crate_for(mod_file).unwrap().is_empty()); - let mut crate_graph = CrateGraph::new(); + let mut crate_graph = CrateGraph::default(); let crate_id = crate_graph.add_crate_root(root_file); let mut change = AnalysisChange::new(); change.set_crate_graph(crate_graph); diff --git a/crates/ra_db/Cargo.toml b/crates/ra_db/Cargo.toml new file mode 100644 index 000000000..3bf2f635e --- /dev/null +++ b/crates/ra_db/Cargo.toml @@ -0,0 +1,16 @@ +[package] +edition = "2018" +name = "ra_db" +version = "0.1.0" +authors = ["Aleksey Kladov "] + +[dependencies] +log = "0.4.5" +relative-path = "0.4.0" +salsa = "0.8.0" +rustc-hash = "1.0" +parking_lot = "0.6.4" +id-arena = { git = "https://github.com/fitzgen/id-arena/", rev = "43ecd67" } +ra_syntax = { path = "../ra_syntax" } +ra_editor = { path = "../ra_editor" } +test_utils = { path = "../test_utils" } diff --git a/crates/ra_db/src/file_resolver.rs b/crates/ra_db/src/file_resolver.rs new file mode 100644 index 000000000..f849ac752 --- /dev/null +++ b/crates/ra_db/src/file_resolver.rs @@ -0,0 +1,76 @@ +use std::{ + sync::Arc, + hash::{Hash, Hasher}, + fmt, +}; + +use relative_path::RelativePath; + +use crate::input::FileId; + +pub trait FileResolver: fmt::Debug + Send + Sync + 'static { + fn file_stem(&self, file_id: FileId) -> String; + fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option; + fn debug_path(&self, _1file_id: FileId) -> Option { + None + } +} + +#[derive(Clone, Debug)] +pub struct FileResolverImp { + inner: Arc, +} + +impl PartialEq for FileResolverImp { + fn eq(&self, other: &FileResolverImp) -> bool { + self.inner() == other.inner() + } +} + +impl Eq for FileResolverImp {} + +impl Hash for FileResolverImp { + fn hash(&self, hasher: &mut H) { + self.inner().hash(hasher); + } +} + +impl FileResolverImp { + pub fn new(inner: Arc) -> FileResolverImp { + FileResolverImp { inner } + } + pub fn file_stem(&self, file_id: FileId) -> String { + self.inner.file_stem(file_id) + } + pub fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option { + self.inner.resolve(file_id, path) + } + pub fn debug_path(&self, file_id: FileId) -> Option { + self.inner.debug_path(file_id) + } + fn inner(&self) -> *const FileResolver { + &*self.inner + } +} + +impl Default for FileResolverImp { + fn default() -> FileResolverImp { + #[derive(Debug)] + struct DummyResolver; + impl FileResolver for DummyResolver { + fn file_stem(&self, _file_: FileId) -> String { + panic!("file resolver not set") + } + fn resolve( + &self, + _file_id: FileId, + _path: &::relative_path::RelativePath, + ) -> Option { + panic!("file resolver not set") + } + } + FileResolverImp { + inner: Arc::new(DummyResolver), + } + } +} diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs new file mode 100644 index 000000000..9101ac7a8 --- /dev/null +++ b/crates/ra_db/src/input.rs @@ -0,0 +1,73 @@ +use std::sync::Arc; + +use rustc_hash::FxHashMap; +use rustc_hash::FxHashSet; +use salsa; + +use crate::file_resolver::FileResolverImp; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct FileId(pub u32); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct CrateId(pub u32); + +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct CrateGraph { + pub(crate) crate_roots: FxHashMap, +} + +impl CrateGraph { + pub fn crate_root(&self, crate_id: CrateId) -> FileId { + self.crate_roots[&crate_id] + } + pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId { + let crate_id = CrateId(self.crate_roots.len() as u32); + let prev = self.crate_roots.insert(crate_id, file_id); + assert!(prev.is_none()); + crate_id + } + pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option { + let (&crate_id, _) = self + .crate_roots + .iter() + .find(|(_crate_id, &root_id)| root_id == file_id)?; + Some(crate_id) + } +} + +salsa::query_group! { + pub trait FilesDatabase: salsa::Database { + fn file_text(file_id: FileId) -> Arc { + type FileTextQuery; + storage input; + } + fn file_source_root(file_id: FileId) -> SourceRootId { + type FileSourceRootQuery; + storage input; + } + fn source_root(id: SourceRootId) -> Arc { + type SourceRootQuery; + storage input; + } + fn libraries() -> Arc> { + type LibrariesQuery; + storage input; + } + fn crate_graph() -> Arc { + type CrateGraphQuery; + storage input; + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct SourceRootId(pub u32); + +#[derive(Default, Clone, Debug, PartialEq, Eq)] +pub struct SourceRoot { + pub file_resolver: FileResolverImp, + pub files: FxHashSet, +} + +pub const WORKSPACE: SourceRootId = SourceRootId(0); diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs new file mode 100644 index 000000000..833f95eeb --- /dev/null +++ b/crates/ra_db/src/lib.rs @@ -0,0 +1,69 @@ +//! ra_db defines basic database traits. Concrete DB is defined by ra_analysis. + +extern crate ra_editor; +extern crate ra_syntax; +extern crate relative_path; +extern crate rustc_hash; +extern crate salsa; + +mod syntax_ptr; +mod file_resolver; +mod input; +mod loc2id; + +use std::sync::Arc; +use ra_editor::LineIndex; +use ra_syntax::SourceFileNode; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct Canceled; + +pub type Cancelable = Result; + +impl std::fmt::Display for Canceled { + fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fmt.write_str("Canceled") + } +} + +impl std::error::Error for Canceled {} + +pub use crate::{ + syntax_ptr::LocalSyntaxPtr, + file_resolver::{FileResolver, FileResolverImp}, + input::{ + FilesDatabase, FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, WORKSPACE, + FileTextQuery, FileSourceRootQuery, SourceRootQuery, LibrariesQuery, CrateGraphQuery, + }, + loc2id::{LocationIntener, NumericId}, +}; + +pub trait BaseDatabase: salsa::Database { + fn check_canceled(&self) -> Cancelable<()> { + if self.salsa_runtime().is_current_revision_canceled() { + Err(Canceled) + } else { + Ok(()) + } + } +} + +salsa::query_group! { + pub trait SyntaxDatabase: crate::input::FilesDatabase + BaseDatabase { + fn source_file(file_id: FileId) -> SourceFileNode { + type SourceFileQuery; + } + fn file_lines(file_id: FileId) -> Arc { + type FileLinesQuery; + } + } +} + +fn source_file(db: &impl SyntaxDatabase, file_id: FileId) -> SourceFileNode { + let text = db.file_text(file_id); + SourceFileNode::parse(&*text) +} +fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc { + let text = db.file_text(file_id); + Arc::new(LineIndex::new(&*text)) +} diff --git a/crates/ra_db/src/loc2id.rs b/crates/ra_db/src/loc2id.rs new file mode 100644 index 000000000..69ba43d0f --- /dev/null +++ b/crates/ra_db/src/loc2id.rs @@ -0,0 +1,100 @@ +use parking_lot::Mutex; + +use std::hash::Hash; + +use rustc_hash::FxHashMap; + +/// There are two principle ways to refer to things: +/// - by their locatinon (module in foo/bar/baz.rs at line 42) +/// - by their numeric id (module `ModuleId(42)`) +/// +/// The first one is more powerful (you can actually find the thing in question +/// by id), but the second one is so much more compact. +/// +/// `Loc2IdMap` allows us to have a cake an eat it as well: by maintaining a +/// bidirectional mapping between positional and numeric ids, we can use compact +/// representation wich still allows us to get the actual item +#[derive(Debug)] +struct Loc2IdMap +where + ID: NumericId, + LOC: Clone + Eq + Hash, +{ + loc2id: FxHashMap, + id2loc: FxHashMap, +} + +impl Default for Loc2IdMap +where + ID: NumericId, + LOC: Clone + Eq + Hash, +{ + fn default() -> Self { + Loc2IdMap { + loc2id: FxHashMap::default(), + id2loc: FxHashMap::default(), + } + } +} + +impl Loc2IdMap +where + ID: NumericId, + LOC: Clone + Eq + Hash, +{ + pub fn loc2id(&mut self, loc: &LOC) -> ID { + match self.loc2id.get(loc) { + Some(id) => return id.clone(), + None => (), + } + let id = self.loc2id.len(); + assert!(id < u32::max_value() as usize); + let id = ID::from_u32(id as u32); + self.loc2id.insert(loc.clone(), id.clone()); + self.id2loc.insert(id.clone(), loc.clone()); + id + } + + pub fn id2loc(&self, id: ID) -> LOC { + self.id2loc[&id].clone() + } +} + +pub trait NumericId: Clone + Eq + Hash { + fn from_u32(id: u32) -> Self; + fn to_u32(self) -> u32; +} + +#[derive(Debug)] +pub struct LocationIntener +where + ID: NumericId, + LOC: Clone + Eq + Hash, +{ + map: Mutex>, +} + +impl Default for LocationIntener +where + ID: NumericId, + LOC: Clone + Eq + Hash, +{ + fn default() -> Self { + LocationIntener { + map: Default::default(), + } + } +} + +impl LocationIntener +where + ID: NumericId, + LOC: Clone + Eq + Hash, +{ + pub fn loc2id(&self, loc: &LOC) -> ID { + self.map.lock().loc2id(loc) + } + pub fn id2loc(&self, id: ID) -> LOC { + self.map.lock().id2loc(id) + } +} diff --git a/crates/ra_db/src/syntax_ptr.rs b/crates/ra_db/src/syntax_ptr.rs new file mode 100644 index 000000000..dac94dd36 --- /dev/null +++ b/crates/ra_db/src/syntax_ptr.rs @@ -0,0 +1,48 @@ +use ra_syntax::{SourceFileNode, SyntaxKind, SyntaxNode, SyntaxNodeRef, TextRange}; + +/// A pionter to a syntax node inside a file. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct LocalSyntaxPtr { + range: TextRange, + kind: SyntaxKind, +} + +impl LocalSyntaxPtr { + pub fn new(node: SyntaxNodeRef) -> LocalSyntaxPtr { + LocalSyntaxPtr { + range: node.range(), + kind: node.kind(), + } + } + + pub fn resolve(self, file: &SourceFileNode) -> SyntaxNode { + let mut curr = file.syntax(); + loop { + if curr.range() == self.range && curr.kind() == self.kind { + return curr.owned(); + } + curr = curr + .children() + .find(|it| self.range.is_subrange(&it.range())) + .unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self)) + } + } + + pub fn range(self) -> TextRange { + self.range + } +} + +#[test] +fn test_local_syntax_ptr() { + use ra_syntax::{ast, AstNode}; + let file = SourceFileNode::parse("struct Foo { f: u32, }"); + let field = file + .syntax() + .descendants() + .find_map(ast::NamedFieldDef::cast) + .unwrap(); + let ptr = LocalSyntaxPtr::new(field.syntax()); + let field_syntax = ptr.resolve(&file); + assert_eq!(field.syntax(), field_syntax); +} diff --git a/crates/ra_lsp_server/src/server_world.rs b/crates/ra_lsp_server/src/server_world.rs index 3e7670fcc..12faeb93a 100644 --- a/crates/ra_lsp_server/src/server_world.rs +++ b/crates/ra_lsp_server/src/server_world.rs @@ -140,7 +140,7 @@ impl ServerWorldState { Ok(file_id) } pub fn set_workspaces(&mut self, ws: Vec) { - let mut crate_graph = CrateGraph::new(); + let mut crate_graph = CrateGraph::default(); ws.iter() .flat_map(|ws| { ws.packages() -- cgit v1.2.3 From f66e5b6e6b6f7b2b899ef4207dfe46655d77334c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 03:31:50 +0300 Subject: move ids to HIR --- crates/ra_analysis/src/db.rs | 9 +++- crates/ra_analysis/src/hir/db.rs | 2 +- crates/ra_analysis/src/hir/function/mod.rs | 3 +- crates/ra_analysis/src/hir/mod.rs | 45 +++++++++++++++- crates/ra_analysis/src/hir/module/mod.rs | 3 +- crates/ra_analysis/src/hir/module/nameres.rs | 2 +- crates/ra_analysis/src/hir/query_definitions.rs | 3 +- crates/ra_analysis/src/lib.rs | 1 - crates/ra_analysis/src/loc2id.rs | 69 ------------------------- crates/ra_db/src/lib.rs | 14 +++++ 10 files changed, 71 insertions(+), 80 deletions(-) delete mode 100644 crates/ra_analysis/src/loc2id.rs (limited to 'crates') diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index 1b2dd4b3d..2bc1c8f8f 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -5,9 +5,8 @@ use salsa::{self, Database}; use ra_db::{LocationIntener, BaseDatabase}; use crate::{ - hir, + hir::{self, DefId, DefLoc, FnId, SourceItemId}, symbol_index, - loc2id::{IdMaps, DefId, DefLoc, FnId}, }; #[derive(Debug)] @@ -21,6 +20,12 @@ pub(crate) struct RootDatabase { id_maps: Arc, } +#[derive(Debug, Default)] +struct IdMaps { + fns: LocationIntener, + defs: LocationIntener, +} + impl salsa::Database for RootDatabase { fn salsa_runtime(&self) -> &salsa::Runtime { &self.runtime diff --git a/crates/ra_analysis/src/hir/db.rs b/crates/ra_analysis/src/hir/db.rs index 0ae2086ff..c8ae551c5 100644 --- a/crates/ra_analysis/src/hir/db.rs +++ b/crates/ra_analysis/src/hir/db.rs @@ -9,13 +9,13 @@ use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase}; use crate::{ FileId, hir::{ + DefLoc, DefId, FnId, SourceFileItems, SourceItemId, query_definitions, function::{FnScopes}, module::{ModuleId, ModuleTree, ModuleSource, nameres::{ItemMap, InputModuleItems}}, }, - loc2id::{DefLoc, DefId, FnId}, Cancelable, }; diff --git a/crates/ra_analysis/src/hir/function/mod.rs b/crates/ra_analysis/src/hir/function/mod.rs index e64a9f045..a399d2a9e 100644 --- a/crates/ra_analysis/src/hir/function/mod.rs +++ b/crates/ra_analysis/src/hir/function/mod.rs @@ -11,12 +11,11 @@ use ra_syntax::{ }; use crate::{ - hir::{HirDatabase, SourceItemId}, + hir::{FnId, HirDatabase, SourceItemId}, FileId, }; pub(crate) use self::scope::FnScopes; -pub(crate) use crate::loc2id::FnId; impl FnId { pub(crate) fn get(db: &impl HirDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId { diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs index 61e6c9913..83131384d 100644 --- a/crates/ra_analysis/src/hir/mod.rs +++ b/crates/ra_analysis/src/hir/mod.rs @@ -14,11 +14,11 @@ mod path; use std::ops::Index; use ra_syntax::{SyntaxNodeRef, SyntaxNode}; +use ra_db::{LocationIntener, SourceRootId}; use crate::{ FileId, hir::db::HirDatabase, - loc2id::{DefId, DefLoc}, Cancelable, arena::{Arena, Id}, }; @@ -31,6 +31,49 @@ pub(crate) use self::{ pub use self::function::FnSignatureInfo; +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub(crate) struct FnId(u32); +ra_db::impl_numeric_id!(FnId); + +impl FnId { + pub(crate) fn from_loc( + db: &impl AsRef>, + loc: &SourceItemId, + ) -> FnId { + db.as_ref().loc2id(loc) + } + pub(crate) fn loc(self, db: &impl AsRef>) -> SourceItemId { + db.as_ref().id2loc(self) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub(crate) struct DefId(u32); +ra_db::impl_numeric_id!(DefId); + +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub(crate) enum DefLoc { + Module { + id: ModuleId, + source_root: SourceRootId, + }, + Item { + source_item_id: SourceItemId, + }, +} + +impl DefId { + pub(crate) fn loc(self, db: &impl AsRef>) -> DefLoc { + db.as_ref().id2loc(self) + } +} + +impl DefLoc { + pub(crate) fn id(&self, db: &impl AsRef>) -> DefId { + db.as_ref().loc2id(&self) + } +} + pub(crate) enum Def { Module(Module), Item, diff --git a/crates/ra_analysis/src/hir/module/mod.rs b/crates/ra_analysis/src/hir/module/mod.rs index 683cb5d4c..d2096b01e 100644 --- a/crates/ra_analysis/src/hir/module/mod.rs +++ b/crates/ra_analysis/src/hir/module/mod.rs @@ -15,9 +15,8 @@ use relative_path::RelativePathBuf; use crate::{ FileId, FilePosition, Cancelable, - hir::{Path, PathKind, HirDatabase, SourceItemId}, + hir::{DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId}, arena::{Arena, Id}, - loc2id::{DefLoc, DefId}, }; pub(crate) use self::nameres::ModuleScope; diff --git a/crates/ra_analysis/src/hir/module/nameres.rs b/crates/ra_analysis/src/hir/module/nameres.rs index 5c87e7af2..d4ecc010b 100644 --- a/crates/ra_analysis/src/hir/module/nameres.rs +++ b/crates/ra_analysis/src/hir/module/nameres.rs @@ -28,8 +28,8 @@ use ra_db::SourceRootId; use crate::{ Cancelable, FileId, - loc2id::{DefId, DefLoc}, hir::{ + DefId, DefLoc, SourceItemId, SourceFileItemId, SourceFileItems, Path, PathKind, HirDatabase, diff --git a/crates/ra_analysis/src/hir/query_definitions.rs b/crates/ra_analysis/src/hir/query_definitions.rs index fbdf8eb67..00237b633 100644 --- a/crates/ra_analysis/src/hir/query_definitions.rs +++ b/crates/ra_analysis/src/hir/query_definitions.rs @@ -13,9 +13,10 @@ use ra_db::SourceRootId; use crate::{ FileId, Cancelable, hir::{ + FnId, SourceFileItems, SourceItemId, db::HirDatabase, - function::{FnId, FnScopes}, + function::FnScopes, module::{ ModuleSource, ModuleSourceNode, ModuleId, imp::Submodule, diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 012d36b8e..5b6e4df8c 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -20,7 +20,6 @@ macro_rules! ctry { mod arena; mod db; -mod loc2id; mod imp; mod completion; mod hir; diff --git a/crates/ra_analysis/src/loc2id.rs b/crates/ra_analysis/src/loc2id.rs deleted file mode 100644 index 7956431ab..000000000 --- a/crates/ra_analysis/src/loc2id.rs +++ /dev/null @@ -1,69 +0,0 @@ -use ra_db::SourceRootId; - -use crate::{ - hir::{SourceItemId, ModuleId}, -}; - -use ra_db::{NumericId, LocationIntener}; - -macro_rules! impl_numeric_id { - ($id:ident) => { - impl NumericId for $id { - fn from_u32(id: u32) -> Self { - $id(id) - } - fn to_u32(self) -> u32 { - self.0 - } - } - }; -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(crate) struct FnId(u32); -impl_numeric_id!(FnId); - -impl FnId { - pub(crate) fn from_loc( - db: &impl AsRef>, - loc: &SourceItemId, - ) -> FnId { - db.as_ref().loc2id(loc) - } - pub(crate) fn loc(self, db: &impl AsRef>) -> SourceItemId { - db.as_ref().id2loc(self) - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(crate) struct DefId(u32); -impl_numeric_id!(DefId); - -#[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub(crate) enum DefLoc { - Module { - id: ModuleId, - source_root: SourceRootId, - }, - Item { - source_item_id: SourceItemId, - }, -} - -impl DefId { - pub(crate) fn loc(self, db: &impl AsRef>) -> DefLoc { - db.as_ref().id2loc(self) - } -} - -impl DefLoc { - pub(crate) fn id(&self, db: &impl AsRef>) -> DefId { - db.as_ref().loc2id(&self) - } -} - -#[derive(Debug, Default)] -pub(crate) struct IdMaps { - pub(crate) fns: LocationIntener, - pub(crate) defs: LocationIntener, -} diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index 833f95eeb..c5587c950 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -38,6 +38,20 @@ pub use crate::{ loc2id::{LocationIntener, NumericId}, }; +#[macro_export] +macro_rules! impl_numeric_id { + ($id:ident) => { + impl $crate::NumericId for $id { + fn from_u32(id: u32) -> Self { + $id(id) + } + fn to_u32(self) -> u32 { + self.0 + } + } + }; +} + pub trait BaseDatabase: salsa::Database { fn check_canceled(&self) -> Cancelable<()> { if self.salsa_runtime().is_current_revision_canceled() { -- cgit v1.2.3 From 0e4b710af83844f4a7c471c5335c99aaaa25a90c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 03:42:26 +0300 Subject: introduce hir crate --- crates/ra_analysis/src/lib.rs | 17 +- crates/ra_db/src/lib.rs | 8 +- crates/ra_hir/Cargo.toml | 17 ++ crates/ra_hir/src/arena.rs | 66 +++++ crates/ra_hir/src/db.rs | 66 +++++ crates/ra_hir/src/function/mod.rs | 190 ++++++++++++++ crates/ra_hir/src/function/scope.rs | 450 +++++++++++++++++++++++++++++++++ crates/ra_hir/src/lib.rs | 139 ++++++++++ crates/ra_hir/src/module/imp.rs | 194 ++++++++++++++ crates/ra_hir/src/module/mod.rs | 378 +++++++++++++++++++++++++++ crates/ra_hir/src/module/nameres.rs | 444 ++++++++++++++++++++++++++++++++ crates/ra_hir/src/path.rs | 148 +++++++++++ crates/ra_hir/src/query_definitions.rs | 154 +++++++++++ 13 files changed, 2254 insertions(+), 17 deletions(-) create mode 100644 crates/ra_hir/Cargo.toml create mode 100644 crates/ra_hir/src/arena.rs create mode 100644 crates/ra_hir/src/db.rs create mode 100644 crates/ra_hir/src/function/mod.rs create mode 100644 crates/ra_hir/src/function/scope.rs create mode 100644 crates/ra_hir/src/lib.rs create mode 100644 crates/ra_hir/src/module/imp.rs create mode 100644 crates/ra_hir/src/module/mod.rs create mode 100644 crates/ra_hir/src/module/nameres.rs create mode 100644 crates/ra_hir/src/path.rs create mode 100644 crates/ra_hir/src/query_definitions.rs (limited to 'crates') diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 5b6e4df8c..a1912e90b 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -9,15 +9,6 @@ extern crate relative_path; extern crate rustc_hash; extern crate salsa; -macro_rules! ctry { - ($expr:expr) => { - match $expr { - None => return Ok(None), - Some(it) => it, - } - }; -} - mod arena; mod db; mod imp; @@ -47,7 +38,7 @@ pub use ra_editor::{ }; pub use ra_db::{ - Canceled, Cancelable, + Canceled, Cancelable, FilePosition, CrateGraph, CrateId, FileId, FileResolver }; @@ -119,12 +110,6 @@ impl AnalysisHost { } } -#[derive(Clone, Copy, Debug)] -pub struct FilePosition { - pub file_id: FileId, - pub offset: TextUnit, -} - #[derive(Debug)] pub struct SourceChange { pub label: String, diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index c5587c950..33cb0e2ec 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -13,7 +13,7 @@ mod loc2id; use std::sync::Arc; use ra_editor::LineIndex; -use ra_syntax::SourceFileNode; +use ra_syntax::{TextUnit, SourceFileNode}; #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct Canceled; @@ -81,3 +81,9 @@ fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc { let text = db.file_text(file_id); Arc::new(LineIndex::new(&*text)) } + +#[derive(Clone, Copy, Debug)] +pub struct FilePosition { + pub file_id: FileId, + pub offset: TextUnit, +} diff --git a/crates/ra_hir/Cargo.toml b/crates/ra_hir/Cargo.toml new file mode 100644 index 000000000..9bde289e7 --- /dev/null +++ b/crates/ra_hir/Cargo.toml @@ -0,0 +1,17 @@ +[package] +edition = "2018" +name = "ra_hir" +version = "0.1.0" +authors = ["Aleksey Kladov "] + +[dependencies] +log = "0.4.5" +relative-path = "0.4.0" +salsa = "0.8.0" +rustc-hash = "1.0" +parking_lot = "0.6.4" +id-arena = { git = "https://github.com/fitzgen/id-arena/", rev = "43ecd67" } +ra_syntax = { path = "../ra_syntax" } +ra_editor = { path = "../ra_editor" } +ra_db = { path = "../ra_db" } +test_utils = { path = "../test_utils" } diff --git a/crates/ra_hir/src/arena.rs b/crates/ra_hir/src/arena.rs new file mode 100644 index 000000000..a752ec0c1 --- /dev/null +++ b/crates/ra_hir/src/arena.rs @@ -0,0 +1,66 @@ +//! A simple id-based arena, similar to https://github.com/fitzgen/id-arena. +//! We use our own version for more compact id's and to allow inherent impls +//! on Ids. + +use std::{ + fmt, + hash::{Hash, Hasher}, + marker::PhantomData, +}; + +pub(crate) struct Id { + idx: u32, + _ty: PhantomData T>, +} + +impl fmt::Debug for Id { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_tuple("Id").field(&self.idx).finish() + } +} +impl Copy for Id {} +impl Clone for Id { + fn clone(&self) -> Id { + *self + } +} + +impl PartialEq for Id { + fn eq(&self, other: &Id) -> bool { + self.idx == other.idx + } +} + +impl Eq for Id {} + +impl Hash for Id { + fn hash(&self, h: &mut H) { + self.idx.hash(h); + } +} + +#[derive(Debug, PartialEq, Eq)] +pub(crate) struct ArenaBehavior { + _ty: PhantomData, +} + +impl id_arena::ArenaBehavior for ArenaBehavior { + type Id = Id; + fn new_arena_id() -> usize { + 0 + } + fn new_id(_arena_id: usize, index: usize) -> Id { + Id { + idx: index as u32, + _ty: PhantomData, + } + } + fn index(id: Id) -> usize { + id.idx as usize + } + fn arena_id(_id: Id) -> usize { + 0 + } +} + +pub(crate) type Arena = id_arena::Arena>; diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs new file mode 100644 index 000000000..dbf8785fe --- /dev/null +++ b/crates/ra_hir/src/db.rs @@ -0,0 +1,66 @@ +use std::sync::Arc; + +use ra_syntax::{ + SyntaxNode, + ast::FnDefNode, +}; +use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase, FileId, Cancelable}; + +use crate::{ + DefLoc, DefId, FnId, + SourceFileItems, SourceItemId, + query_definitions, + function::{FnScopes}, + module::{ModuleId, ModuleTree, ModuleSource, + nameres::{ItemMap, InputModuleItems}}, +}; + +salsa::query_group! { + +pub(crate) trait HirDatabase: SyntaxDatabase + + AsRef> + + AsRef> +{ + fn fn_scopes(fn_id: FnId) -> Arc { + type FnScopesQuery; + use fn query_definitions::fn_scopes; + } + fn fn_syntax(fn_id: FnId) -> FnDefNode { + type FnSyntaxQuery; + // Don't retain syntax trees in memory + storage dependencies; + use fn query_definitions::fn_syntax; + } + + fn file_items(file_id: FileId) -> Arc { + type SourceFileItemsQuery; + storage dependencies; + use fn query_definitions::file_items; + } + + fn file_item(source_item_id: SourceItemId) -> SyntaxNode { + type FileItemQuery; + storage dependencies; + use fn query_definitions::file_item; + } + + fn submodules(source: ModuleSource) -> Cancelable>> { + type SubmodulesQuery; + use fn query_definitions::submodules; + } + + fn input_module_items(source_root_id: SourceRootId, module_id: ModuleId) -> Cancelable> { + type InputModuleItemsQuery; + use fn query_definitions::input_module_items; + } + fn item_map(source_root_id: SourceRootId) -> Cancelable> { + type ItemMapQuery; + use fn query_definitions::item_map; + } + fn module_tree(source_root_id: SourceRootId) -> Cancelable> { + type ModuleTreeQuery; + use fn crate::module::imp::module_tree; + } +} + +} diff --git a/crates/ra_hir/src/function/mod.rs b/crates/ra_hir/src/function/mod.rs new file mode 100644 index 000000000..a3ed00f02 --- /dev/null +++ b/crates/ra_hir/src/function/mod.rs @@ -0,0 +1,190 @@ +mod scope; + +use std::{ + cmp::{max, min}, + sync::Arc, +}; + +use ra_syntax::{ + TextRange, TextUnit, SyntaxNodeRef, + ast::{self, AstNode, DocCommentsOwner, NameOwner}, +}; +use ra_db::FileId; + +use crate::{ + FnId, HirDatabase, SourceItemId, +}; + +pub(crate) use self::scope::FnScopes; + +impl FnId { + pub(crate) fn get(db: &impl HirDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId { + let file_items = db.file_items(file_id); + let item_id = file_items.id_of(fn_def.syntax()); + let item_id = SourceItemId { file_id, item_id }; + FnId::from_loc(db, &item_id) + } +} + +pub(crate) struct Function { + fn_id: FnId, +} + +impl Function { + pub(crate) fn guess_from_source( + db: &impl HirDatabase, + file_id: FileId, + fn_def: ast::FnDef, + ) -> Function { + let fn_id = FnId::get(db, file_id, fn_def); + Function { fn_id } + } + + pub(crate) fn guess_for_name_ref( + db: &impl HirDatabase, + file_id: FileId, + name_ref: ast::NameRef, + ) -> Option { + Function::guess_for_node(db, file_id, name_ref.syntax()) + } + + pub(crate) fn guess_for_bind_pat( + db: &impl HirDatabase, + file_id: FileId, + bind_pat: ast::BindPat, + ) -> Option { + Function::guess_for_node(db, file_id, bind_pat.syntax()) + } + + fn guess_for_node( + db: &impl HirDatabase, + file_id: FileId, + node: SyntaxNodeRef, + ) -> Option { + let fn_def = node.ancestors().find_map(ast::FnDef::cast)?; + let res = Function::guess_from_source(db, file_id, fn_def); + Some(res) + } + + pub(crate) fn scope(&self, db: &impl HirDatabase) -> Arc { + db.fn_scopes(self.fn_id) + } + + pub(crate) fn signature_info(&self, db: &impl HirDatabase) -> Option { + let syntax = db.fn_syntax(self.fn_id); + FnSignatureInfo::new(syntax.borrowed()) + } +} + +#[derive(Debug, Clone)] +pub struct FnSignatureInfo { + pub name: String, + pub label: String, + pub ret_type: Option, + pub params: Vec, + pub doc: Option, +} + +impl FnSignatureInfo { + fn new(node: ast::FnDef) -> Option { + let name = node.name()?.text().to_string(); + + let mut doc = None; + + // Strip the body out for the label. + let mut label: String = if let Some(body) = node.body() { + let body_range = body.syntax().range(); + let label: String = node + .syntax() + .children() + .filter(|child| !child.range().is_subrange(&body_range)) + .map(|node| node.text().to_string()) + .collect(); + label + } else { + node.syntax().text().to_string() + }; + + if let Some((comment_range, docs)) = FnSignatureInfo::extract_doc_comments(node) { + let comment_range = comment_range + .checked_sub(node.syntax().range().start()) + .unwrap(); + let start = comment_range.start().to_usize(); + let end = comment_range.end().to_usize(); + + // Remove the comment from the label + label.replace_range(start..end, ""); + + // Massage markdown + let mut processed_lines = Vec::new(); + let mut in_code_block = false; + for line in docs.lines() { + if line.starts_with("```") { + in_code_block = !in_code_block; + } + + let line = if in_code_block && line.starts_with("```") && !line.contains("rust") { + "```rust".into() + } else { + line.to_string() + }; + + processed_lines.push(line); + } + + if !processed_lines.is_empty() { + doc = Some(processed_lines.join("\n")); + } + } + + let params = FnSignatureInfo::param_list(node); + let ret_type = node.ret_type().map(|r| r.syntax().text().to_string()); + + Some(FnSignatureInfo { + name, + ret_type, + params, + label: label.trim().to_owned(), + doc, + }) + } + + fn extract_doc_comments(node: ast::FnDef) -> Option<(TextRange, String)> { + if node.doc_comments().count() == 0 { + return None; + } + + let comment_text = node.doc_comment_text(); + + let (begin, end) = node + .doc_comments() + .map(|comment| comment.syntax().range()) + .map(|range| (range.start().to_usize(), range.end().to_usize())) + .fold((std::usize::MAX, std::usize::MIN), |acc, range| { + (min(acc.0, range.0), max(acc.1, range.1)) + }); + + let range = TextRange::from_to(TextUnit::from_usize(begin), TextUnit::from_usize(end)); + + Some((range, comment_text)) + } + + fn param_list(node: ast::FnDef) -> Vec { + let mut res = vec![]; + if let Some(param_list) = node.param_list() { + if let Some(self_param) = param_list.self_param() { + res.push(self_param.syntax().text().to_string()) + } + + // Maybe use param.pat here? See if we can just extract the name? + //res.extend(param_list.params().map(|p| p.syntax().text().to_string())); + res.extend( + param_list + .params() + .filter_map(|p| p.pat()) + .map(|pat| pat.syntax().text().to_string()), + ); + } + res + } +} diff --git a/crates/ra_hir/src/function/scope.rs b/crates/ra_hir/src/function/scope.rs new file mode 100644 index 000000000..c8b6b1934 --- /dev/null +++ b/crates/ra_hir/src/function/scope.rs @@ -0,0 +1,450 @@ +use rustc_hash::{FxHashMap, FxHashSet}; + +use ra_syntax::{ + AstNode, SmolStr, SyntaxNodeRef, TextRange, + algo::generate, + ast::{self, ArgListOwner, LoopBodyOwner, NameOwner}, +}; +use ra_db::LocalSyntaxPtr; + +use crate::{ + arena::{Arena, Id}, +}; + +pub(crate) type ScopeId = Id; + +#[derive(Debug, PartialEq, Eq)] +pub struct FnScopes { + pub(crate) self_param: Option, + scopes: Arena, + scope_for: FxHashMap, +} + +#[derive(Debug, PartialEq, Eq)] +pub struct ScopeEntry { + name: SmolStr, + ptr: LocalSyntaxPtr, +} + +#[derive(Debug, PartialEq, Eq)] +pub(crate) struct ScopeData { + parent: Option, + entries: Vec, +} + +impl FnScopes { + pub(crate) fn new(fn_def: ast::FnDef) -> FnScopes { + let mut scopes = FnScopes { + self_param: fn_def + .param_list() + .and_then(|it| it.self_param()) + .map(|it| LocalSyntaxPtr::new(it.syntax())), + scopes: Arena::default(), + scope_for: FxHashMap::default(), + }; + let root = scopes.root_scope(); + scopes.add_params_bindings(root, fn_def.param_list()); + if let Some(body) = fn_def.body() { + compute_block_scopes(body, &mut scopes, root) + } + scopes + } + pub(crate) fn entries(&self, scope: ScopeId) -> &[ScopeEntry] { + &self.scopes[scope].entries + } + pub fn scope_chain<'a>(&'a self, node: SyntaxNodeRef) -> impl Iterator + 'a { + generate(self.scope_for(node), move |&scope| { + self.scopes[scope].parent + }) + } + pub(crate) fn resolve_local_name<'a>( + &'a self, + name_ref: ast::NameRef, + ) -> Option<&'a ScopeEntry> { + let mut shadowed = FxHashSet::default(); + let ret = self + .scope_chain(name_ref.syntax()) + .flat_map(|scope| self.entries(scope).iter()) + .filter(|entry| shadowed.insert(entry.name())) + .filter(|entry| entry.name() == &name_ref.text()) + .nth(0); + ret + } + + pub fn find_all_refs(&self, pat: ast::BindPat) -> Vec { + let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); + let name_ptr = LocalSyntaxPtr::new(pat.syntax()); + let refs: Vec<_> = fn_def + .syntax() + .descendants() + .filter_map(ast::NameRef::cast) + .filter(|name_ref| match self.resolve_local_name(*name_ref) { + None => false, + Some(entry) => entry.ptr() == name_ptr, + }) + .map(|name_ref| ReferenceDescriptor { + name: name_ref.syntax().text().to_string(), + range: name_ref.syntax().range(), + }) + .collect(); + + refs + } + + fn root_scope(&mut self) -> ScopeId { + self.scopes.alloc(ScopeData { + parent: None, + entries: vec![], + }) + } + fn new_scope(&mut self, parent: ScopeId) -> ScopeId { + self.scopes.alloc(ScopeData { + parent: Some(parent), + entries: vec![], + }) + } + fn add_bindings(&mut self, scope: ScopeId, pat: ast::Pat) { + let entries = pat + .syntax() + .descendants() + .filter_map(ast::BindPat::cast) + .filter_map(ScopeEntry::new); + self.scopes[scope].entries.extend(entries); + } + fn add_params_bindings(&mut self, scope: ScopeId, params: Option) { + params + .into_iter() + .flat_map(|it| it.params()) + .filter_map(|it| it.pat()) + .for_each(|it| self.add_bindings(scope, it)); + } + fn set_scope(&mut self, node: SyntaxNodeRef, scope: ScopeId) { + self.scope_for.insert(LocalSyntaxPtr::new(node), scope); + } + fn scope_for(&self, node: SyntaxNodeRef) -> Option { + node.ancestors() + .map(LocalSyntaxPtr::new) + .filter_map(|it| self.scope_for.get(&it).map(|&scope| scope)) + .next() + } +} + +impl ScopeEntry { + fn new(pat: ast::BindPat) -> Option { + let name = pat.name()?; + let res = ScopeEntry { + name: name.text(), + ptr: LocalSyntaxPtr::new(pat.syntax()), + }; + Some(res) + } + pub(crate) fn name(&self) -> &SmolStr { + &self.name + } + pub(crate) fn ptr(&self) -> LocalSyntaxPtr { + self.ptr + } +} + +fn compute_block_scopes(block: ast::Block, scopes: &mut FnScopes, mut scope: ScopeId) { + for stmt in block.statements() { + match stmt { + ast::Stmt::LetStmt(stmt) => { + if let Some(expr) = stmt.initializer() { + scopes.set_scope(expr.syntax(), scope); + compute_expr_scopes(expr, scopes, scope); + } + scope = scopes.new_scope(scope); + if let Some(pat) = stmt.pat() { + scopes.add_bindings(scope, pat); + } + } + ast::Stmt::ExprStmt(expr_stmt) => { + if let Some(expr) = expr_stmt.expr() { + scopes.set_scope(expr.syntax(), scope); + compute_expr_scopes(expr, scopes, scope); + } + } + } + } + if let Some(expr) = block.expr() { + scopes.set_scope(expr.syntax(), scope); + compute_expr_scopes(expr, scopes, scope); + } +} + +fn compute_expr_scopes(expr: ast::Expr, scopes: &mut FnScopes, scope: ScopeId) { + match expr { + ast::Expr::IfExpr(e) => { + let cond_scope = e + .condition() + .and_then(|cond| compute_cond_scopes(cond, scopes, scope)); + if let Some(block) = e.then_branch() { + compute_block_scopes(block, scopes, cond_scope.unwrap_or(scope)); + } + if let Some(block) = e.else_branch() { + compute_block_scopes(block, scopes, scope); + } + } + ast::Expr::BlockExpr(e) => { + if let Some(block) = e.block() { + compute_block_scopes(block, scopes, scope); + } + } + ast::Expr::LoopExpr(e) => { + if let Some(block) = e.loop_body() { + compute_block_scopes(block, scopes, scope); + } + } + ast::Expr::WhileExpr(e) => { + let cond_scope = e + .condition() + .and_then(|cond| compute_cond_scopes(cond, scopes, scope)); + if let Some(block) = e.loop_body() { + compute_block_scopes(block, scopes, cond_scope.unwrap_or(scope)); + } + } + ast::Expr::ForExpr(e) => { + if let Some(expr) = e.iterable() { + compute_expr_scopes(expr, scopes, scope); + } + let mut scope = scope; + if let Some(pat) = e.pat() { + scope = scopes.new_scope(scope); + scopes.add_bindings(scope, pat); + } + if let Some(block) = e.loop_body() { + compute_block_scopes(block, scopes, scope); + } + } + ast::Expr::LambdaExpr(e) => { + let scope = scopes.new_scope(scope); + scopes.add_params_bindings(scope, e.param_list()); + if let Some(body) = e.body() { + scopes.set_scope(body.syntax(), scope); + compute_expr_scopes(body, scopes, scope); + } + } + ast::Expr::CallExpr(e) => { + compute_call_scopes(e.expr(), e.arg_list(), scopes, scope); + } + ast::Expr::MethodCallExpr(e) => { + compute_call_scopes(e.expr(), e.arg_list(), scopes, scope); + } + ast::Expr::MatchExpr(e) => { + if let Some(expr) = e.expr() { + compute_expr_scopes(expr, scopes, scope); + } + for arm in e.match_arm_list().into_iter().flat_map(|it| it.arms()) { + let scope = scopes.new_scope(scope); + for pat in arm.pats() { + scopes.add_bindings(scope, pat); + } + if let Some(expr) = arm.expr() { + compute_expr_scopes(expr, scopes, scope); + } + } + } + _ => expr + .syntax() + .children() + .filter_map(ast::Expr::cast) + .for_each(|expr| compute_expr_scopes(expr, scopes, scope)), + }; + + fn compute_call_scopes( + receiver: Option, + arg_list: Option, + scopes: &mut FnScopes, + scope: ScopeId, + ) { + arg_list + .into_iter() + .flat_map(|it| it.args()) + .chain(receiver) + .for_each(|expr| compute_expr_scopes(expr, scopes, scope)); + } + + fn compute_cond_scopes( + cond: ast::Condition, + scopes: &mut FnScopes, + scope: ScopeId, + ) -> Option { + if let Some(expr) = cond.expr() { + compute_expr_scopes(expr, scopes, scope); + } + if let Some(pat) = cond.pat() { + let s = scopes.new_scope(scope); + scopes.add_bindings(s, pat); + Some(s) + } else { + None + } + } +} + +#[derive(Debug)] +pub struct ReferenceDescriptor { + pub range: TextRange, + pub name: String, +} + +#[cfg(test)] +mod tests { + use ra_editor::find_node_at_offset; + use ra_syntax::SourceFileNode; + use test_utils::extract_offset; + + use super::*; + + fn do_check(code: &str, expected: &[&str]) { + let (off, code) = extract_offset(code); + let code = { + let mut buf = String::new(); + let off = u32::from(off) as usize; + buf.push_str(&code[..off]); + buf.push_str("marker"); + buf.push_str(&code[off..]); + buf + }; + let file = SourceFileNode::parse(&code); + let marker: ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); + let fn_def: ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); + let scopes = FnScopes::new(fn_def); + let actual = scopes + .scope_chain(marker.syntax()) + .flat_map(|scope| scopes.entries(scope)) + .map(|it| it.name()) + .collect::>(); + assert_eq!(actual.as_slice(), expected); + } + + #[test] + fn test_lambda_scope() { + do_check( + r" + fn quux(foo: i32) { + let f = |bar, baz: i32| { + <|> + }; + }", + &["bar", "baz", "foo"], + ); + } + + #[test] + fn test_call_scope() { + do_check( + r" + fn quux() { + f(|x| <|> ); + }", + &["x"], + ); + } + + #[test] + fn test_metod_call_scope() { + do_check( + r" + fn quux() { + z.f(|x| <|> ); + }", + &["x"], + ); + } + + #[test] + fn test_loop_scope() { + do_check( + r" + fn quux() { + loop { + let x = (); + <|> + }; + }", + &["x"], + ); + } + + #[test] + fn test_match() { + do_check( + r" + fn quux() { + match () { + Some(x) => { + <|> + } + }; + }", + &["x"], + ); + } + + #[test] + fn test_shadow_variable() { + do_check( + r" + fn foo(x: String) { + let x : &str = &x<|>; + }", + &["x"], + ); + } + + fn do_check_local_name(code: &str, expected_offset: u32) { + let (off, code) = extract_offset(code); + let file = SourceFileNode::parse(&code); + let fn_def: ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); + let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap(); + + let scopes = FnScopes::new(fn_def); + + let local_name_entry = scopes.resolve_local_name(name_ref).unwrap(); + let local_name = local_name_entry.ptr().resolve(&file); + let expected_name = + find_node_at_offset::(file.syntax(), expected_offset.into()).unwrap(); + assert_eq!(local_name.range(), expected_name.syntax().range()); + } + + #[test] + fn test_resolve_local_name() { + do_check_local_name( + r#" + fn foo(x: i32, y: u32) { + { + let z = x * 2; + } + { + let t = x<|> * 3; + } + }"#, + 21, + ); + } + + #[test] + fn test_resolve_local_name_declaration() { + do_check_local_name( + r#" + fn foo(x: String) { + let x : &str = &x<|>; + }"#, + 21, + ); + } + + #[test] + fn test_resolve_local_name_shadow() { + do_check_local_name( + r" + fn foo(x: String) { + let x : &str = &x; + x<|> + }", + 46, + ); + } +} diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs new file mode 100644 index 000000000..7bf06c7f7 --- /dev/null +++ b/crates/ra_hir/src/lib.rs @@ -0,0 +1,139 @@ +//! HIR (previsouly known as descriptors) provides a high-level OO acess to Rust +//! code. +//! +//! The principal difference between HIR and syntax trees is that HIR is bound +//! to a particular crate instance. That is, it has cfg flags and features +//! applied. So, there relation between syntax and HIR is many-to-one. + +macro_rules! ctry { + ($expr:expr) => { + match $expr { + None => return Ok(None), + Some(it) => it, + } + }; +} + +pub(crate) mod db; +mod query_definitions; +mod function; +mod module; +mod path; +mod arena; + +use std::ops::Index; + +use ra_syntax::{SyntaxNodeRef, SyntaxNode}; +use ra_db::{LocationIntener, SourceRootId, FileId, Cancelable}; + +use crate::{ + db::HirDatabase, + arena::{Arena, Id}, +}; + +pub(crate) use self::{ + path::{Path, PathKind}, + module::{Module, ModuleId, Problem}, + function::{Function, FnScopes}, +}; + +pub use self::function::FnSignatureInfo; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub(crate) struct FnId(u32); +ra_db::impl_numeric_id!(FnId); + +impl FnId { + pub(crate) fn from_loc( + db: &impl AsRef>, + loc: &SourceItemId, + ) -> FnId { + db.as_ref().loc2id(loc) + } + pub(crate) fn loc(self, db: &impl AsRef>) -> SourceItemId { + db.as_ref().id2loc(self) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub(crate) struct DefId(u32); +ra_db::impl_numeric_id!(DefId); + +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub(crate) enum DefLoc { + Module { + id: ModuleId, + source_root: SourceRootId, + }, + Item { + source_item_id: SourceItemId, + }, +} + +impl DefId { + pub(crate) fn loc(self, db: &impl AsRef>) -> DefLoc { + db.as_ref().id2loc(self) + } +} + +impl DefLoc { + pub(crate) fn id(&self, db: &impl AsRef>) -> DefId { + db.as_ref().loc2id(&self) + } +} + +pub(crate) enum Def { + Module(Module), + Item, +} + +impl DefId { + pub(crate) fn resolve(self, db: &impl HirDatabase) -> Cancelable { + let loc = self.loc(db); + let res = match loc { + DefLoc::Module { id, source_root } => { + let descr = Module::new(db, source_root, id)?; + Def::Module(descr) + } + DefLoc::Item { .. } => Def::Item, + }; + Ok(res) + } +} + +/// Identifier of item within a specific file. This is stable over reparses, so +/// it's OK to use it as a salsa key/value. +pub(crate) type SourceFileItemId = Id; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub(crate) struct SourceItemId { + file_id: FileId, + item_id: SourceFileItemId, +} + +/// Maps item's `SyntaxNode`s to `SourceFileItemId` and back. +#[derive(Debug, PartialEq, Eq, Default)] +pub(crate) struct SourceFileItems { + arena: Arena, +} + +impl SourceFileItems { + fn alloc(&mut self, item: SyntaxNode) -> SourceFileItemId { + self.arena.alloc(item) + } + fn id_of(&self, item: SyntaxNodeRef) -> SourceFileItemId { + let (id, _item) = self + .arena + .iter() + .find(|(_id, i)| i.borrowed() == item) + .unwrap(); + id + } +} + +impl Index for SourceFileItems { + type Output = SyntaxNode; + fn index(&self, idx: SourceFileItemId) -> &SyntaxNode { + &self.arena[idx] + } +} diff --git a/crates/ra_hir/src/module/imp.rs b/crates/ra_hir/src/module/imp.rs new file mode 100644 index 000000000..d55fa3e6b --- /dev/null +++ b/crates/ra_hir/src/module/imp.rs @@ -0,0 +1,194 @@ +use std::sync::Arc; + +use ra_syntax::{ + ast::{self, NameOwner}, + SmolStr, +}; +use relative_path::RelativePathBuf; +use rustc_hash::{FxHashMap, FxHashSet}; +use ra_db::{SourceRoot, SourceRootId, FileResolverImp, Cancelable, FileId,}; + +use crate::{ + HirDatabase, +}; + +use super::{ + LinkData, LinkId, ModuleData, ModuleId, ModuleSource, + ModuleTree, Problem, +}; + +#[derive(Clone, Hash, PartialEq, Eq, Debug)] +pub(crate) enum Submodule { + Declaration(SmolStr), + Definition(SmolStr, ModuleSource), +} + +impl Submodule { + fn name(&self) -> &SmolStr { + match self { + Submodule::Declaration(name) => name, + Submodule::Definition(name, _) => name, + } + } +} + +pub(crate) fn modules<'a>( + root: impl ast::ModuleItemOwner<'a>, +) -> impl Iterator)> { + root.items() + .filter_map(|item| match item { + ast::ModuleItem::Module(m) => Some(m), + _ => None, + }) + .filter_map(|module| { + let name = module.name()?.text(); + Some((name, module)) + }) +} + +pub(crate) fn module_tree( + db: &impl HirDatabase, + source_root: SourceRootId, +) -> Cancelable> { + db.check_canceled()?; + let res = create_module_tree(db, source_root)?; + Ok(Arc::new(res)) +} + +fn create_module_tree<'a>( + db: &impl HirDatabase, + source_root: SourceRootId, +) -> Cancelable { + let mut tree = ModuleTree::default(); + + let mut roots = FxHashMap::default(); + let mut visited = FxHashSet::default(); + + let source_root = db.source_root(source_root); + for &file_id in source_root.files.iter() { + let source = ModuleSource::SourceFile(file_id); + if visited.contains(&source) { + continue; // TODO: use explicit crate_roots here + } + assert!(!roots.contains_key(&file_id)); + let module_id = build_subtree( + db, + &source_root, + &mut tree, + &mut visited, + &mut roots, + None, + source, + )?; + roots.insert(file_id, module_id); + } + Ok(tree) +} + +fn build_subtree( + db: &impl HirDatabase, + source_root: &SourceRoot, + tree: &mut ModuleTree, + visited: &mut FxHashSet, + roots: &mut FxHashMap, + parent: Option, + source: ModuleSource, +) -> Cancelable { + visited.insert(source); + let id = tree.push_mod(ModuleData { + source, + parent, + children: Vec::new(), + }); + for sub in db.submodules(source)?.iter() { + let link = tree.push_link(LinkData { + name: sub.name().clone(), + owner: id, + points_to: Vec::new(), + problem: None, + }); + + let (points_to, problem) = match sub { + Submodule::Declaration(name) => { + let (points_to, problem) = + resolve_submodule(source, &name, &source_root.file_resolver); + let points_to = points_to + .into_iter() + .map(|file_id| match roots.remove(&file_id) { + Some(module_id) => { + tree.mods[module_id].parent = Some(link); + Ok(module_id) + } + None => build_subtree( + db, + source_root, + tree, + visited, + roots, + Some(link), + ModuleSource::SourceFile(file_id), + ), + }) + .collect::>>()?; + (points_to, problem) + } + Submodule::Definition(_name, submodule_source) => { + let points_to = build_subtree( + db, + source_root, + tree, + visited, + roots, + Some(link), + *submodule_source, + )?; + (vec![points_to], None) + } + }; + + tree.links[link].points_to = points_to; + tree.links[link].problem = problem; + } + Ok(id) +} + +fn resolve_submodule( + source: ModuleSource, + name: &SmolStr, + file_resolver: &FileResolverImp, +) -> (Vec, Option) { + let file_id = match source { + ModuleSource::SourceFile(it) => it, + ModuleSource::Module(..) => { + // TODO + return (Vec::new(), None); + } + }; + let mod_name = file_resolver.file_stem(file_id); + let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main"; + + let file_mod = RelativePathBuf::from(format!("../{}.rs", name)); + let dir_mod = RelativePathBuf::from(format!("../{}/mod.rs", name)); + let points_to: Vec; + let problem: Option; + if is_dir_owner { + points_to = [&file_mod, &dir_mod] + .iter() + .filter_map(|path| file_resolver.resolve(file_id, path)) + .collect(); + problem = if points_to.is_empty() { + Some(Problem::UnresolvedModule { + candidate: file_mod, + }) + } else { + None + } + } else { + points_to = Vec::new(); + problem = Some(Problem::NotDirOwner { + move_to: RelativePathBuf::from(format!("../{}/mod.rs", mod_name)), + candidate: file_mod, + }); + } + (points_to, problem) +} diff --git a/crates/ra_hir/src/module/mod.rs b/crates/ra_hir/src/module/mod.rs new file mode 100644 index 000000000..81b9f948d --- /dev/null +++ b/crates/ra_hir/src/module/mod.rs @@ -0,0 +1,378 @@ +pub(super) mod imp; +pub(super) mod nameres; + +use std::sync::Arc; + +use ra_editor::find_node_at_offset; + +use ra_syntax::{ + algo::generate, + ast::{self, AstNode, NameOwner}, + SmolStr, SyntaxNode, +}; +use ra_db::{SourceRootId, FileId, FilePosition, Cancelable}; +use relative_path::RelativePathBuf; + +use crate::{ + DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, + arena::{Arena, Id}, +}; + +pub(crate) use self::nameres::ModuleScope; + +/// `Module` is API entry point to get all the information +/// about a particular module. +#[derive(Debug, Clone)] +pub(crate) struct Module { + tree: Arc, + source_root_id: SourceRootId, + module_id: ModuleId, +} + +impl Module { + /// Lookup `Module` by `FileId`. Note that this is inherently + /// lossy transformation: in general, a single source might correspond to + /// several modules. + pub fn guess_from_file_id( + db: &impl HirDatabase, + file_id: FileId, + ) -> Cancelable> { + Module::guess_from_source(db, file_id, ModuleSource::SourceFile(file_id)) + } + + /// Lookup `Module` by position in the source code. Note that this + /// is inherently lossy transformation: in general, a single source might + /// correspond to several modules. + pub fn guess_from_position( + db: &impl HirDatabase, + position: FilePosition, + ) -> Cancelable> { + let file = db.source_file(position.file_id); + let module_source = match find_node_at_offset::(file.syntax(), position.offset) + { + Some(m) if !m.has_semi() => ModuleSource::new_inline(db, position.file_id, m), + _ => ModuleSource::SourceFile(position.file_id), + }; + Module::guess_from_source(db, position.file_id, module_source) + } + + fn guess_from_source( + db: &impl HirDatabase, + file_id: FileId, + module_source: ModuleSource, + ) -> Cancelable> { + let source_root_id = db.file_source_root(file_id); + let module_tree = db.module_tree(source_root_id)?; + + let res = match module_tree.any_module_for_source(module_source) { + None => None, + Some(module_id) => Some(Module { + tree: module_tree, + source_root_id, + module_id, + }), + }; + Ok(res) + } + + pub(super) fn new( + db: &impl HirDatabase, + source_root_id: SourceRootId, + module_id: ModuleId, + ) -> Cancelable { + let module_tree = db.module_tree(source_root_id)?; + let res = Module { + tree: module_tree, + source_root_id, + module_id, + }; + Ok(res) + } + + /// Returns `mod foo;` or `mod foo {}` node whihc declared this module. + /// Returns `None` for the root module + pub fn parent_link_source(&self, db: &impl HirDatabase) -> Option<(FileId, ast::ModuleNode)> { + let link = self.module_id.parent_link(&self.tree)?; + let file_id = link.owner(&self.tree).source(&self.tree).file_id(); + let src = link.bind_source(&self.tree, db); + Some((file_id, src)) + } + + pub fn source(&self) -> ModuleSource { + self.module_id.source(&self.tree) + } + + /// Parent module. Returns `None` if this is a root module. + pub fn parent(&self) -> Option { + let parent_id = self.module_id.parent(&self.tree)?; + Some(Module { + module_id: parent_id, + ..self.clone() + }) + } + + /// The root of the tree this module is part of + pub fn crate_root(&self) -> Module { + let root_id = self.module_id.crate_root(&self.tree); + Module { + module_id: root_id, + ..self.clone() + } + } + + /// `name` is `None` for the crate's root module + #[allow(unused)] + pub fn name(&self) -> Option { + let link = self.module_id.parent_link(&self.tree)?; + Some(link.name(&self.tree)) + } + + pub fn def_id(&self, db: &impl HirDatabase) -> DefId { + let def_loc = DefLoc::Module { + id: self.module_id, + source_root: self.source_root_id, + }; + def_loc.id(db) + } + + /// Finds a child module with the specified name. + pub fn child(&self, name: &str) -> Option { + let child_id = self.module_id.child(&self.tree, name)?; + Some(Module { + module_id: child_id, + ..self.clone() + }) + } + + /// Returns a `ModuleScope`: a set of items, visible in this module. + pub(crate) fn scope(&self, db: &impl HirDatabase) -> Cancelable { + let item_map = db.item_map(self.source_root_id)?; + let res = item_map.per_module[&self.module_id].clone(); + Ok(res) + } + + pub(crate) fn resolve_path( + &self, + db: &impl HirDatabase, + path: Path, + ) -> Cancelable> { + let mut curr = match path.kind { + PathKind::Crate => self.crate_root(), + PathKind::Self_ | PathKind::Plain => self.clone(), + PathKind::Super => ctry!(self.parent()), + } + .def_id(db); + + let segments = path.segments; + for name in segments.iter() { + let module = match curr.loc(db) { + DefLoc::Module { id, source_root } => Module::new(db, source_root, id)?, + _ => return Ok(None), + }; + let scope = module.scope(db)?; + curr = ctry!(ctry!(scope.get(&name)).def_id); + } + Ok(Some(curr)) + } + + pub fn problems(&self, db: &impl HirDatabase) -> Vec<(SyntaxNode, Problem)> { + self.module_id.problems(&self.tree, db) + } +} + +/// Phisically, rust source is organized as a set of files, but logically it is +/// organized as a tree of modules. Usually, a single file corresponds to a +/// single module, but it is not nessary the case. +/// +/// Module encapsulate the logic of transitioning from the fuzzy world of files +/// (which can have multiple parents) to the precise world of modules (which +/// always have one parent). +#[derive(Default, Debug, PartialEq, Eq)] +pub(crate) struct ModuleTree { + mods: Arena, + links: Arena, +} + +impl ModuleTree { + pub(crate) fn modules<'a>(&'a self) -> impl Iterator + 'a { + self.mods.iter().map(|(id, _)| id) + } + + fn modules_for_source(&self, source: ModuleSource) -> Vec { + self.mods + .iter() + .filter(|(_idx, it)| it.source == source) + .map(|(idx, _)| idx) + .collect() + } + + fn any_module_for_source(&self, source: ModuleSource) -> Option { + self.modules_for_source(source).pop() + } +} + +/// `ModuleSource` is the syntax tree element that produced this module: +/// either a file, or an inlinde module. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub(crate) enum ModuleSource { + SourceFile(FileId), + Module(SourceItemId), +} + +/// An owned syntax node for a module. Unlike `ModuleSource`, +/// this holds onto the AST for the whole file. +pub(crate) enum ModuleSourceNode { + SourceFile(ast::SourceFileNode), + Module(ast::ModuleNode), +} + +pub(crate) type ModuleId = Id; +type LinkId = Id; + +#[derive(Clone, Debug, Hash, PartialEq, Eq)] +pub enum Problem { + UnresolvedModule { + candidate: RelativePathBuf, + }, + NotDirOwner { + move_to: RelativePathBuf, + candidate: RelativePathBuf, + }, +} + +impl ModuleId { + pub(crate) fn source(self, tree: &ModuleTree) -> ModuleSource { + tree.mods[self].source + } + fn parent_link(self, tree: &ModuleTree) -> Option { + tree.mods[self].parent + } + fn parent(self, tree: &ModuleTree) -> Option { + let link = self.parent_link(tree)?; + Some(tree.links[link].owner) + } + fn crate_root(self, tree: &ModuleTree) -> ModuleId { + generate(Some(self), move |it| it.parent(tree)) + .last() + .unwrap() + } + fn child(self, tree: &ModuleTree, name: &str) -> Option { + let link = tree.mods[self] + .children + .iter() + .map(|&it| &tree.links[it]) + .find(|it| it.name == name)?; + Some(*link.points_to.first()?) + } + fn children<'a>(self, tree: &'a ModuleTree) -> impl Iterator + 'a { + tree.mods[self].children.iter().filter_map(move |&it| { + let link = &tree.links[it]; + let module = *link.points_to.first()?; + Some((link.name.clone(), module)) + }) + } + fn problems(self, tree: &ModuleTree, db: &impl HirDatabase) -> Vec<(SyntaxNode, Problem)> { + tree.mods[self] + .children + .iter() + .filter_map(|&it| { + let p = tree.links[it].problem.clone()?; + let s = it.bind_source(tree, db); + let s = s.borrowed().name().unwrap().syntax().owned(); + Some((s, p)) + }) + .collect() + } +} + +impl LinkId { + fn owner(self, tree: &ModuleTree) -> ModuleId { + tree.links[self].owner + } + fn name(self, tree: &ModuleTree) -> SmolStr { + tree.links[self].name.clone() + } + fn bind_source<'a>(self, tree: &ModuleTree, db: &impl HirDatabase) -> ast::ModuleNode { + let owner = self.owner(tree); + match owner.source(tree).resolve(db) { + ModuleSourceNode::SourceFile(root) => { + let ast = imp::modules(root.borrowed()) + .find(|(name, _)| name == &tree.links[self].name) + .unwrap() + .1; + ast.owned() + } + ModuleSourceNode::Module(it) => it, + } + } +} + +#[derive(Debug, PartialEq, Eq, Hash)] +pub(crate) struct ModuleData { + source: ModuleSource, + parent: Option, + children: Vec, +} + +impl ModuleSource { + pub(crate) fn new_inline( + db: &impl HirDatabase, + file_id: FileId, + module: ast::Module, + ) -> ModuleSource { + assert!(!module.has_semi()); + let items = db.file_items(file_id); + let item_id = items.id_of(module.syntax()); + let id = SourceItemId { file_id, item_id }; + ModuleSource::Module(id) + } + + pub(crate) fn as_file(self) -> Option { + match self { + ModuleSource::SourceFile(f) => Some(f), + ModuleSource::Module(..) => None, + } + } + + pub(crate) fn file_id(self) -> FileId { + match self { + ModuleSource::SourceFile(f) => f, + ModuleSource::Module(source_item_id) => source_item_id.file_id, + } + } + + pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { + match self { + ModuleSource::SourceFile(file_id) => { + let syntax = db.source_file(file_id); + ModuleSourceNode::SourceFile(syntax.ast().owned()) + } + ModuleSource::Module(item_id) => { + let syntax = db.file_item(item_id); + let syntax = syntax.borrowed(); + let module = ast::Module::cast(syntax).unwrap(); + ModuleSourceNode::Module(module.owned()) + } + } + } +} + +#[derive(Hash, Debug, PartialEq, Eq)] +struct LinkData { + owner: ModuleId, + name: SmolStr, + points_to: Vec, + problem: Option, +} + +impl ModuleTree { + fn push_mod(&mut self, data: ModuleData) -> ModuleId { + self.mods.alloc(data) + } + fn push_link(&mut self, data: LinkData) -> LinkId { + let owner = data.owner; + let id = self.links.alloc(data); + self.mods[owner].children.push(id); + id + } +} diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs new file mode 100644 index 000000000..513a37646 --- /dev/null +++ b/crates/ra_hir/src/module/nameres.rs @@ -0,0 +1,444 @@ +//! Name resolution algorithm. The end result of the algorithm is `ItemMap`: a +//! map with maps each module to it's scope: the set of items, visible in the +//! module. That is, we only resolve imports here, name resolution of item +//! bodies will be done in a separate step. +//! +//! Like Rustc, we use an interative per-crate algorithm: we start with scopes +//! containing only directly defined items, and then iteratively resolve +//! imports. +//! +//! To make this work nicely in the IDE scenarios, we place `InputModuleItems` +//! in between raw syntax and name resolution. `InputModuleItems` are computed +//! using only the module's syntax, and it is all directly defined items plus +//! imports. The plain is to make `InputModuleItems` independent of local +//! modifications (that is, typing inside a function shold not change IMIs), +//! such that the results of name resolution can be preserved unless the module +//! structure itself is modified. +use std::{ + sync::Arc, +}; + +use rustc_hash::FxHashMap; +use ra_syntax::{ + TextRange, + SmolStr, SyntaxKind::{self, *}, + ast::{self, AstNode} +}; +use ra_db::SourceRootId; + +use crate::{ + Cancelable, FileId, + DefId, DefLoc, + SourceItemId, SourceFileItemId, SourceFileItems, + Path, PathKind, + HirDatabase, + module::{ModuleId, ModuleTree}, +}; + +/// Item map is the result of the name resolution. Item map contains, for each +/// module, the set of visible items. +#[derive(Default, Debug, PartialEq, Eq)] +pub(crate) struct ItemMap { + pub(crate) per_module: FxHashMap, +} + +#[derive(Debug, Default, PartialEq, Eq, Clone)] +pub(crate) struct ModuleScope { + items: FxHashMap, +} + +impl ModuleScope { + pub(crate) fn entries<'a>(&'a self) -> impl Iterator + 'a { + self.items.iter() + } + pub(crate) fn get(&self, name: &SmolStr) -> Option<&Resolution> { + self.items.get(name) + } +} + +/// A set of items and imports declared inside a module, without relation to +/// other modules. +/// +/// This stands in-between raw syntax and name resolution and alow us to avoid +/// recomputing name res: if `InputModuleItems` are the same, we can avoid +/// running name resolution. +#[derive(Debug, Default, PartialEq, Eq)] +pub(crate) struct InputModuleItems { + items: Vec, + imports: Vec, +} + +#[derive(Debug, PartialEq, Eq)] +struct ModuleItem { + id: SourceFileItemId, + name: SmolStr, + kind: SyntaxKind, + vis: Vis, +} + +#[derive(Debug, PartialEq, Eq)] +enum Vis { + // Priv, + Other, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct Import { + path: Path, + kind: ImportKind, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct NamedImport { + file_item_id: SourceFileItemId, + relative_range: TextRange, +} + +impl NamedImport { + pub(crate) fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange { + let source_item_id = SourceItemId { + file_id, + item_id: self.file_item_id, + }; + let syntax = db.file_item(source_item_id); + let offset = syntax.borrowed().range().start(); + self.relative_range + offset + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +enum ImportKind { + Glob, + Named(NamedImport), +} + +/// Resolution is basically `DefId` atm, but it should account for stuff like +/// multiple namespaces, ambiguity and errors. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct Resolution { + /// None for unresolved + pub(crate) def_id: Option, + /// ident by whitch this is imported into local scope. + pub(crate) import: Option, +} + +// #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +// enum Namespace { +// Types, +// Values, +// } + +// #[derive(Debug)] +// struct PerNs { +// types: Option, +// values: Option, +// } + +impl InputModuleItems { + pub(crate) fn new<'a>( + file_items: &SourceFileItems, + items: impl Iterator>, + ) -> InputModuleItems { + let mut res = InputModuleItems::default(); + for item in items { + res.add_item(file_items, item); + } + res + } + + fn add_item(&mut self, file_items: &SourceFileItems, item: ast::ModuleItem) -> Option<()> { + match item { + ast::ModuleItem::StructDef(it) => self.items.push(ModuleItem::new(file_items, it)?), + ast::ModuleItem::EnumDef(it) => self.items.push(ModuleItem::new(file_items, it)?), + ast::ModuleItem::FnDef(it) => self.items.push(ModuleItem::new(file_items, it)?), + ast::ModuleItem::TraitDef(it) => self.items.push(ModuleItem::new(file_items, it)?), + ast::ModuleItem::TypeDef(it) => self.items.push(ModuleItem::new(file_items, it)?), + ast::ModuleItem::ImplItem(_) => { + // impls don't define items + } + ast::ModuleItem::UseItem(it) => self.add_use_item(file_items, it), + ast::ModuleItem::ExternCrateItem(_) => { + // TODO + } + ast::ModuleItem::ConstDef(it) => self.items.push(ModuleItem::new(file_items, it)?), + ast::ModuleItem::StaticDef(it) => self.items.push(ModuleItem::new(file_items, it)?), + ast::ModuleItem::Module(it) => self.items.push(ModuleItem::new(file_items, it)?), + } + Some(()) + } + + fn add_use_item(&mut self, file_items: &SourceFileItems, item: ast::UseItem) { + let file_item_id = file_items.id_of(item.syntax()); + let start_offset = item.syntax().range().start(); + Path::expand_use_item(item, |path, range| { + let kind = match range { + None => ImportKind::Glob, + Some(range) => ImportKind::Named(NamedImport { + file_item_id, + relative_range: range - start_offset, + }), + }; + self.imports.push(Import { kind, path }) + }) + } +} + +impl ModuleItem { + fn new<'a>(file_items: &SourceFileItems, item: impl ast::NameOwner<'a>) -> Option { + let name = item.name()?.text(); + let kind = item.syntax().kind(); + let vis = Vis::Other; + let id = file_items.id_of(item.syntax()); + let res = ModuleItem { + id, + name, + kind, + vis, + }; + Some(res) + } +} + +pub(crate) struct Resolver<'a, DB> { + pub db: &'a DB, + pub input: &'a FxHashMap>, + pub source_root: SourceRootId, + pub module_tree: Arc, + pub result: ItemMap, +} + +impl<'a, DB> Resolver<'a, DB> +where + DB: HirDatabase, +{ + pub(crate) fn resolve(mut self) -> Cancelable { + for (&module_id, items) in self.input.iter() { + self.populate_module(module_id, items) + } + + for &module_id in self.input.keys() { + self.db.check_canceled()?; + self.resolve_imports(module_id); + } + Ok(self.result) + } + + fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) { + let file_id = module_id.source(&self.module_tree).file_id(); + + let mut module_items = ModuleScope::default(); + + for import in input.imports.iter() { + if let Some(name) = import.path.segments.iter().last() { + if let ImportKind::Named(import) = import.kind { + module_items.items.insert( + name.clone(), + Resolution { + def_id: None, + import: Some(import), + }, + ); + } + } + } + + for item in input.items.iter() { + if item.kind == MODULE { + // handle submodules separatelly + continue; + } + let def_loc = DefLoc::Item { + source_item_id: SourceItemId { + file_id, + item_id: item.id, + }, + }; + let def_id = def_loc.id(self.db); + let resolution = Resolution { + def_id: Some(def_id), + import: None, + }; + module_items.items.insert(item.name.clone(), resolution); + } + + for (name, mod_id) in module_id.children(&self.module_tree) { + let def_loc = DefLoc::Module { + id: mod_id, + source_root: self.source_root, + }; + let def_id = def_loc.id(self.db); + let resolution = Resolution { + def_id: Some(def_id), + import: None, + }; + module_items.items.insert(name, resolution); + } + + self.result.per_module.insert(module_id, module_items); + } + + fn resolve_imports(&mut self, module_id: ModuleId) { + for import in self.input[&module_id].imports.iter() { + self.resolve_import(module_id, import); + } + } + + fn resolve_import(&mut self, module_id: ModuleId, import: &Import) { + let ptr = match import.kind { + ImportKind::Glob => return, + ImportKind::Named(ptr) => ptr, + }; + + let mut curr = match import.path.kind { + // TODO: handle extern crates + PathKind::Plain => return, + PathKind::Self_ => module_id, + PathKind::Super => { + match module_id.parent(&self.module_tree) { + Some(it) => it, + // TODO: error + None => return, + } + } + PathKind::Crate => module_id.crate_root(&self.module_tree), + }; + + for (i, name) in import.path.segments.iter().enumerate() { + let is_last = i == import.path.segments.len() - 1; + + let def_id = match self.result.per_module[&curr].items.get(name) { + None => return, + Some(res) => match res.def_id { + Some(it) => it, + None => return, + }, + }; + + if !is_last { + curr = match def_id.loc(self.db) { + DefLoc::Module { id, .. } => id, + _ => return, + } + } else { + self.update(module_id, |items| { + let res = Resolution { + def_id: Some(def_id), + import: Some(ptr), + }; + items.items.insert(name.clone(), res); + }) + } + } + } + + fn update(&mut self, module_id: ModuleId, f: impl FnOnce(&mut ModuleScope)) { + let module_items = self.result.per_module.get_mut(&module_id).unwrap(); + f(module_items) + } +} + +#[cfg(test)] +mod tests { + use ra_db::FilesDatabase; + use crate::{ + AnalysisChange, + mock_analysis::{MockAnalysis, analysis_and_position}, + hir::{self, HirDatabase}, +}; + use super::*; + + fn item_map(fixture: &str) -> (Arc, ModuleId) { + let (analysis, pos) = analysis_and_position(fixture); + let db = analysis.imp.db; + let source_root = db.file_source_root(pos.file_id); + let descr = hir::Module::guess_from_position(&*db, pos) + .unwrap() + .unwrap(); + let module_id = descr.module_id; + (db.item_map(source_root).unwrap(), module_id) + } + + #[test] + fn test_item_map() { + let (item_map, module_id) = item_map( + " + //- /lib.rs + mod foo; + + use crate::foo::bar::Baz; + <|> + + //- /foo/mod.rs + pub mod bar; + + //- /foo/bar.rs + pub struct Baz; + ", + ); + let name = SmolStr::from("Baz"); + let resolution = &item_map.per_module[&module_id].items[&name]; + assert!(resolution.def_id.is_some()); + } + + #[test] + fn typing_inside_a_function_should_not_invalidate_item_map() { + let mock_analysis = MockAnalysis::with_files( + " + //- /lib.rs + mod foo; + + use crate::foo::bar::Baz; + + fn foo() -> i32 { + 1 + 1 + } + //- /foo/mod.rs + pub mod bar; + + //- /foo/bar.rs + pub struct Baz; + ", + ); + + let file_id = mock_analysis.id_of("/lib.rs"); + let mut host = mock_analysis.analysis_host(); + + let source_root = host.analysis().imp.db.file_source_root(file_id); + + { + let db = host.analysis().imp.db; + let events = db.log_executed(|| { + db.item_map(source_root).unwrap(); + }); + assert!(format!("{:?}", events).contains("item_map")) + } + + let mut change = AnalysisChange::new(); + + change.change_file( + file_id, + " + mod foo; + + use crate::foo::bar::Baz; + + fn foo() -> i32 { 92 } + " + .to_string(), + ); + + host.apply_change(change); + + { + let db = host.analysis().imp.db; + let events = db.log_executed(|| { + db.item_map(source_root).unwrap(); + }); + assert!( + !format!("{:?}", events).contains("_item_map"), + "{:#?}", + events + ) + } + } +} diff --git a/crates/ra_hir/src/path.rs b/crates/ra_hir/src/path.rs new file mode 100644 index 000000000..8279daf4b --- /dev/null +++ b/crates/ra_hir/src/path.rs @@ -0,0 +1,148 @@ +use ra_syntax::{SmolStr, ast, AstNode, TextRange}; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct Path { + pub(crate) kind: PathKind, + pub(crate) segments: Vec, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum PathKind { + Plain, + Self_, + Super, + Crate, +} + +impl Path { + /// Calls `cb` with all paths, represented by this use item. + pub(crate) fn expand_use_item(item: ast::UseItem, mut cb: impl FnMut(Path, Option)) { + if let Some(tree) = item.use_tree() { + expand_use_tree(None, tree, &mut cb); + } + } + + /// Converts an `ast::Path` to `Path`. Works with use trees. + pub(crate) fn from_ast(mut path: ast::Path) -> Option { + let mut kind = PathKind::Plain; + let mut segments = Vec::new(); + loop { + let segment = path.segment()?; + match segment.kind()? { + ast::PathSegmentKind::Name(name) => segments.push(name.text()), + ast::PathSegmentKind::CrateKw => { + kind = PathKind::Crate; + break; + } + ast::PathSegmentKind::SelfKw => { + kind = PathKind::Self_; + break; + } + ast::PathSegmentKind::SuperKw => { + kind = PathKind::Super; + break; + } + } + path = match qualifier(path) { + Some(it) => it, + None => break, + }; + } + segments.reverse(); + return Some(Path { kind, segments }); + + fn qualifier(path: ast::Path) -> Option { + if let Some(q) = path.qualifier() { + return Some(q); + } + // TODO: this bottom up traversal is not too precise. + // Should we handle do a top-down analysiss, recording results? + let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?; + let use_tree = use_tree_list.parent_use_tree(); + use_tree.path() + } + } + + /// `true` is this path is a single identifier, like `foo` + pub(crate) fn is_ident(&self) -> bool { + self.kind == PathKind::Plain && self.segments.len() == 1 + } +} + +fn expand_use_tree( + prefix: Option, + tree: ast::UseTree, + cb: &mut impl FnMut(Path, Option), +) { + if let Some(use_tree_list) = tree.use_tree_list() { + let prefix = match tree.path() { + None => prefix, + Some(path) => match convert_path(prefix, path) { + Some(it) => Some(it), + None => return, // TODO: report errors somewhere + }, + }; + for tree in use_tree_list.use_trees() { + expand_use_tree(prefix.clone(), tree, cb); + } + } else { + if let Some(ast_path) = tree.path() { + if let Some(path) = convert_path(prefix, ast_path) { + let range = if tree.has_star() { + None + } else { + let range = ast_path.segment().unwrap().syntax().range(); + Some(range) + }; + cb(path, range) + } + } + } +} + +fn convert_path(prefix: Option, path: ast::Path) -> Option { + let prefix = if let Some(qual) = path.qualifier() { + Some(convert_path(prefix, qual)?) + } else { + None + }; + let segment = path.segment()?; + let res = match segment.kind()? { + ast::PathSegmentKind::Name(name) => { + let mut res = prefix.unwrap_or_else(|| Path { + kind: PathKind::Plain, + segments: Vec::with_capacity(1), + }); + res.segments.push(name.text()); + res + } + ast::PathSegmentKind::CrateKw => { + if prefix.is_some() { + return None; + } + Path { + kind: PathKind::Crate, + segments: Vec::new(), + } + } + ast::PathSegmentKind::SelfKw => { + if prefix.is_some() { + return None; + } + Path { + kind: PathKind::Self_, + segments: Vec::new(), + } + } + ast::PathSegmentKind::SuperKw => { + if prefix.is_some() { + return None; + } + Path { + kind: PathKind::Super, + segments: Vec::new(), + } + } + }; + Some(res) +} diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs new file mode 100644 index 000000000..6f602878c --- /dev/null +++ b/crates/ra_hir/src/query_definitions.rs @@ -0,0 +1,154 @@ +use std::{ + sync::Arc, + time::Instant, +}; + +use rustc_hash::FxHashMap; +use ra_syntax::{ + AstNode, SyntaxNode, SmolStr, + ast::{self, FnDef, FnDefNode, NameOwner, ModuleItemOwner} +}; +use ra_db::{SourceRootId, FileId, Cancelable,}; + +use crate::{ + FnId, + SourceFileItems, SourceItemId, + db::HirDatabase, + function::FnScopes, + module::{ + ModuleSource, ModuleSourceNode, ModuleId, + imp::Submodule, + nameres::{InputModuleItems, ItemMap, Resolver}, + }, +}; + +/// Resolve `FnId` to the corresponding `SyntaxNode` +pub(super) fn fn_syntax(db: &impl HirDatabase, fn_id: FnId) -> FnDefNode { + let item_id = fn_id.loc(db); + let syntax = db.file_item(item_id); + FnDef::cast(syntax.borrowed()).unwrap().owned() +} + +pub(super) fn fn_scopes(db: &impl HirDatabase, fn_id: FnId) -> Arc { + let syntax = db.fn_syntax(fn_id); + let res = FnScopes::new(syntax.borrowed()); + Arc::new(res) +} + +pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc { + let source_file = db.source_file(file_id); + let source_file = source_file.borrowed(); + let mut res = SourceFileItems::default(); + source_file + .syntax() + .descendants() + .filter_map(ast::ModuleItem::cast) + .map(|it| it.syntax().owned()) + .for_each(|it| { + res.alloc(it); + }); + Arc::new(res) +} + +pub(super) fn file_item(db: &impl HirDatabase, source_item_id: SourceItemId) -> SyntaxNode { + db.file_items(source_item_id.file_id)[source_item_id.item_id].clone() +} + +pub(crate) fn submodules( + db: &impl HirDatabase, + source: ModuleSource, +) -> Cancelable>> { + db.check_canceled()?; + let file_id = source.file_id(); + let submodules = match source.resolve(db) { + ModuleSourceNode::SourceFile(it) => collect_submodules(db, file_id, it.borrowed()), + ModuleSourceNode::Module(it) => it + .borrowed() + .item_list() + .map(|it| collect_submodules(db, file_id, it)) + .unwrap_or_else(Vec::new), + }; + return Ok(Arc::new(submodules)); + + fn collect_submodules<'a>( + db: &impl HirDatabase, + file_id: FileId, + root: impl ast::ModuleItemOwner<'a>, + ) -> Vec { + modules(root) + .map(|(name, m)| { + if m.has_semi() { + Submodule::Declaration(name) + } else { + let src = ModuleSource::new_inline(db, file_id, m); + Submodule::Definition(name, src) + } + }) + .collect() + } +} + +pub(crate) fn modules<'a>( + root: impl ast::ModuleItemOwner<'a>, +) -> impl Iterator)> { + root.items() + .filter_map(|item| match item { + ast::ModuleItem::Module(m) => Some(m), + _ => None, + }) + .filter_map(|module| { + let name = module.name()?.text(); + Some((name, module)) + }) +} + +pub(super) fn input_module_items( + db: &impl HirDatabase, + source_root: SourceRootId, + module_id: ModuleId, +) -> Cancelable> { + let module_tree = db.module_tree(source_root)?; + let source = module_id.source(&module_tree); + let file_items = db.file_items(source.file_id()); + let res = match source.resolve(db) { + ModuleSourceNode::SourceFile(it) => { + let items = it.borrowed().items(); + InputModuleItems::new(&file_items, items) + } + ModuleSourceNode::Module(it) => { + let items = it + .borrowed() + .item_list() + .into_iter() + .flat_map(|it| it.items()); + InputModuleItems::new(&file_items, items) + } + }; + Ok(Arc::new(res)) +} + +pub(super) fn item_map( + db: &impl HirDatabase, + source_root: SourceRootId, +) -> Cancelable> { + let start = Instant::now(); + let module_tree = db.module_tree(source_root)?; + let input = module_tree + .modules() + .map(|id| { + let items = db.input_module_items(source_root, id)?; + Ok((id, items)) + }) + .collect::>>()?; + let resolver = Resolver { + db: db, + input: &input, + source_root, + module_tree, + result: ItemMap::default(), + }; + let res = resolver.resolve()?; + let elapsed = start.elapsed(); + log::info!("item_map: {:?}", elapsed); + Ok(Arc::new(res)) +} -- cgit v1.2.3 From 59e29aef633e906837f8fed604435976a46be691 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 04:09:44 +0300 Subject: Move hir to a separate crate --- crates/ra_analysis/Cargo.toml | 2 +- crates/ra_analysis/src/arena.rs | 66 --- crates/ra_analysis/src/completion/mod.rs | 1 - .../src/completion/reference_completion.rs | 12 +- crates/ra_analysis/src/db.rs | 2 +- crates/ra_analysis/src/hir/db.rs | 70 ---- crates/ra_analysis/src/hir/function/mod.rs | 190 --------- crates/ra_analysis/src/hir/function/scope.rs | 451 --------------------- crates/ra_analysis/src/hir/mod.rs | 131 ------ crates/ra_analysis/src/hir/module/imp.rs | 195 --------- crates/ra_analysis/src/hir/module/mod.rs | 379 ----------------- crates/ra_analysis/src/hir/module/nameres.rs | 446 -------------------- crates/ra_analysis/src/hir/path.rs | 148 ------- crates/ra_analysis/src/hir/query_definitions.rs | 157 ------- crates/ra_analysis/src/imp.rs | 10 +- crates/ra_analysis/src/lib.rs | 122 +++++- crates/ra_hir/src/arena.rs | 2 +- crates/ra_hir/src/db.rs | 4 +- crates/ra_hir/src/function/mod.rs | 16 +- crates/ra_hir/src/function/scope.rs | 17 +- crates/ra_hir/src/lib.rs | 30 +- crates/ra_hir/src/module/imp.rs | 2 +- crates/ra_hir/src/module/mod.rs | 31 +- crates/ra_hir/src/module/nameres.rs | 134 +----- crates/ra_hir/src/path.rs | 14 +- 25 files changed, 201 insertions(+), 2431 deletions(-) delete mode 100644 crates/ra_analysis/src/arena.rs delete mode 100644 crates/ra_analysis/src/hir/db.rs delete mode 100644 crates/ra_analysis/src/hir/function/mod.rs delete mode 100644 crates/ra_analysis/src/hir/function/scope.rs delete mode 100644 crates/ra_analysis/src/hir/mod.rs delete mode 100644 crates/ra_analysis/src/hir/module/imp.rs delete mode 100644 crates/ra_analysis/src/hir/module/mod.rs delete mode 100644 crates/ra_analysis/src/hir/module/nameres.rs delete mode 100644 crates/ra_analysis/src/hir/path.rs delete mode 100644 crates/ra_analysis/src/hir/query_definitions.rs (limited to 'crates') diff --git a/crates/ra_analysis/Cargo.toml b/crates/ra_analysis/Cargo.toml index 48d8e56e3..fe9765a66 100644 --- a/crates/ra_analysis/Cargo.toml +++ b/crates/ra_analysis/Cargo.toml @@ -12,8 +12,8 @@ fst = "0.3.1" salsa = "0.8.0" rustc-hash = "1.0" parking_lot = "0.6.4" -id-arena = { git = "https://github.com/fitzgen/id-arena/", rev = "43ecd67" } ra_syntax = { path = "../ra_syntax" } ra_editor = { path = "../ra_editor" } ra_db = { path = "../ra_db" } +hir = { path = "../ra_hir", package = "ra_hir" } test_utils = { path = "../test_utils" } diff --git a/crates/ra_analysis/src/arena.rs b/crates/ra_analysis/src/arena.rs deleted file mode 100644 index a752ec0c1..000000000 --- a/crates/ra_analysis/src/arena.rs +++ /dev/null @@ -1,66 +0,0 @@ -//! A simple id-based arena, similar to https://github.com/fitzgen/id-arena. -//! We use our own version for more compact id's and to allow inherent impls -//! on Ids. - -use std::{ - fmt, - hash::{Hash, Hasher}, - marker::PhantomData, -}; - -pub(crate) struct Id { - idx: u32, - _ty: PhantomData T>, -} - -impl fmt::Debug for Id { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_tuple("Id").field(&self.idx).finish() - } -} -impl Copy for Id {} -impl Clone for Id { - fn clone(&self) -> Id { - *self - } -} - -impl PartialEq for Id { - fn eq(&self, other: &Id) -> bool { - self.idx == other.idx - } -} - -impl Eq for Id {} - -impl Hash for Id { - fn hash(&self, h: &mut H) { - self.idx.hash(h); - } -} - -#[derive(Debug, PartialEq, Eq)] -pub(crate) struct ArenaBehavior { - _ty: PhantomData, -} - -impl id_arena::ArenaBehavior for ArenaBehavior { - type Id = Id; - fn new_arena_id() -> usize { - 0 - } - fn new_id(_arena_id: usize, index: usize) -> Id { - Id { - idx: index as u32, - _ty: PhantomData, - } - } - fn index(id: Id) -> usize { - id.idx as usize - } - fn arena_id(_id: Id) -> usize { - 0 - } -} - -pub(crate) type Arena = id_arena::Arena>; diff --git a/crates/ra_analysis/src/completion/mod.rs b/crates/ra_analysis/src/completion/mod.rs index 538b51633..e5ba92acd 100644 --- a/crates/ra_analysis/src/completion/mod.rs +++ b/crates/ra_analysis/src/completion/mod.rs @@ -12,7 +12,6 @@ use rustc_hash::{FxHashMap}; use crate::{ db, - hir, Cancelable, FilePosition }; diff --git a/crates/ra_analysis/src/completion/reference_completion.rs b/crates/ra_analysis/src/completion/reference_completion.rs index 881d29916..e1a2d5241 100644 --- a/crates/ra_analysis/src/completion/reference_completion.rs +++ b/crates/ra_analysis/src/completion/reference_completion.rs @@ -6,16 +6,16 @@ use ra_syntax::{ ast::{self, LoopBodyOwner}, SyntaxKind::*, }; - -use crate::{ - db::RootDatabase, - completion::CompletionItem, - hir::{ +use hir::{ self, FnScopes, Def, Path, - }, +}; + +use crate::{ + db::RootDatabase, + completion::CompletionItem, Cancelable }; diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index 2bc1c8f8f..7fc3fe31b 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -3,9 +3,9 @@ use std::sync::Arc; use parking_lot::Mutex; use salsa::{self, Database}; use ra_db::{LocationIntener, BaseDatabase}; +use hir::{self, DefId, DefLoc, FnId, SourceItemId}; use crate::{ - hir::{self, DefId, DefLoc, FnId, SourceItemId}, symbol_index, }; diff --git a/crates/ra_analysis/src/hir/db.rs b/crates/ra_analysis/src/hir/db.rs deleted file mode 100644 index c8ae551c5..000000000 --- a/crates/ra_analysis/src/hir/db.rs +++ /dev/null @@ -1,70 +0,0 @@ -use std::sync::Arc; - -use ra_syntax::{ - SyntaxNode, - ast::FnDefNode, -}; -use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase}; - -use crate::{ - FileId, - hir::{ - DefLoc, DefId, FnId, - SourceFileItems, SourceItemId, - query_definitions, - function::{FnScopes}, - module::{ModuleId, ModuleTree, ModuleSource, - nameres::{ItemMap, InputModuleItems}}, - }, - Cancelable, -}; - -salsa::query_group! { - -pub(crate) trait HirDatabase: SyntaxDatabase - + AsRef> - + AsRef> -{ - fn fn_scopes(fn_id: FnId) -> Arc { - type FnScopesQuery; - use fn query_definitions::fn_scopes; - } - fn fn_syntax(fn_id: FnId) -> FnDefNode { - type FnSyntaxQuery; - // Don't retain syntax trees in memory - storage dependencies; - use fn query_definitions::fn_syntax; - } - - fn file_items(file_id: FileId) -> Arc { - type SourceFileItemsQuery; - storage dependencies; - use fn query_definitions::file_items; - } - - fn file_item(source_item_id: SourceItemId) -> SyntaxNode { - type FileItemQuery; - storage dependencies; - use fn query_definitions::file_item; - } - - fn submodules(source: ModuleSource) -> Cancelable>> { - type SubmodulesQuery; - use fn query_definitions::submodules; - } - - fn input_module_items(source_root_id: SourceRootId, module_id: ModuleId) -> Cancelable> { - type InputModuleItemsQuery; - use fn query_definitions::input_module_items; - } - fn item_map(source_root_id: SourceRootId) -> Cancelable> { - type ItemMapQuery; - use fn query_definitions::item_map; - } - fn module_tree(source_root_id: SourceRootId) -> Cancelable> { - type ModuleTreeQuery; - use fn crate::hir::module::imp::module_tree; - } -} - -} diff --git a/crates/ra_analysis/src/hir/function/mod.rs b/crates/ra_analysis/src/hir/function/mod.rs deleted file mode 100644 index a399d2a9e..000000000 --- a/crates/ra_analysis/src/hir/function/mod.rs +++ /dev/null @@ -1,190 +0,0 @@ -mod scope; - -use std::{ - cmp::{max, min}, - sync::Arc, -}; - -use ra_syntax::{ - TextRange, TextUnit, SyntaxNodeRef, - ast::{self, AstNode, DocCommentsOwner, NameOwner}, -}; - -use crate::{ - hir::{FnId, HirDatabase, SourceItemId}, - FileId, -}; - -pub(crate) use self::scope::FnScopes; - -impl FnId { - pub(crate) fn get(db: &impl HirDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId { - let file_items = db.file_items(file_id); - let item_id = file_items.id_of(fn_def.syntax()); - let item_id = SourceItemId { file_id, item_id }; - FnId::from_loc(db, &item_id) - } -} - -pub(crate) struct Function { - fn_id: FnId, -} - -impl Function { - pub(crate) fn guess_from_source( - db: &impl HirDatabase, - file_id: FileId, - fn_def: ast::FnDef, - ) -> Function { - let fn_id = FnId::get(db, file_id, fn_def); - Function { fn_id } - } - - pub(crate) fn guess_for_name_ref( - db: &impl HirDatabase, - file_id: FileId, - name_ref: ast::NameRef, - ) -> Option { - Function::guess_for_node(db, file_id, name_ref.syntax()) - } - - pub(crate) fn guess_for_bind_pat( - db: &impl HirDatabase, - file_id: FileId, - bind_pat: ast::BindPat, - ) -> Option { - Function::guess_for_node(db, file_id, bind_pat.syntax()) - } - - fn guess_for_node( - db: &impl HirDatabase, - file_id: FileId, - node: SyntaxNodeRef, - ) -> Option { - let fn_def = node.ancestors().find_map(ast::FnDef::cast)?; - let res = Function::guess_from_source(db, file_id, fn_def); - Some(res) - } - - pub(crate) fn scope(&self, db: &impl HirDatabase) -> Arc { - db.fn_scopes(self.fn_id) - } - - pub(crate) fn signature_info(&self, db: &impl HirDatabase) -> Option { - let syntax = db.fn_syntax(self.fn_id); - FnSignatureInfo::new(syntax.borrowed()) - } -} - -#[derive(Debug, Clone)] -pub struct FnSignatureInfo { - pub name: String, - pub label: String, - pub ret_type: Option, - pub params: Vec, - pub doc: Option, -} - -impl FnSignatureInfo { - fn new(node: ast::FnDef) -> Option { - let name = node.name()?.text().to_string(); - - let mut doc = None; - - // Strip the body out for the label. - let mut label: String = if let Some(body) = node.body() { - let body_range = body.syntax().range(); - let label: String = node - .syntax() - .children() - .filter(|child| !child.range().is_subrange(&body_range)) - .map(|node| node.text().to_string()) - .collect(); - label - } else { - node.syntax().text().to_string() - }; - - if let Some((comment_range, docs)) = FnSignatureInfo::extract_doc_comments(node) { - let comment_range = comment_range - .checked_sub(node.syntax().range().start()) - .unwrap(); - let start = comment_range.start().to_usize(); - let end = comment_range.end().to_usize(); - - // Remove the comment from the label - label.replace_range(start..end, ""); - - // Massage markdown - let mut processed_lines = Vec::new(); - let mut in_code_block = false; - for line in docs.lines() { - if line.starts_with("```") { - in_code_block = !in_code_block; - } - - let line = if in_code_block && line.starts_with("```") && !line.contains("rust") { - "```rust".into() - } else { - line.to_string() - }; - - processed_lines.push(line); - } - - if !processed_lines.is_empty() { - doc = Some(processed_lines.join("\n")); - } - } - - let params = FnSignatureInfo::param_list(node); - let ret_type = node.ret_type().map(|r| r.syntax().text().to_string()); - - Some(FnSignatureInfo { - name, - ret_type, - params, - label: label.trim().to_owned(), - doc, - }) - } - - fn extract_doc_comments(node: ast::FnDef) -> Option<(TextRange, String)> { - if node.doc_comments().count() == 0 { - return None; - } - - let comment_text = node.doc_comment_text(); - - let (begin, end) = node - .doc_comments() - .map(|comment| comment.syntax().range()) - .map(|range| (range.start().to_usize(), range.end().to_usize())) - .fold((std::usize::MAX, std::usize::MIN), |acc, range| { - (min(acc.0, range.0), max(acc.1, range.1)) - }); - - let range = TextRange::from_to(TextUnit::from_usize(begin), TextUnit::from_usize(end)); - - Some((range, comment_text)) - } - - fn param_list(node: ast::FnDef) -> Vec { - let mut res = vec![]; - if let Some(param_list) = node.param_list() { - if let Some(self_param) = param_list.self_param() { - res.push(self_param.syntax().text().to_string()) - } - - // Maybe use param.pat here? See if we can just extract the name? - //res.extend(param_list.params().map(|p| p.syntax().text().to_string())); - res.extend( - param_list - .params() - .filter_map(|p| p.pat()) - .map(|pat| pat.syntax().text().to_string()), - ); - } - res - } -} diff --git a/crates/ra_analysis/src/hir/function/scope.rs b/crates/ra_analysis/src/hir/function/scope.rs deleted file mode 100644 index ed789fede..000000000 --- a/crates/ra_analysis/src/hir/function/scope.rs +++ /dev/null @@ -1,451 +0,0 @@ -use rustc_hash::{FxHashMap, FxHashSet}; - -use ra_syntax::{ - AstNode, SmolStr, SyntaxNodeRef, TextRange, - algo::generate, - ast::{self, ArgListOwner, LoopBodyOwner, NameOwner}, -}; -use ra_db::LocalSyntaxPtr; - -use crate::{ - - arena::{Arena, Id}, -}; - -pub(crate) type ScopeId = Id; - -#[derive(Debug, PartialEq, Eq)] -pub struct FnScopes { - pub(crate) self_param: Option, - scopes: Arena, - scope_for: FxHashMap, -} - -#[derive(Debug, PartialEq, Eq)] -pub struct ScopeEntry { - name: SmolStr, - ptr: LocalSyntaxPtr, -} - -#[derive(Debug, PartialEq, Eq)] -pub(crate) struct ScopeData { - parent: Option, - entries: Vec, -} - -impl FnScopes { - pub(crate) fn new(fn_def: ast::FnDef) -> FnScopes { - let mut scopes = FnScopes { - self_param: fn_def - .param_list() - .and_then(|it| it.self_param()) - .map(|it| LocalSyntaxPtr::new(it.syntax())), - scopes: Arena::default(), - scope_for: FxHashMap::default(), - }; - let root = scopes.root_scope(); - scopes.add_params_bindings(root, fn_def.param_list()); - if let Some(body) = fn_def.body() { - compute_block_scopes(body, &mut scopes, root) - } - scopes - } - pub(crate) fn entries(&self, scope: ScopeId) -> &[ScopeEntry] { - &self.scopes[scope].entries - } - pub fn scope_chain<'a>(&'a self, node: SyntaxNodeRef) -> impl Iterator + 'a { - generate(self.scope_for(node), move |&scope| { - self.scopes[scope].parent - }) - } - pub(crate) fn resolve_local_name<'a>( - &'a self, - name_ref: ast::NameRef, - ) -> Option<&'a ScopeEntry> { - let mut shadowed = FxHashSet::default(); - let ret = self - .scope_chain(name_ref.syntax()) - .flat_map(|scope| self.entries(scope).iter()) - .filter(|entry| shadowed.insert(entry.name())) - .filter(|entry| entry.name() == &name_ref.text()) - .nth(0); - ret - } - - pub fn find_all_refs(&self, pat: ast::BindPat) -> Vec { - let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); - let name_ptr = LocalSyntaxPtr::new(pat.syntax()); - let refs: Vec<_> = fn_def - .syntax() - .descendants() - .filter_map(ast::NameRef::cast) - .filter(|name_ref| match self.resolve_local_name(*name_ref) { - None => false, - Some(entry) => entry.ptr() == name_ptr, - }) - .map(|name_ref| ReferenceDescriptor { - name: name_ref.syntax().text().to_string(), - range: name_ref.syntax().range(), - }) - .collect(); - - refs - } - - fn root_scope(&mut self) -> ScopeId { - self.scopes.alloc(ScopeData { - parent: None, - entries: vec![], - }) - } - fn new_scope(&mut self, parent: ScopeId) -> ScopeId { - self.scopes.alloc(ScopeData { - parent: Some(parent), - entries: vec![], - }) - } - fn add_bindings(&mut self, scope: ScopeId, pat: ast::Pat) { - let entries = pat - .syntax() - .descendants() - .filter_map(ast::BindPat::cast) - .filter_map(ScopeEntry::new); - self.scopes[scope].entries.extend(entries); - } - fn add_params_bindings(&mut self, scope: ScopeId, params: Option) { - params - .into_iter() - .flat_map(|it| it.params()) - .filter_map(|it| it.pat()) - .for_each(|it| self.add_bindings(scope, it)); - } - fn set_scope(&mut self, node: SyntaxNodeRef, scope: ScopeId) { - self.scope_for.insert(LocalSyntaxPtr::new(node), scope); - } - fn scope_for(&self, node: SyntaxNodeRef) -> Option { - node.ancestors() - .map(LocalSyntaxPtr::new) - .filter_map(|it| self.scope_for.get(&it).map(|&scope| scope)) - .next() - } -} - -impl ScopeEntry { - fn new(pat: ast::BindPat) -> Option { - let name = pat.name()?; - let res = ScopeEntry { - name: name.text(), - ptr: LocalSyntaxPtr::new(pat.syntax()), - }; - Some(res) - } - pub(crate) fn name(&self) -> &SmolStr { - &self.name - } - pub(crate) fn ptr(&self) -> LocalSyntaxPtr { - self.ptr - } -} - -fn compute_block_scopes(block: ast::Block, scopes: &mut FnScopes, mut scope: ScopeId) { - for stmt in block.statements() { - match stmt { - ast::Stmt::LetStmt(stmt) => { - if let Some(expr) = stmt.initializer() { - scopes.set_scope(expr.syntax(), scope); - compute_expr_scopes(expr, scopes, scope); - } - scope = scopes.new_scope(scope); - if let Some(pat) = stmt.pat() { - scopes.add_bindings(scope, pat); - } - } - ast::Stmt::ExprStmt(expr_stmt) => { - if let Some(expr) = expr_stmt.expr() { - scopes.set_scope(expr.syntax(), scope); - compute_expr_scopes(expr, scopes, scope); - } - } - } - } - if let Some(expr) = block.expr() { - scopes.set_scope(expr.syntax(), scope); - compute_expr_scopes(expr, scopes, scope); - } -} - -fn compute_expr_scopes(expr: ast::Expr, scopes: &mut FnScopes, scope: ScopeId) { - match expr { - ast::Expr::IfExpr(e) => { - let cond_scope = e - .condition() - .and_then(|cond| compute_cond_scopes(cond, scopes, scope)); - if let Some(block) = e.then_branch() { - compute_block_scopes(block, scopes, cond_scope.unwrap_or(scope)); - } - if let Some(block) = e.else_branch() { - compute_block_scopes(block, scopes, scope); - } - } - ast::Expr::BlockExpr(e) => { - if let Some(block) = e.block() { - compute_block_scopes(block, scopes, scope); - } - } - ast::Expr::LoopExpr(e) => { - if let Some(block) = e.loop_body() { - compute_block_scopes(block, scopes, scope); - } - } - ast::Expr::WhileExpr(e) => { - let cond_scope = e - .condition() - .and_then(|cond| compute_cond_scopes(cond, scopes, scope)); - if let Some(block) = e.loop_body() { - compute_block_scopes(block, scopes, cond_scope.unwrap_or(scope)); - } - } - ast::Expr::ForExpr(e) => { - if let Some(expr) = e.iterable() { - compute_expr_scopes(expr, scopes, scope); - } - let mut scope = scope; - if let Some(pat) = e.pat() { - scope = scopes.new_scope(scope); - scopes.add_bindings(scope, pat); - } - if let Some(block) = e.loop_body() { - compute_block_scopes(block, scopes, scope); - } - } - ast::Expr::LambdaExpr(e) => { - let scope = scopes.new_scope(scope); - scopes.add_params_bindings(scope, e.param_list()); - if let Some(body) = e.body() { - scopes.set_scope(body.syntax(), scope); - compute_expr_scopes(body, scopes, scope); - } - } - ast::Expr::CallExpr(e) => { - compute_call_scopes(e.expr(), e.arg_list(), scopes, scope); - } - ast::Expr::MethodCallExpr(e) => { - compute_call_scopes(e.expr(), e.arg_list(), scopes, scope); - } - ast::Expr::MatchExpr(e) => { - if let Some(expr) = e.expr() { - compute_expr_scopes(expr, scopes, scope); - } - for arm in e.match_arm_list().into_iter().flat_map(|it| it.arms()) { - let scope = scopes.new_scope(scope); - for pat in arm.pats() { - scopes.add_bindings(scope, pat); - } - if let Some(expr) = arm.expr() { - compute_expr_scopes(expr, scopes, scope); - } - } - } - _ => expr - .syntax() - .children() - .filter_map(ast::Expr::cast) - .for_each(|expr| compute_expr_scopes(expr, scopes, scope)), - }; - - fn compute_call_scopes( - receiver: Option, - arg_list: Option, - scopes: &mut FnScopes, - scope: ScopeId, - ) { - arg_list - .into_iter() - .flat_map(|it| it.args()) - .chain(receiver) - .for_each(|expr| compute_expr_scopes(expr, scopes, scope)); - } - - fn compute_cond_scopes( - cond: ast::Condition, - scopes: &mut FnScopes, - scope: ScopeId, - ) -> Option { - if let Some(expr) = cond.expr() { - compute_expr_scopes(expr, scopes, scope); - } - if let Some(pat) = cond.pat() { - let s = scopes.new_scope(scope); - scopes.add_bindings(s, pat); - Some(s) - } else { - None - } - } -} - -#[derive(Debug)] -pub struct ReferenceDescriptor { - pub range: TextRange, - pub name: String, -} - -#[cfg(test)] -mod tests { - use ra_editor::find_node_at_offset; - use ra_syntax::SourceFileNode; - use test_utils::extract_offset; - - use super::*; - - fn do_check(code: &str, expected: &[&str]) { - let (off, code) = extract_offset(code); - let code = { - let mut buf = String::new(); - let off = u32::from(off) as usize; - buf.push_str(&code[..off]); - buf.push_str("marker"); - buf.push_str(&code[off..]); - buf - }; - let file = SourceFileNode::parse(&code); - let marker: ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); - let fn_def: ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); - let scopes = FnScopes::new(fn_def); - let actual = scopes - .scope_chain(marker.syntax()) - .flat_map(|scope| scopes.entries(scope)) - .map(|it| it.name()) - .collect::>(); - assert_eq!(actual.as_slice(), expected); - } - - #[test] - fn test_lambda_scope() { - do_check( - r" - fn quux(foo: i32) { - let f = |bar, baz: i32| { - <|> - }; - }", - &["bar", "baz", "foo"], - ); - } - - #[test] - fn test_call_scope() { - do_check( - r" - fn quux() { - f(|x| <|> ); - }", - &["x"], - ); - } - - #[test] - fn test_metod_call_scope() { - do_check( - r" - fn quux() { - z.f(|x| <|> ); - }", - &["x"], - ); - } - - #[test] - fn test_loop_scope() { - do_check( - r" - fn quux() { - loop { - let x = (); - <|> - }; - }", - &["x"], - ); - } - - #[test] - fn test_match() { - do_check( - r" - fn quux() { - match () { - Some(x) => { - <|> - } - }; - }", - &["x"], - ); - } - - #[test] - fn test_shadow_variable() { - do_check( - r" - fn foo(x: String) { - let x : &str = &x<|>; - }", - &["x"], - ); - } - - fn do_check_local_name(code: &str, expected_offset: u32) { - let (off, code) = extract_offset(code); - let file = SourceFileNode::parse(&code); - let fn_def: ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); - let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap(); - - let scopes = FnScopes::new(fn_def); - - let local_name_entry = scopes.resolve_local_name(name_ref).unwrap(); - let local_name = local_name_entry.ptr().resolve(&file); - let expected_name = - find_node_at_offset::(file.syntax(), expected_offset.into()).unwrap(); - assert_eq!(local_name.range(), expected_name.syntax().range()); - } - - #[test] - fn test_resolve_local_name() { - do_check_local_name( - r#" - fn foo(x: i32, y: u32) { - { - let z = x * 2; - } - { - let t = x<|> * 3; - } - }"#, - 21, - ); - } - - #[test] - fn test_resolve_local_name_declaration() { - do_check_local_name( - r#" - fn foo(x: String) { - let x : &str = &x<|>; - }"#, - 21, - ); - } - - #[test] - fn test_resolve_local_name_shadow() { - do_check_local_name( - r" - fn foo(x: String) { - let x : &str = &x; - x<|> - }", - 46, - ); - } -} diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs deleted file mode 100644 index 83131384d..000000000 --- a/crates/ra_analysis/src/hir/mod.rs +++ /dev/null @@ -1,131 +0,0 @@ -//! HIR (previsouly known as descriptors) provides a high-level OO acess to Rust -//! code. -//! -//! The principal difference between HIR and syntax trees is that HIR is bound -//! to a particular crate instance. That is, it has cfg flags and features -//! applied. So, there relation between syntax and HIR is many-to-one. - -pub(crate) mod db; -mod query_definitions; -mod function; -mod module; -mod path; - -use std::ops::Index; - -use ra_syntax::{SyntaxNodeRef, SyntaxNode}; -use ra_db::{LocationIntener, SourceRootId}; - -use crate::{ - FileId, - hir::db::HirDatabase, - Cancelable, - arena::{Arena, Id}, -}; - -pub(crate) use self::{ - path::{Path, PathKind}, - module::{Module, ModuleId, Problem}, - function::{Function, FnScopes}, -}; - -pub use self::function::FnSignatureInfo; - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(crate) struct FnId(u32); -ra_db::impl_numeric_id!(FnId); - -impl FnId { - pub(crate) fn from_loc( - db: &impl AsRef>, - loc: &SourceItemId, - ) -> FnId { - db.as_ref().loc2id(loc) - } - pub(crate) fn loc(self, db: &impl AsRef>) -> SourceItemId { - db.as_ref().id2loc(self) - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(crate) struct DefId(u32); -ra_db::impl_numeric_id!(DefId); - -#[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub(crate) enum DefLoc { - Module { - id: ModuleId, - source_root: SourceRootId, - }, - Item { - source_item_id: SourceItemId, - }, -} - -impl DefId { - pub(crate) fn loc(self, db: &impl AsRef>) -> DefLoc { - db.as_ref().id2loc(self) - } -} - -impl DefLoc { - pub(crate) fn id(&self, db: &impl AsRef>) -> DefId { - db.as_ref().loc2id(&self) - } -} - -pub(crate) enum Def { - Module(Module), - Item, -} - -impl DefId { - pub(crate) fn resolve(self, db: &impl HirDatabase) -> Cancelable { - let loc = self.loc(db); - let res = match loc { - DefLoc::Module { id, source_root } => { - let descr = Module::new(db, source_root, id)?; - Def::Module(descr) - } - DefLoc::Item { .. } => Def::Item, - }; - Ok(res) - } -} - -/// Identifier of item within a specific file. This is stable over reparses, so -/// it's OK to use it as a salsa key/value. -pub(crate) type SourceFileItemId = Id; - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(crate) struct SourceItemId { - file_id: FileId, - item_id: SourceFileItemId, -} - -/// Maps item's `SyntaxNode`s to `SourceFileItemId` and back. -#[derive(Debug, PartialEq, Eq, Default)] -pub(crate) struct SourceFileItems { - arena: Arena, -} - -impl SourceFileItems { - fn alloc(&mut self, item: SyntaxNode) -> SourceFileItemId { - self.arena.alloc(item) - } - fn id_of(&self, item: SyntaxNodeRef) -> SourceFileItemId { - let (id, _item) = self - .arena - .iter() - .find(|(_id, i)| i.borrowed() == item) - .unwrap(); - id - } -} - -impl Index for SourceFileItems { - type Output = SyntaxNode; - fn index(&self, idx: SourceFileItemId) -> &SyntaxNode { - &self.arena[idx] - } -} diff --git a/crates/ra_analysis/src/hir/module/imp.rs b/crates/ra_analysis/src/hir/module/imp.rs deleted file mode 100644 index c8f7ed58d..000000000 --- a/crates/ra_analysis/src/hir/module/imp.rs +++ /dev/null @@ -1,195 +0,0 @@ -use std::sync::Arc; - -use ra_syntax::{ - ast::{self, NameOwner}, - SmolStr, -}; -use relative_path::RelativePathBuf; -use rustc_hash::{FxHashMap, FxHashSet}; -use ra_db::{SourceRoot, SourceRootId, FileResolverImp}; - -use crate::{ - hir::HirDatabase, - Cancelable, FileId, -}; - -use super::{ - LinkData, LinkId, ModuleData, ModuleId, ModuleSource, - ModuleTree, Problem, -}; - -#[derive(Clone, Hash, PartialEq, Eq, Debug)] -pub(crate) enum Submodule { - Declaration(SmolStr), - Definition(SmolStr, ModuleSource), -} - -impl Submodule { - fn name(&self) -> &SmolStr { - match self { - Submodule::Declaration(name) => name, - Submodule::Definition(name, _) => name, - } - } -} - -pub(crate) fn modules<'a>( - root: impl ast::ModuleItemOwner<'a>, -) -> impl Iterator)> { - root.items() - .filter_map(|item| match item { - ast::ModuleItem::Module(m) => Some(m), - _ => None, - }) - .filter_map(|module| { - let name = module.name()?.text(); - Some((name, module)) - }) -} - -pub(crate) fn module_tree( - db: &impl HirDatabase, - source_root: SourceRootId, -) -> Cancelable> { - db.check_canceled()?; - let res = create_module_tree(db, source_root)?; - Ok(Arc::new(res)) -} - -fn create_module_tree<'a>( - db: &impl HirDatabase, - source_root: SourceRootId, -) -> Cancelable { - let mut tree = ModuleTree::default(); - - let mut roots = FxHashMap::default(); - let mut visited = FxHashSet::default(); - - let source_root = db.source_root(source_root); - for &file_id in source_root.files.iter() { - let source = ModuleSource::SourceFile(file_id); - if visited.contains(&source) { - continue; // TODO: use explicit crate_roots here - } - assert!(!roots.contains_key(&file_id)); - let module_id = build_subtree( - db, - &source_root, - &mut tree, - &mut visited, - &mut roots, - None, - source, - )?; - roots.insert(file_id, module_id); - } - Ok(tree) -} - -fn build_subtree( - db: &impl HirDatabase, - source_root: &SourceRoot, - tree: &mut ModuleTree, - visited: &mut FxHashSet, - roots: &mut FxHashMap, - parent: Option, - source: ModuleSource, -) -> Cancelable { - visited.insert(source); - let id = tree.push_mod(ModuleData { - source, - parent, - children: Vec::new(), - }); - for sub in db.submodules(source)?.iter() { - let link = tree.push_link(LinkData { - name: sub.name().clone(), - owner: id, - points_to: Vec::new(), - problem: None, - }); - - let (points_to, problem) = match sub { - Submodule::Declaration(name) => { - let (points_to, problem) = - resolve_submodule(source, &name, &source_root.file_resolver); - let points_to = points_to - .into_iter() - .map(|file_id| match roots.remove(&file_id) { - Some(module_id) => { - tree.mods[module_id].parent = Some(link); - Ok(module_id) - } - None => build_subtree( - db, - source_root, - tree, - visited, - roots, - Some(link), - ModuleSource::SourceFile(file_id), - ), - }) - .collect::>>()?; - (points_to, problem) - } - Submodule::Definition(_name, submodule_source) => { - let points_to = build_subtree( - db, - source_root, - tree, - visited, - roots, - Some(link), - *submodule_source, - )?; - (vec![points_to], None) - } - }; - - tree.links[link].points_to = points_to; - tree.links[link].problem = problem; - } - Ok(id) -} - -fn resolve_submodule( - source: ModuleSource, - name: &SmolStr, - file_resolver: &FileResolverImp, -) -> (Vec, Option) { - let file_id = match source { - ModuleSource::SourceFile(it) => it, - ModuleSource::Module(..) => { - // TODO - return (Vec::new(), None); - } - }; - let mod_name = file_resolver.file_stem(file_id); - let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main"; - - let file_mod = RelativePathBuf::from(format!("../{}.rs", name)); - let dir_mod = RelativePathBuf::from(format!("../{}/mod.rs", name)); - let points_to: Vec; - let problem: Option; - if is_dir_owner { - points_to = [&file_mod, &dir_mod] - .iter() - .filter_map(|path| file_resolver.resolve(file_id, path)) - .collect(); - problem = if points_to.is_empty() { - Some(Problem::UnresolvedModule { - candidate: file_mod, - }) - } else { - None - } - } else { - points_to = Vec::new(); - problem = Some(Problem::NotDirOwner { - move_to: RelativePathBuf::from(format!("../{}/mod.rs", mod_name)), - candidate: file_mod, - }); - } - (points_to, problem) -} diff --git a/crates/ra_analysis/src/hir/module/mod.rs b/crates/ra_analysis/src/hir/module/mod.rs deleted file mode 100644 index d2096b01e..000000000 --- a/crates/ra_analysis/src/hir/module/mod.rs +++ /dev/null @@ -1,379 +0,0 @@ -pub(super) mod imp; -pub(super) mod nameres; - -use std::sync::Arc; - -use ra_editor::find_node_at_offset; - -use ra_syntax::{ - algo::generate, - ast::{self, AstNode, NameOwner}, - SmolStr, SyntaxNode, -}; -use ra_db::SourceRootId; -use relative_path::RelativePathBuf; - -use crate::{ - FileId, FilePosition, Cancelable, - hir::{DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId}, - arena::{Arena, Id}, -}; - -pub(crate) use self::nameres::ModuleScope; - -/// `Module` is API entry point to get all the information -/// about a particular module. -#[derive(Debug, Clone)] -pub(crate) struct Module { - tree: Arc, - source_root_id: SourceRootId, - module_id: ModuleId, -} - -impl Module { - /// Lookup `Module` by `FileId`. Note that this is inherently - /// lossy transformation: in general, a single source might correspond to - /// several modules. - pub fn guess_from_file_id( - db: &impl HirDatabase, - file_id: FileId, - ) -> Cancelable> { - Module::guess_from_source(db, file_id, ModuleSource::SourceFile(file_id)) - } - - /// Lookup `Module` by position in the source code. Note that this - /// is inherently lossy transformation: in general, a single source might - /// correspond to several modules. - pub fn guess_from_position( - db: &impl HirDatabase, - position: FilePosition, - ) -> Cancelable> { - let file = db.source_file(position.file_id); - let module_source = match find_node_at_offset::(file.syntax(), position.offset) - { - Some(m) if !m.has_semi() => ModuleSource::new_inline(db, position.file_id, m), - _ => ModuleSource::SourceFile(position.file_id), - }; - Module::guess_from_source(db, position.file_id, module_source) - } - - fn guess_from_source( - db: &impl HirDatabase, - file_id: FileId, - module_source: ModuleSource, - ) -> Cancelable> { - let source_root_id = db.file_source_root(file_id); - let module_tree = db.module_tree(source_root_id)?; - - let res = match module_tree.any_module_for_source(module_source) { - None => None, - Some(module_id) => Some(Module { - tree: module_tree, - source_root_id, - module_id, - }), - }; - Ok(res) - } - - pub(super) fn new( - db: &impl HirDatabase, - source_root_id: SourceRootId, - module_id: ModuleId, - ) -> Cancelable { - let module_tree = db.module_tree(source_root_id)?; - let res = Module { - tree: module_tree, - source_root_id, - module_id, - }; - Ok(res) - } - - /// Returns `mod foo;` or `mod foo {}` node whihc declared this module. - /// Returns `None` for the root module - pub fn parent_link_source(&self, db: &impl HirDatabase) -> Option<(FileId, ast::ModuleNode)> { - let link = self.module_id.parent_link(&self.tree)?; - let file_id = link.owner(&self.tree).source(&self.tree).file_id(); - let src = link.bind_source(&self.tree, db); - Some((file_id, src)) - } - - pub fn source(&self) -> ModuleSource { - self.module_id.source(&self.tree) - } - - /// Parent module. Returns `None` if this is a root module. - pub fn parent(&self) -> Option { - let parent_id = self.module_id.parent(&self.tree)?; - Some(Module { - module_id: parent_id, - ..self.clone() - }) - } - - /// The root of the tree this module is part of - pub fn crate_root(&self) -> Module { - let root_id = self.module_id.crate_root(&self.tree); - Module { - module_id: root_id, - ..self.clone() - } - } - - /// `name` is `None` for the crate's root module - #[allow(unused)] - pub fn name(&self) -> Option { - let link = self.module_id.parent_link(&self.tree)?; - Some(link.name(&self.tree)) - } - - pub fn def_id(&self, db: &impl HirDatabase) -> DefId { - let def_loc = DefLoc::Module { - id: self.module_id, - source_root: self.source_root_id, - }; - def_loc.id(db) - } - - /// Finds a child module with the specified name. - pub fn child(&self, name: &str) -> Option { - let child_id = self.module_id.child(&self.tree, name)?; - Some(Module { - module_id: child_id, - ..self.clone() - }) - } - - /// Returns a `ModuleScope`: a set of items, visible in this module. - pub(crate) fn scope(&self, db: &impl HirDatabase) -> Cancelable { - let item_map = db.item_map(self.source_root_id)?; - let res = item_map.per_module[&self.module_id].clone(); - Ok(res) - } - - pub(crate) fn resolve_path( - &self, - db: &impl HirDatabase, - path: Path, - ) -> Cancelable> { - let mut curr = match path.kind { - PathKind::Crate => self.crate_root(), - PathKind::Self_ | PathKind::Plain => self.clone(), - PathKind::Super => ctry!(self.parent()), - } - .def_id(db); - - let segments = path.segments; - for name in segments.iter() { - let module = match curr.loc(db) { - DefLoc::Module { id, source_root } => Module::new(db, source_root, id)?, - _ => return Ok(None), - }; - let scope = module.scope(db)?; - curr = ctry!(ctry!(scope.get(&name)).def_id); - } - Ok(Some(curr)) - } - - pub fn problems(&self, db: &impl HirDatabase) -> Vec<(SyntaxNode, Problem)> { - self.module_id.problems(&self.tree, db) - } -} - -/// Phisically, rust source is organized as a set of files, but logically it is -/// organized as a tree of modules. Usually, a single file corresponds to a -/// single module, but it is not nessary the case. -/// -/// Module encapsulate the logic of transitioning from the fuzzy world of files -/// (which can have multiple parents) to the precise world of modules (which -/// always have one parent). -#[derive(Default, Debug, PartialEq, Eq)] -pub(crate) struct ModuleTree { - mods: Arena, - links: Arena, -} - -impl ModuleTree { - pub(in crate::hir) fn modules<'a>(&'a self) -> impl Iterator + 'a { - self.mods.iter().map(|(id, _)| id) - } - - fn modules_for_source(&self, source: ModuleSource) -> Vec { - self.mods - .iter() - .filter(|(_idx, it)| it.source == source) - .map(|(idx, _)| idx) - .collect() - } - - fn any_module_for_source(&self, source: ModuleSource) -> Option { - self.modules_for_source(source).pop() - } -} - -/// `ModuleSource` is the syntax tree element that produced this module: -/// either a file, or an inlinde module. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub(crate) enum ModuleSource { - SourceFile(FileId), - Module(SourceItemId), -} - -/// An owned syntax node for a module. Unlike `ModuleSource`, -/// this holds onto the AST for the whole file. -pub(crate) enum ModuleSourceNode { - SourceFile(ast::SourceFileNode), - Module(ast::ModuleNode), -} - -pub(crate) type ModuleId = Id; -type LinkId = Id; - -#[derive(Clone, Debug, Hash, PartialEq, Eq)] -pub enum Problem { - UnresolvedModule { - candidate: RelativePathBuf, - }, - NotDirOwner { - move_to: RelativePathBuf, - candidate: RelativePathBuf, - }, -} - -impl ModuleId { - pub(in crate::hir) fn source(self, tree: &ModuleTree) -> ModuleSource { - tree.mods[self].source - } - fn parent_link(self, tree: &ModuleTree) -> Option { - tree.mods[self].parent - } - fn parent(self, tree: &ModuleTree) -> Option { - let link = self.parent_link(tree)?; - Some(tree.links[link].owner) - } - fn crate_root(self, tree: &ModuleTree) -> ModuleId { - generate(Some(self), move |it| it.parent(tree)) - .last() - .unwrap() - } - fn child(self, tree: &ModuleTree, name: &str) -> Option { - let link = tree.mods[self] - .children - .iter() - .map(|&it| &tree.links[it]) - .find(|it| it.name == name)?; - Some(*link.points_to.first()?) - } - fn children<'a>(self, tree: &'a ModuleTree) -> impl Iterator + 'a { - tree.mods[self].children.iter().filter_map(move |&it| { - let link = &tree.links[it]; - let module = *link.points_to.first()?; - Some((link.name.clone(), module)) - }) - } - fn problems(self, tree: &ModuleTree, db: &impl HirDatabase) -> Vec<(SyntaxNode, Problem)> { - tree.mods[self] - .children - .iter() - .filter_map(|&it| { - let p = tree.links[it].problem.clone()?; - let s = it.bind_source(tree, db); - let s = s.borrowed().name().unwrap().syntax().owned(); - Some((s, p)) - }) - .collect() - } -} - -impl LinkId { - fn owner(self, tree: &ModuleTree) -> ModuleId { - tree.links[self].owner - } - fn name(self, tree: &ModuleTree) -> SmolStr { - tree.links[self].name.clone() - } - fn bind_source<'a>(self, tree: &ModuleTree, db: &impl HirDatabase) -> ast::ModuleNode { - let owner = self.owner(tree); - match owner.source(tree).resolve(db) { - ModuleSourceNode::SourceFile(root) => { - let ast = imp::modules(root.borrowed()) - .find(|(name, _)| name == &tree.links[self].name) - .unwrap() - .1; - ast.owned() - } - ModuleSourceNode::Module(it) => it, - } - } -} - -#[derive(Debug, PartialEq, Eq, Hash)] -pub(crate) struct ModuleData { - source: ModuleSource, - parent: Option, - children: Vec, -} - -impl ModuleSource { - pub(crate) fn new_inline( - db: &impl HirDatabase, - file_id: FileId, - module: ast::Module, - ) -> ModuleSource { - assert!(!module.has_semi()); - let items = db.file_items(file_id); - let item_id = items.id_of(module.syntax()); - let id = SourceItemId { file_id, item_id }; - ModuleSource::Module(id) - } - - pub(crate) fn as_file(self) -> Option { - match self { - ModuleSource::SourceFile(f) => Some(f), - ModuleSource::Module(..) => None, - } - } - - pub(crate) fn file_id(self) -> FileId { - match self { - ModuleSource::SourceFile(f) => f, - ModuleSource::Module(source_item_id) => source_item_id.file_id, - } - } - - pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { - match self { - ModuleSource::SourceFile(file_id) => { - let syntax = db.source_file(file_id); - ModuleSourceNode::SourceFile(syntax.ast().owned()) - } - ModuleSource::Module(item_id) => { - let syntax = db.file_item(item_id); - let syntax = syntax.borrowed(); - let module = ast::Module::cast(syntax).unwrap(); - ModuleSourceNode::Module(module.owned()) - } - } - } -} - -#[derive(Hash, Debug, PartialEq, Eq)] -struct LinkData { - owner: ModuleId, - name: SmolStr, - points_to: Vec, - problem: Option, -} - -impl ModuleTree { - fn push_mod(&mut self, data: ModuleData) -> ModuleId { - self.mods.alloc(data) - } - fn push_link(&mut self, data: LinkData) -> LinkId { - let owner = data.owner; - let id = self.links.alloc(data); - self.mods[owner].children.push(id); - id - } -} diff --git a/crates/ra_analysis/src/hir/module/nameres.rs b/crates/ra_analysis/src/hir/module/nameres.rs deleted file mode 100644 index d4ecc010b..000000000 --- a/crates/ra_analysis/src/hir/module/nameres.rs +++ /dev/null @@ -1,446 +0,0 @@ -//! Name resolution algorithm. The end result of the algorithm is `ItemMap`: a -//! map with maps each module to it's scope: the set of items, visible in the -//! module. That is, we only resolve imports here, name resolution of item -//! bodies will be done in a separate step. -//! -//! Like Rustc, we use an interative per-crate algorithm: we start with scopes -//! containing only directly defined items, and then iteratively resolve -//! imports. -//! -//! To make this work nicely in the IDE scenarios, we place `InputModuleItems` -//! in between raw syntax and name resolution. `InputModuleItems` are computed -//! using only the module's syntax, and it is all directly defined items plus -//! imports. The plain is to make `InputModuleItems` independent of local -//! modifications (that is, typing inside a function shold not change IMIs), -//! such that the results of name resolution can be preserved unless the module -//! structure itself is modified. -use std::{ - sync::Arc, -}; - -use rustc_hash::FxHashMap; -use ra_syntax::{ - TextRange, - SmolStr, SyntaxKind::{self, *}, - ast::{self, AstNode} -}; -use ra_db::SourceRootId; - -use crate::{ - Cancelable, FileId, - hir::{ - DefId, DefLoc, - SourceItemId, SourceFileItemId, SourceFileItems, - Path, PathKind, - HirDatabase, - module::{ModuleId, ModuleTree}, - }, -}; - -/// Item map is the result of the name resolution. Item map contains, for each -/// module, the set of visible items. -#[derive(Default, Debug, PartialEq, Eq)] -pub(crate) struct ItemMap { - pub(crate) per_module: FxHashMap, -} - -#[derive(Debug, Default, PartialEq, Eq, Clone)] -pub(crate) struct ModuleScope { - items: FxHashMap, -} - -impl ModuleScope { - pub(crate) fn entries<'a>(&'a self) -> impl Iterator + 'a { - self.items.iter() - } - pub(crate) fn get(&self, name: &SmolStr) -> Option<&Resolution> { - self.items.get(name) - } -} - -/// A set of items and imports declared inside a module, without relation to -/// other modules. -/// -/// This stands in-between raw syntax and name resolution and alow us to avoid -/// recomputing name res: if `InputModuleItems` are the same, we can avoid -/// running name resolution. -#[derive(Debug, Default, PartialEq, Eq)] -pub(crate) struct InputModuleItems { - items: Vec, - imports: Vec, -} - -#[derive(Debug, PartialEq, Eq)] -struct ModuleItem { - id: SourceFileItemId, - name: SmolStr, - kind: SyntaxKind, - vis: Vis, -} - -#[derive(Debug, PartialEq, Eq)] -enum Vis { - // Priv, - Other, -} - -#[derive(Debug, Clone, PartialEq, Eq)] -struct Import { - path: Path, - kind: ImportKind, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) struct NamedImport { - file_item_id: SourceFileItemId, - relative_range: TextRange, -} - -impl NamedImport { - pub(crate) fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange { - let source_item_id = SourceItemId { - file_id, - item_id: self.file_item_id, - }; - let syntax = db.file_item(source_item_id); - let offset = syntax.borrowed().range().start(); - self.relative_range + offset - } -} - -#[derive(Debug, Clone, PartialEq, Eq)] -enum ImportKind { - Glob, - Named(NamedImport), -} - -/// Resolution is basically `DefId` atm, but it should account for stuff like -/// multiple namespaces, ambiguity and errors. -#[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct Resolution { - /// None for unresolved - pub(crate) def_id: Option, - /// ident by whitch this is imported into local scope. - pub(crate) import: Option, -} - -// #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -// enum Namespace { -// Types, -// Values, -// } - -// #[derive(Debug)] -// struct PerNs { -// types: Option, -// values: Option, -// } - -impl InputModuleItems { - pub(in crate::hir) fn new<'a>( - file_items: &SourceFileItems, - items: impl Iterator>, - ) -> InputModuleItems { - let mut res = InputModuleItems::default(); - for item in items { - res.add_item(file_items, item); - } - res - } - - fn add_item(&mut self, file_items: &SourceFileItems, item: ast::ModuleItem) -> Option<()> { - match item { - ast::ModuleItem::StructDef(it) => self.items.push(ModuleItem::new(file_items, it)?), - ast::ModuleItem::EnumDef(it) => self.items.push(ModuleItem::new(file_items, it)?), - ast::ModuleItem::FnDef(it) => self.items.push(ModuleItem::new(file_items, it)?), - ast::ModuleItem::TraitDef(it) => self.items.push(ModuleItem::new(file_items, it)?), - ast::ModuleItem::TypeDef(it) => self.items.push(ModuleItem::new(file_items, it)?), - ast::ModuleItem::ImplItem(_) => { - // impls don't define items - } - ast::ModuleItem::UseItem(it) => self.add_use_item(file_items, it), - ast::ModuleItem::ExternCrateItem(_) => { - // TODO - } - ast::ModuleItem::ConstDef(it) => self.items.push(ModuleItem::new(file_items, it)?), - ast::ModuleItem::StaticDef(it) => self.items.push(ModuleItem::new(file_items, it)?), - ast::ModuleItem::Module(it) => self.items.push(ModuleItem::new(file_items, it)?), - } - Some(()) - } - - fn add_use_item(&mut self, file_items: &SourceFileItems, item: ast::UseItem) { - let file_item_id = file_items.id_of(item.syntax()); - let start_offset = item.syntax().range().start(); - Path::expand_use_item(item, |path, range| { - let kind = match range { - None => ImportKind::Glob, - Some(range) => ImportKind::Named(NamedImport { - file_item_id, - relative_range: range - start_offset, - }), - }; - self.imports.push(Import { kind, path }) - }) - } -} - -impl ModuleItem { - fn new<'a>(file_items: &SourceFileItems, item: impl ast::NameOwner<'a>) -> Option { - let name = item.name()?.text(); - let kind = item.syntax().kind(); - let vis = Vis::Other; - let id = file_items.id_of(item.syntax()); - let res = ModuleItem { - id, - name, - kind, - vis, - }; - Some(res) - } -} - -pub(in crate::hir) struct Resolver<'a, DB> { - pub db: &'a DB, - pub input: &'a FxHashMap>, - pub source_root: SourceRootId, - pub module_tree: Arc, - pub result: ItemMap, -} - -impl<'a, DB> Resolver<'a, DB> -where - DB: HirDatabase, -{ - pub(in crate::hir) fn resolve(mut self) -> Cancelable { - for (&module_id, items) in self.input.iter() { - self.populate_module(module_id, items) - } - - for &module_id in self.input.keys() { - self.db.check_canceled()?; - self.resolve_imports(module_id); - } - Ok(self.result) - } - - fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) { - let file_id = module_id.source(&self.module_tree).file_id(); - - let mut module_items = ModuleScope::default(); - - for import in input.imports.iter() { - if let Some(name) = import.path.segments.iter().last() { - if let ImportKind::Named(import) = import.kind { - module_items.items.insert( - name.clone(), - Resolution { - def_id: None, - import: Some(import), - }, - ); - } - } - } - - for item in input.items.iter() { - if item.kind == MODULE { - // handle submodules separatelly - continue; - } - let def_loc = DefLoc::Item { - source_item_id: SourceItemId { - file_id, - item_id: item.id, - }, - }; - let def_id = def_loc.id(self.db); - let resolution = Resolution { - def_id: Some(def_id), - import: None, - }; - module_items.items.insert(item.name.clone(), resolution); - } - - for (name, mod_id) in module_id.children(&self.module_tree) { - let def_loc = DefLoc::Module { - id: mod_id, - source_root: self.source_root, - }; - let def_id = def_loc.id(self.db); - let resolution = Resolution { - def_id: Some(def_id), - import: None, - }; - module_items.items.insert(name, resolution); - } - - self.result.per_module.insert(module_id, module_items); - } - - fn resolve_imports(&mut self, module_id: ModuleId) { - for import in self.input[&module_id].imports.iter() { - self.resolve_import(module_id, import); - } - } - - fn resolve_import(&mut self, module_id: ModuleId, import: &Import) { - let ptr = match import.kind { - ImportKind::Glob => return, - ImportKind::Named(ptr) => ptr, - }; - - let mut curr = match import.path.kind { - // TODO: handle extern crates - PathKind::Plain => return, - PathKind::Self_ => module_id, - PathKind::Super => { - match module_id.parent(&self.module_tree) { - Some(it) => it, - // TODO: error - None => return, - } - } - PathKind::Crate => module_id.crate_root(&self.module_tree), - }; - - for (i, name) in import.path.segments.iter().enumerate() { - let is_last = i == import.path.segments.len() - 1; - - let def_id = match self.result.per_module[&curr].items.get(name) { - None => return, - Some(res) => match res.def_id { - Some(it) => it, - None => return, - }, - }; - - if !is_last { - curr = match def_id.loc(self.db) { - DefLoc::Module { id, .. } => id, - _ => return, - } - } else { - self.update(module_id, |items| { - let res = Resolution { - def_id: Some(def_id), - import: Some(ptr), - }; - items.items.insert(name.clone(), res); - }) - } - } - } - - fn update(&mut self, module_id: ModuleId, f: impl FnOnce(&mut ModuleScope)) { - let module_items = self.result.per_module.get_mut(&module_id).unwrap(); - f(module_items) - } -} - -#[cfg(test)] -mod tests { - use ra_db::FilesDatabase; - use crate::{ - AnalysisChange, - mock_analysis::{MockAnalysis, analysis_and_position}, - hir::{self, HirDatabase}, -}; - use super::*; - - fn item_map(fixture: &str) -> (Arc, ModuleId) { - let (analysis, pos) = analysis_and_position(fixture); - let db = analysis.imp.db; - let source_root = db.file_source_root(pos.file_id); - let descr = hir::Module::guess_from_position(&*db, pos) - .unwrap() - .unwrap(); - let module_id = descr.module_id; - (db.item_map(source_root).unwrap(), module_id) - } - - #[test] - fn test_item_map() { - let (item_map, module_id) = item_map( - " - //- /lib.rs - mod foo; - - use crate::foo::bar::Baz; - <|> - - //- /foo/mod.rs - pub mod bar; - - //- /foo/bar.rs - pub struct Baz; - ", - ); - let name = SmolStr::from("Baz"); - let resolution = &item_map.per_module[&module_id].items[&name]; - assert!(resolution.def_id.is_some()); - } - - #[test] - fn typing_inside_a_function_should_not_invalidate_item_map() { - let mock_analysis = MockAnalysis::with_files( - " - //- /lib.rs - mod foo; - - use crate::foo::bar::Baz; - - fn foo() -> i32 { - 1 + 1 - } - //- /foo/mod.rs - pub mod bar; - - //- /foo/bar.rs - pub struct Baz; - ", - ); - - let file_id = mock_analysis.id_of("/lib.rs"); - let mut host = mock_analysis.analysis_host(); - - let source_root = host.analysis().imp.db.file_source_root(file_id); - - { - let db = host.analysis().imp.db; - let events = db.log_executed(|| { - db.item_map(source_root).unwrap(); - }); - assert!(format!("{:?}", events).contains("item_map")) - } - - let mut change = AnalysisChange::new(); - - change.change_file( - file_id, - " - mod foo; - - use crate::foo::bar::Baz; - - fn foo() -> i32 { 92 } - " - .to_string(), - ); - - host.apply_change(change); - - { - let db = host.analysis().imp.db; - let events = db.log_executed(|| { - db.item_map(source_root).unwrap(); - }); - assert!( - !format!("{:?}", events).contains("_item_map"), - "{:#?}", - events - ) - } - } -} diff --git a/crates/ra_analysis/src/hir/path.rs b/crates/ra_analysis/src/hir/path.rs deleted file mode 100644 index 8279daf4b..000000000 --- a/crates/ra_analysis/src/hir/path.rs +++ /dev/null @@ -1,148 +0,0 @@ -use ra_syntax::{SmolStr, ast, AstNode, TextRange}; - -#[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct Path { - pub(crate) kind: PathKind, - pub(crate) segments: Vec, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) enum PathKind { - Plain, - Self_, - Super, - Crate, -} - -impl Path { - /// Calls `cb` with all paths, represented by this use item. - pub(crate) fn expand_use_item(item: ast::UseItem, mut cb: impl FnMut(Path, Option)) { - if let Some(tree) = item.use_tree() { - expand_use_tree(None, tree, &mut cb); - } - } - - /// Converts an `ast::Path` to `Path`. Works with use trees. - pub(crate) fn from_ast(mut path: ast::Path) -> Option { - let mut kind = PathKind::Plain; - let mut segments = Vec::new(); - loop { - let segment = path.segment()?; - match segment.kind()? { - ast::PathSegmentKind::Name(name) => segments.push(name.text()), - ast::PathSegmentKind::CrateKw => { - kind = PathKind::Crate; - break; - } - ast::PathSegmentKind::SelfKw => { - kind = PathKind::Self_; - break; - } - ast::PathSegmentKind::SuperKw => { - kind = PathKind::Super; - break; - } - } - path = match qualifier(path) { - Some(it) => it, - None => break, - }; - } - segments.reverse(); - return Some(Path { kind, segments }); - - fn qualifier(path: ast::Path) -> Option { - if let Some(q) = path.qualifier() { - return Some(q); - } - // TODO: this bottom up traversal is not too precise. - // Should we handle do a top-down analysiss, recording results? - let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?; - let use_tree = use_tree_list.parent_use_tree(); - use_tree.path() - } - } - - /// `true` is this path is a single identifier, like `foo` - pub(crate) fn is_ident(&self) -> bool { - self.kind == PathKind::Plain && self.segments.len() == 1 - } -} - -fn expand_use_tree( - prefix: Option, - tree: ast::UseTree, - cb: &mut impl FnMut(Path, Option), -) { - if let Some(use_tree_list) = tree.use_tree_list() { - let prefix = match tree.path() { - None => prefix, - Some(path) => match convert_path(prefix, path) { - Some(it) => Some(it), - None => return, // TODO: report errors somewhere - }, - }; - for tree in use_tree_list.use_trees() { - expand_use_tree(prefix.clone(), tree, cb); - } - } else { - if let Some(ast_path) = tree.path() { - if let Some(path) = convert_path(prefix, ast_path) { - let range = if tree.has_star() { - None - } else { - let range = ast_path.segment().unwrap().syntax().range(); - Some(range) - }; - cb(path, range) - } - } - } -} - -fn convert_path(prefix: Option, path: ast::Path) -> Option { - let prefix = if let Some(qual) = path.qualifier() { - Some(convert_path(prefix, qual)?) - } else { - None - }; - let segment = path.segment()?; - let res = match segment.kind()? { - ast::PathSegmentKind::Name(name) => { - let mut res = prefix.unwrap_or_else(|| Path { - kind: PathKind::Plain, - segments: Vec::with_capacity(1), - }); - res.segments.push(name.text()); - res - } - ast::PathSegmentKind::CrateKw => { - if prefix.is_some() { - return None; - } - Path { - kind: PathKind::Crate, - segments: Vec::new(), - } - } - ast::PathSegmentKind::SelfKw => { - if prefix.is_some() { - return None; - } - Path { - kind: PathKind::Self_, - segments: Vec::new(), - } - } - ast::PathSegmentKind::SuperKw => { - if prefix.is_some() { - return None; - } - Path { - kind: PathKind::Super, - segments: Vec::new(), - } - } - }; - Some(res) -} diff --git a/crates/ra_analysis/src/hir/query_definitions.rs b/crates/ra_analysis/src/hir/query_definitions.rs deleted file mode 100644 index 00237b633..000000000 --- a/crates/ra_analysis/src/hir/query_definitions.rs +++ /dev/null @@ -1,157 +0,0 @@ -use std::{ - sync::Arc, - time::Instant, -}; - -use rustc_hash::FxHashMap; -use ra_syntax::{ - AstNode, SyntaxNode, SmolStr, - ast::{self, FnDef, FnDefNode, NameOwner, ModuleItemOwner} -}; -use ra_db::SourceRootId; - -use crate::{ - FileId, Cancelable, - hir::{ - FnId, - SourceFileItems, SourceItemId, - db::HirDatabase, - function::FnScopes, - module::{ - ModuleSource, ModuleSourceNode, ModuleId, - imp::Submodule, - nameres::{InputModuleItems, ItemMap, Resolver}, - }, - }, -}; - -/// Resolve `FnId` to the corresponding `SyntaxNode` -pub(super) fn fn_syntax(db: &impl HirDatabase, fn_id: FnId) -> FnDefNode { - let item_id = fn_id.loc(db); - let syntax = db.file_item(item_id); - FnDef::cast(syntax.borrowed()).unwrap().owned() -} - -pub(super) fn fn_scopes(db: &impl HirDatabase, fn_id: FnId) -> Arc { - let syntax = db.fn_syntax(fn_id); - let res = FnScopes::new(syntax.borrowed()); - Arc::new(res) -} - -pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc { - let source_file = db.source_file(file_id); - let source_file = source_file.borrowed(); - let mut res = SourceFileItems::default(); - source_file - .syntax() - .descendants() - .filter_map(ast::ModuleItem::cast) - .map(|it| it.syntax().owned()) - .for_each(|it| { - res.alloc(it); - }); - Arc::new(res) -} - -pub(super) fn file_item(db: &impl HirDatabase, source_item_id: SourceItemId) -> SyntaxNode { - db.file_items(source_item_id.file_id)[source_item_id.item_id].clone() -} - -pub(crate) fn submodules( - db: &impl HirDatabase, - source: ModuleSource, -) -> Cancelable>> { - db.check_canceled()?; - let file_id = source.file_id(); - let submodules = match source.resolve(db) { - ModuleSourceNode::SourceFile(it) => collect_submodules(db, file_id, it.borrowed()), - ModuleSourceNode::Module(it) => it - .borrowed() - .item_list() - .map(|it| collect_submodules(db, file_id, it)) - .unwrap_or_else(Vec::new), - }; - return Ok(Arc::new(submodules)); - - fn collect_submodules<'a>( - db: &impl HirDatabase, - file_id: FileId, - root: impl ast::ModuleItemOwner<'a>, - ) -> Vec { - modules(root) - .map(|(name, m)| { - if m.has_semi() { - Submodule::Declaration(name) - } else { - let src = ModuleSource::new_inline(db, file_id, m); - Submodule::Definition(name, src) - } - }) - .collect() - } -} - -pub(crate) fn modules<'a>( - root: impl ast::ModuleItemOwner<'a>, -) -> impl Iterator)> { - root.items() - .filter_map(|item| match item { - ast::ModuleItem::Module(m) => Some(m), - _ => None, - }) - .filter_map(|module| { - let name = module.name()?.text(); - Some((name, module)) - }) -} - -pub(super) fn input_module_items( - db: &impl HirDatabase, - source_root: SourceRootId, - module_id: ModuleId, -) -> Cancelable> { - let module_tree = db.module_tree(source_root)?; - let source = module_id.source(&module_tree); - let file_items = db.file_items(source.file_id()); - let res = match source.resolve(db) { - ModuleSourceNode::SourceFile(it) => { - let items = it.borrowed().items(); - InputModuleItems::new(&file_items, items) - } - ModuleSourceNode::Module(it) => { - let items = it - .borrowed() - .item_list() - .into_iter() - .flat_map(|it| it.items()); - InputModuleItems::new(&file_items, items) - } - }; - Ok(Arc::new(res)) -} - -pub(super) fn item_map( - db: &impl HirDatabase, - source_root: SourceRootId, -) -> Cancelable> { - let start = Instant::now(); - let module_tree = db.module_tree(source_root)?; - let input = module_tree - .modules() - .map(|id| { - let items = db.input_module_items(source_root, id)?; - Ok((id, items)) - }) - .collect::>>()?; - let resolver = Resolver { - db: db, - input: &input, - source_root, - module_tree, - result: ItemMap::default(), - }; - let res = resolver.resolve()?; - let elapsed = start.elapsed(); - log::info!("item_map: {:?}", elapsed); - Ok(Arc::new(res)) -} diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 9a8694221..f5cb3550e 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -14,15 +14,15 @@ use ra_db::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE, SyntaxDatabase, use rayon::prelude::*; use rustc_hash::FxHashSet; use salsa::{Database, ParallelDatabase}; +use hir::{ + self, + FnSignatureInfo, + Problem, +}; use crate::{ completion::{completions, CompletionItem}, db, - hir::{ - self, - FnSignatureInfo, - Problem, - }, symbol_index::{SymbolIndex, SymbolsDatabase}, AnalysisChange, Cancelable, CrateId, Diagnostic, FileId, FileSystemEdit, FilePosition, Query, SourceChange, SourceFileNodeEdit, diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index a1912e90b..350a6d627 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -9,11 +9,18 @@ extern crate relative_path; extern crate rustc_hash; extern crate salsa; -mod arena; +macro_rules! ctry { + ($expr:expr) => { + match $expr { + None => return Ok(None), + Some(it) => it, + } + }; +} + mod db; mod imp; mod completion; -mod hir; mod symbol_index; pub mod mock_analysis; @@ -31,11 +38,11 @@ use crate::{ pub use crate::{ completion::CompletionItem, - hir::FnSignatureInfo, }; pub use ra_editor::{ FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode, }; +pub use hir::FnSignatureInfo; pub use ra_db::{ Canceled, Cancelable, FilePosition, @@ -310,3 +317,112 @@ fn analysis_is_send() { fn is_send() {} is_send::(); } + +//TODO: move to hir +#[cfg(test)] +mod hir_namres_tests { + use std::sync::Arc; + use ra_db::FilesDatabase; + use ra_syntax::SmolStr; + use hir::{self, db::HirDatabase}; + + use crate::{ + AnalysisChange, + mock_analysis::{MockAnalysis, analysis_and_position}, +}; + + fn item_map(fixture: &str) -> (Arc, hir::ModuleId) { + let (analysis, pos) = analysis_and_position(fixture); + let db = analysis.imp.db; + let source_root = db.file_source_root(pos.file_id); + let descr = hir::Module::guess_from_position(&*db, pos) + .unwrap() + .unwrap(); + let module_id = descr.module_id; + (db.item_map(source_root).unwrap(), module_id) + } + + #[test] + fn test_item_map() { + let (item_map, module_id) = item_map( + " + //- /lib.rs + mod foo; + + use crate::foo::bar::Baz; + <|> + + //- /foo/mod.rs + pub mod bar; + + //- /foo/bar.rs + pub struct Baz; + ", + ); + let name = SmolStr::from("Baz"); + let resolution = &item_map.per_module[&module_id].items[&name]; + assert!(resolution.def_id.is_some()); + } + + #[test] + fn typing_inside_a_function_should_not_invalidate_item_map() { + let mock_analysis = MockAnalysis::with_files( + " + //- /lib.rs + mod foo; + + use crate::foo::bar::Baz; + + fn foo() -> i32 { + 1 + 1 + } + //- /foo/mod.rs + pub mod bar; + + //- /foo/bar.rs + pub struct Baz; + ", + ); + + let file_id = mock_analysis.id_of("/lib.rs"); + let mut host = mock_analysis.analysis_host(); + + let source_root = host.analysis().imp.db.file_source_root(file_id); + + { + let db = host.analysis().imp.db; + let events = db.log_executed(|| { + db.item_map(source_root).unwrap(); + }); + assert!(format!("{:?}", events).contains("item_map")) + } + + let mut change = AnalysisChange::new(); + + change.change_file( + file_id, + " + mod foo; + + use crate::foo::bar::Baz; + + fn foo() -> i32 { 92 } + " + .to_string(), + ); + + host.apply_change(change); + + { + let db = host.analysis().imp.db; + let events = db.log_executed(|| { + db.item_map(source_root).unwrap(); + }); + assert!( + !format!("{:?}", events).contains("_item_map"), + "{:#?}", + events + ) + } + } +} diff --git a/crates/ra_hir/src/arena.rs b/crates/ra_hir/src/arena.rs index a752ec0c1..8d67ab1c9 100644 --- a/crates/ra_hir/src/arena.rs +++ b/crates/ra_hir/src/arena.rs @@ -8,7 +8,7 @@ use std::{ marker::PhantomData, }; -pub(crate) struct Id { +pub struct Id { idx: u32, _ty: PhantomData T>, } diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index dbf8785fe..2f01bae6d 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -10,14 +10,14 @@ use crate::{ DefLoc, DefId, FnId, SourceFileItems, SourceItemId, query_definitions, - function::{FnScopes}, + FnScopes, module::{ModuleId, ModuleTree, ModuleSource, nameres::{ItemMap, InputModuleItems}}, }; salsa::query_group! { -pub(crate) trait HirDatabase: SyntaxDatabase +pub trait HirDatabase: SyntaxDatabase + AsRef> + AsRef> { diff --git a/crates/ra_hir/src/function/mod.rs b/crates/ra_hir/src/function/mod.rs index a3ed00f02..c8af2e54f 100644 --- a/crates/ra_hir/src/function/mod.rs +++ b/crates/ra_hir/src/function/mod.rs @@ -15,10 +15,10 @@ use crate::{ FnId, HirDatabase, SourceItemId, }; -pub(crate) use self::scope::FnScopes; +pub use self::scope::FnScopes; impl FnId { - pub(crate) fn get(db: &impl HirDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId { + pub fn get(db: &impl HirDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId { let file_items = db.file_items(file_id); let item_id = file_items.id_of(fn_def.syntax()); let item_id = SourceItemId { file_id, item_id }; @@ -26,12 +26,12 @@ impl FnId { } } -pub(crate) struct Function { +pub struct Function { fn_id: FnId, } impl Function { - pub(crate) fn guess_from_source( + pub fn guess_from_source( db: &impl HirDatabase, file_id: FileId, fn_def: ast::FnDef, @@ -40,7 +40,7 @@ impl Function { Function { fn_id } } - pub(crate) fn guess_for_name_ref( + pub fn guess_for_name_ref( db: &impl HirDatabase, file_id: FileId, name_ref: ast::NameRef, @@ -48,7 +48,7 @@ impl Function { Function::guess_for_node(db, file_id, name_ref.syntax()) } - pub(crate) fn guess_for_bind_pat( + pub fn guess_for_bind_pat( db: &impl HirDatabase, file_id: FileId, bind_pat: ast::BindPat, @@ -66,11 +66,11 @@ impl Function { Some(res) } - pub(crate) fn scope(&self, db: &impl HirDatabase) -> Arc { + pub fn scope(&self, db: &impl HirDatabase) -> Arc { db.fn_scopes(self.fn_id) } - pub(crate) fn signature_info(&self, db: &impl HirDatabase) -> Option { + pub fn signature_info(&self, db: &impl HirDatabase) -> Option { let syntax = db.fn_syntax(self.fn_id); FnSignatureInfo::new(syntax.borrowed()) } diff --git a/crates/ra_hir/src/function/scope.rs b/crates/ra_hir/src/function/scope.rs index c8b6b1934..863453291 100644 --- a/crates/ra_hir/src/function/scope.rs +++ b/crates/ra_hir/src/function/scope.rs @@ -15,7 +15,7 @@ pub(crate) type ScopeId = Id; #[derive(Debug, PartialEq, Eq)] pub struct FnScopes { - pub(crate) self_param: Option, + pub self_param: Option, scopes: Arena, scope_for: FxHashMap, } @@ -27,13 +27,13 @@ pub struct ScopeEntry { } #[derive(Debug, PartialEq, Eq)] -pub(crate) struct ScopeData { +pub struct ScopeData { parent: Option, entries: Vec, } impl FnScopes { - pub(crate) fn new(fn_def: ast::FnDef) -> FnScopes { + pub fn new(fn_def: ast::FnDef) -> FnScopes { let mut scopes = FnScopes { self_param: fn_def .param_list() @@ -49,7 +49,7 @@ impl FnScopes { } scopes } - pub(crate) fn entries(&self, scope: ScopeId) -> &[ScopeEntry] { + pub fn entries(&self, scope: ScopeId) -> &[ScopeEntry] { &self.scopes[scope].entries } pub fn scope_chain<'a>(&'a self, node: SyntaxNodeRef) -> impl Iterator + 'a { @@ -57,10 +57,7 @@ impl FnScopes { self.scopes[scope].parent }) } - pub(crate) fn resolve_local_name<'a>( - &'a self, - name_ref: ast::NameRef, - ) -> Option<&'a ScopeEntry> { + pub fn resolve_local_name<'a>(&'a self, name_ref: ast::NameRef) -> Option<&'a ScopeEntry> { let mut shadowed = FxHashSet::default(); let ret = self .scope_chain(name_ref.syntax()) @@ -138,10 +135,10 @@ impl ScopeEntry { }; Some(res) } - pub(crate) fn name(&self) -> &SmolStr { + pub fn name(&self) -> &SmolStr { &self.name } - pub(crate) fn ptr(&self) -> LocalSyntaxPtr { + pub fn ptr(&self) -> LocalSyntaxPtr { self.ptr } } diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 7bf06c7f7..f13f0107e 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -14,7 +14,7 @@ macro_rules! ctry { }; } -pub(crate) mod db; +pub mod db; mod query_definitions; mod function; mod module; @@ -31,36 +31,36 @@ use crate::{ arena::{Arena, Id}, }; -pub(crate) use self::{ +pub use self::{ path::{Path, PathKind}, - module::{Module, ModuleId, Problem}, + module::{Module, ModuleId, Problem, nameres::ItemMap}, function::{Function, FnScopes}, }; pub use self::function::FnSignatureInfo; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(crate) struct FnId(u32); +pub struct FnId(u32); ra_db::impl_numeric_id!(FnId); impl FnId { - pub(crate) fn from_loc( + pub fn from_loc( db: &impl AsRef>, loc: &SourceItemId, ) -> FnId { db.as_ref().loc2id(loc) } - pub(crate) fn loc(self, db: &impl AsRef>) -> SourceItemId { + pub fn loc(self, db: &impl AsRef>) -> SourceItemId { db.as_ref().id2loc(self) } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(crate) struct DefId(u32); +pub struct DefId(u32); ra_db::impl_numeric_id!(DefId); #[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub(crate) enum DefLoc { +pub enum DefLoc { Module { id: ModuleId, source_root: SourceRootId, @@ -71,24 +71,24 @@ pub(crate) enum DefLoc { } impl DefId { - pub(crate) fn loc(self, db: &impl AsRef>) -> DefLoc { + pub fn loc(self, db: &impl AsRef>) -> DefLoc { db.as_ref().id2loc(self) } } impl DefLoc { - pub(crate) fn id(&self, db: &impl AsRef>) -> DefId { + pub fn id(&self, db: &impl AsRef>) -> DefId { db.as_ref().loc2id(&self) } } -pub(crate) enum Def { +pub enum Def { Module(Module), Item, } impl DefId { - pub(crate) fn resolve(self, db: &impl HirDatabase) -> Cancelable { + pub fn resolve(self, db: &impl HirDatabase) -> Cancelable { let loc = self.loc(db); let res = match loc { DefLoc::Module { id, source_root } => { @@ -106,14 +106,14 @@ impl DefId { pub(crate) type SourceFileItemId = Id; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(crate) struct SourceItemId { +pub struct SourceItemId { file_id: FileId, item_id: SourceFileItemId, } /// Maps item's `SyntaxNode`s to `SourceFileItemId` and back. #[derive(Debug, PartialEq, Eq, Default)] -pub(crate) struct SourceFileItems { +pub struct SourceFileItems { arena: Arena, } @@ -121,7 +121,7 @@ impl SourceFileItems { fn alloc(&mut self, item: SyntaxNode) -> SourceFileItemId { self.arena.alloc(item) } - fn id_of(&self, item: SyntaxNodeRef) -> SourceFileItemId { + pub fn id_of(&self, item: SyntaxNodeRef) -> SourceFileItemId { let (id, _item) = self .arena .iter() diff --git a/crates/ra_hir/src/module/imp.rs b/crates/ra_hir/src/module/imp.rs index d55fa3e6b..76ea129a7 100644 --- a/crates/ra_hir/src/module/imp.rs +++ b/crates/ra_hir/src/module/imp.rs @@ -18,7 +18,7 @@ use super::{ }; #[derive(Clone, Hash, PartialEq, Eq, Debug)] -pub(crate) enum Submodule { +pub enum Submodule { Declaration(SmolStr), Definition(SmolStr, ModuleSource), } diff --git a/crates/ra_hir/src/module/mod.rs b/crates/ra_hir/src/module/mod.rs index 81b9f948d..a011fd53e 100644 --- a/crates/ra_hir/src/module/mod.rs +++ b/crates/ra_hir/src/module/mod.rs @@ -18,15 +18,16 @@ use crate::{ arena::{Arena, Id}, }; -pub(crate) use self::nameres::ModuleScope; +pub use self::nameres::ModuleScope; /// `Module` is API entry point to get all the information /// about a particular module. #[derive(Debug, Clone)] -pub(crate) struct Module { +pub struct Module { tree: Arc, source_root_id: SourceRootId, - module_id: ModuleId, + //TODO: make private + pub module_id: ModuleId, } impl Module { @@ -145,17 +146,13 @@ impl Module { } /// Returns a `ModuleScope`: a set of items, visible in this module. - pub(crate) fn scope(&self, db: &impl HirDatabase) -> Cancelable { + pub fn scope(&self, db: &impl HirDatabase) -> Cancelable { let item_map = db.item_map(self.source_root_id)?; let res = item_map.per_module[&self.module_id].clone(); Ok(res) } - pub(crate) fn resolve_path( - &self, - db: &impl HirDatabase, - path: Path, - ) -> Cancelable> { + pub fn resolve_path(&self, db: &impl HirDatabase, path: Path) -> Cancelable> { let mut curr = match path.kind { PathKind::Crate => self.crate_root(), PathKind::Self_ | PathKind::Plain => self.clone(), @@ -188,7 +185,7 @@ impl Module { /// (which can have multiple parents) to the precise world of modules (which /// always have one parent). #[derive(Default, Debug, PartialEq, Eq)] -pub(crate) struct ModuleTree { +pub struct ModuleTree { mods: Arena, links: Arena, } @@ -214,19 +211,19 @@ impl ModuleTree { /// `ModuleSource` is the syntax tree element that produced this module: /// either a file, or an inlinde module. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub(crate) enum ModuleSource { +pub enum ModuleSource { SourceFile(FileId), Module(SourceItemId), } /// An owned syntax node for a module. Unlike `ModuleSource`, /// this holds onto the AST for the whole file. -pub(crate) enum ModuleSourceNode { +pub enum ModuleSourceNode { SourceFile(ast::SourceFileNode), Module(ast::ModuleNode), } -pub(crate) type ModuleId = Id; +pub type ModuleId = Id; type LinkId = Id; #[derive(Clone, Debug, Hash, PartialEq, Eq)] @@ -308,7 +305,7 @@ impl LinkId { } #[derive(Debug, PartialEq, Eq, Hash)] -pub(crate) struct ModuleData { +pub struct ModuleData { source: ModuleSource, parent: Option, children: Vec, @@ -327,21 +324,21 @@ impl ModuleSource { ModuleSource::Module(id) } - pub(crate) fn as_file(self) -> Option { + pub fn as_file(self) -> Option { match self { ModuleSource::SourceFile(f) => Some(f), ModuleSource::Module(..) => None, } } - pub(crate) fn file_id(self) -> FileId { + pub fn file_id(self) -> FileId { match self { ModuleSource::SourceFile(f) => f, ModuleSource::Module(source_item_id) => source_item_id.file_id, } } - pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { + pub fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { match self { ModuleSource::SourceFile(file_id) => { let syntax = db.source_file(file_id); diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs index 513a37646..837a8d5ae 100644 --- a/crates/ra_hir/src/module/nameres.rs +++ b/crates/ra_hir/src/module/nameres.rs @@ -38,20 +38,20 @@ use crate::{ /// Item map is the result of the name resolution. Item map contains, for each /// module, the set of visible items. #[derive(Default, Debug, PartialEq, Eq)] -pub(crate) struct ItemMap { - pub(crate) per_module: FxHashMap, +pub struct ItemMap { + pub per_module: FxHashMap, } #[derive(Debug, Default, PartialEq, Eq, Clone)] -pub(crate) struct ModuleScope { - items: FxHashMap, +pub struct ModuleScope { + pub items: FxHashMap, } impl ModuleScope { - pub(crate) fn entries<'a>(&'a self) -> impl Iterator + 'a { + pub fn entries<'a>(&'a self) -> impl Iterator + 'a { self.items.iter() } - pub(crate) fn get(&self, name: &SmolStr) -> Option<&Resolution> { + pub fn get(&self, name: &SmolStr) -> Option<&Resolution> { self.items.get(name) } } @@ -63,7 +63,7 @@ impl ModuleScope { /// recomputing name res: if `InputModuleItems` are the same, we can avoid /// running name resolution. #[derive(Debug, Default, PartialEq, Eq)] -pub(crate) struct InputModuleItems { +pub struct InputModuleItems { items: Vec, imports: Vec, } @@ -89,13 +89,13 @@ struct Import { } #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) struct NamedImport { - file_item_id: SourceFileItemId, - relative_range: TextRange, +pub struct NamedImport { + pub file_item_id: SourceFileItemId, + pub relative_range: TextRange, } impl NamedImport { - pub(crate) fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange { + pub fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange { let source_item_id = SourceItemId { file_id, item_id: self.file_item_id, @@ -115,11 +115,11 @@ enum ImportKind { /// Resolution is basically `DefId` atm, but it should account for stuff like /// multiple namespaces, ambiguity and errors. #[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct Resolution { +pub struct Resolution { /// None for unresolved - pub(crate) def_id: Option, + pub def_id: Option, /// ident by whitch this is imported into local scope. - pub(crate) import: Option, + pub import: Option, } // #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -336,109 +336,3 @@ where f(module_items) } } - -#[cfg(test)] -mod tests { - use ra_db::FilesDatabase; - use crate::{ - AnalysisChange, - mock_analysis::{MockAnalysis, analysis_and_position}, - hir::{self, HirDatabase}, -}; - use super::*; - - fn item_map(fixture: &str) -> (Arc, ModuleId) { - let (analysis, pos) = analysis_and_position(fixture); - let db = analysis.imp.db; - let source_root = db.file_source_root(pos.file_id); - let descr = hir::Module::guess_from_position(&*db, pos) - .unwrap() - .unwrap(); - let module_id = descr.module_id; - (db.item_map(source_root).unwrap(), module_id) - } - - #[test] - fn test_item_map() { - let (item_map, module_id) = item_map( - " - //- /lib.rs - mod foo; - - use crate::foo::bar::Baz; - <|> - - //- /foo/mod.rs - pub mod bar; - - //- /foo/bar.rs - pub struct Baz; - ", - ); - let name = SmolStr::from("Baz"); - let resolution = &item_map.per_module[&module_id].items[&name]; - assert!(resolution.def_id.is_some()); - } - - #[test] - fn typing_inside_a_function_should_not_invalidate_item_map() { - let mock_analysis = MockAnalysis::with_files( - " - //- /lib.rs - mod foo; - - use crate::foo::bar::Baz; - - fn foo() -> i32 { - 1 + 1 - } - //- /foo/mod.rs - pub mod bar; - - //- /foo/bar.rs - pub struct Baz; - ", - ); - - let file_id = mock_analysis.id_of("/lib.rs"); - let mut host = mock_analysis.analysis_host(); - - let source_root = host.analysis().imp.db.file_source_root(file_id); - - { - let db = host.analysis().imp.db; - let events = db.log_executed(|| { - db.item_map(source_root).unwrap(); - }); - assert!(format!("{:?}", events).contains("item_map")) - } - - let mut change = AnalysisChange::new(); - - change.change_file( - file_id, - " - mod foo; - - use crate::foo::bar::Baz; - - fn foo() -> i32 { 92 } - " - .to_string(), - ); - - host.apply_change(change); - - { - let db = host.analysis().imp.db; - let events = db.log_executed(|| { - db.item_map(source_root).unwrap(); - }); - assert!( - !format!("{:?}", events).contains("_item_map"), - "{:#?}", - events - ) - } - } -} diff --git a/crates/ra_hir/src/path.rs b/crates/ra_hir/src/path.rs index 8279daf4b..4a2e427cd 100644 --- a/crates/ra_hir/src/path.rs +++ b/crates/ra_hir/src/path.rs @@ -1,13 +1,13 @@ use ra_syntax::{SmolStr, ast, AstNode, TextRange}; #[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct Path { - pub(crate) kind: PathKind, - pub(crate) segments: Vec, +pub struct Path { + pub kind: PathKind, + pub segments: Vec, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) enum PathKind { +pub enum PathKind { Plain, Self_, Super, @@ -16,14 +16,14 @@ pub(crate) enum PathKind { impl Path { /// Calls `cb` with all paths, represented by this use item. - pub(crate) fn expand_use_item(item: ast::UseItem, mut cb: impl FnMut(Path, Option)) { + pub fn expand_use_item(item: ast::UseItem, mut cb: impl FnMut(Path, Option)) { if let Some(tree) = item.use_tree() { expand_use_tree(None, tree, &mut cb); } } /// Converts an `ast::Path` to `Path`. Works with use trees. - pub(crate) fn from_ast(mut path: ast::Path) -> Option { + pub fn from_ast(mut path: ast::Path) -> Option { let mut kind = PathKind::Plain; let mut segments = Vec::new(); loop { @@ -64,7 +64,7 @@ impl Path { } /// `true` is this path is a single identifier, like `foo` - pub(crate) fn is_ident(&self) -> bool { + pub fn is_ident(&self) -> bool { self.kind == PathKind::Plain && self.segments.len() == 1 } } -- cgit v1.2.3