@@ -11,9 +11,9 @@ use either::Either;
1111use hir_def:: {
1212 body:: {
1313 scope:: { ExprScopes , ScopeId } ,
14- BodySourceMap ,
14+ Body , BodySourceMap ,
1515 } ,
16- expr:: { ExprId , PatId } ,
16+ expr:: { ExprId , Pat , PatId } ,
1717 resolver:: { resolver_for_scope, Resolver , TypeNs , ValueNs } ,
1818 AsMacroCall , DefWithBodyId ,
1919} ;
@@ -25,8 +25,8 @@ use ra_syntax::{
2525} ;
2626
2727use crate :: {
28- db:: HirDatabase , Adt , Const , EnumVariant , Function , Local , MacroDef , Path , Static , Struct ,
29- Trait , Type , TypeAlias , TypeParam ,
28+ db:: HirDatabase , Adt , Const , EnumVariant , Function , Local , MacroDef , ModuleDef , Path , Static ,
29+ Struct , Trait , Type , TypeAlias , TypeParam ,
3030} ;
3131
3232/// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of
@@ -35,6 +35,7 @@ use crate::{
3535pub ( crate ) struct SourceAnalyzer {
3636 file_id : HirFileId ,
3737 pub ( crate ) resolver : Resolver ,
38+ body : Option < Arc < Body > > ,
3839 body_source_map : Option < Arc < BodySourceMap > > ,
3940 infer : Option < Arc < InferenceResult > > ,
4041 scopes : Option < Arc < ExprScopes > > ,
@@ -66,7 +67,7 @@ impl SourceAnalyzer {
6667 node : InFile < & SyntaxNode > ,
6768 offset : Option < TextUnit > ,
6869 ) -> SourceAnalyzer {
69- let ( _body , source_map) = db. body_with_source_map ( def) ;
70+ let ( body , source_map) = db. body_with_source_map ( def) ;
7071 let scopes = db. expr_scopes ( def) ;
7172 let scope = match offset {
7273 None => scope_for ( & scopes, & source_map, node) ,
@@ -75,6 +76,7 @@ impl SourceAnalyzer {
7576 let resolver = resolver_for_scope ( db, def, scope) ;
7677 SourceAnalyzer {
7778 resolver,
79+ body : Some ( body) ,
7880 body_source_map : Some ( source_map) ,
7981 infer : Some ( db. infer ( def) ) ,
8082 scopes : Some ( scopes) ,
@@ -88,6 +90,7 @@ impl SourceAnalyzer {
8890 ) -> SourceAnalyzer {
8991 SourceAnalyzer {
9092 resolver,
93+ body : None ,
9194 body_source_map : None ,
9295 infer : None ,
9396 scopes : None ,
@@ -197,6 +200,24 @@ impl SourceAnalyzer {
197200 self . resolver . resolve_path_as_macro ( db, path. mod_path ( ) ) . map ( |it| it. into ( ) )
198201 }
199202
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+
200221 pub ( crate ) fn resolve_path (
201222 & self ,
202223 db : & impl HirDatabase ,
0 commit comments