aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-02-19 13:46:48 +0000
committerGitHub <[email protected]>2020-02-19 13:46:48 +0000
commitff415582960dbd91a18a092271c0848d6c90b0ec (patch)
treec9a1d088b4683a0379eebdc3d374b42e48548ffc
parentb560de4117b5195717445b7c8894d39877035655 (diff)
parent0d5ae89948903bb150b72885c6d9f24dc7a2bb51 (diff)
Merge #3235
3235: Derive visibility as well r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
-rw-r--r--crates/ra_ide/src/references/classify.rs23
-rw-r--r--crates/ra_ide/src/references/search_scope.rs2
-rw-r--r--crates/ra_ide_db/src/defs.rs83
3 files changed, 55 insertions, 53 deletions
diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs
index 2394e68d1..ca5750521 100644
--- a/crates/ra_ide/src/references/classify.rs
+++ b/crates/ra_ide/src/references/classify.rs
@@ -22,14 +22,14 @@ pub(crate) fn classify_name_ref(
22 if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { 22 if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) {
23 tested_by!(goto_def_for_methods); 23 tested_by!(goto_def_for_methods);
24 if let Some(func) = analyzer.resolve_method_call(&method_call) { 24 if let Some(func) = analyzer.resolve_method_call(&method_call) {
25 return Some(from_module_def(sb.db, func.into())); 25 return Some(from_module_def(func.into()));
26 } 26 }
27 } 27 }
28 28
29 if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { 29 if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) {
30 tested_by!(goto_def_for_fields); 30 tested_by!(goto_def_for_fields);
31 if let Some(field) = analyzer.resolve_field(&field_expr) { 31 if let Some(field) = analyzer.resolve_field(&field_expr) {
32 return Some(from_struct_field(sb.db, field)); 32 return Some(from_struct_field(field));
33 } 33 }
34 } 34 }
35 35
@@ -37,50 +37,47 @@ pub(crate) fn classify_name_ref(
37 tested_by!(goto_def_for_record_fields); 37 tested_by!(goto_def_for_record_fields);
38 tested_by!(goto_def_for_field_init_shorthand); 38 tested_by!(goto_def_for_field_init_shorthand);
39 if let Some(field_def) = analyzer.resolve_record_field(&record_field) { 39 if let Some(field_def) = analyzer.resolve_record_field(&record_field) {
40 return Some(from_struct_field(sb.db, field_def)); 40 return Some(from_struct_field(field_def));
41 } 41 }
42 } 42 }
43 43
44 // FIXME: find correct container and visibility for each case
45 let visibility = None;
46
47 if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { 44 if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) {
48 tested_by!(goto_def_for_macros); 45 tested_by!(goto_def_for_macros);
49 if let Some(macro_def) = 46 if let Some(macro_def) =
50 analyzer.resolve_macro_call(sb.db, name_ref.with_value(&macro_call)) 47 analyzer.resolve_macro_call(sb.db, name_ref.with_value(&macro_call))
51 { 48 {
52 let kind = NameKind::Macro(macro_def); 49 let kind = NameKind::Macro(macro_def);
53 return Some(NameDefinition { kind, visibility }); 50 return Some(NameDefinition { kind });
54 } 51 }
55 } 52 }
56 53
57 let path = name_ref.value.syntax().ancestors().find_map(ast::Path::cast)?; 54 let path = name_ref.value.syntax().ancestors().find_map(ast::Path::cast)?;
58 let resolved = analyzer.resolve_path(sb.db, &path)?; 55 let resolved = analyzer.resolve_path(sb.db, &path)?;
59 let res = match resolved { 56 let res = match resolved {
60 PathResolution::Def(def) => from_module_def(sb.db, def), 57 PathResolution::Def(def) => from_module_def(def),
61 PathResolution::AssocItem(item) => { 58 PathResolution::AssocItem(item) => {
62 let def = match item { 59 let def = match item {
63 hir::AssocItem::Function(it) => it.into(), 60 hir::AssocItem::Function(it) => it.into(),
64 hir::AssocItem::Const(it) => it.into(), 61 hir::AssocItem::Const(it) => it.into(),
65 hir::AssocItem::TypeAlias(it) => it.into(), 62 hir::AssocItem::TypeAlias(it) => it.into(),
66 }; 63 };
67 from_module_def(sb.db, def) 64 from_module_def(def)
68 } 65 }
69 PathResolution::Local(local) => { 66 PathResolution::Local(local) => {
70 let kind = NameKind::Local(local); 67 let kind = NameKind::Local(local);
71 NameDefinition { kind, visibility: None } 68 NameDefinition { kind }
72 } 69 }
73 PathResolution::TypeParam(par) => { 70 PathResolution::TypeParam(par) => {
74 let kind = NameKind::TypeParam(par); 71 let kind = NameKind::TypeParam(par);
75 NameDefinition { kind, visibility } 72 NameDefinition { kind }
76 } 73 }
77 PathResolution::Macro(def) => { 74 PathResolution::Macro(def) => {
78 let kind = NameKind::Macro(def); 75 let kind = NameKind::Macro(def);
79 NameDefinition { kind, visibility } 76 NameDefinition { kind }
80 } 77 }
81 PathResolution::SelfType(impl_block) => { 78 PathResolution::SelfType(impl_block) => {
82 let kind = NameKind::SelfType(impl_block); 79 let kind = NameKind::SelfType(impl_block);
83 NameDefinition { kind, visibility } 80 NameDefinition { kind }
84 } 81 }
85 }; 82 };
86 Some(res) 83 Some(res)
diff --git a/crates/ra_ide/src/references/search_scope.rs b/crates/ra_ide/src/references/search_scope.rs
index f9bbe64a4..e5ac12044 100644
--- a/crates/ra_ide/src/references/search_scope.rs
+++ b/crates/ra_ide/src/references/search_scope.rs
@@ -43,7 +43,7 @@ impl SearchScope {
43 return SearchScope::new(res); 43 return SearchScope::new(res);
44 } 44 }
45 45
46 let vis = def.visibility.as_ref().map(|v| v.syntax().to_string()).unwrap_or_default(); 46 let vis = def.visibility(db).as_ref().map(|v| v.syntax().to_string()).unwrap_or_default();
47 47
48 if vis.as_str() == "pub(super)" { 48 if vis.as_str() == "pub(super)" {
49 if let Some(parent_module) = module.parent(db) { 49 if let Some(parent_module) = module.parent(db) {
diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs
index cc772de51..aec748abf 100644
--- a/crates/ra_ide_db/src/defs.rs
+++ b/crates/ra_ide_db/src/defs.rs
@@ -6,8 +6,8 @@
6// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). 6// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06).
7 7
8use hir::{ 8use hir::{
9 Adt, HasSource, ImplBlock, InFile, Local, MacroDef, Module, ModuleDef, SourceBinder, 9 Adt, FieldSource, HasSource, ImplBlock, InFile, Local, MacroDef, Module, ModuleDef,
10 StructField, TypeParam, VariantDef, 10 SourceBinder, StructField, TypeParam,
11}; 11};
12use ra_prof::profile; 12use ra_prof::profile;
13use ra_syntax::{ 13use ra_syntax::{
@@ -29,7 +29,6 @@ pub enum NameKind {
29 29
30#[derive(PartialEq, Eq)] 30#[derive(PartialEq, Eq)]
31pub struct NameDefinition { 31pub struct NameDefinition {
32 pub visibility: Option<ast::Visibility>,
33 /// FIXME: this doesn't really make sense. For example, builtin types don't 32 /// FIXME: this doesn't really make sense. For example, builtin types don't
34 /// really have a module. 33 /// really have a module.
35 pub kind: NameKind, 34 pub kind: NameKind,
@@ -46,6 +45,34 @@ impl NameDefinition {
46 NameKind::TypeParam(it) => Some(it.module(db)), 45 NameKind::TypeParam(it) => Some(it.module(db)),
47 } 46 }
48 } 47 }
48
49 pub fn visibility(&self, db: &RootDatabase) -> Option<ast::Visibility> {
50 match self.kind {
51 NameKind::Macro(_) => None,
52 NameKind::StructField(sf) => match sf.source(db).value {
53 FieldSource::Named(it) => it.visibility(),
54 FieldSource::Pos(it) => it.visibility(),
55 },
56 NameKind::ModuleDef(def) => match def {
57 ModuleDef::Module(it) => it.declaration_source(db)?.value.visibility(),
58 ModuleDef::Function(it) => it.source(db).value.visibility(),
59 ModuleDef::Adt(adt) => match adt {
60 Adt::Struct(it) => it.source(db).value.visibility(),
61 Adt::Union(it) => it.source(db).value.visibility(),
62 Adt::Enum(it) => it.source(db).value.visibility(),
63 },
64 ModuleDef::Const(it) => it.source(db).value.visibility(),
65 ModuleDef::Static(it) => it.source(db).value.visibility(),
66 ModuleDef::Trait(it) => it.source(db).value.visibility(),
67 ModuleDef::TypeAlias(it) => it.source(db).value.visibility(),
68 ModuleDef::EnumVariant(_) => None,
69 ModuleDef::BuiltinType(_) => None,
70 },
71 NameKind::SelfType(_) => None,
72 NameKind::Local(_) => None,
73 NameKind::TypeParam(_) => None,
74 }
75 }
49} 76}
50 77
51pub fn classify_name( 78pub fn classify_name(
@@ -61,65 +88,63 @@ pub fn classify_name(
61 let src = name.with_value(it); 88 let src = name.with_value(it);
62 let local = sb.to_def(src)?; 89 let local = sb.to_def(src)?;
63 Some(NameDefinition { 90 Some(NameDefinition {
64 visibility: None,
65 kind: NameKind::Local(local), 91 kind: NameKind::Local(local),
66 }) 92 })
67 }, 93 },
68 ast::RecordFieldDef(it) => { 94 ast::RecordFieldDef(it) => {
69 let src = name.with_value(it); 95 let src = name.with_value(it);
70 let field: hir::StructField = sb.to_def(src)?; 96 let field: hir::StructField = sb.to_def(src)?;
71 Some(from_struct_field(sb.db, field)) 97 Some(from_struct_field(field))
72 }, 98 },
73 ast::Module(it) => { 99 ast::Module(it) => {
74 let def = sb.to_def(name.with_value(it))?; 100 let def = sb.to_def(name.with_value(it))?;
75 Some(from_module_def(sb.db, def.into())) 101 Some(from_module_def(def.into()))
76 }, 102 },
77 ast::StructDef(it) => { 103 ast::StructDef(it) => {
78 let src = name.with_value(it); 104 let src = name.with_value(it);
79 let def: hir::Struct = sb.to_def(src)?; 105 let def: hir::Struct = sb.to_def(src)?;
80 Some(from_module_def(sb.db, def.into())) 106 Some(from_module_def(def.into()))
81 }, 107 },
82 ast::EnumDef(it) => { 108 ast::EnumDef(it) => {
83 let src = name.with_value(it); 109 let src = name.with_value(it);
84 let def: hir::Enum = sb.to_def(src)?; 110 let def: hir::Enum = sb.to_def(src)?;
85 Some(from_module_def(sb.db, def.into())) 111 Some(from_module_def(def.into()))
86 }, 112 },
87 ast::TraitDef(it) => { 113 ast::TraitDef(it) => {
88 let src = name.with_value(it); 114 let src = name.with_value(it);
89 let def: hir::Trait = sb.to_def(src)?; 115 let def: hir::Trait = sb.to_def(src)?;
90 Some(from_module_def(sb.db, def.into())) 116 Some(from_module_def(def.into()))
91 }, 117 },
92 ast::StaticDef(it) => { 118 ast::StaticDef(it) => {
93 let src = name.with_value(it); 119 let src = name.with_value(it);
94 let def: hir::Static = sb.to_def(src)?; 120 let def: hir::Static = sb.to_def(src)?;
95 Some(from_module_def(sb.db, def.into())) 121 Some(from_module_def(def.into()))
96 }, 122 },
97 ast::EnumVariant(it) => { 123 ast::EnumVariant(it) => {
98 let src = name.with_value(it); 124 let src = name.with_value(it);
99 let def: hir::EnumVariant = sb.to_def(src)?; 125 let def: hir::EnumVariant = sb.to_def(src)?;
100 Some(from_module_def(sb.db, def.into())) 126 Some(from_module_def(def.into()))
101 }, 127 },
102 ast::FnDef(it) => { 128 ast::FnDef(it) => {
103 let src = name.with_value(it); 129 let src = name.with_value(it);
104 let def: hir::Function = sb.to_def(src)?; 130 let def: hir::Function = sb.to_def(src)?;
105 Some(from_module_def(sb.db, def.into())) 131 Some(from_module_def(def.into()))
106 }, 132 },
107 ast::ConstDef(it) => { 133 ast::ConstDef(it) => {
108 let src = name.with_value(it); 134 let src = name.with_value(it);
109 let def: hir::Const = sb.to_def(src)?; 135 let def: hir::Const = sb.to_def(src)?;
110 Some(from_module_def(sb.db, def.into())) 136 Some(from_module_def(def.into()))
111 }, 137 },
112 ast::TypeAliasDef(it) => { 138 ast::TypeAliasDef(it) => {
113 let src = name.with_value(it); 139 let src = name.with_value(it);
114 let def: hir::TypeAlias = sb.to_def(src)?; 140 let def: hir::TypeAlias = sb.to_def(src)?;
115 Some(from_module_def(sb.db, def.into())) 141 Some(from_module_def(def.into()))
116 }, 142 },
117 ast::MacroCall(it) => { 143 ast::MacroCall(it) => {
118 let src = name.with_value(it); 144 let src = name.with_value(it);
119 let def = sb.to_def(src.clone())?; 145 let def = sb.to_def(src.clone())?;
120 146
121 Some(NameDefinition { 147 Some(NameDefinition {
122 visibility: None,
123 kind: NameKind::Macro(def), 148 kind: NameKind::Macro(def),
124 }) 149 })
125 }, 150 },
@@ -127,7 +152,6 @@ pub fn classify_name(
127 let src = name.with_value(it); 152 let src = name.with_value(it);
128 let def = sb.to_def(src)?; 153 let def = sb.to_def(src)?;
129 Some(NameDefinition { 154 Some(NameDefinition {
130 visibility: None,
131 kind: NameKind::TypeParam(def), 155 kind: NameKind::TypeParam(def),
132 }) 156 })
133 }, 157 },
@@ -136,31 +160,12 @@ pub fn classify_name(
136 } 160 }
137} 161}
138 162
139pub fn from_struct_field(db: &RootDatabase, field: StructField) -> NameDefinition { 163pub fn from_struct_field(field: StructField) -> NameDefinition {
140 let kind = NameKind::StructField(field); 164 let kind = NameKind::StructField(field);
141 let parent = field.parent_def(db); 165 NameDefinition { kind }
142 let visibility = match parent {
143 VariantDef::Struct(s) => s.source(db).value.visibility(),
144 VariantDef::Union(e) => e.source(db).value.visibility(),
145 VariantDef::EnumVariant(e) => e.source(db).value.parent_enum().visibility(),
146 };
147 NameDefinition { kind, visibility }
148} 166}
149 167
150pub fn from_module_def(db: &RootDatabase, def: ModuleDef) -> NameDefinition { 168pub fn from_module_def(def: ModuleDef) -> NameDefinition {
151 let kind = NameKind::ModuleDef(def); 169 let kind = NameKind::ModuleDef(def);
152 let visibility = match def { 170 NameDefinition { kind }
153 ModuleDef::Module(it) => it.declaration_source(db).and_then(|s| s.value.visibility()),
154 ModuleDef::EnumVariant(it) => it.source(db).value.parent_enum().visibility(),
155 ModuleDef::Function(it) => it.source(db).value.visibility(),
156 ModuleDef::Const(it) => it.source(db).value.visibility(),
157 ModuleDef::Static(it) => it.source(db).value.visibility(),
158 ModuleDef::Trait(it) => it.source(db).value.visibility(),
159 ModuleDef::TypeAlias(it) => it.source(db).value.visibility(),
160 ModuleDef::Adt(Adt::Struct(it)) => it.source(db).value.visibility(),
161 ModuleDef::Adt(Adt::Union(it)) => it.source(db).value.visibility(),
162 ModuleDef::Adt(Adt::Enum(it)) => it.source(db).value.visibility(),
163 ModuleDef::BuiltinType(..) => None,
164 };
165 NameDefinition { kind, visibility }
166} 171}