diff options
Diffstat (limited to 'crates/ra_hir/src/source_binder.rs')
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 52 |
1 files changed, 47 insertions, 5 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 9dae4c3d1..182ed4c91 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -9,11 +9,11 @@ use ra_db::{FileId, FilePosition}; | |||
9 | use ra_syntax::{ | 9 | use ra_syntax::{ |
10 | SyntaxNode, | 10 | SyntaxNode, |
11 | ast::{self, AstNode, NameOwner}, | 11 | ast::{self, AstNode, NameOwner}, |
12 | algo::{find_node_at_offset, find_leaf_at_offset}, | 12 | algo::{find_node_at_offset, find_token_at_offset}, |
13 | }; | 13 | }; |
14 | 14 | ||
15 | use crate::{ | 15 | use crate::{ |
16 | HirDatabase, Function, Struct, Enum, | 16 | HirDatabase, Function, Struct, Enum,Const,Static, |
17 | AsName, Module, HirFileId, Crate, Trait, Resolver, | 17 | AsName, Module, HirFileId, Crate, Trait, Resolver, |
18 | ids::LocationCtx, | 18 | ids::LocationCtx, |
19 | expr, AstId | 19 | expr, AstId |
@@ -87,6 +87,27 @@ fn module_from_source( | |||
87 | ) | 87 | ) |
88 | } | 88 | } |
89 | 89 | ||
90 | pub fn const_from_source( | ||
91 | db: &impl HirDatabase, | ||
92 | file_id: FileId, | ||
93 | const_def: &ast::ConstDef, | ||
94 | ) -> Option<Const> { | ||
95 | let module = module_from_child_node(db, file_id, const_def.syntax())?; | ||
96 | let res = const_from_module(db, module, const_def); | ||
97 | Some(res) | ||
98 | } | ||
99 | |||
100 | pub fn const_from_module( | ||
101 | db: &impl HirDatabase, | ||
102 | module: Module, | ||
103 | const_def: &ast::ConstDef, | ||
104 | ) -> Const { | ||
105 | let (file_id, _) = module.definition_source(db); | ||
106 | let file_id = file_id.into(); | ||
107 | let ctx = LocationCtx::new(db, module, file_id); | ||
108 | Const { id: ctx.to_def(const_def) } | ||
109 | } | ||
110 | |||
90 | pub fn function_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Function> { | 111 | pub fn function_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Function> { |
91 | let file = db.parse(position.file_id); | 112 | let file = db.parse(position.file_id); |
92 | let fn_def = find_node_at_offset::<ast::FnDef>(file.syntax(), position.offset)?; | 113 | let fn_def = find_node_at_offset::<ast::FnDef>(file.syntax(), position.offset)?; |
@@ -134,6 +155,27 @@ pub fn struct_from_module( | |||
134 | Struct { id: ctx.to_def(struct_def) } | 155 | Struct { id: ctx.to_def(struct_def) } |
135 | } | 156 | } |
136 | 157 | ||
158 | pub fn static_from_source( | ||
159 | db: &impl HirDatabase, | ||
160 | file_id: FileId, | ||
161 | static_def: &ast::StaticDef, | ||
162 | ) -> Option<Static> { | ||
163 | let module = module_from_child_node(db, file_id, static_def.syntax())?; | ||
164 | let res = static_from_module(db, module, static_def); | ||
165 | Some(res) | ||
166 | } | ||
167 | |||
168 | pub fn static_from_module( | ||
169 | db: &impl HirDatabase, | ||
170 | module: Module, | ||
171 | static_def: &ast::StaticDef, | ||
172 | ) -> Static { | ||
173 | let (file_id, _) = module.definition_source(db); | ||
174 | let file_id = file_id.into(); | ||
175 | let ctx = LocationCtx::new(db, module, file_id); | ||
176 | Static { id: ctx.to_def(static_def) } | ||
177 | } | ||
178 | |||
137 | pub fn enum_from_module(db: &impl HirDatabase, module: Module, enum_def: &ast::EnumDef) -> Enum { | 179 | pub fn enum_from_module(db: &impl HirDatabase, module: Module, enum_def: &ast::EnumDef) -> Enum { |
138 | let (file_id, _) = module.definition_source(db); | 180 | let (file_id, _) = module.definition_source(db); |
139 | let file_id = file_id.into(); | 181 | let file_id = file_id.into(); |
@@ -155,9 +197,9 @@ pub fn trait_from_module( | |||
155 | pub fn resolver_for_position(db: &impl HirDatabase, position: FilePosition) -> Resolver { | 197 | pub fn resolver_for_position(db: &impl HirDatabase, position: FilePosition) -> Resolver { |
156 | let file_id = position.file_id; | 198 | let file_id = position.file_id; |
157 | let file = db.parse(file_id); | 199 | let file = db.parse(file_id); |
158 | find_leaf_at_offset(file.syntax(), position.offset) | 200 | find_token_at_offset(file.syntax(), position.offset) |
159 | .find_map(|node| { | 201 | .find_map(|token| { |
160 | node.ancestors().find_map(|node| { | 202 | token.parent().ancestors().find_map(|node| { |
161 | if ast::Expr::cast(node).is_some() || ast::Block::cast(node).is_some() { | 203 | if ast::Expr::cast(node).is_some() || ast::Block::cast(node).is_some() { |
162 | if let Some(func) = function_from_child_node(db, file_id, node) { | 204 | if let Some(func) = function_from_child_node(db, file_id, node) { |
163 | let scopes = func.scopes(db); | 205 | let scopes = func.scopes(db); |