aboutsummaryrefslogtreecommitdiff
path: root/crates/hir/src/semantics.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir/src/semantics.rs')
-rw-r--r--crates/hir/src/semantics.rs38
1 files changed, 30 insertions, 8 deletions
diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs
index 945638cc5..03c9371b5 100644
--- a/crates/hir/src/semantics.rs
+++ b/crates/hir/src/semantics.rs
@@ -143,6 +143,12 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
143 self.imp.diagnostics_display_range(diagnostics) 143 self.imp.diagnostics_display_range(diagnostics)
144 } 144 }
145 145
146 pub fn token_ancestors_with_macros(
147 &self,
148 token: SyntaxToken,
149 ) -> impl Iterator<Item = SyntaxNode> + '_ {
150 token.parent().into_iter().flat_map(move |it| self.ancestors_with_macros(it))
151 }
146 pub fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator<Item = SyntaxNode> + '_ { 152 pub fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator<Item = SyntaxNode> + '_ {
147 self.imp.ancestors_with_macros(node) 153 self.imp.ancestors_with_macros(node)
148 } 154 }
@@ -259,6 +265,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
259 } 265 }
260 266
261 pub fn to_module_def(&self, file: FileId) -> Option<Module> { 267 pub fn to_module_def(&self, file: FileId) -> Option<Module> {
268 self.imp.to_module_def(file).next()
269 }
270
271 pub fn to_module_defs(&self, file: FileId) -> impl Iterator<Item = Module> {
262 self.imp.to_module_def(file) 272 self.imp.to_module_def(file)
263 } 273 }
264 274
@@ -266,8 +276,8 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
266 self.imp.scope(node) 276 self.imp.scope(node)
267 } 277 }
268 278
269 pub fn scope_at_offset(&self, node: &SyntaxNode, offset: TextSize) -> SemanticsScope<'db> { 279 pub fn scope_at_offset(&self, token: &SyntaxToken, offset: TextSize) -> SemanticsScope<'db> {
270 self.imp.scope_at_offset(node, offset) 280 self.imp.scope_at_offset(&token.parent().unwrap(), offset)
271 } 281 }
272 282
273 pub fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> { 283 pub fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> {
@@ -337,7 +347,10 @@ impl<'db> SemanticsImpl<'db> {
337 347
338 fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken { 348 fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken {
339 let _p = profile::span("descend_into_macros"); 349 let _p = profile::span("descend_into_macros");
340 let parent = token.parent(); 350 let parent = match token.parent() {
351 Some(it) => it,
352 None => return token,
353 };
341 let sa = self.analyze(&parent); 354 let sa = self.analyze(&parent);
342 355
343 let token = successors(Some(InFile::new(sa.file_id, token)), |token| { 356 let token = successors(Some(InFile::new(sa.file_id, token)), |token| {
@@ -356,7 +369,9 @@ impl<'db> SemanticsImpl<'db> {
356 .as_ref()? 369 .as_ref()?
357 .map_token_down(token.as_ref())?; 370 .map_token_down(token.as_ref())?;
358 371
359 self.cache(find_root(&token.value.parent()), token.file_id); 372 if let Some(parent) = token.value.parent() {
373 self.cache(find_root(&parent), token.file_id);
374 }
360 375
361 Some(token) 376 Some(token)
362 }) 377 })
@@ -374,7 +389,7 @@ impl<'db> SemanticsImpl<'db> {
374 // Handle macro token cases 389 // Handle macro token cases
375 node.token_at_offset(offset) 390 node.token_at_offset(offset)
376 .map(|token| self.descend_into_macros(token)) 391 .map(|token| self.descend_into_macros(token))
377 .map(|it| self.ancestors_with_macros(it.parent())) 392 .map(|it| self.token_ancestors_with_macros(it))
378 .flatten() 393 .flatten()
379 } 394 }
380 395
@@ -390,6 +405,13 @@ impl<'db> SemanticsImpl<'db> {
390 src.with_value(&node).original_file_range(self.db.upcast()) 405 src.with_value(&node).original_file_range(self.db.upcast())
391 } 406 }
392 407
408 fn token_ancestors_with_macros(
409 &self,
410 token: SyntaxToken,
411 ) -> impl Iterator<Item = SyntaxNode> + '_ {
412 token.parent().into_iter().flat_map(move |parent| self.ancestors_with_macros(parent))
413 }
414
393 fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator<Item = SyntaxNode> + '_ { 415 fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator<Item = SyntaxNode> + '_ {
394 let node = self.find_file(node); 416 let node = self.find_file(node);
395 node.ancestors_with_macros(self.db.upcast()).map(|it| it.value) 417 node.ancestors_with_macros(self.db.upcast()).map(|it| it.value)
@@ -401,7 +423,7 @@ impl<'db> SemanticsImpl<'db> {
401 offset: TextSize, 423 offset: TextSize,
402 ) -> impl Iterator<Item = SyntaxNode> + '_ { 424 ) -> impl Iterator<Item = SyntaxNode> + '_ {
403 node.token_at_offset(offset) 425 node.token_at_offset(offset)
404 .map(|token| self.ancestors_with_macros(token.parent())) 426 .map(|token| self.token_ancestors_with_macros(token))
405 .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len()) 427 .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len())
406 } 428 }
407 429
@@ -537,8 +559,8 @@ impl<'db> SemanticsImpl<'db> {
537 f(&mut ctx) 559 f(&mut ctx)
538 } 560 }
539 561
540 fn to_module_def(&self, file: FileId) -> Option<Module> { 562 fn to_module_def(&self, file: FileId) -> impl Iterator<Item = Module> {
541 self.with_ctx(|ctx| ctx.file_to_def(file)).map(Module::from) 563 self.with_ctx(|ctx| ctx.file_to_def(file)).into_iter().map(Module::from)
542 } 564 }
543 565
544 fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> { 566 fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> {