aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/ast_transform.rs9
-rw-r--r--crates/ra_assists/src/handlers/add_missing_impl_members.rs4
-rw-r--r--crates/ra_assists/src/handlers/fill_match_arms.rs8
-rw-r--r--crates/ra_hir_def/src/data.rs26
-rw-r--r--crates/ra_hir_def/src/visibility.rs33
-rw-r--r--crates/ra_ide/src/completion/complete_dot.rs32
-rw-r--r--crates/ra_ide/src/completion/presentation.rs9
-rw-r--r--crates/ra_ide/src/typing.rs40
8 files changed, 113 insertions, 48 deletions
diff --git a/crates/ra_assists/src/ast_transform.rs b/crates/ra_assists/src/ast_transform.rs
index 42856f0ca..45558c448 100644
--- a/crates/ra_assists/src/ast_transform.rs
+++ b/crates/ra_assists/src/ast_transform.rs
@@ -37,7 +37,6 @@ pub struct SubstituteTypeParams<'a> {
37impl<'a> SubstituteTypeParams<'a> { 37impl<'a> SubstituteTypeParams<'a> {
38 pub fn for_trait_impl( 38 pub fn for_trait_impl(
39 source_scope: &'a SemanticsScope<'a, RootDatabase>, 39 source_scope: &'a SemanticsScope<'a, RootDatabase>,
40 db: &'a RootDatabase,
41 // FIXME: there's implicit invariant that `trait_` and `source_scope` match... 40 // FIXME: there's implicit invariant that `trait_` and `source_scope` match...
42 trait_: hir::Trait, 41 trait_: hir::Trait,
43 impl_def: ast::ImplDef, 42 impl_def: ast::ImplDef,
@@ -45,7 +44,7 @@ impl<'a> SubstituteTypeParams<'a> {
45 let substs = get_syntactic_substs(impl_def).unwrap_or_default(); 44 let substs = get_syntactic_substs(impl_def).unwrap_or_default();
46 let generic_def: hir::GenericDef = trait_.into(); 45 let generic_def: hir::GenericDef = trait_.into();
47 let substs_by_param: FxHashMap<_, _> = generic_def 46 let substs_by_param: FxHashMap<_, _> = generic_def
48 .params(db) 47 .params(source_scope.db)
49 .into_iter() 48 .into_iter()
50 // this is a trait impl, so we need to skip the first type parameter -- this is a bit hacky 49 // this is a trait impl, so we need to skip the first type parameter -- this is a bit hacky
51 .skip(1) 50 .skip(1)
@@ -104,7 +103,6 @@ impl<'a> AstTransform<'a> for SubstituteTypeParams<'a> {
104pub struct QualifyPaths<'a> { 103pub struct QualifyPaths<'a> {
105 target_scope: &'a SemanticsScope<'a, RootDatabase>, 104 target_scope: &'a SemanticsScope<'a, RootDatabase>,
106 source_scope: &'a SemanticsScope<'a, RootDatabase>, 105 source_scope: &'a SemanticsScope<'a, RootDatabase>,
107 db: &'a RootDatabase,
108 previous: Box<dyn AstTransform<'a> + 'a>, 106 previous: Box<dyn AstTransform<'a> + 'a>,
109} 107}
110 108
@@ -112,9 +110,8 @@ impl<'a> QualifyPaths<'a> {
112 pub fn new( 110 pub fn new(
113 target_scope: &'a SemanticsScope<'a, RootDatabase>, 111 target_scope: &'a SemanticsScope<'a, RootDatabase>,
114 source_scope: &'a SemanticsScope<'a, RootDatabase>, 112 source_scope: &'a SemanticsScope<'a, RootDatabase>,
115 db: &'a RootDatabase,
116 ) -> Self { 113 ) -> Self {
117 Self { target_scope, source_scope, db, previous: Box::new(NullTransformer) } 114 Self { target_scope, source_scope, previous: Box::new(NullTransformer) }
118 } 115 }
119 116
120 fn get_substitution_inner( 117 fn get_substitution_inner(
@@ -132,7 +129,7 @@ impl<'a> QualifyPaths<'a> {
132 let resolution = self.source_scope.resolve_hir_path(&hir_path?)?; 129 let resolution = self.source_scope.resolve_hir_path(&hir_path?)?;
133 match resolution { 130 match resolution {
134 PathResolution::Def(def) => { 131 PathResolution::Def(def) => {
135 let found_path = from.find_use_path(self.db, def)?; 132 let found_path = from.find_use_path(self.source_scope.db, def)?;
136 let mut path = path_to_ast(found_path); 133 let mut path = path_to_ast(found_path);
137 134
138 let type_args = p 135 let type_args = p
diff --git a/crates/ra_assists/src/handlers/add_missing_impl_members.rs b/crates/ra_assists/src/handlers/add_missing_impl_members.rs
index 639180d37..e5920b6f6 100644
--- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs
+++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs
@@ -142,8 +142,8 @@ fn add_missing_impl_members_inner(
142 let n_existing_items = impl_item_list.impl_items().count(); 142 let n_existing_items = impl_item_list.impl_items().count();
143 let source_scope = sema.scope_for_def(trait_); 143 let source_scope = sema.scope_for_def(trait_);
144 let target_scope = sema.scope(impl_item_list.syntax()); 144 let target_scope = sema.scope(impl_item_list.syntax());
145 let ast_transform = QualifyPaths::new(&target_scope, &source_scope, sema.db) 145 let ast_transform = QualifyPaths::new(&target_scope, &source_scope)
146 .or(SubstituteTypeParams::for_trait_impl(&source_scope, sema.db, trait_, impl_node)); 146 .or(SubstituteTypeParams::for_trait_impl(&source_scope, trait_, impl_node));
147 let items = missing_items 147 let items = missing_items
148 .into_iter() 148 .into_iter()
149 .map(|it| ast_transform::apply(&*ast_transform, it)) 149 .map(|it| ast_transform::apply(&*ast_transform, it))
diff --git a/crates/ra_assists/src/handlers/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs
index e5d8c639d..97cf90ae4 100644
--- a/crates/ra_assists/src/handlers/fill_match_arms.rs
+++ b/crates/ra_assists/src/handlers/fill_match_arms.rs
@@ -2,7 +2,7 @@
2 2
3use std::iter; 3use std::iter;
4 4
5use hir::{db::HirDatabase, Adt, HasSource, Semantics}; 5use hir::{Adt, HasSource, Semantics};
6use ra_syntax::ast::{self, edit::IndentLevel, make, AstNode, NameOwner}; 6use ra_syntax::ast::{self, edit::IndentLevel, make, AstNode, NameOwner};
7 7
8use crate::{Assist, AssistCtx, AssistId}; 8use crate::{Assist, AssistCtx, AssistId};
@@ -88,11 +88,7 @@ fn resolve_enum_def(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<
88 }) 88 })
89} 89}
90 90
91fn build_pat( 91fn build_pat(db: &RootDatabase, module: hir::Module, var: hir::EnumVariant) -> Option<ast::Pat> {
92 db: &impl HirDatabase,
93 module: hir::Module,
94 var: hir::EnumVariant,
95) -> Option<ast::Pat> {
96 let path = crate::ast_transform::path_to_ast(module.find_use_path(db, var.into())?); 92 let path = crate::ast_transform::path_to_ast(module.find_use_path(db, var.into())?);
97 93
98 // FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though 94 // FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs
index eb4c5de45..c0b16b7fa 100644
--- a/crates/ra_hir_def/src/data.rs
+++ b/crates/ra_hir_def/src/data.rs
@@ -34,7 +34,8 @@ pub struct FunctionData {
34 34
35impl FunctionData { 35impl FunctionData {
36 pub(crate) fn fn_data_query(db: &impl DefDatabase, func: FunctionId) -> Arc<FunctionData> { 36 pub(crate) fn fn_data_query(db: &impl DefDatabase, func: FunctionId) -> Arc<FunctionData> {
37 let src = func.lookup(db).source(db); 37 let loc = func.lookup(db);
38 let src = loc.source(db);
38 let name = src.value.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); 39 let name = src.value.name().map(|n| n.as_name()).unwrap_or_else(Name::missing);
39 let mut params = Vec::new(); 40 let mut params = Vec::new();
40 let mut has_self_param = false; 41 let mut has_self_param = false;
@@ -76,7 +77,9 @@ impl FunctionData {
76 ret_type 77 ret_type
77 }; 78 };
78 79
79 let visibility = RawVisibility::from_ast(db, src.map(|s| s.visibility())); 80 let vis_default = RawVisibility::default_for_container(loc.container);
81 let visibility =
82 RawVisibility::from_ast_with_default(db, vis_default, src.map(|s| s.visibility()));
80 83
81 let sig = FunctionData { name, params, ret_type, has_self_param, visibility }; 84 let sig = FunctionData { name, params, ret_type, has_self_param, visibility };
82 Arc::new(sig) 85 Arc::new(sig)
@@ -105,10 +108,13 @@ impl TypeAliasData {
105 db: &impl DefDatabase, 108 db: &impl DefDatabase,
106 typ: TypeAliasId, 109 typ: TypeAliasId,
107 ) -> Arc<TypeAliasData> { 110 ) -> Arc<TypeAliasData> {
108 let node = typ.lookup(db).source(db); 111 let loc = typ.lookup(db);
112 let node = loc.source(db);
109 let name = node.value.name().map_or_else(Name::missing, |n| n.as_name()); 113 let name = node.value.name().map_or_else(Name::missing, |n| n.as_name());
110 let type_ref = node.value.type_ref().map(TypeRef::from_ast); 114 let type_ref = node.value.type_ref().map(TypeRef::from_ast);
111 let visibility = RawVisibility::from_ast(db, node.map(|n| n.visibility())); 115 let vis_default = RawVisibility::default_for_container(loc.container);
116 let visibility =
117 RawVisibility::from_ast_with_default(db, vis_default, node.map(|n| n.visibility()));
112 Arc::new(TypeAliasData { name, type_ref, visibility }) 118 Arc::new(TypeAliasData { name, type_ref, visibility })
113 } 119 }
114} 120}
@@ -230,22 +236,26 @@ pub struct ConstData {
230 236
231impl ConstData { 237impl ConstData {
232 pub(crate) fn const_data_query(db: &impl DefDatabase, konst: ConstId) -> Arc<ConstData> { 238 pub(crate) fn const_data_query(db: &impl DefDatabase, konst: ConstId) -> Arc<ConstData> {
233 let node = konst.lookup(db).source(db); 239 let loc = konst.lookup(db);
234 Arc::new(ConstData::new(db, node)) 240 let node = loc.source(db);
241 let vis_default = RawVisibility::default_for_container(loc.container);
242 Arc::new(ConstData::new(db, vis_default, node))
235 } 243 }
236 244
237 pub(crate) fn static_data_query(db: &impl DefDatabase, konst: StaticId) -> Arc<ConstData> { 245 pub(crate) fn static_data_query(db: &impl DefDatabase, konst: StaticId) -> Arc<ConstData> {
238 let node = konst.lookup(db).source(db); 246 let node = konst.lookup(db).source(db);
239 Arc::new(ConstData::new(db, node)) 247 Arc::new(ConstData::new(db, RawVisibility::private(), node))
240 } 248 }
241 249
242 fn new<N: NameOwner + TypeAscriptionOwner + VisibilityOwner>( 250 fn new<N: NameOwner + TypeAscriptionOwner + VisibilityOwner>(
243 db: &impl DefDatabase, 251 db: &impl DefDatabase,
252 vis_default: RawVisibility,
244 node: InFile<N>, 253 node: InFile<N>,
245 ) -> ConstData { 254 ) -> ConstData {
246 let name = node.value.name().map(|n| n.as_name()); 255 let name = node.value.name().map(|n| n.as_name());
247 let type_ref = TypeRef::from_ast_opt(node.value.ascribed_type()); 256 let type_ref = TypeRef::from_ast_opt(node.value.ascribed_type());
248 let visibility = RawVisibility::from_ast(db, node.map(|n| n.visibility())); 257 let visibility =
258 RawVisibility::from_ast_with_default(db, vis_default, node.map(|n| n.visibility()));
249 ConstData { name, type_ref, visibility } 259 ConstData { name, type_ref, visibility }
250 } 260 }
251} 261}
diff --git a/crates/ra_hir_def/src/visibility.rs b/crates/ra_hir_def/src/visibility.rs
index d8296da4b..e0c59e905 100644
--- a/crates/ra_hir_def/src/visibility.rs
+++ b/crates/ra_hir_def/src/visibility.rs
@@ -6,7 +6,7 @@ use ra_syntax::ast;
6use crate::{ 6use crate::{
7 db::DefDatabase, 7 db::DefDatabase,
8 path::{ModPath, PathKind}, 8 path::{ModPath, PathKind},
9 ModuleId, 9 AssocContainerId, ModuleId,
10}; 10};
11 11
12/// Visibility of an item, not yet resolved. 12/// Visibility of an item, not yet resolved.
@@ -20,11 +20,30 @@ pub enum RawVisibility {
20} 20}
21 21
22impl RawVisibility { 22impl RawVisibility {
23 const fn private() -> RawVisibility { 23 pub(crate) const fn private() -> RawVisibility {
24 let path = ModPath { kind: PathKind::Super(0), segments: Vec::new() }; 24 let path = ModPath { kind: PathKind::Super(0), segments: Vec::new() };
25 RawVisibility::Module(path) 25 RawVisibility::Module(path)
26 } 26 }
27 27
28 pub(crate) fn default_for_container(container_id: AssocContainerId) -> Self {
29 match container_id {
30 AssocContainerId::TraitId(_) => RawVisibility::Public,
31 _ => RawVisibility::private(),
32 }
33 }
34
35 pub(crate) fn from_ast_with_default(
36 db: &impl DefDatabase,
37 default: RawVisibility,
38 node: InFile<Option<ast::Visibility>>,
39 ) -> RawVisibility {
40 Self::from_ast_with_hygiene_and_default(
41 node.value,
42 default,
43 &Hygiene::new(db, node.file_id),
44 )
45 }
46
28 pub(crate) fn from_ast( 47 pub(crate) fn from_ast(
29 db: &impl DefDatabase, 48 db: &impl DefDatabase,
30 node: InFile<Option<ast::Visibility>>, 49 node: InFile<Option<ast::Visibility>>,
@@ -36,8 +55,16 @@ impl RawVisibility {
36 node: Option<ast::Visibility>, 55 node: Option<ast::Visibility>,
37 hygiene: &Hygiene, 56 hygiene: &Hygiene,
38 ) -> RawVisibility { 57 ) -> RawVisibility {
58 Self::from_ast_with_hygiene_and_default(node, RawVisibility::private(), hygiene)
59 }
60
61 pub(crate) fn from_ast_with_hygiene_and_default(
62 node: Option<ast::Visibility>,
63 default: RawVisibility,
64 hygiene: &Hygiene,
65 ) -> RawVisibility {
39 let node = match node { 66 let node = match node {
40 None => return RawVisibility::private(), 67 None => return default,
41 Some(node) => node, 68 Some(node) => node,
42 }; 69 };
43 match node.kind() { 70 match node.kind() {
diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs
index 81e5037aa..f07611d88 100644
--- a/crates/ra_ide/src/completion/complete_dot.rs
+++ b/crates/ra_ide/src/completion/complete_dot.rs
@@ -402,6 +402,38 @@ mod tests {
402 } 402 }
403 403
404 #[test] 404 #[test]
405 fn completes_trait_method_from_other_module() {
406 assert_debug_snapshot!(
407 do_ref_completion(
408 r"
409 struct A {}
410 mod m {
411 pub trait Trait { fn the_method(&self); }
412 }
413 use m::Trait;
414 impl Trait for A {}
415 fn foo(a: A) {
416 a.<|>
417 }
418 ",
419 ),
420 @r###"
421 [
422 CompletionItem {
423 label: "the_method()",
424 source_range: [219; 219),
425 delete: [219; 219),
426 insert: "the_method()$0",
427 kind: Method,
428 lookup: "the_method",
429 detail: "fn the_method(&self)",
430 },
431 ]
432 "###
433 );
434 }
435
436 #[test]
405 fn test_no_non_self_method() { 437 fn test_no_non_self_method() {
406 assert_debug_snapshot!( 438 assert_debug_snapshot!(
407 do_ref_completion( 439 do_ref_completion(
diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs
index 910844244..253848602 100644
--- a/crates/ra_ide/src/completion/presentation.rs
+++ b/crates/ra_ide/src/completion/presentation.rs
@@ -1,6 +1,6 @@
1//! This modules takes care of rendering various definitions as completion items. 1//! This modules takes care of rendering various definitions as completion items.
2 2
3use hir::{db::HirDatabase, Docs, HasAttrs, HasSource, HirDisplay, ScopeDef, StructKind, Type}; 3use hir::{Docs, HasAttrs, HasSource, HirDisplay, ScopeDef, StructKind, Type};
4use join_to_string::join; 4use join_to_string::join;
5use ra_syntax::ast::NameOwner; 5use ra_syntax::ast::NameOwner;
6use test_utils::tested_by; 6use test_utils::tested_by;
@@ -9,7 +9,10 @@ use crate::completion::{
9 CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions, 9 CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions,
10}; 10};
11 11
12use crate::display::{const_label, macro_label, type_label, FunctionSignature}; 12use crate::{
13 display::{const_label, macro_label, type_label, FunctionSignature},
14 RootDatabase,
15};
13 16
14impl Completions { 17impl Completions {
15 pub(crate) fn add_field( 18 pub(crate) fn add_field(
@@ -300,7 +303,7 @@ impl Completions {
300 } 303 }
301} 304}
302 305
303fn is_deprecated(node: impl HasAttrs, db: &impl HirDatabase) -> bool { 306fn is_deprecated(node: impl HasAttrs, db: &RootDatabase) -> bool {
304 node.attrs(db).by_key("deprecated").exists() 307 node.attrs(db).by_key("deprecated").exists()
305} 308}
306 309
diff --git a/crates/ra_ide/src/typing.rs b/crates/ra_ide/src/typing.rs
index 53c65f8bc..cb2cd2479 100644
--- a/crates/ra_ide/src/typing.rs
+++ b/crates/ra_ide/src/typing.rs
@@ -213,14 +213,14 @@ fn foo() {
213 type_char( 213 type_char(
214 '.', 214 '.',
215 r" 215 r"
216 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> { 216 fn main() {
217 self.child_impl(db, name) 217 xs.foo()
218 <|> 218 <|>
219 } 219 }
220 ", 220 ",
221 r" 221 r"
222 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> { 222 fn main() {
223 self.child_impl(db, name) 223 xs.foo()
224 . 224 .
225 } 225 }
226 ", 226 ",
@@ -228,8 +228,8 @@ fn foo() {
228 type_char_noop( 228 type_char_noop(
229 '.', 229 '.',
230 r" 230 r"
231 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> { 231 fn main() {
232 self.child_impl(db, name) 232 xs.foo()
233 <|> 233 <|>
234 } 234 }
235 ", 235 ",
@@ -241,14 +241,14 @@ fn foo() {
241 type_char( 241 type_char(
242 '.', 242 '.',
243 r" 243 r"
244 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> { 244 fn main() {
245 self.child_impl(db, name) 245 xs.foo()
246 <|>; 246 <|>;
247 } 247 }
248 ", 248 ",
249 r" 249 r"
250 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> { 250 fn main() {
251 self.child_impl(db, name) 251 xs.foo()
252 .; 252 .;
253 } 253 }
254 ", 254 ",
@@ -256,8 +256,8 @@ fn foo() {
256 type_char_noop( 256 type_char_noop(
257 '.', 257 '.',
258 r" 258 r"
259 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> { 259 fn main() {
260 self.child_impl(db, name) 260 xs.foo()
261 <|>; 261 <|>;
262 } 262 }
263 ", 263 ",
@@ -269,15 +269,15 @@ fn foo() {
269 type_char( 269 type_char(
270 '.', 270 '.',
271 r" 271 r"
272 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> { 272 fn main() {
273 self.child_impl(db, name) 273 xs.foo()
274 .first() 274 .first()
275 <|> 275 <|>
276 } 276 }
277 ", 277 ",
278 r" 278 r"
279 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> { 279 fn main() {
280 self.child_impl(db, name) 280 xs.foo()
281 .first() 281 .first()
282 . 282 .
283 } 283 }
@@ -286,8 +286,8 @@ fn foo() {
286 type_char_noop( 286 type_char_noop(
287 '.', 287 '.',
288 r" 288 r"
289 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> { 289 fn main() {
290 self.child_impl(db, name) 290 xs.foo()
291 .first() 291 .first()
292 <|> 292 <|>
293 } 293 }
@@ -334,7 +334,7 @@ fn foo() {
334 type_char_noop( 334 type_char_noop(
335 '.', 335 '.',
336 r" 336 r"
337 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> { 337 fn main() {
338 <|> 338 <|>
339 } 339 }
340 ", 340 ",
@@ -342,7 +342,7 @@ fn foo() {
342 type_char_noop( 342 type_char_noop(
343 '.', 343 '.',
344 r" 344 r"
345 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> { 345 fn main() {
346 <|> 346 <|>
347 } 347 }
348 ", 348 ",