aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists')
-rw-r--r--crates/ra_assists/src/ast_transform.rs8
-rw-r--r--crates/ra_assists/src/handlers/add_missing_impl_members.rs6
-rw-r--r--crates/ra_assists/src/handlers/add_new.rs16
-rw-r--r--crates/ra_assists/src/handlers/move_bounds.rs2
-rw-r--r--crates/ra_assists/src/utils.rs10
-rw-r--r--crates/ra_assists/src/utils/insert_use.rs2
6 files changed, 23 insertions, 21 deletions
diff --git a/crates/ra_assists/src/ast_transform.rs b/crates/ra_assists/src/ast_transform.rs
index a74ac42d5..0e53c1eee 100644
--- a/crates/ra_assists/src/ast_transform.rs
+++ b/crates/ra_assists/src/ast_transform.rs
@@ -40,9 +40,9 @@ impl<'a> SubstituteTypeParams<'a> {
40 db: &'a RootDatabase, 40 db: &'a RootDatabase,
41 // FIXME: there's implicit invariant that `trait_` and `source_scope` match... 41 // FIXME: there's implicit invariant that `trait_` and `source_scope` match...
42 trait_: hir::Trait, 42 trait_: hir::Trait,
43 impl_block: ast::ImplBlock, 43 impl_def: ast::ImplDef,
44 ) -> SubstituteTypeParams<'a> { 44 ) -> SubstituteTypeParams<'a> {
45 let substs = get_syntactic_substs(impl_block).unwrap_or_default(); 45 let substs = get_syntactic_substs(impl_def).unwrap_or_default();
46 let generic_def: hir::GenericDef = trait_.into(); 46 let generic_def: hir::GenericDef = trait_.into();
47 let substs_by_param: FxHashMap<_, _> = generic_def 47 let substs_by_param: FxHashMap<_, _> = generic_def
48 .params(db) 48 .params(db)
@@ -59,8 +59,8 @@ impl<'a> SubstituteTypeParams<'a> {
59 59
60 // FIXME: It would probably be nicer if we could get this via HIR (i.e. get the 60 // FIXME: It would probably be nicer if we could get this via HIR (i.e. get the
61 // trait ref, and then go from the types in the substs back to the syntax) 61 // trait ref, and then go from the types in the substs back to the syntax)
62 fn get_syntactic_substs(impl_block: ast::ImplBlock) -> Option<Vec<ast::TypeRef>> { 62 fn get_syntactic_substs(impl_def: ast::ImplDef) -> Option<Vec<ast::TypeRef>> {
63 let target_trait = impl_block.target_trait()?; 63 let target_trait = impl_def.target_trait()?;
64 let path_type = match target_trait { 64 let path_type = match target_trait {
65 ast::TypeRef::PathType(path) => path, 65 ast::TypeRef::PathType(path) => path,
66 _ => return None, 66 _ => return None,
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 4005014bd..639180d37 100644
--- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs
+++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs
@@ -101,7 +101,7 @@ fn add_missing_impl_members_inner(
101 label: &'static str, 101 label: &'static str,
102) -> Option<Assist> { 102) -> Option<Assist> {
103 let _p = ra_prof::profile("add_missing_impl_members_inner"); 103 let _p = ra_prof::profile("add_missing_impl_members_inner");
104 let impl_node = ctx.find_node_at_offset::<ast::ImplBlock>()?; 104 let impl_node = ctx.find_node_at_offset::<ast::ImplDef>()?;
105 let impl_item_list = impl_node.item_list()?; 105 let impl_item_list = impl_node.item_list()?;
106 106
107 let trait_ = resolve_target_trait(&ctx.sema, &impl_node)?; 107 let trait_ = resolve_target_trait(&ctx.sema, &impl_node)?;
@@ -257,7 +257,7 @@ impl Foo for S {
257 } 257 }
258 258
259 #[test] 259 #[test]
260 fn test_empty_impl_block() { 260 fn test_empty_impl_def() {
261 check_assist( 261 check_assist(
262 add_missing_impl_members, 262 add_missing_impl_members,
263 " 263 "
@@ -308,7 +308,7 @@ impl<U> Foo<U> for S {
308 } 308 }
309 309
310 #[test] 310 #[test]
311 fn test_cursor_after_empty_impl_block() { 311 fn test_cursor_after_empty_impl_def() {
312 check_assist( 312 check_assist(
313 add_missing_impl_members, 313 add_missing_impl_members,
314 " 314 "
diff --git a/crates/ra_assists/src/handlers/add_new.rs b/crates/ra_assists/src/handlers/add_new.rs
index 166e907fb..697e7cda6 100644
--- a/crates/ra_assists/src/handlers/add_new.rs
+++ b/crates/ra_assists/src/handlers/add_new.rs
@@ -41,14 +41,14 @@ pub(crate) fn add_new(ctx: AssistCtx) -> Option<Assist> {
41 }; 41 };
42 42
43 // Return early if we've found an existing new fn 43 // Return early if we've found an existing new fn
44 let impl_block = find_struct_impl(&ctx, &strukt)?; 44 let impl_def = find_struct_impl(&ctx, &strukt)?;
45 45
46 ctx.add_assist(AssistId("add_new"), "Add default constructor", |edit| { 46 ctx.add_assist(AssistId("add_new"), "Add default constructor", |edit| {
47 edit.target(strukt.syntax().text_range()); 47 edit.target(strukt.syntax().text_range());
48 48
49 let mut buf = String::with_capacity(512); 49 let mut buf = String::with_capacity(512);
50 50
51 if impl_block.is_some() { 51 if impl_def.is_some() {
52 buf.push('\n'); 52 buf.push('\n');
53 } 53 }
54 54
@@ -71,10 +71,10 @@ pub(crate) fn add_new(ctx: AssistCtx) -> Option<Assist> {
71 71
72 buf.push_str("} }"); 72 buf.push_str("} }");
73 73
74 let (start_offset, end_offset) = impl_block 74 let (start_offset, end_offset) = impl_def
75 .and_then(|impl_block| { 75 .and_then(|impl_def| {
76 buf.push('\n'); 76 buf.push('\n');
77 let start = impl_block 77 let start = impl_def
78 .syntax() 78 .syntax()
79 .descendants_with_tokens() 79 .descendants_with_tokens()
80 .find(|t| t.kind() == T!['{'])? 80 .find(|t| t.kind() == T!['{'])?
@@ -128,7 +128,7 @@ fn generate_impl_text(strukt: &ast::StructDef, code: &str) -> String {
128// 128//
129// FIXME: change the new fn checking to a more semantic approach when that's more 129// FIXME: change the new fn checking to a more semantic approach when that's more
130// viable (e.g. we process proc macros, etc) 130// viable (e.g. we process proc macros, etc)
131fn find_struct_impl(ctx: &AssistCtx, strukt: &ast::StructDef) -> Option<Option<ast::ImplBlock>> { 131fn find_struct_impl(ctx: &AssistCtx, strukt: &ast::StructDef) -> Option<Option<ast::ImplDef>> {
132 let db = ctx.db; 132 let db = ctx.db;
133 let module = strukt.syntax().ancestors().find(|node| { 133 let module = strukt.syntax().ancestors().find(|node| {
134 ast::Module::can_cast(node.kind()) || ast::SourceFile::can_cast(node.kind()) 134 ast::Module::can_cast(node.kind()) || ast::SourceFile::can_cast(node.kind())
@@ -136,7 +136,7 @@ fn find_struct_impl(ctx: &AssistCtx, strukt: &ast::StructDef) -> Option<Option<a
136 136
137 let struct_def = ctx.sema.to_def(strukt)?; 137 let struct_def = ctx.sema.to_def(strukt)?;
138 138
139 let block = module.descendants().filter_map(ast::ImplBlock::cast).find_map(|impl_blk| { 139 let block = module.descendants().filter_map(ast::ImplDef::cast).find_map(|impl_blk| {
140 let blk = ctx.sema.to_def(&impl_blk)?; 140 let blk = ctx.sema.to_def(&impl_blk)?;
141 141
142 // FIXME: handle e.g. `struct S<T>; impl<U> S<U> {}` 142 // FIXME: handle e.g. `struct S<T>; impl<U> S<U> {}`
@@ -164,7 +164,7 @@ fn find_struct_impl(ctx: &AssistCtx, strukt: &ast::StructDef) -> Option<Option<a
164 Some(block) 164 Some(block)
165} 165}
166 166
167fn has_new_fn(imp: &ast::ImplBlock) -> bool { 167fn has_new_fn(imp: &ast::ImplDef) -> bool {
168 if let Some(il) = imp.item_list() { 168 if let Some(il) = imp.item_list() {
169 for item in il.impl_items() { 169 for item in il.impl_items() {
170 if let ast::ImplItem::FnDef(f) = item { 170 if let ast::ImplItem::FnDef(f) = item {
diff --git a/crates/ra_assists/src/handlers/move_bounds.rs b/crates/ra_assists/src/handlers/move_bounds.rs
index 86b235366..0b501f3e5 100644
--- a/crates/ra_assists/src/handlers/move_bounds.rs
+++ b/crates/ra_assists/src/handlers/move_bounds.rs
@@ -37,7 +37,7 @@ pub(crate) fn move_bounds_to_where_clause(ctx: AssistCtx) -> Option<Assist> {
37 let anchor: SyntaxElement = match parent.kind() { 37 let anchor: SyntaxElement = match parent.kind() {
38 FN_DEF => ast::FnDef::cast(parent)?.body()?.syntax().clone().into(), 38 FN_DEF => ast::FnDef::cast(parent)?.body()?.syntax().clone().into(),
39 TRAIT_DEF => ast::TraitDef::cast(parent)?.item_list()?.syntax().clone().into(), 39 TRAIT_DEF => ast::TraitDef::cast(parent)?.item_list()?.syntax().clone().into(),
40 IMPL_BLOCK => ast::ImplBlock::cast(parent)?.item_list()?.syntax().clone().into(), 40 IMPL_DEF => ast::ImplDef::cast(parent)?.item_list()?.syntax().clone().into(),
41 ENUM_DEF => ast::EnumDef::cast(parent)?.variant_list()?.syntax().clone().into(), 41 ENUM_DEF => ast::EnumDef::cast(parent)?.variant_list()?.syntax().clone().into(),
42 STRUCT_DEF => parent 42 STRUCT_DEF => parent
43 .children_with_tokens() 43 .children_with_tokens()
diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs
index d544caee7..3d6c59bda 100644
--- a/crates/ra_assists/src/utils.rs
+++ b/crates/ra_assists/src/utils.rs
@@ -13,14 +13,14 @@ pub use insert_use::insert_use_statement;
13 13
14pub fn get_missing_impl_items( 14pub fn get_missing_impl_items(
15 sema: &Semantics<RootDatabase>, 15 sema: &Semantics<RootDatabase>,
16 impl_block: &ast::ImplBlock, 16 impl_def: &ast::ImplDef,
17) -> Vec<hir::AssocItem> { 17) -> Vec<hir::AssocItem> {
18 // Names must be unique between constants and functions. However, type aliases 18 // Names must be unique between constants and functions. However, type aliases
19 // may share the same name as a function or constant. 19 // may share the same name as a function or constant.
20 let mut impl_fns_consts = FxHashSet::default(); 20 let mut impl_fns_consts = FxHashSet::default();
21 let mut impl_type = FxHashSet::default(); 21 let mut impl_type = FxHashSet::default();
22 22
23 if let Some(item_list) = impl_block.item_list() { 23 if let Some(item_list) = impl_def.item_list() {
24 for item in item_list.impl_items() { 24 for item in item_list.impl_items() {
25 match item { 25 match item {
26 ast::ImplItem::FnDef(f) => { 26 ast::ImplItem::FnDef(f) => {
@@ -44,7 +44,7 @@ pub fn get_missing_impl_items(
44 } 44 }
45 } 45 }
46 46
47 resolve_target_trait(sema, impl_block).map_or(vec![], |target_trait| { 47 resolve_target_trait(sema, impl_def).map_or(vec![], |target_trait| {
48 target_trait 48 target_trait
49 .items(sema.db) 49 .items(sema.db)
50 .iter() 50 .iter()
@@ -65,9 +65,9 @@ pub fn get_missing_impl_items(
65 65
66pub(crate) fn resolve_target_trait( 66pub(crate) fn resolve_target_trait(
67 sema: &Semantics<RootDatabase>, 67 sema: &Semantics<RootDatabase>,
68 impl_block: &ast::ImplBlock, 68 impl_def: &ast::ImplDef,
69) -> Option<hir::Trait> { 69) -> Option<hir::Trait> {
70 let ast_path = impl_block 70 let ast_path = impl_def
71 .target_trait() 71 .target_trait()
72 .map(|it| it.syntax().clone()) 72 .map(|it| it.syntax().clone())
73 .and_then(ast::PathType::cast)? 73 .and_then(ast::PathType::cast)?
diff --git a/crates/ra_assists/src/utils/insert_use.rs b/crates/ra_assists/src/utils/insert_use.rs
index 36fd2fc0b..c507e71e0 100644
--- a/crates/ra_assists/src/utils/insert_use.rs
+++ b/crates/ra_assists/src/utils/insert_use.rs
@@ -1,4 +1,6 @@
1//! Handle syntactic aspects of inserting a new `use`. 1//! Handle syntactic aspects of inserting a new `use`.
2// FIXME: rewrite according to the plan, outlined in
3// https://github.com/rust-analyzer/rust-analyzer/issues/3301#issuecomment-592931553
2 4
3use hir::{self, ModPath}; 5use hir::{self, ModPath};
4use ra_syntax::{ 6use ra_syntax::{