aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-15 21:40:54 +0000
committerAleksey Kladov <[email protected]>2019-11-15 21:40:54 +0000
commit0404e647e69f758ac84c742b06dddefb01cc4ad6 (patch)
treec0fb90f2bfa0cec5eb62e80bfe5cf7c009a3fc18 /crates
parentbd8af6a413bb2f762bb696d20727e21cb2d4cb62 (diff)
Prepare SourceAnalyzer for macros
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/source_binder.rs13
-rw-r--r--crates/ra_hir_expand/src/lib.rs10
2 files changed, 14 insertions, 9 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index f3bccd8ed..540ddd0b5 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -91,7 +91,7 @@ fn def_with_body_from_child_node(
91#[derive(Debug)] 91#[derive(Debug)]
92pub struct SourceAnalyzer { 92pub struct SourceAnalyzer {
93 // FIXME: this doesn't handle macros at all 93 // FIXME: this doesn't handle macros at all
94 file_id: FileId, 94 file_id: HirFileId,
95 resolver: Resolver, 95 resolver: Resolver,
96 body_owner: Option<DefWithBody>, 96 body_owner: Option<DefWithBody>,
97 body_source_map: Option<Arc<BodySourceMap>>, 97 body_source_map: Option<Arc<BodySourceMap>>,
@@ -159,7 +159,7 @@ impl SourceAnalyzer {
159 body_source_map: Some(source_map), 159 body_source_map: Some(source_map),
160 infer: Some(def.infer(db)), 160 infer: Some(def.infer(db)),
161 scopes: Some(scopes), 161 scopes: Some(scopes),
162 file_id, 162 file_id: file_id.into(),
163 } 163 }
164 } else { 164 } else {
165 SourceAnalyzer { 165 SourceAnalyzer {
@@ -171,18 +171,18 @@ impl SourceAnalyzer {
171 body_source_map: None, 171 body_source_map: None,
172 infer: None, 172 infer: None,
173 scopes: None, 173 scopes: None,
174 file_id, 174 file_id: file_id.into(),
175 } 175 }
176 } 176 }
177 } 177 }
178 178
179 fn expr_id(&self, expr: &ast::Expr) -> Option<ExprId> { 179 fn expr_id(&self, expr: &ast::Expr) -> Option<ExprId> {
180 let src = Source { file_id: self.file_id.into(), ast: expr }; 180 let src = Source { file_id: self.file_id, ast: expr };
181 self.body_source_map.as_ref()?.node_expr(src) 181 self.body_source_map.as_ref()?.node_expr(src)
182 } 182 }
183 183
184 fn pat_id(&self, pat: &ast::Pat) -> Option<PatId> { 184 fn pat_id(&self, pat: &ast::Pat) -> Option<PatId> {
185 let src = Source { file_id: self.file_id.into(), ast: pat }; 185 let src = Source { file_id: self.file_id, ast: pat };
186 self.body_source_map.as_ref()?.node_pat(src) 186 self.body_source_map.as_ref()?.node_pat(src)
187 } 187 }
188 188
@@ -290,8 +290,7 @@ impl SourceAnalyzer {
290 let name = name_ref.as_name(); 290 let name = name_ref.as_name();
291 let source_map = self.body_source_map.as_ref()?; 291 let source_map = self.body_source_map.as_ref()?;
292 let scopes = self.scopes.as_ref()?; 292 let scopes = self.scopes.as_ref()?;
293 let scope = 293 let scope = scope_for(scopes, source_map, Source::new(self.file_id, name_ref.syntax()))?;
294 scope_for(scopes, source_map, Source::new(self.file_id.into(), name_ref.syntax()))?;
295 let entry = scopes.resolve_name_in_scope(scope, &name)?; 294 let entry = scopes.resolve_name_in_scope(scope, &name)?;
296 Some(ScopeEntryWithSyntax { 295 Some(ScopeEntryWithSyntax {
297 name: entry.name().clone(), 296 name: entry.name().clone(),
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs
index 6c2cc3af3..26531cb05 100644
--- a/crates/ra_hir_expand/src/lib.rs
+++ b/crates/ra_hir_expand/src/lib.rs
@@ -223,6 +223,7 @@ impl<N: AstNode> AstId<N> {
223 } 223 }
224} 224}
225 225
226/// FIXME: https://github.com/matklad/with ?
226#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] 227#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
227pub struct Source<T> { 228pub struct Source<T> {
228 pub file_id: HirFileId, 229 pub file_id: HirFileId,
@@ -236,11 +237,16 @@ impl<T> Source<T> {
236 Source { file_id, ast } 237 Source { file_id, ast }
237 } 238 }
238 239
240 // Similarly, naming here is stupid...
241 pub fn with_ast<U>(&self, ast: U) -> Source<U> {
242 Source::new(self.file_id, ast)
243 }
244
239 pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> { 245 pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> {
240 Source { file_id: self.file_id, ast: f(self.ast) } 246 Source::new(self.file_id, f(self.ast))
241 } 247 }
242 pub fn as_ref(&self) -> Source<&T> { 248 pub fn as_ref(&self) -> Source<&T> {
243 Source { file_id: self.file_id, ast: &self.ast } 249 self.with_ast(&self.ast)
244 } 250 }
245 pub fn file_syntax(&self, db: &impl db::AstDatabase) -> SyntaxNode { 251 pub fn file_syntax(&self, db: &impl db::AstDatabase) -> SyntaxNode {
246 db.parse_or_expand(self.file_id).expect("source created from invalid file") 252 db.parse_or_expand(self.file_id).expect("source created from invalid file")