aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/Cargo.toml2
-rw-r--r--crates/ra_hir/src/code_model.rs92
-rw-r--r--crates/ra_hir/src/semantics.rs35
3 files changed, 120 insertions, 9 deletions
diff --git a/crates/ra_hir/Cargo.toml b/crates/ra_hir/Cargo.toml
index 0555a0de7..266c4cff3 100644
--- a/crates/ra_hir/Cargo.toml
+++ b/crates/ra_hir/Cargo.toml
@@ -12,6 +12,8 @@ log = "0.4.8"
12rustc-hash = "1.1.0" 12rustc-hash = "1.1.0"
13either = "1.5.3" 13either = "1.5.3"
14 14
15itertools = "0.8.2"
16
15ra_syntax = { path = "../ra_syntax" } 17ra_syntax = { path = "../ra_syntax" }
16ra_db = { path = "../ra_db" } 18ra_db = { path = "../ra_db" }
17ra_prof = { path = "../ra_prof" } 19ra_prof = { path = "../ra_prof" }
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 2944926e6..41d4e2ed3 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -54,10 +54,11 @@ pub struct CrateDependency {
54 54
55impl Crate { 55impl Crate {
56 pub fn dependencies(self, db: &impl DefDatabase) -> Vec<CrateDependency> { 56 pub fn dependencies(self, db: &impl DefDatabase) -> Vec<CrateDependency> {
57 db.crate_graph() 57 db.crate_graph()[self.id]
58 .dependencies(self.id) 58 .dependencies
59 .iter()
59 .map(|dep| { 60 .map(|dep| {
60 let krate = Crate { id: dep.crate_id() }; 61 let krate = Crate { id: dep.crate_id };
61 let name = dep.as_name(); 62 let name = dep.as_name();
62 CrateDependency { krate, name } 63 CrateDependency { krate, name }
63 }) 64 })
@@ -69,7 +70,9 @@ impl Crate {
69 let crate_graph = db.crate_graph(); 70 let crate_graph = db.crate_graph();
70 crate_graph 71 crate_graph
71 .iter() 72 .iter()
72 .filter(|&krate| crate_graph.dependencies(krate).any(|it| it.crate_id == self.id)) 73 .filter(|&krate| {
74 crate_graph[krate].dependencies.iter().any(|it| it.crate_id == self.id)
75 })
73 .map(|id| Crate { id }) 76 .map(|id| Crate { id })
74 .collect() 77 .collect()
75 } 78 }
@@ -80,12 +83,11 @@ impl Crate {
80 } 83 }
81 84
82 pub fn root_file(self, db: &impl DefDatabase) -> FileId { 85 pub fn root_file(self, db: &impl DefDatabase) -> FileId {
83 db.crate_graph().crate_root(self.id) 86 db.crate_graph()[self.id].root_file_id
84 } 87 }
85 88
86 pub fn edition(self, db: &impl DefDatabase) -> Edition { 89 pub fn edition(self, db: &impl DefDatabase) -> Edition {
87 let crate_graph = db.crate_graph(); 90 db.crate_graph()[self.id].edition
88 crate_graph.edition(self.id)
89 } 91 }
90 92
91 pub fn all(db: &impl DefDatabase) -> Vec<Crate> { 93 pub fn all(db: &impl DefDatabase) -> Vec<Crate> {
@@ -204,10 +206,26 @@ impl Module {
204 } 206 }
205 207
206 /// Returns a `ModuleScope`: a set of items, visible in this module. 208 /// Returns a `ModuleScope`: a set of items, visible in this module.
207 pub fn scope(self, db: &impl HirDatabase) -> Vec<(Name, ScopeDef)> { 209 pub fn scope(
210 self,
211 db: &impl HirDatabase,
212 visible_from: Option<Module>,
213 ) -> Vec<(Name, ScopeDef)> {
208 db.crate_def_map(self.id.krate)[self.id.local_id] 214 db.crate_def_map(self.id.krate)[self.id.local_id]
209 .scope 215 .scope
210 .entries() 216 .entries()
217 .filter_map(|(name, def)| {
218 if let Some(m) = visible_from {
219 let filtered = def.filter_visibility(|vis| vis.is_visible_from(db, m.id));
220 if filtered.is_none() && !def.is_none() {
221 None
222 } else {
223 Some((name, filtered))
224 }
225 } else {
226 Some((name, def))
227 }
228 })
211 .map(|(name, def)| (name.clone(), def.into())) 229 .map(|(name, def)| (name.clone(), def.into()))
212 .collect() 230 .collect()
213 } 231 }
@@ -480,6 +498,14 @@ impl Adt {
480 pub fn krate(self, db: &impl HirDatabase) -> Option<Crate> { 498 pub fn krate(self, db: &impl HirDatabase) -> Option<Crate> {
481 Some(self.module(db).krate()) 499 Some(self.module(db).krate())
482 } 500 }
501
502 pub fn name(&self, db: &impl HirDatabase) -> Name {
503 match self {
504 Adt::Struct(s) => s.name(db),
505 Adt::Union(u) => u.name(db),
506 Adt::Enum(e) => e.name(db),
507 }
508 }
483} 509}
484 510
485#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 511#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -507,6 +533,14 @@ impl VariantDef {
507 } 533 }
508 } 534 }
509 535
536 pub fn name(&self, db: &impl HirDatabase) -> Name {
537 match self {
538 VariantDef::Struct(s) => s.name(db),
539 VariantDef::Union(u) => u.name(db),
540 VariantDef::EnumVariant(e) => e.name(db),
541 }
542 }
543
510 pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { 544 pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
511 match self { 545 match self {
512 VariantDef::Struct(it) => it.variant_data(db), 546 VariantDef::Struct(it) => it.variant_data(db),
@@ -534,6 +568,14 @@ impl DefWithBody {
534 DefWithBody::Static(s) => s.module(db), 568 DefWithBody::Static(s) => s.module(db),
535 } 569 }
536 } 570 }
571
572 pub fn name(self, db: &impl HirDatabase) -> Option<Name> {
573 match self {
574 DefWithBody::Function(f) => Some(f.name(db)),
575 DefWithBody::Static(s) => s.name(db),
576 DefWithBody::Const(c) => c.name(db),
577 }
578 }
537} 579}
538 580
539#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 581#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -571,6 +613,14 @@ impl Function {
571 } 613 }
572} 614}
573 615
616impl HasVisibility for Function {
617 fn visibility(&self, db: &impl HirDatabase) -> Visibility {
618 let function_data = db.function_data(self.id);
619 let visibility = &function_data.visibility;
620 visibility.resolve(db, &self.id.resolver(db))
621 }
622}
623
574#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 624#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
575pub struct Const { 625pub struct Const {
576 pub(crate) id: ConstId, 626 pub(crate) id: ConstId,
@@ -590,6 +640,14 @@ impl Const {
590 } 640 }
591} 641}
592 642
643impl HasVisibility for Const {
644 fn visibility(&self, db: &impl HirDatabase) -> Visibility {
645 let function_data = db.const_data(self.id);
646 let visibility = &function_data.visibility;
647 visibility.resolve(db, &self.id.resolver(db))
648 }
649}
650
593#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 651#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
594pub struct Static { 652pub struct Static {
595 pub(crate) id: StaticId, 653 pub(crate) id: StaticId,
@@ -664,6 +722,14 @@ impl TypeAlias {
664 } 722 }
665} 723}
666 724
725impl HasVisibility for TypeAlias {
726 fn visibility(&self, db: &impl HirDatabase) -> Visibility {
727 let function_data = db.type_alias_data(self.id);
728 let visibility = &function_data.visibility;
729 visibility.resolve(db, &self.id.resolver(db))
730 }
731}
732
667#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 733#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
668pub struct MacroDef { 734pub struct MacroDef {
669 pub(crate) id: MacroDefId, 735 pub(crate) id: MacroDefId,
@@ -751,6 +817,16 @@ impl AssocItem {
751 } 817 }
752} 818}
753 819
820impl HasVisibility for AssocItem {
821 fn visibility(&self, db: &impl HirDatabase) -> Visibility {
822 match self {
823 AssocItem::Function(f) => f.visibility(db),
824 AssocItem::Const(c) => c.visibility(db),
825 AssocItem::TypeAlias(t) => t.visibility(db),
826 }
827 }
828}
829
754#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] 830#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
755pub enum GenericDef { 831pub enum GenericDef {
756 Function(Function), 832 Function(Function),
diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs
index 965d185a4..3782a9984 100644
--- a/crates/ra_hir/src/semantics.rs
+++ b/crates/ra_hir/src/semantics.rs
@@ -6,7 +6,7 @@ use std::{cell::RefCell, fmt, iter::successors};
6 6
7use hir_def::{ 7use hir_def::{
8 resolver::{self, HasResolver, Resolver}, 8 resolver::{self, HasResolver, Resolver},
9 TraitId, 9 AsMacroCall, TraitId,
10}; 10};
11use hir_expand::ExpansionInfo; 11use hir_expand::ExpansionInfo;
12use ra_db::{FileId, FileRange}; 12use ra_db::{FileId, FileRange};
@@ -70,6 +70,20 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
70 Some(node) 70 Some(node)
71 } 71 }
72 72
73 pub fn expand_hypothetical(
74 &self,
75 actual_macro_call: &ast::MacroCall,
76 hypothetical_args: &ast::TokenTree,
77 token_to_map: SyntaxToken,
78 ) -> Option<(SyntaxNode, SyntaxToken)> {
79 let macro_call =
80 self.find_file(actual_macro_call.syntax().clone()).with_value(actual_macro_call);
81 let sa = self.analyze2(macro_call.map(|it| it.syntax()), None);
82 let macro_call_id = macro_call
83 .as_call_id(self.db, |path| sa.resolver.resolve_path_as_macro(self.db, &path))?;
84 hir_expand::db::expand_hypothetical(self.db, macro_call_id, hypothetical_args, token_to_map)
85 }
86
73 pub fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken { 87 pub fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken {
74 let parent = token.parent(); 88 let parent = token.parent();
75 let parent = self.find_file(parent); 89 let parent = self.find_file(parent);
@@ -104,6 +118,25 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
104 node.ancestors_with_macros(self.db).map(|it| it.value) 118 node.ancestors_with_macros(self.db).map(|it| it.value)
105 } 119 }
106 120
121 pub fn ancestors_at_offset_with_macros(
122 &self,
123 node: &SyntaxNode,
124 offset: TextUnit,
125 ) -> impl Iterator<Item = SyntaxNode> + '_ {
126 use itertools::Itertools;
127 node.token_at_offset(offset)
128 .map(|token| self.ancestors_with_macros(token.parent()))
129 .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len())
130 }
131
132 pub fn find_node_at_offset_with_macros<N: AstNode>(
133 &self,
134 node: &SyntaxNode,
135 offset: TextUnit,
136 ) -> Option<N> {
137 self.ancestors_at_offset_with_macros(node, offset).find_map(N::cast)
138 }
139
107 pub fn type_of_expr(&self, expr: &ast::Expr) -> Option<Type> { 140 pub fn type_of_expr(&self, expr: &ast::Expr) -> Option<Type> {
108 self.analyze(expr.syntax()).type_of(self.db, &expr) 141 self.analyze(expr.syntax()).type_of(self.db, &expr)
109 } 142 }