diff options
-rw-r--r-- | crates/ra_assists/src/inline_local_variable.rs | 11 | ||||
-rw-r--r-- | crates/ra_hir/src/expr/scope.rs | 7 | ||||
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 11 | ||||
-rw-r--r-- | crates/ra_ide_api/src/references.rs | 22 |
4 files changed, 28 insertions, 23 deletions
diff --git a/crates/ra_assists/src/inline_local_variable.rs b/crates/ra_assists/src/inline_local_variable.rs index 950c2910b..9493acec9 100644 --- a/crates/ra_assists/src/inline_local_variable.rs +++ b/crates/ra_assists/src/inline_local_variable.rs | |||
@@ -1,7 +1,4 @@ | |||
1 | use hir::{ | 1 | use hir::db::HirDatabase; |
2 | db::HirDatabase, | ||
3 | source_binder::function_from_child_node, | ||
4 | }; | ||
5 | use ra_syntax::{ | 2 | use ra_syntax::{ |
6 | ast::{self, AstNode, AstToken, PatKind, ExprKind}, | 3 | ast::{self, AstNode, AstToken, PatKind, ExprKind}, |
7 | TextRange, | 4 | TextRange, |
@@ -29,10 +26,8 @@ pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx<impl HirDatabase>) -> Opt | |||
29 | } else { | 26 | } else { |
30 | let_stmt.syntax().range() | 27 | let_stmt.syntax().range() |
31 | }; | 28 | }; |
32 | 29 | let analyzer = hir::SourceAnalyzer::new(ctx.db, ctx.frange.file_id, bind_pat.syntax(), None); | |
33 | let function = function_from_child_node(ctx.db, ctx.frange.file_id, bind_pat.syntax())?; | 30 | let refs = analyzer.find_all_refs(bind_pat)?; |
34 | let scope = function.scopes(ctx.db); | ||
35 | let refs = scope.find_all_refs(bind_pat); | ||
36 | 31 | ||
37 | let mut wrap_in_parens = vec![true; refs.len()]; | 32 | let mut wrap_in_parens = vec![true; refs.len()]; |
38 | 33 | ||
diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs index 404c979eb..a9be9fbdb 100644 --- a/crates/ra_hir/src/expr/scope.rs +++ b/crates/ra_hir/src/expr/scope.rs | |||
@@ -173,7 +173,10 @@ impl ScopesWithSourceMap { | |||
173 | .unwrap_or(original_scope) | 173 | .unwrap_or(original_scope) |
174 | } | 174 | } |
175 | 175 | ||
176 | pub fn resolve_local_name(&self, name_ref: &ast::NameRef) -> Option<ScopeEntryWithSyntax> { | 176 | pub(crate) fn resolve_local_name( |
177 | &self, | ||
178 | name_ref: &ast::NameRef, | ||
179 | ) -> Option<ScopeEntryWithSyntax> { | ||
177 | let mut shadowed = FxHashSet::default(); | 180 | let mut shadowed = FxHashSet::default(); |
178 | let name = name_ref.as_name(); | 181 | let name = name_ref.as_name(); |
179 | let ret = self | 182 | let ret = self |
@@ -190,7 +193,7 @@ impl ScopesWithSourceMap { | |||
190 | }) | 193 | }) |
191 | } | 194 | } |
192 | 195 | ||
193 | pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec<ReferenceDescriptor> { | 196 | pub(crate) fn find_all_refs(&self, pat: &ast::BindPat) -> Vec<ReferenceDescriptor> { |
194 | let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); | 197 | let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); |
195 | let ptr = Either::A(AstPtr::new(pat.into())); | 198 | let ptr = Either::A(AstPtr::new(pat.into())); |
196 | fn_def | 199 | fn_def |
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 0d2746ac0..bdb300311 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -17,6 +17,7 @@ use ra_syntax::{ | |||
17 | use crate::{ | 17 | use crate::{ |
18 | HirDatabase, Function, Struct, Enum, Const, Static, Either, DefWithBody, | 18 | HirDatabase, Function, Struct, Enum, Const, Static, Either, DefWithBody, |
19 | AsName, Module, HirFileId, Crate, Trait, Resolver, | 19 | AsName, Module, HirFileId, Crate, Trait, Resolver, |
20 | expr::scope::{ReferenceDescriptor, ScopeEntryWithSyntax}, | ||
20 | ids::LocationCtx, | 21 | ids::LocationCtx, |
21 | expr, AstId | 22 | expr, AstId |
22 | }; | 23 | }; |
@@ -222,6 +223,7 @@ pub struct SourceAnalyzer { | |||
222 | resolver: Resolver, | 223 | resolver: Resolver, |
223 | body_source_map: Option<Arc<crate::expr::BodySourceMap>>, | 224 | body_source_map: Option<Arc<crate::expr::BodySourceMap>>, |
224 | infer: Option<Arc<crate::ty::InferenceResult>>, | 225 | infer: Option<Arc<crate::ty::InferenceResult>>, |
226 | scopes: Option<crate::expr::ScopesWithSourceMap>, | ||
225 | } | 227 | } |
226 | 228 | ||
227 | #[derive(Debug, Clone, PartialEq, Eq)] | 229 | #[derive(Debug, Clone, PartialEq, Eq)] |
@@ -248,6 +250,7 @@ impl SourceAnalyzer { | |||
248 | resolver: resolver_for_node(db, file_id, node, offset), | 250 | resolver: resolver_for_node(db, file_id, node, offset), |
249 | body_source_map: def_with_body.map(|it| it.body_source_map(db)), | 251 | body_source_map: def_with_body.map(|it| it.body_source_map(db)), |
250 | infer: def_with_body.map(|it| it.infer(db)), | 252 | infer: def_with_body.map(|it| it.infer(db)), |
253 | scopes: def_with_body.map(|it| it.scopes(db)), | ||
251 | } | 254 | } |
252 | } | 255 | } |
253 | 256 | ||
@@ -302,6 +305,14 @@ impl SourceAnalyzer { | |||
302 | Some(res) | 305 | Some(res) |
303 | } | 306 | } |
304 | 307 | ||
308 | pub fn find_all_refs(&self, pat: &ast::BindPat) -> Option<Vec<ReferenceDescriptor>> { | ||
309 | self.scopes.as_ref().map(|it| it.find_all_refs(pat)) | ||
310 | } | ||
311 | |||
312 | pub fn resolve_local_name(&self, name_ref: &ast::NameRef) -> Option<ScopeEntryWithSyntax> { | ||
313 | self.scopes.as_ref()?.resolve_local_name(name_ref) | ||
314 | } | ||
315 | |||
305 | #[cfg(test)] | 316 | #[cfg(test)] |
306 | pub(crate) fn body_source_map(&self) -> Arc<crate::expr::BodySourceMap> { | 317 | pub(crate) fn body_source_map(&self) -> Arc<crate::expr::BodySourceMap> { |
307 | self.body_source_map.clone().unwrap() | 318 | self.body_source_map.clone().unwrap() |
diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs index 3e30e047c..ee2c1d0f0 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs | |||
@@ -61,12 +61,11 @@ pub(crate) fn find_all_refs( | |||
61 | position: FilePosition, | 61 | position: FilePosition, |
62 | ) -> Option<ReferenceSearchResult> { | 62 | ) -> Option<ReferenceSearchResult> { |
63 | let file = db.parse(position.file_id); | 63 | let file = db.parse(position.file_id); |
64 | let (binding, descr) = find_binding(db, &file, position)?; | 64 | let (binding, analyzer) = find_binding(db, &file, position)?; |
65 | let declaration = NavigationTarget::from_bind_pat(position.file_id, binding); | 65 | let declaration = NavigationTarget::from_bind_pat(position.file_id, binding); |
66 | 66 | ||
67 | let references = descr | 67 | let references = analyzer |
68 | .scopes(db) | 68 | .find_all_refs(binding)? |
69 | .find_all_refs(binding) | ||
70 | .into_iter() | 69 | .into_iter() |
71 | .map(move |ref_desc| FileRange { file_id: position.file_id, range: ref_desc.range }) | 70 | .map(move |ref_desc| FileRange { file_id: position.file_id, range: ref_desc.range }) |
72 | .collect::<Vec<_>>(); | 71 | .collect::<Vec<_>>(); |
@@ -77,21 +76,18 @@ pub(crate) fn find_all_refs( | |||
77 | db: &RootDatabase, | 76 | db: &RootDatabase, |
78 | source_file: &'a SourceFile, | 77 | source_file: &'a SourceFile, |
79 | position: FilePosition, | 78 | position: FilePosition, |
80 | ) -> Option<(&'a ast::BindPat, hir::Function)> { | 79 | ) -> Option<(&'a ast::BindPat, hir::SourceAnalyzer)> { |
81 | let syntax = source_file.syntax(); | 80 | let syntax = source_file.syntax(); |
82 | if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) { | 81 | if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) { |
83 | let descr = | 82 | let analyzer = hir::SourceAnalyzer::new(db, position.file_id, binding.syntax(), None); |
84 | source_binder::function_from_child_node(db, position.file_id, binding.syntax())?; | 83 | return Some((binding, analyzer)); |
85 | return Some((binding, descr)); | ||
86 | }; | 84 | }; |
87 | let name_ref = find_node_at_offset::<ast::NameRef>(syntax, position.offset)?; | 85 | let name_ref = find_node_at_offset::<ast::NameRef>(syntax, position.offset)?; |
88 | let descr = | 86 | let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None); |
89 | source_binder::function_from_child_node(db, position.file_id, name_ref.syntax())?; | 87 | let resolved = analyzer.resolve_local_name(name_ref)?; |
90 | let scope = descr.scopes(db); | ||
91 | let resolved = scope.resolve_local_name(name_ref)?; | ||
92 | if let Either::A(ptr) = resolved.ptr() { | 88 | if let Either::A(ptr) = resolved.ptr() { |
93 | if let ast::PatKind::BindPat(binding) = ptr.to_node(source_file).kind() { | 89 | if let ast::PatKind::BindPat(binding) = ptr.to_node(source_file).kind() { |
94 | return Some((binding, descr)); | 90 | return Some((binding, analyzer)); |
95 | } | 91 | } |
96 | } | 92 | } |
97 | None | 93 | None |