aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/source_analyzer.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/source_analyzer.rs')
-rw-r--r--crates/ra_hir/src/source_analyzer.rs31
1 files changed, 26 insertions, 5 deletions
diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs
index b655e2c32..c650a9e08 100644
--- a/crates/ra_hir/src/source_analyzer.rs
+++ b/crates/ra_hir/src/source_analyzer.rs
@@ -11,9 +11,9 @@ use either::Either;
11use hir_def::{ 11use hir_def::{
12 body::{ 12 body::{
13 scope::{ExprScopes, ScopeId}, 13 scope::{ExprScopes, ScopeId},
14 BodySourceMap, 14 Body, BodySourceMap,
15 }, 15 },
16 expr::{ExprId, PatId}, 16 expr::{ExprId, Pat, PatId},
17 resolver::{resolver_for_scope, Resolver, TypeNs, ValueNs}, 17 resolver::{resolver_for_scope, Resolver, TypeNs, ValueNs},
18 AsMacroCall, DefWithBodyId, 18 AsMacroCall, DefWithBodyId,
19}; 19};
@@ -25,8 +25,8 @@ use ra_syntax::{
25}; 25};
26 26
27use crate::{ 27use crate::{
28 db::HirDatabase, Adt, Const, EnumVariant, Function, Local, MacroDef, Path, Static, Struct, 28 db::HirDatabase, Adt, Const, EnumVariant, Function, Local, MacroDef, ModuleDef, Path, Static,
29 Trait, Type, TypeAlias, TypeParam, 29 Struct, Trait, Type, TypeAlias, TypeParam,
30}; 30};
31 31
32/// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of 32/// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of
@@ -35,6 +35,7 @@ use crate::{
35pub(crate) struct SourceAnalyzer { 35pub(crate) struct SourceAnalyzer {
36 file_id: HirFileId, 36 file_id: HirFileId,
37 pub(crate) resolver: Resolver, 37 pub(crate) resolver: Resolver,
38 body: Option<Arc<Body>>,
38 body_source_map: Option<Arc<BodySourceMap>>, 39 body_source_map: Option<Arc<BodySourceMap>>,
39 infer: Option<Arc<InferenceResult>>, 40 infer: Option<Arc<InferenceResult>>,
40 scopes: Option<Arc<ExprScopes>>, 41 scopes: Option<Arc<ExprScopes>>,
@@ -66,7 +67,7 @@ impl SourceAnalyzer {
66 node: InFile<&SyntaxNode>, 67 node: InFile<&SyntaxNode>,
67 offset: Option<TextUnit>, 68 offset: Option<TextUnit>,
68 ) -> SourceAnalyzer { 69 ) -> SourceAnalyzer {
69 let (_body, source_map) = db.body_with_source_map(def); 70 let (body, source_map) = db.body_with_source_map(def);
70 let scopes = db.expr_scopes(def); 71 let scopes = db.expr_scopes(def);
71 let scope = match offset { 72 let scope = match offset {
72 None => scope_for(&scopes, &source_map, node), 73 None => scope_for(&scopes, &source_map, node),
@@ -75,6 +76,7 @@ impl SourceAnalyzer {
75 let resolver = resolver_for_scope(db, def, scope); 76 let resolver = resolver_for_scope(db, def, scope);
76 SourceAnalyzer { 77 SourceAnalyzer {
77 resolver, 78 resolver,
79 body: Some(body),
78 body_source_map: Some(source_map), 80 body_source_map: Some(source_map),
79 infer: Some(db.infer(def)), 81 infer: Some(db.infer(def)),
80 scopes: Some(scopes), 82 scopes: Some(scopes),
@@ -88,6 +90,7 @@ impl SourceAnalyzer {
88 ) -> SourceAnalyzer { 90 ) -> SourceAnalyzer {
89 SourceAnalyzer { 91 SourceAnalyzer {
90 resolver, 92 resolver,
93 body: None,
91 body_source_map: None, 94 body_source_map: None,
92 infer: None, 95 infer: None,
93 scopes: None, 96 scopes: None,
@@ -197,6 +200,24 @@ impl SourceAnalyzer {
197 self.resolver.resolve_path_as_macro(db, path.mod_path()).map(|it| it.into()) 200 self.resolver.resolve_path_as_macro(db, path.mod_path()).map(|it| it.into())
198 } 201 }
199 202
203 pub(crate) fn resolve_bind_pat_to_const(
204 &self,
205 db: &impl HirDatabase,
206 pat: &ast::BindPat,
207 ) -> Option<ModuleDef> {
208 let pat_id = self.pat_id(&pat.clone().into())?;
209 let body = self.body.as_ref()?;
210 let path = match &body[pat_id] {
211 Pat::Path(path) => path,
212 _ => return None,
213 };
214 let res = resolve_hir_path(db, &self.resolver, &path)?;
215 match res {
216 PathResolution::Def(def) => Some(def),
217 _ => None,
218 }
219 }
220
200 pub(crate) fn resolve_path( 221 pub(crate) fn resolve_path(
201 &self, 222 &self,
202 db: &impl HirDatabase, 223 db: &impl HirDatabase,