From 5208c2aa930ae452e062dcdc2563c1bbb67d2e4a Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 19 Jan 2019 21:23:26 +0100 Subject: Sketching the resolver API --- crates/ra_hir/src/expr/scope.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'crates/ra_hir/src/expr') diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs index b7971088d..504a087a3 100644 --- a/crates/ra_hir/src/expr/scope.rs +++ b/crates/ra_hir/src/expr/scope.rs @@ -73,6 +73,7 @@ impl ExprScopes { context_expr: ExprId, name: Name, ) -> Option<&'a ScopeEntry> { + // TODO replace by Resolver::resolve_name let mut shadowed = FxHashSet::default(); let ret = self .scope_chain_for(context_expr) @@ -179,6 +180,7 @@ impl ScopesWithSyntaxMapping { // XXX: during completion, cursor might be outside of any particular // expression. Try to figure out the correct scope... + // TODO: move this to source binder? fn adjust(&self, ptr: SyntaxNodePtr, original_scope: ScopeId, offset: TextUnit) -> ScopeId { let r = ptr.range(); let child_scopes = self -- cgit v1.2.3 From 758bc72873efe36f579236d1abf240d14866fd82 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 23 Jan 2019 23:08:41 +0100 Subject: Implement methods to build a resolver --- crates/ra_hir/src/expr/scope.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src/expr') diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs index 504a087a3..887ad8dd8 100644 --- a/crates/ra_hir/src/expr/scope.rs +++ b/crates/ra_hir/src/expr/scope.rs @@ -338,6 +338,7 @@ pub struct ReferenceDescriptor { mod tests { use ra_syntax::{SourceFile, algo::find_node_at_offset}; use test_utils::{extract_offset, assert_eq_text}; + use ra_arena::ArenaId; use crate::expr; @@ -356,7 +357,10 @@ mod tests { let file = SourceFile::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 body_hir = expr::collect_fn_body_syntax(fn_def); + let irrelevant_function = Function { + id: crate::ids::FunctionId::from_raw(0.into()), + }; + let body_hir = expr::collect_fn_body_syntax(irrelevant_function, fn_def); let scopes = ExprScopes::new(Arc::clone(body_hir.body())); let scopes = ScopesWithSyntaxMapping { scopes: Arc::new(scopes), @@ -456,7 +460,10 @@ mod tests { 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 body_hir = expr::collect_fn_body_syntax(fn_def); + let irrelevant_function = Function { + id: crate::ids::FunctionId::from_raw(0.into()), + }; + let body_hir = expr::collect_fn_body_syntax(irrelevant_function, fn_def); let scopes = ExprScopes::new(Arc::clone(body_hir.body())); let scopes = ScopesWithSyntaxMapping { scopes: Arc::new(scopes), -- cgit v1.2.3 From 33ff7b56ff353410e7bcb7aed27004d4f0a57d8e Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 27 Jan 2019 20:50:57 +0100 Subject: Use the new Resolver API in completion --- crates/ra_hir/src/expr/scope.rs | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) (limited to 'crates/ra_hir/src/expr') diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs index 887ad8dd8..23f1c5e7f 100644 --- a/crates/ra_hir/src/expr/scope.rs +++ b/crates/ra_hir/src/expr/scope.rs @@ -62,25 +62,11 @@ impl ExprScopes { &self.scopes[scope].entries } - pub fn scope_chain_for<'a>(&'a self, expr: ExprId) -> impl Iterator + 'a { - generate(self.scope_for(expr), move |&scope| { - self.scopes[scope].parent - }) - } - - pub fn resolve_local_name<'a>( + pub fn scope_chain_for<'a>( &'a self, - context_expr: ExprId, - name: Name, - ) -> Option<&'a ScopeEntry> { - // TODO replace by Resolver::resolve_name - let mut shadowed = FxHashSet::default(); - let ret = self - .scope_chain_for(context_expr) - .flat_map(|scope| self.entries(scope).iter()) - .filter(|entry| shadowed.insert(entry.name())) - .find(|entry| entry.name() == &name); - ret + scope: Option, + ) -> impl Iterator + 'a { + generate(scope, move |&scope| self.scopes[scope].parent) } fn root_scope(&mut self) -> ScopeId { @@ -123,7 +109,7 @@ impl ExprScopes { self.scope_for.insert(node, scope); } - fn scope_for(&self, expr: ExprId) -> Option { + pub fn scope_for(&self, expr: ExprId) -> Option { self.scope_for.get(&expr).map(|&scope| scope) } } @@ -151,18 +137,14 @@ impl ScopeEntryWithSyntax { } impl ScopesWithSyntaxMapping { - pub fn scope_chain<'a>(&'a self, node: &SyntaxNode) -> impl Iterator + 'a { + fn scope_chain<'a>(&'a self, node: &SyntaxNode) -> impl Iterator + 'a { generate(self.scope_for(node), move |&scope| { self.scopes.scopes[scope].parent }) } - pub fn scope_chain_for_offset<'a>( - &'a self, - offset: TextUnit, - ) -> impl Iterator + 'a { - let scope = self - .scopes + pub fn scope_for_offset<'a>(&'a self, offset: TextUnit) -> Option { + self.scopes .scope_for .iter() .filter_map(|(id, scope)| Some((self.syntax_mapping.expr_syntax(*id)?, scope))) @@ -173,9 +155,7 @@ impl ScopesWithSyntaxMapping { ptr.range().len(), ) }) - .map(|(ptr, scope)| self.adjust(ptr, *scope, offset)); - - generate(scope, move |&scope| self.scopes.scopes[scope].parent) + .map(|(ptr, scope)| self.adjust(ptr, *scope, offset)) } // XXX: during completion, cursor might be outside of any particular -- cgit v1.2.3 From afce8e442639fa9ed954b3659a2d1eccb7d80113 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Tue, 29 Jan 2019 20:49:31 +0100 Subject: Use the new Resolver API for goto def --- crates/ra_hir/src/expr/scope.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir/src/expr') diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs index 23f1c5e7f..9202e3671 100644 --- a/crates/ra_hir/src/expr/scope.rs +++ b/crates/ra_hir/src/expr/scope.rs @@ -58,6 +58,10 @@ impl ExprScopes { scopes } + pub fn body(&self) -> Arc { + self.body.clone() + } + pub fn entries(&self, scope: ScopeId) -> &[ScopeEntry] { &self.scopes[scope].entries } @@ -220,7 +224,7 @@ impl ScopesWithSyntaxMapping { .collect() } - fn scope_for(&self, node: &SyntaxNode) -> Option { + pub fn scope_for(&self, node: &SyntaxNode) -> Option { node.ancestors() .map(SyntaxNodePtr::new) .filter_map(|ptr| self.syntax_mapping.syntax_expr(ptr)) -- cgit v1.2.3