aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-01-14 10:00:17 +0000
committerAleksey Kladov <[email protected]>2020-01-14 10:29:43 +0000
commitaedff7cdcfb2340a3a23b540b50cadc94a367428 (patch)
treeac43872656e88ec3ec26fefb89aa934294729dd1
parenteb8989f9e4b0fd7ceb6e9f28505c2173341a217e (diff)
Move utility functions down
-rw-r--r--crates/ra_hir/src/source_analyzer.rs116
1 files changed, 58 insertions, 58 deletions
diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs
index a2a9d968c..f0511c742 100644
--- a/crates/ra_hir/src/source_analyzer.rs
+++ b/crates/ra_hir/src/source_analyzer.rs
@@ -40,64 +40,6 @@ use crate::{
40 TypeParam, 40 TypeParam,
41}; 41};
42 42
43fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> Option<Resolver> {
44 match_ast! {
45 match (node.value) {
46 ast::Module(it) => {
47 let src = node.with_value(it);
48 Some(crate::Module::from_declaration(db, src)?.id.resolver(db))
49 },
50 ast::SourceFile(it) => {
51 let src = node.with_value(ModuleSource::SourceFile(it));
52 Some(crate::Module::from_definition(db, src)?.id.resolver(db))
53 },
54 ast::StructDef(it) => {
55 let src = node.with_value(it);
56 Some(Struct::from_source(db, src)?.id.resolver(db))
57 },
58 ast::EnumDef(it) => {
59 let src = node.with_value(it);
60 Some(Enum::from_source(db, src)?.id.resolver(db))
61 },
62 ast::ImplBlock(it) => {
63 let src = node.with_value(it);
64 Some(ImplBlock::from_source(db, src)?.id.resolver(db))
65 },
66 ast::TraitDef(it) => {
67 let src = node.with_value(it);
68 Some(Trait::from_source(db, src)?.id.resolver(db))
69 },
70 _ => match node.value.kind() {
71 FN_DEF | CONST_DEF | STATIC_DEF => {
72 let def = def_with_body_from_child_node(db, node)?;
73 let def = DefWithBodyId::from(def);
74 Some(def.resolver(db))
75 }
76 // FIXME add missing cases
77 _ => None
78 }
79 }
80 }
81}
82
83fn def_with_body_from_child_node(
84 db: &impl HirDatabase,
85 child: InFile<&SyntaxNode>,
86) -> Option<DefWithBody> {
87 let _p = profile("def_with_body_from_child_node");
88 child.cloned().ancestors_with_macros(db).find_map(|node| {
89 let n = &node.value;
90 match_ast! {
91 match n {
92 ast::FnDef(def) => { return Function::from_source(db, node.with_value(def)).map(DefWithBody::from); },
93 ast::ConstDef(def) => { return Const::from_source(db, node.with_value(def)).map(DefWithBody::from); },
94 ast::StaticDef(def) => { return Static::from_source(db, node.with_value(def)).map(DefWithBody::from); },
95 _ => { None },
96 }
97 }
98 })
99}
100
101/// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of 43/// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of
102/// original source files. It should not be used inside the HIR itself. 44/// original source files. It should not be used inside the HIR itself.
103#[derive(Debug)] 45#[derive(Debug)]
@@ -487,6 +429,64 @@ impl SourceAnalyzer {
487 } 429 }
488} 430}
489 431
432fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> Option<Resolver> {
433 match_ast! {
434 match (node.value) {
435 ast::Module(it) => {
436 let src = node.with_value(it);
437 Some(crate::Module::from_declaration(db, src)?.id.resolver(db))
438 },
439 ast::SourceFile(it) => {
440 let src = node.with_value(ModuleSource::SourceFile(it));
441 Some(crate::Module::from_definition(db, src)?.id.resolver(db))
442 },
443 ast::StructDef(it) => {
444 let src = node.with_value(it);
445 Some(Struct::from_source(db, src)?.id.resolver(db))
446 },
447 ast::EnumDef(it) => {
448 let src = node.with_value(it);
449 Some(Enum::from_source(db, src)?.id.resolver(db))
450 },
451 ast::ImplBlock(it) => {
452 let src = node.with_value(it);
453 Some(ImplBlock::from_source(db, src)?.id.resolver(db))
454 },
455 ast::TraitDef(it) => {
456 let src = node.with_value(it);
457 Some(Trait::from_source(db, src)?.id.resolver(db))
458 },
459 _ => match node.value.kind() {
460 FN_DEF | CONST_DEF | STATIC_DEF => {
461 let def = def_with_body_from_child_node(db, node)?;
462 let def = DefWithBodyId::from(def);
463 Some(def.resolver(db))
464 }
465 // FIXME add missing cases
466 _ => None
467 }
468 }
469 }
470}
471
472fn def_with_body_from_child_node(
473 db: &impl HirDatabase,
474 child: InFile<&SyntaxNode>,
475) -> Option<DefWithBody> {
476 let _p = profile("def_with_body_from_child_node");
477 child.cloned().ancestors_with_macros(db).find_map(|node| {
478 let n = &node.value;
479 match_ast! {
480 match n {
481 ast::FnDef(def) => { return Function::from_source(db, node.with_value(def)).map(DefWithBody::from); },
482 ast::ConstDef(def) => { return Const::from_source(db, node.with_value(def)).map(DefWithBody::from); },
483 ast::StaticDef(def) => { return Static::from_source(db, node.with_value(def)).map(DefWithBody::from); },
484 _ => { None },
485 }
486 }
487 })
488}
489
490fn scope_for( 490fn scope_for(
491 scopes: &ExprScopes, 491 scopes: &ExprScopes,
492 source_map: &BodySourceMap, 492 source_map: &BodySourceMap,