diff options
Diffstat (limited to 'crates/ra_assists')
21 files changed, 837 insertions, 223 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index 2fe7c3de3..da2880037 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs | |||
@@ -105,7 +105,7 @@ impl<'a> AssistCtx<'a> { | |||
105 | let mut info = AssistInfo::new(label); | 105 | let mut info = AssistInfo::new(label); |
106 | if self.should_compute_edit { | 106 | if self.should_compute_edit { |
107 | let action = { | 107 | let action = { |
108 | let mut edit = ActionBuilder::default(); | 108 | let mut edit = ActionBuilder::new(&self); |
109 | f(&mut edit); | 109 | f(&mut edit); |
110 | edit.build() | 110 | edit.build() |
111 | }; | 111 | }; |
@@ -130,6 +130,12 @@ impl<'a> AssistCtx<'a> { | |||
130 | pub(crate) fn find_node_at_offset<N: AstNode>(&self) -> Option<N> { | 130 | pub(crate) fn find_node_at_offset<N: AstNode>(&self) -> Option<N> { |
131 | find_node_at_offset(self.source_file.syntax(), self.frange.range.start()) | 131 | find_node_at_offset(self.source_file.syntax(), self.frange.range.start()) |
132 | } | 132 | } |
133 | |||
134 | pub(crate) fn find_node_at_offset_with_descend<N: AstNode>(&self) -> Option<N> { | ||
135 | self.sema | ||
136 | .find_node_at_offset_with_descend(self.source_file.syntax(), self.frange.range.start()) | ||
137 | } | ||
138 | |||
133 | pub(crate) fn covering_element(&self) -> SyntaxElement { | 139 | pub(crate) fn covering_element(&self) -> SyntaxElement { |
134 | find_covering_element(self.source_file.syntax(), self.frange.range) | 140 | find_covering_element(self.source_file.syntax(), self.frange.range) |
135 | } | 141 | } |
@@ -156,7 +162,7 @@ impl<'a> AssistGroup<'a> { | |||
156 | let mut info = AssistInfo::new(label).with_group(GroupLabel(self.group_name.clone())); | 162 | let mut info = AssistInfo::new(label).with_group(GroupLabel(self.group_name.clone())); |
157 | if self.ctx.should_compute_edit { | 163 | if self.ctx.should_compute_edit { |
158 | let action = { | 164 | let action = { |
159 | let mut edit = ActionBuilder::default(); | 165 | let mut edit = ActionBuilder::new(&self.ctx); |
160 | f(&mut edit); | 166 | f(&mut edit); |
161 | edit.build() | 167 | edit.build() |
162 | }; | 168 | }; |
@@ -175,15 +181,29 @@ impl<'a> AssistGroup<'a> { | |||
175 | } | 181 | } |
176 | } | 182 | } |
177 | 183 | ||
178 | #[derive(Default)] | 184 | pub(crate) struct ActionBuilder<'a, 'b> { |
179 | pub(crate) struct ActionBuilder { | ||
180 | edit: TextEditBuilder, | 185 | edit: TextEditBuilder, |
181 | cursor_position: Option<TextSize>, | 186 | cursor_position: Option<TextSize>, |
182 | target: Option<TextRange>, | 187 | target: Option<TextRange>, |
183 | file: AssistFile, | 188 | file: AssistFile, |
189 | ctx: &'a AssistCtx<'b>, | ||
184 | } | 190 | } |
185 | 191 | ||
186 | impl ActionBuilder { | 192 | impl<'a, 'b> ActionBuilder<'a, 'b> { |
193 | fn new(ctx: &'a AssistCtx<'b>) -> Self { | ||
194 | Self { | ||
195 | edit: TextEditBuilder::default(), | ||
196 | cursor_position: None, | ||
197 | target: None, | ||
198 | file: AssistFile::default(), | ||
199 | ctx, | ||
200 | } | ||
201 | } | ||
202 | |||
203 | pub(crate) fn ctx(&self) -> &AssistCtx<'b> { | ||
204 | &self.ctx | ||
205 | } | ||
206 | |||
187 | /// Replaces specified `range` of text with a given string. | 207 | /// Replaces specified `range` of text with a given string. |
188 | pub(crate) fn replace(&mut self, range: TextRange, replace_with: impl Into<String>) { | 208 | pub(crate) fn replace(&mut self, range: TextRange, replace_with: impl Into<String>) { |
189 | self.edit.replace(range, replace_with.into()) | 209 | self.edit.replace(range, replace_with.into()) |
diff --git a/crates/ra_assists/src/ast_transform.rs b/crates/ra_assists/src/ast_transform.rs index 52b4c82db..9ac65ab39 100644 --- a/crates/ra_assists/src/ast_transform.rs +++ b/crates/ra_assists/src/ast_transform.rs | |||
@@ -85,6 +85,7 @@ impl<'a> SubstituteTypeParams<'a> { | |||
85 | ast::TypeRef::PathType(path_type) => path_type.path()?, | 85 | ast::TypeRef::PathType(path_type) => path_type.path()?, |
86 | _ => return None, | 86 | _ => return None, |
87 | }; | 87 | }; |
88 | // FIXME: use `hir::Path::from_src` instead. | ||
88 | let path = hir::Path::from_ast(path)?; | 89 | let path = hir::Path::from_ast(path)?; |
89 | let resolution = self.source_scope.resolve_hir_path(&path)?; | 90 | let resolution = self.source_scope.resolve_hir_path(&path)?; |
90 | match resolution { | 91 | match resolution { |
@@ -128,6 +129,7 @@ impl<'a> QualifyPaths<'a> { | |||
128 | // don't try to qualify `Fn(Foo) -> Bar` paths, they are in prelude anyway | 129 | // don't try to qualify `Fn(Foo) -> Bar` paths, they are in prelude anyway |
129 | return None; | 130 | return None; |
130 | } | 131 | } |
132 | // FIXME: use `hir::Path::from_src` instead. | ||
131 | let hir_path = hir::Path::from_ast(p.clone()); | 133 | let hir_path = hir::Path::from_ast(p.clone()); |
132 | let resolution = self.source_scope.resolve_hir_path(&hir_path?)?; | 134 | let resolution = self.source_scope.resolve_hir_path(&hir_path?)?; |
133 | match resolution { | 135 | match resolution { |
diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs index e4fa9ee36..6696cc832 100644 --- a/crates/ra_assists/src/doc_tests/generated.rs +++ b/crates/ra_assists/src/doc_tests/generated.rs | |||
@@ -180,7 +180,9 @@ trait Trait<T> { | |||
180 | } | 180 | } |
181 | 181 | ||
182 | impl Trait<u32> for () { | 182 | impl Trait<u32> for () { |
183 | fn foo(&self) -> u32 { todo!() } | 183 | fn foo(&self) -> u32 { |
184 | todo!() | ||
185 | } | ||
184 | 186 | ||
185 | } | 187 | } |
186 | "#####, | 188 | "#####, |
@@ -726,3 +728,22 @@ use std::{collections::HashMap}; | |||
726 | "#####, | 728 | "#####, |
727 | ) | 729 | ) |
728 | } | 730 | } |
731 | |||
732 | #[test] | ||
733 | fn doctest_unwrap_block() { | ||
734 | check( | ||
735 | "unwrap_block", | ||
736 | r#####" | ||
737 | fn foo() { | ||
738 | if true {<|> | ||
739 | println!("foo"); | ||
740 | } | ||
741 | } | ||
742 | "#####, | ||
743 | r#####" | ||
744 | fn foo() { | ||
745 | println!("foo"); | ||
746 | } | ||
747 | "#####, | ||
748 | ) | ||
749 | } | ||
diff --git a/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs b/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs index 03806724a..49deb6701 100644 --- a/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs +++ b/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs | |||
@@ -1,11 +1,12 @@ | |||
1 | use ra_ide_db::RootDatabase; | ||
1 | use ra_syntax::{ | 2 | use ra_syntax::{ |
2 | ast::{self, AstNode, NameOwner}, | 3 | ast::{self, AstNode, NameOwner}, |
3 | TextSize, | 4 | TextSize, |
4 | }; | 5 | }; |
5 | use stdx::format_to; | 6 | use stdx::format_to; |
6 | 7 | ||
7 | use crate::{Assist, AssistCtx, AssistId}; | 8 | use crate::{utils::FamousDefs, Assist, AssistCtx, AssistId}; |
8 | use ra_ide_db::RootDatabase; | 9 | use test_utils::tested_by; |
9 | 10 | ||
10 | // Assist add_from_impl_for_enum | 11 | // Assist add_from_impl_for_enum |
11 | // | 12 | // |
@@ -41,7 +42,8 @@ pub(crate) fn add_from_impl_for_enum(ctx: AssistCtx) -> Option<Assist> { | |||
41 | _ => return None, | 42 | _ => return None, |
42 | }; | 43 | }; |
43 | 44 | ||
44 | if already_has_from_impl(ctx.sema, &variant) { | 45 | if existing_from_impl(ctx.sema, &variant).is_some() { |
46 | tested_by!(test_add_from_impl_already_exists); | ||
45 | return None; | 47 | return None; |
46 | } | 48 | } |
47 | 49 | ||
@@ -70,41 +72,33 @@ impl From<{0}> for {1} {{ | |||
70 | ) | 72 | ) |
71 | } | 73 | } |
72 | 74 | ||
73 | fn already_has_from_impl( | 75 | fn existing_from_impl( |
74 | sema: &'_ hir::Semantics<'_, RootDatabase>, | 76 | sema: &'_ hir::Semantics<'_, RootDatabase>, |
75 | variant: &ast::EnumVariant, | 77 | variant: &ast::EnumVariant, |
76 | ) -> bool { | 78 | ) -> Option<()> { |
77 | let scope = sema.scope(&variant.syntax()); | 79 | let variant = sema.to_def(variant)?; |
80 | let enum_ = variant.parent_enum(sema.db); | ||
81 | let krate = enum_.module(sema.db).krate(); | ||
78 | 82 | ||
79 | let from_path = ast::make::path_from_text("From"); | 83 | let from_trait = FamousDefs(sema, krate).core_convert_From()?; |
80 | let from_hir_path = match hir::Path::from_ast(from_path) { | ||
81 | Some(p) => p, | ||
82 | None => return false, | ||
83 | }; | ||
84 | let from_trait = match scope.resolve_hir_path(&from_hir_path) { | ||
85 | Some(hir::PathResolution::Def(hir::ModuleDef::Trait(t))) => t, | ||
86 | _ => return false, | ||
87 | }; | ||
88 | 84 | ||
89 | let e: hir::Enum = match sema.to_def(&variant.parent_enum()) { | 85 | let enum_type = enum_.ty(sema.db); |
90 | Some(e) => e, | ||
91 | None => return false, | ||
92 | }; | ||
93 | let e_ty = e.ty(sema.db); | ||
94 | 86 | ||
95 | let hir_enum_var: hir::EnumVariant = match sema.to_def(variant) { | 87 | let wrapped_type = variant.fields(sema.db).get(0)?.signature_ty(sema.db); |
96 | Some(ev) => ev, | ||
97 | None => return false, | ||
98 | }; | ||
99 | let var_ty = hir_enum_var.fields(sema.db)[0].signature_ty(sema.db); | ||
100 | 88 | ||
101 | e_ty.impls_trait(sema.db, from_trait, &[var_ty]) | 89 | if enum_type.impls_trait(sema.db, from_trait, &[wrapped_type]) { |
90 | Some(()) | ||
91 | } else { | ||
92 | None | ||
93 | } | ||
102 | } | 94 | } |
103 | 95 | ||
104 | #[cfg(test)] | 96 | #[cfg(test)] |
105 | mod tests { | 97 | mod tests { |
106 | use super::*; | 98 | use super::*; |
99 | |||
107 | use crate::helpers::{check_assist, check_assist_not_applicable}; | 100 | use crate::helpers::{check_assist, check_assist_not_applicable}; |
101 | use test_utils::covers; | ||
108 | 102 | ||
109 | #[test] | 103 | #[test] |
110 | fn test_add_from_impl_for_enum() { | 104 | fn test_add_from_impl_for_enum() { |
@@ -136,36 +130,40 @@ mod tests { | |||
136 | ); | 130 | ); |
137 | } | 131 | } |
138 | 132 | ||
133 | fn check_not_applicable(ra_fixture: &str) { | ||
134 | let fixture = | ||
135 | format!("//- main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE); | ||
136 | check_assist_not_applicable(add_from_impl_for_enum, &fixture) | ||
137 | } | ||
138 | |||
139 | #[test] | 139 | #[test] |
140 | fn test_add_from_impl_no_element() { | 140 | fn test_add_from_impl_no_element() { |
141 | check_assist_not_applicable(add_from_impl_for_enum, "enum A { <|>One }"); | 141 | check_not_applicable("enum A { <|>One }"); |
142 | } | 142 | } |
143 | 143 | ||
144 | #[test] | 144 | #[test] |
145 | fn test_add_from_impl_more_than_one_element_in_tuple() { | 145 | fn test_add_from_impl_more_than_one_element_in_tuple() { |
146 | check_assist_not_applicable(add_from_impl_for_enum, "enum A { <|>One(u32, String) }"); | 146 | check_not_applicable("enum A { <|>One(u32, String) }"); |
147 | } | 147 | } |
148 | 148 | ||
149 | #[test] | 149 | #[test] |
150 | fn test_add_from_impl_struct_variant() { | 150 | fn test_add_from_impl_struct_variant() { |
151 | check_assist_not_applicable(add_from_impl_for_enum, "enum A { <|>One { x: u32 } }"); | 151 | check_not_applicable("enum A { <|>One { x: u32 } }"); |
152 | } | 152 | } |
153 | 153 | ||
154 | #[test] | 154 | #[test] |
155 | fn test_add_from_impl_already_exists() { | 155 | fn test_add_from_impl_already_exists() { |
156 | check_assist_not_applicable( | 156 | covers!(test_add_from_impl_already_exists); |
157 | add_from_impl_for_enum, | 157 | check_not_applicable( |
158 | r#"enum A { <|>One(u32), } | 158 | r#" |
159 | enum A { <|>One(u32), } | ||
159 | 160 | ||
160 | impl From<u32> for A { | 161 | impl From<u32> for A { |
161 | fn from(v: u32) -> Self { | 162 | fn from(v: u32) -> Self { |
162 | A::One(v) | 163 | A::One(v) |
163 | } | 164 | } |
164 | } | 165 | } |
165 | 166 | "#, | |
166 | pub trait From<T> { | ||
167 | fn from(T) -> Self; | ||
168 | }"#, | ||
169 | ); | 167 | ); |
170 | } | 168 | } |
171 | 169 | ||
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 2d6d44980..e47feda71 100644 --- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs | |||
@@ -1,12 +1,16 @@ | |||
1 | use hir::HasSource; | 1 | use hir::HasSource; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | ast::{self, edit, make, AstNode, NameOwner}, | 3 | ast::{ |
4 | self, | ||
5 | edit::{self, IndentLevel}, | ||
6 | make, AstNode, NameOwner, | ||
7 | }, | ||
4 | SmolStr, | 8 | SmolStr, |
5 | }; | 9 | }; |
6 | 10 | ||
7 | use crate::{ | 11 | use crate::{ |
8 | ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}, | 12 | ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}, |
9 | utils::{get_missing_impl_items, resolve_target_trait}, | 13 | utils::{get_missing_assoc_items, resolve_target_trait}, |
10 | Assist, AssistCtx, AssistId, | 14 | Assist, AssistCtx, AssistId, |
11 | }; | 15 | }; |
12 | 16 | ||
@@ -40,7 +44,9 @@ enum AddMissingImplMembersMode { | |||
40 | // } | 44 | // } |
41 | // | 45 | // |
42 | // impl Trait<u32> for () { | 46 | // impl Trait<u32> for () { |
43 | // fn foo(&self) -> u32 { todo!() } | 47 | // fn foo(&self) -> u32 { |
48 | // todo!() | ||
49 | // } | ||
44 | // | 50 | // |
45 | // } | 51 | // } |
46 | // ``` | 52 | // ``` |
@@ -106,25 +112,25 @@ fn add_missing_impl_members_inner( | |||
106 | 112 | ||
107 | let trait_ = resolve_target_trait(&ctx.sema, &impl_node)?; | 113 | let trait_ = resolve_target_trait(&ctx.sema, &impl_node)?; |
108 | 114 | ||
109 | let def_name = |item: &ast::ImplItem| -> Option<SmolStr> { | 115 | let def_name = |item: &ast::AssocItem| -> Option<SmolStr> { |
110 | match item { | 116 | match item { |
111 | ast::ImplItem::FnDef(def) => def.name(), | 117 | ast::AssocItem::FnDef(def) => def.name(), |
112 | ast::ImplItem::TypeAliasDef(def) => def.name(), | 118 | ast::AssocItem::TypeAliasDef(def) => def.name(), |
113 | ast::ImplItem::ConstDef(def) => def.name(), | 119 | ast::AssocItem::ConstDef(def) => def.name(), |
114 | } | 120 | } |
115 | .map(|it| it.text().clone()) | 121 | .map(|it| it.text().clone()) |
116 | }; | 122 | }; |
117 | 123 | ||
118 | let missing_items = get_missing_impl_items(&ctx.sema, &impl_node) | 124 | let missing_items = get_missing_assoc_items(&ctx.sema, &impl_node) |
119 | .iter() | 125 | .iter() |
120 | .map(|i| match i { | 126 | .map(|i| match i { |
121 | hir::AssocItem::Function(i) => ast::ImplItem::FnDef(i.source(ctx.db).value), | 127 | hir::AssocItem::Function(i) => ast::AssocItem::FnDef(i.source(ctx.db).value), |
122 | hir::AssocItem::TypeAlias(i) => ast::ImplItem::TypeAliasDef(i.source(ctx.db).value), | 128 | hir::AssocItem::TypeAlias(i) => ast::AssocItem::TypeAliasDef(i.source(ctx.db).value), |
123 | hir::AssocItem::Const(i) => ast::ImplItem::ConstDef(i.source(ctx.db).value), | 129 | hir::AssocItem::Const(i) => ast::AssocItem::ConstDef(i.source(ctx.db).value), |
124 | }) | 130 | }) |
125 | .filter(|t| def_name(&t).is_some()) | 131 | .filter(|t| def_name(&t).is_some()) |
126 | .filter(|t| match t { | 132 | .filter(|t| match t { |
127 | ast::ImplItem::FnDef(def) => match mode { | 133 | ast::AssocItem::FnDef(def) => match mode { |
128 | AddMissingImplMembersMode::DefaultMethodsOnly => def.body().is_some(), | 134 | AddMissingImplMembersMode::DefaultMethodsOnly => def.body().is_some(), |
129 | AddMissingImplMembersMode::NoDefaultMethods => def.body().is_none(), | 135 | AddMissingImplMembersMode::NoDefaultMethods => def.body().is_none(), |
130 | }, | 136 | }, |
@@ -139,7 +145,7 @@ fn add_missing_impl_members_inner( | |||
139 | let sema = ctx.sema; | 145 | let sema = ctx.sema; |
140 | 146 | ||
141 | ctx.add_assist(AssistId(assist_id), label, |edit| { | 147 | ctx.add_assist(AssistId(assist_id), label, |edit| { |
142 | let n_existing_items = impl_item_list.impl_items().count(); | 148 | let n_existing_items = impl_item_list.assoc_items().count(); |
143 | let source_scope = sema.scope_for_def(trait_); | 149 | let source_scope = sema.scope_for_def(trait_); |
144 | let target_scope = sema.scope(impl_item_list.syntax()); | 150 | let target_scope = sema.scope(impl_item_list.syntax()); |
145 | let ast_transform = QualifyPaths::new(&target_scope, &source_scope) | 151 | let ast_transform = QualifyPaths::new(&target_scope, &source_scope) |
@@ -148,13 +154,13 @@ fn add_missing_impl_members_inner( | |||
148 | .into_iter() | 154 | .into_iter() |
149 | .map(|it| ast_transform::apply(&*ast_transform, it)) | 155 | .map(|it| ast_transform::apply(&*ast_transform, it)) |
150 | .map(|it| match it { | 156 | .map(|it| match it { |
151 | ast::ImplItem::FnDef(def) => ast::ImplItem::FnDef(add_body(def)), | 157 | ast::AssocItem::FnDef(def) => ast::AssocItem::FnDef(add_body(def)), |
152 | _ => it, | 158 | _ => it, |
153 | }) | 159 | }) |
154 | .map(|it| edit::remove_attrs_and_docs(&it)); | 160 | .map(|it| edit::remove_attrs_and_docs(&it)); |
155 | let new_impl_item_list = impl_item_list.append_items(items); | 161 | let new_impl_item_list = impl_item_list.append_items(items); |
156 | let cursor_position = { | 162 | let cursor_position = { |
157 | let first_new_item = new_impl_item_list.impl_items().nth(n_existing_items).unwrap(); | 163 | let first_new_item = new_impl_item_list.assoc_items().nth(n_existing_items).unwrap(); |
158 | first_new_item.syntax().text_range().start() | 164 | first_new_item.syntax().text_range().start() |
159 | }; | 165 | }; |
160 | 166 | ||
@@ -165,7 +171,9 @@ fn add_missing_impl_members_inner( | |||
165 | 171 | ||
166 | fn add_body(fn_def: ast::FnDef) -> ast::FnDef { | 172 | fn add_body(fn_def: ast::FnDef) -> ast::FnDef { |
167 | if fn_def.body().is_none() { | 173 | if fn_def.body().is_none() { |
168 | fn_def.with_body(make::block_from_expr(make::expr_todo())) | 174 | let body = make::block_expr(None, Some(make::expr_todo())); |
175 | let body = IndentLevel(1).increase_indent(body); | ||
176 | fn_def.with_body(body) | ||
169 | } else { | 177 | } else { |
170 | fn_def | 178 | fn_def |
171 | } | 179 | } |
@@ -181,7 +189,7 @@ mod tests { | |||
181 | fn test_add_missing_impl_members() { | 189 | fn test_add_missing_impl_members() { |
182 | check_assist( | 190 | check_assist( |
183 | add_missing_impl_members, | 191 | add_missing_impl_members, |
184 | " | 192 | r#" |
185 | trait Foo { | 193 | trait Foo { |
186 | type Output; | 194 | type Output; |
187 | 195 | ||
@@ -197,8 +205,8 @@ struct S; | |||
197 | impl Foo for S { | 205 | impl Foo for S { |
198 | fn bar(&self) {} | 206 | fn bar(&self) {} |
199 | <|> | 207 | <|> |
200 | }", | 208 | }"#, |
201 | " | 209 | r#" |
202 | trait Foo { | 210 | trait Foo { |
203 | type Output; | 211 | type Output; |
204 | 212 | ||
@@ -215,10 +223,14 @@ impl Foo for S { | |||
215 | fn bar(&self) {} | 223 | fn bar(&self) {} |
216 | <|>type Output; | 224 | <|>type Output; |
217 | const CONST: usize = 42; | 225 | const CONST: usize = 42; |
218 | fn foo(&self) { todo!() } | 226 | fn foo(&self) { |
219 | fn baz(&self) { todo!() } | 227 | todo!() |
228 | } | ||
229 | fn baz(&self) { | ||
230 | todo!() | ||
231 | } | ||
220 | 232 | ||
221 | }", | 233 | }"#, |
222 | ); | 234 | ); |
223 | } | 235 | } |
224 | 236 | ||
@@ -226,7 +238,7 @@ impl Foo for S { | |||
226 | fn test_copied_overriden_members() { | 238 | fn test_copied_overriden_members() { |
227 | check_assist( | 239 | check_assist( |
228 | add_missing_impl_members, | 240 | add_missing_impl_members, |
229 | " | 241 | r#" |
230 | trait Foo { | 242 | trait Foo { |
231 | fn foo(&self); | 243 | fn foo(&self); |
232 | fn bar(&self) -> bool { true } | 244 | fn bar(&self) -> bool { true } |
@@ -238,8 +250,8 @@ struct S; | |||
238 | impl Foo for S { | 250 | impl Foo for S { |
239 | fn bar(&self) {} | 251 | fn bar(&self) {} |
240 | <|> | 252 | <|> |
241 | }", | 253 | }"#, |
242 | " | 254 | r#" |
243 | trait Foo { | 255 | trait Foo { |
244 | fn foo(&self); | 256 | fn foo(&self); |
245 | fn bar(&self) -> bool { true } | 257 | fn bar(&self) -> bool { true } |
@@ -250,9 +262,11 @@ struct S; | |||
250 | 262 | ||
251 | impl Foo for S { | 263 | impl Foo for S { |
252 | fn bar(&self) {} | 264 | fn bar(&self) {} |
253 | <|>fn foo(&self) { todo!() } | 265 | <|>fn foo(&self) { |
266 | todo!() | ||
267 | } | ||
254 | 268 | ||
255 | }", | 269 | }"#, |
256 | ); | 270 | ); |
257 | } | 271 | } |
258 | 272 | ||
@@ -260,16 +274,18 @@ impl Foo for S { | |||
260 | fn test_empty_impl_def() { | 274 | fn test_empty_impl_def() { |
261 | check_assist( | 275 | check_assist( |
262 | add_missing_impl_members, | 276 | add_missing_impl_members, |
263 | " | 277 | r#" |
264 | trait Foo { fn foo(&self); } | 278 | trait Foo { fn foo(&self); } |
265 | struct S; | 279 | struct S; |
266 | impl Foo for S { <|> }", | 280 | impl Foo for S { <|> }"#, |
267 | " | 281 | r#" |
268 | trait Foo { fn foo(&self); } | 282 | trait Foo { fn foo(&self); } |
269 | struct S; | 283 | struct S; |
270 | impl Foo for S { | 284 | impl Foo for S { |
271 | <|>fn foo(&self) { todo!() } | 285 | <|>fn foo(&self) { |
272 | }", | 286 | todo!() |
287 | } | ||
288 | }"#, | ||
273 | ); | 289 | ); |
274 | } | 290 | } |
275 | 291 | ||
@@ -277,16 +293,18 @@ impl Foo for S { | |||
277 | fn fill_in_type_params_1() { | 293 | fn fill_in_type_params_1() { |
278 | check_assist( | 294 | check_assist( |
279 | add_missing_impl_members, | 295 | add_missing_impl_members, |
280 | " | 296 | r#" |
281 | trait Foo<T> { fn foo(&self, t: T) -> &T; } | 297 | trait Foo<T> { fn foo(&self, t: T) -> &T; } |
282 | struct S; | 298 | struct S; |
283 | impl Foo<u32> for S { <|> }", | 299 | impl Foo<u32> for S { <|> }"#, |
284 | " | 300 | r#" |
285 | trait Foo<T> { fn foo(&self, t: T) -> &T; } | 301 | trait Foo<T> { fn foo(&self, t: T) -> &T; } |
286 | struct S; | 302 | struct S; |
287 | impl Foo<u32> for S { | 303 | impl Foo<u32> for S { |
288 | <|>fn foo(&self, t: u32) -> &u32 { todo!() } | 304 | <|>fn foo(&self, t: u32) -> &u32 { |
289 | }", | 305 | todo!() |
306 | } | ||
307 | }"#, | ||
290 | ); | 308 | ); |
291 | } | 309 | } |
292 | 310 | ||
@@ -294,16 +312,18 @@ impl Foo<u32> for S { | |||
294 | fn fill_in_type_params_2() { | 312 | fn fill_in_type_params_2() { |
295 | check_assist( | 313 | check_assist( |
296 | add_missing_impl_members, | 314 | add_missing_impl_members, |
297 | " | 315 | r#" |
298 | trait Foo<T> { fn foo(&self, t: T) -> &T; } | 316 | trait Foo<T> { fn foo(&self, t: T) -> &T; } |
299 | struct S; | 317 | struct S; |
300 | impl<U> Foo<U> for S { <|> }", | 318 | impl<U> Foo<U> for S { <|> }"#, |
301 | " | 319 | r#" |
302 | trait Foo<T> { fn foo(&self, t: T) -> &T; } | 320 | trait Foo<T> { fn foo(&self, t: T) -> &T; } |
303 | struct S; | 321 | struct S; |
304 | impl<U> Foo<U> for S { | 322 | impl<U> Foo<U> for S { |
305 | <|>fn foo(&self, t: U) -> &U { todo!() } | 323 | <|>fn foo(&self, t: U) -> &U { |
306 | }", | 324 | todo!() |
325 | } | ||
326 | }"#, | ||
307 | ); | 327 | ); |
308 | } | 328 | } |
309 | 329 | ||
@@ -311,16 +331,18 @@ impl<U> Foo<U> for S { | |||
311 | fn test_cursor_after_empty_impl_def() { | 331 | fn test_cursor_after_empty_impl_def() { |
312 | check_assist( | 332 | check_assist( |
313 | add_missing_impl_members, | 333 | add_missing_impl_members, |
314 | " | 334 | r#" |
315 | trait Foo { fn foo(&self); } | 335 | trait Foo { fn foo(&self); } |
316 | struct S; | 336 | struct S; |
317 | impl Foo for S {}<|>", | 337 | impl Foo for S {}<|>"#, |
318 | " | 338 | r#" |
319 | trait Foo { fn foo(&self); } | 339 | trait Foo { fn foo(&self); } |
320 | struct S; | 340 | struct S; |
321 | impl Foo for S { | 341 | impl Foo for S { |
322 | <|>fn foo(&self) { todo!() } | 342 | <|>fn foo(&self) { |
323 | }", | 343 | todo!() |
344 | } | ||
345 | }"#, | ||
324 | ) | 346 | ) |
325 | } | 347 | } |
326 | 348 | ||
@@ -328,22 +350,24 @@ impl Foo for S { | |||
328 | fn test_qualify_path_1() { | 350 | fn test_qualify_path_1() { |
329 | check_assist( | 351 | check_assist( |
330 | add_missing_impl_members, | 352 | add_missing_impl_members, |
331 | " | 353 | r#" |
332 | mod foo { | 354 | mod foo { |
333 | pub struct Bar; | 355 | pub struct Bar; |
334 | trait Foo { fn foo(&self, bar: Bar); } | 356 | trait Foo { fn foo(&self, bar: Bar); } |
335 | } | 357 | } |
336 | struct S; | 358 | struct S; |
337 | impl foo::Foo for S { <|> }", | 359 | impl foo::Foo for S { <|> }"#, |
338 | " | 360 | r#" |
339 | mod foo { | 361 | mod foo { |
340 | pub struct Bar; | 362 | pub struct Bar; |
341 | trait Foo { fn foo(&self, bar: Bar); } | 363 | trait Foo { fn foo(&self, bar: Bar); } |
342 | } | 364 | } |
343 | struct S; | 365 | struct S; |
344 | impl foo::Foo for S { | 366 | impl foo::Foo for S { |
345 | <|>fn foo(&self, bar: foo::Bar) { todo!() } | 367 | <|>fn foo(&self, bar: foo::Bar) { |
346 | }", | 368 | todo!() |
369 | } | ||
370 | }"#, | ||
347 | ); | 371 | ); |
348 | } | 372 | } |
349 | 373 | ||
@@ -351,22 +375,24 @@ impl foo::Foo for S { | |||
351 | fn test_qualify_path_generic() { | 375 | fn test_qualify_path_generic() { |
352 | check_assist( | 376 | check_assist( |
353 | add_missing_impl_members, | 377 | add_missing_impl_members, |
354 | " | 378 | r#" |
355 | mod foo { | 379 | mod foo { |
356 | pub struct Bar<T>; | 380 | pub struct Bar<T>; |
357 | trait Foo { fn foo(&self, bar: Bar<u32>); } | 381 | trait Foo { fn foo(&self, bar: Bar<u32>); } |
358 | } | 382 | } |
359 | struct S; | 383 | struct S; |
360 | impl foo::Foo for S { <|> }", | 384 | impl foo::Foo for S { <|> }"#, |
361 | " | 385 | r#" |
362 | mod foo { | 386 | mod foo { |
363 | pub struct Bar<T>; | 387 | pub struct Bar<T>; |
364 | trait Foo { fn foo(&self, bar: Bar<u32>); } | 388 | trait Foo { fn foo(&self, bar: Bar<u32>); } |
365 | } | 389 | } |
366 | struct S; | 390 | struct S; |
367 | impl foo::Foo for S { | 391 | impl foo::Foo for S { |
368 | <|>fn foo(&self, bar: foo::Bar<u32>) { todo!() } | 392 | <|>fn foo(&self, bar: foo::Bar<u32>) { |
369 | }", | 393 | todo!() |
394 | } | ||
395 | }"#, | ||
370 | ); | 396 | ); |
371 | } | 397 | } |
372 | 398 | ||
@@ -374,22 +400,24 @@ impl foo::Foo for S { | |||
374 | fn test_qualify_path_and_substitute_param() { | 400 | fn test_qualify_path_and_substitute_param() { |
375 | check_assist( | 401 | check_assist( |
376 | add_missing_impl_members, | 402 | add_missing_impl_members, |
377 | " | 403 | r#" |
378 | mod foo { | 404 | mod foo { |
379 | pub struct Bar<T>; | 405 | pub struct Bar<T>; |
380 | trait Foo<T> { fn foo(&self, bar: Bar<T>); } | 406 | trait Foo<T> { fn foo(&self, bar: Bar<T>); } |
381 | } | 407 | } |
382 | struct S; | 408 | struct S; |
383 | impl foo::Foo<u32> for S { <|> }", | 409 | impl foo::Foo<u32> for S { <|> }"#, |
384 | " | 410 | r#" |
385 | mod foo { | 411 | mod foo { |
386 | pub struct Bar<T>; | 412 | pub struct Bar<T>; |
387 | trait Foo<T> { fn foo(&self, bar: Bar<T>); } | 413 | trait Foo<T> { fn foo(&self, bar: Bar<T>); } |
388 | } | 414 | } |
389 | struct S; | 415 | struct S; |
390 | impl foo::Foo<u32> for S { | 416 | impl foo::Foo<u32> for S { |
391 | <|>fn foo(&self, bar: foo::Bar<u32>) { todo!() } | 417 | <|>fn foo(&self, bar: foo::Bar<u32>) { |
392 | }", | 418 | todo!() |
419 | } | ||
420 | }"#, | ||
393 | ); | 421 | ); |
394 | } | 422 | } |
395 | 423 | ||
@@ -398,15 +426,15 @@ impl foo::Foo<u32> for S { | |||
398 | // when substituting params, the substituted param should not be qualified! | 426 | // when substituting params, the substituted param should not be qualified! |
399 | check_assist( | 427 | check_assist( |
400 | add_missing_impl_members, | 428 | add_missing_impl_members, |
401 | " | 429 | r#" |
402 | mod foo { | 430 | mod foo { |
403 | trait Foo<T> { fn foo(&self, bar: T); } | 431 | trait Foo<T> { fn foo(&self, bar: T); } |
404 | pub struct Param; | 432 | pub struct Param; |
405 | } | 433 | } |
406 | struct Param; | 434 | struct Param; |
407 | struct S; | 435 | struct S; |
408 | impl foo::Foo<Param> for S { <|> }", | 436 | impl foo::Foo<Param> for S { <|> }"#, |
409 | " | 437 | r#" |
410 | mod foo { | 438 | mod foo { |
411 | trait Foo<T> { fn foo(&self, bar: T); } | 439 | trait Foo<T> { fn foo(&self, bar: T); } |
412 | pub struct Param; | 440 | pub struct Param; |
@@ -414,8 +442,10 @@ mod foo { | |||
414 | struct Param; | 442 | struct Param; |
415 | struct S; | 443 | struct S; |
416 | impl foo::Foo<Param> for S { | 444 | impl foo::Foo<Param> for S { |
417 | <|>fn foo(&self, bar: Param) { todo!() } | 445 | <|>fn foo(&self, bar: Param) { |
418 | }", | 446 | todo!() |
447 | } | ||
448 | }"#, | ||
419 | ); | 449 | ); |
420 | } | 450 | } |
421 | 451 | ||
@@ -423,15 +453,15 @@ impl foo::Foo<Param> for S { | |||
423 | fn test_qualify_path_associated_item() { | 453 | fn test_qualify_path_associated_item() { |
424 | check_assist( | 454 | check_assist( |
425 | add_missing_impl_members, | 455 | add_missing_impl_members, |
426 | " | 456 | r#" |
427 | mod foo { | 457 | mod foo { |
428 | pub struct Bar<T>; | 458 | pub struct Bar<T>; |
429 | impl Bar<T> { type Assoc = u32; } | 459 | impl Bar<T> { type Assoc = u32; } |
430 | trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); } | 460 | trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); } |
431 | } | 461 | } |
432 | struct S; | 462 | struct S; |
433 | impl foo::Foo for S { <|> }", | 463 | impl foo::Foo for S { <|> }"#, |
434 | " | 464 | r#" |
435 | mod foo { | 465 | mod foo { |
436 | pub struct Bar<T>; | 466 | pub struct Bar<T>; |
437 | impl Bar<T> { type Assoc = u32; } | 467 | impl Bar<T> { type Assoc = u32; } |
@@ -439,8 +469,10 @@ mod foo { | |||
439 | } | 469 | } |
440 | struct S; | 470 | struct S; |
441 | impl foo::Foo for S { | 471 | impl foo::Foo for S { |
442 | <|>fn foo(&self, bar: foo::Bar<u32>::Assoc) { todo!() } | 472 | <|>fn foo(&self, bar: foo::Bar<u32>::Assoc) { |
443 | }", | 473 | todo!() |
474 | } | ||
475 | }"#, | ||
444 | ); | 476 | ); |
445 | } | 477 | } |
446 | 478 | ||
@@ -448,15 +480,15 @@ impl foo::Foo for S { | |||
448 | fn test_qualify_path_nested() { | 480 | fn test_qualify_path_nested() { |
449 | check_assist( | 481 | check_assist( |
450 | add_missing_impl_members, | 482 | add_missing_impl_members, |
451 | " | 483 | r#" |
452 | mod foo { | 484 | mod foo { |
453 | pub struct Bar<T>; | 485 | pub struct Bar<T>; |
454 | pub struct Baz; | 486 | pub struct Baz; |
455 | trait Foo { fn foo(&self, bar: Bar<Baz>); } | 487 | trait Foo { fn foo(&self, bar: Bar<Baz>); } |
456 | } | 488 | } |
457 | struct S; | 489 | struct S; |
458 | impl foo::Foo for S { <|> }", | 490 | impl foo::Foo for S { <|> }"#, |
459 | " | 491 | r#" |
460 | mod foo { | 492 | mod foo { |
461 | pub struct Bar<T>; | 493 | pub struct Bar<T>; |
462 | pub struct Baz; | 494 | pub struct Baz; |
@@ -464,8 +496,10 @@ mod foo { | |||
464 | } | 496 | } |
465 | struct S; | 497 | struct S; |
466 | impl foo::Foo for S { | 498 | impl foo::Foo for S { |
467 | <|>fn foo(&self, bar: foo::Bar<foo::Baz>) { todo!() } | 499 | <|>fn foo(&self, bar: foo::Bar<foo::Baz>) { |
468 | }", | 500 | todo!() |
501 | } | ||
502 | }"#, | ||
469 | ); | 503 | ); |
470 | } | 504 | } |
471 | 505 | ||
@@ -473,22 +507,24 @@ impl foo::Foo for S { | |||
473 | fn test_qualify_path_fn_trait_notation() { | 507 | fn test_qualify_path_fn_trait_notation() { |
474 | check_assist( | 508 | check_assist( |
475 | add_missing_impl_members, | 509 | add_missing_impl_members, |
476 | " | 510 | r#" |
477 | mod foo { | 511 | mod foo { |
478 | pub trait Fn<Args> { type Output; } | 512 | pub trait Fn<Args> { type Output; } |
479 | trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); } | 513 | trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); } |
480 | } | 514 | } |
481 | struct S; | 515 | struct S; |
482 | impl foo::Foo for S { <|> }", | 516 | impl foo::Foo for S { <|> }"#, |
483 | " | 517 | r#" |
484 | mod foo { | 518 | mod foo { |
485 | pub trait Fn<Args> { type Output; } | 519 | pub trait Fn<Args> { type Output; } |
486 | trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); } | 520 | trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); } |
487 | } | 521 | } |
488 | struct S; | 522 | struct S; |
489 | impl foo::Foo for S { | 523 | impl foo::Foo for S { |
490 | <|>fn foo(&self, bar: dyn Fn(u32) -> i32) { todo!() } | 524 | <|>fn foo(&self, bar: dyn Fn(u32) -> i32) { |
491 | }", | 525 | todo!() |
526 | } | ||
527 | }"#, | ||
492 | ); | 528 | ); |
493 | } | 529 | } |
494 | 530 | ||
@@ -496,10 +532,10 @@ impl foo::Foo for S { | |||
496 | fn test_empty_trait() { | 532 | fn test_empty_trait() { |
497 | check_assist_not_applicable( | 533 | check_assist_not_applicable( |
498 | add_missing_impl_members, | 534 | add_missing_impl_members, |
499 | " | 535 | r#" |
500 | trait Foo; | 536 | trait Foo; |
501 | struct S; | 537 | struct S; |
502 | impl Foo for S { <|> }", | 538 | impl Foo for S { <|> }"#, |
503 | ) | 539 | ) |
504 | } | 540 | } |
505 | 541 | ||
@@ -507,13 +543,13 @@ impl Foo for S { <|> }", | |||
507 | fn test_ignore_unnamed_trait_members_and_default_methods() { | 543 | fn test_ignore_unnamed_trait_members_and_default_methods() { |
508 | check_assist_not_applicable( | 544 | check_assist_not_applicable( |
509 | add_missing_impl_members, | 545 | add_missing_impl_members, |
510 | " | 546 | r#" |
511 | trait Foo { | 547 | trait Foo { |
512 | fn (arg: u32); | 548 | fn (arg: u32); |
513 | fn valid(some: u32) -> bool { false } | 549 | fn valid(some: u32) -> bool { false } |
514 | } | 550 | } |
515 | struct S; | 551 | struct S; |
516 | impl Foo for S { <|> }", | 552 | impl Foo for S { <|> }"#, |
517 | ) | 553 | ) |
518 | } | 554 | } |
519 | 555 | ||
@@ -544,7 +580,9 @@ trait Foo { | |||
544 | struct S; | 580 | struct S; |
545 | impl Foo for S { | 581 | impl Foo for S { |
546 | <|>type Output; | 582 | <|>type Output; |
547 | fn foo(&self) { todo!() } | 583 | fn foo(&self) { |
584 | todo!() | ||
585 | } | ||
548 | }"#, | 586 | }"#, |
549 | ) | 587 | ) |
550 | } | 588 | } |
@@ -553,7 +591,7 @@ impl Foo for S { | |||
553 | fn test_default_methods() { | 591 | fn test_default_methods() { |
554 | check_assist( | 592 | check_assist( |
555 | add_missing_default_members, | 593 | add_missing_default_members, |
556 | " | 594 | r#" |
557 | trait Foo { | 595 | trait Foo { |
558 | type Output; | 596 | type Output; |
559 | 597 | ||
@@ -563,8 +601,8 @@ trait Foo { | |||
563 | fn foo(some: u32) -> bool; | 601 | fn foo(some: u32) -> bool; |
564 | } | 602 | } |
565 | struct S; | 603 | struct S; |
566 | impl Foo for S { <|> }", | 604 | impl Foo for S { <|> }"#, |
567 | " | 605 | r#" |
568 | trait Foo { | 606 | trait Foo { |
569 | type Output; | 607 | type Output; |
570 | 608 | ||
@@ -576,7 +614,7 @@ trait Foo { | |||
576 | struct S; | 614 | struct S; |
577 | impl Foo for S { | 615 | impl Foo for S { |
578 | <|>fn valid(some: u32) -> bool { false } | 616 | <|>fn valid(some: u32) -> bool { false } |
579 | }", | 617 | }"#, |
580 | ) | 618 | ) |
581 | } | 619 | } |
582 | } | 620 | } |
diff --git a/crates/ra_assists/src/handlers/add_new.rs b/crates/ra_assists/src/handlers/add_new.rs index 0f9174a29..e8a36c7de 100644 --- a/crates/ra_assists/src/handlers/add_new.rs +++ b/crates/ra_assists/src/handlers/add_new.rs | |||
@@ -162,8 +162,8 @@ fn find_struct_impl(ctx: &AssistCtx, strukt: &ast::StructDef) -> Option<Option<a | |||
162 | 162 | ||
163 | fn has_new_fn(imp: &ast::ImplDef) -> bool { | 163 | fn has_new_fn(imp: &ast::ImplDef) -> bool { |
164 | if let Some(il) = imp.item_list() { | 164 | if let Some(il) = imp.item_list() { |
165 | for item in il.impl_items() { | 165 | for item in il.assoc_items() { |
166 | if let ast::ImplItem::FnDef(f) = item { | 166 | if let ast::AssocItem::FnDef(f) = item { |
167 | if let Some(name) = f.name() { | 167 | if let Some(name) = f.name() { |
168 | if name.text().eq_ignore_ascii_case("new") { | 168 | if name.text().eq_ignore_ascii_case("new") { |
169 | return true; | 169 | return true; |
diff --git a/crates/ra_assists/src/handlers/auto_import.rs b/crates/ra_assists/src/handlers/auto_import.rs index 99682e023..db6c4d2fa 100644 --- a/crates/ra_assists/src/handlers/auto_import.rs +++ b/crates/ra_assists/src/handlers/auto_import.rs | |||
@@ -45,15 +45,12 @@ pub(crate) fn auto_import(ctx: AssistCtx) -> Option<Assist> { | |||
45 | return None; | 45 | return None; |
46 | } | 46 | } |
47 | 47 | ||
48 | let range = ctx.sema.original_range(&auto_import_assets.syntax_under_caret).range; | ||
48 | let mut group = ctx.add_assist_group(auto_import_assets.get_import_group_message()); | 49 | let mut group = ctx.add_assist_group(auto_import_assets.get_import_group_message()); |
49 | for import in proposed_imports { | 50 | for import in proposed_imports { |
50 | group.add_assist(AssistId("auto_import"), format!("Import `{}`", &import), |edit| { | 51 | group.add_assist(AssistId("auto_import"), format!("Import `{}`", &import), |edit| { |
51 | edit.target(auto_import_assets.syntax_under_caret.text_range()); | 52 | edit.target(range); |
52 | insert_use_statement( | 53 | insert_use_statement(&auto_import_assets.syntax_under_caret, &import, edit); |
53 | &auto_import_assets.syntax_under_caret, | ||
54 | &import, | ||
55 | edit.text_edit_builder(), | ||
56 | ); | ||
57 | }); | 54 | }); |
58 | } | 55 | } |
59 | group.finish() | 56 | group.finish() |
@@ -68,10 +65,10 @@ struct AutoImportAssets { | |||
68 | 65 | ||
69 | impl AutoImportAssets { | 66 | impl AutoImportAssets { |
70 | fn new(ctx: &AssistCtx) -> Option<Self> { | 67 | fn new(ctx: &AssistCtx) -> Option<Self> { |
71 | if let Some(path_under_caret) = ctx.find_node_at_offset::<ast::Path>() { | 68 | if let Some(path_under_caret) = ctx.find_node_at_offset_with_descend::<ast::Path>() { |
72 | Self::for_regular_path(path_under_caret, &ctx) | 69 | Self::for_regular_path(path_under_caret, &ctx) |
73 | } else { | 70 | } else { |
74 | Self::for_method_call(ctx.find_node_at_offset()?, &ctx) | 71 | Self::for_method_call(ctx.find_node_at_offset_with_descend()?, &ctx) |
75 | } | 72 | } |
76 | } | 73 | } |
77 | 74 | ||
@@ -306,6 +303,35 @@ mod tests { | |||
306 | } | 303 | } |
307 | 304 | ||
308 | #[test] | 305 | #[test] |
306 | fn applicable_when_found_an_import_in_macros() { | ||
307 | check_assist( | ||
308 | auto_import, | ||
309 | r" | ||
310 | macro_rules! foo { | ||
311 | ($i:ident) => { fn foo(a: $i) {} } | ||
312 | } | ||
313 | foo!(Pub<|>Struct); | ||
314 | |||
315 | pub mod PubMod { | ||
316 | pub struct PubStruct; | ||
317 | } | ||
318 | ", | ||
319 | r" | ||
320 | use PubMod::PubStruct; | ||
321 | |||
322 | macro_rules! foo { | ||
323 | ($i:ident) => { fn foo(a: $i) {} } | ||
324 | } | ||
325 | foo!(Pub<|>Struct); | ||
326 | |||
327 | pub mod PubMod { | ||
328 | pub struct PubStruct; | ||
329 | } | ||
330 | ", | ||
331 | ); | ||
332 | } | ||
333 | |||
334 | #[test] | ||
309 | fn auto_imports_are_merged() { | 335 | fn auto_imports_are_merged() { |
310 | check_assist( | 336 | check_assist( |
311 | auto_import, | 337 | auto_import, |
diff --git a/crates/ra_assists/src/handlers/change_visibility.rs b/crates/ra_assists/src/handlers/change_visibility.rs index 44f6a1dae..1cd532e80 100644 --- a/crates/ra_assists/src/handlers/change_visibility.rs +++ b/crates/ra_assists/src/handlers/change_visibility.rs | |||
@@ -47,8 +47,7 @@ fn add_vis(ctx: AssistCtx) -> Option<Assist> { | |||
47 | return None; | 47 | return None; |
48 | } | 48 | } |
49 | (vis_offset(&parent), keyword.text_range()) | 49 | (vis_offset(&parent), keyword.text_range()) |
50 | } else { | 50 | } else if let Some(field_name) = ctx.find_node_at_offset::<ast::Name>() { |
51 | let field_name: ast::Name = ctx.find_node_at_offset()?; | ||
52 | let field = field_name.syntax().ancestors().find_map(ast::RecordFieldDef::cast)?; | 51 | let field = field_name.syntax().ancestors().find_map(ast::RecordFieldDef::cast)?; |
53 | if field.name()? != field_name { | 52 | if field.name()? != field_name { |
54 | tested_by!(change_visibility_field_false_positive); | 53 | tested_by!(change_visibility_field_false_positive); |
@@ -58,6 +57,13 @@ fn add_vis(ctx: AssistCtx) -> Option<Assist> { | |||
58 | return None; | 57 | return None; |
59 | } | 58 | } |
60 | (vis_offset(field.syntax()), field_name.syntax().text_range()) | 59 | (vis_offset(field.syntax()), field_name.syntax().text_range()) |
60 | } else if let Some(field) = ctx.find_node_at_offset::<ast::TupleFieldDef>() { | ||
61 | if field.visibility().is_some() { | ||
62 | return None; | ||
63 | } | ||
64 | (vis_offset(field.syntax()), field.syntax().text_range()) | ||
65 | } else { | ||
66 | return None; | ||
61 | }; | 67 | }; |
62 | 68 | ||
63 | ctx.add_assist(AssistId("change_visibility"), "Change visibility to pub(crate)", |edit| { | 69 | ctx.add_assist(AssistId("change_visibility"), "Change visibility to pub(crate)", |edit| { |
@@ -129,7 +135,8 @@ mod tests { | |||
129 | change_visibility, | 135 | change_visibility, |
130 | r"struct S { <|>field: u32 }", | 136 | r"struct S { <|>field: u32 }", |
131 | r"struct S { <|>pub(crate) field: u32 }", | 137 | r"struct S { <|>pub(crate) field: u32 }", |
132 | ) | 138 | ); |
139 | check_assist(change_visibility, r"struct S ( <|>u32 )", r"struct S ( <|>pub(crate) u32 )"); | ||
133 | } | 140 | } |
134 | 141 | ||
135 | #[test] | 142 | #[test] |
diff --git a/crates/ra_assists/src/handlers/early_return.rs b/crates/ra_assists/src/handlers/early_return.rs index ea6c56f8c..eede2fe91 100644 --- a/crates/ra_assists/src/handlers/early_return.rs +++ b/crates/ra_assists/src/handlers/early_return.rs | |||
@@ -2,7 +2,7 @@ use std::{iter::once, ops::RangeInclusive}; | |||
2 | 2 | ||
3 | use ra_syntax::{ | 3 | use ra_syntax::{ |
4 | algo::replace_children, | 4 | algo::replace_children, |
5 | ast::{self, edit::IndentLevel, make, Block, Pat::TupleStructPat}, | 5 | ast::{self, edit::IndentLevel, make}, |
6 | AstNode, | 6 | AstNode, |
7 | SyntaxKind::{FN_DEF, LOOP_EXPR, L_CURLY, R_CURLY, WHILE_EXPR, WHITESPACE}, | 7 | SyntaxKind::{FN_DEF, LOOP_EXPR, L_CURLY, R_CURLY, WHILE_EXPR, WHITESPACE}, |
8 | SyntaxNode, | 8 | SyntaxNode, |
@@ -47,7 +47,7 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option<Assist> { | |||
47 | // Check if there is an IfLet that we can handle. | 47 | // Check if there is an IfLet that we can handle. |
48 | let if_let_pat = match cond.pat() { | 48 | let if_let_pat = match cond.pat() { |
49 | None => None, // No IfLet, supported. | 49 | None => None, // No IfLet, supported. |
50 | Some(TupleStructPat(pat)) if pat.args().count() == 1 => { | 50 | Some(ast::Pat::TupleStructPat(pat)) if pat.args().count() == 1 => { |
51 | let path = pat.path()?; | 51 | let path = pat.path()?; |
52 | match path.qualifier() { | 52 | match path.qualifier() { |
53 | None => { | 53 | None => { |
@@ -61,9 +61,9 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option<Assist> { | |||
61 | }; | 61 | }; |
62 | 62 | ||
63 | let cond_expr = cond.expr()?; | 63 | let cond_expr = cond.expr()?; |
64 | let then_block = if_expr.then_branch()?.block()?; | 64 | let then_block = if_expr.then_branch()?; |
65 | 65 | ||
66 | let parent_block = if_expr.syntax().parent()?.ancestors().find_map(ast::Block::cast)?; | 66 | let parent_block = if_expr.syntax().parent()?.ancestors().find_map(ast::BlockExpr::cast)?; |
67 | 67 | ||
68 | if parent_block.expr()? != if_expr.clone().into() { | 68 | if parent_block.expr()? != if_expr.clone().into() { |
69 | return None; | 69 | return None; |
@@ -80,7 +80,7 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option<Assist> { | |||
80 | return None; | 80 | return None; |
81 | } | 81 | } |
82 | 82 | ||
83 | let parent_container = parent_block.syntax().parent()?.parent()?; | 83 | let parent_container = parent_block.syntax().parent()?; |
84 | 84 | ||
85 | let early_expression: ast::Expr = match parent_container.kind() { | 85 | let early_expression: ast::Expr = match parent_container.kind() { |
86 | WHILE_EXPR | LOOP_EXPR => make::expr_continue(), | 86 | WHILE_EXPR | LOOP_EXPR => make::expr_continue(), |
@@ -144,13 +144,13 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option<Assist> { | |||
144 | } | 144 | } |
145 | }; | 145 | }; |
146 | edit.target(if_expr.syntax().text_range()); | 146 | edit.target(if_expr.syntax().text_range()); |
147 | edit.replace_ast(parent_block, ast::Block::cast(new_block).unwrap()); | 147 | edit.replace_ast(parent_block, ast::BlockExpr::cast(new_block).unwrap()); |
148 | edit.set_cursor(cursor_position); | 148 | edit.set_cursor(cursor_position); |
149 | 149 | ||
150 | fn replace( | 150 | fn replace( |
151 | new_expr: &SyntaxNode, | 151 | new_expr: &SyntaxNode, |
152 | then_block: &Block, | 152 | then_block: &ast::BlockExpr, |
153 | parent_block: &Block, | 153 | parent_block: &ast::BlockExpr, |
154 | if_expr: &ast::IfExpr, | 154 | if_expr: &ast::IfExpr, |
155 | ) -> SyntaxNode { | 155 | ) -> SyntaxNode { |
156 | let then_block_items = IndentLevel::from(1).decrease_indent(then_block.clone()); | 156 | let then_block_items = IndentLevel::from(1).decrease_indent(then_block.clone()); |
diff --git a/crates/ra_assists/src/handlers/inline_local_variable.rs b/crates/ra_assists/src/handlers/inline_local_variable.rs index f5702f6e0..60ec536a7 100644 --- a/crates/ra_assists/src/handlers/inline_local_variable.rs +++ b/crates/ra_assists/src/handlers/inline_local_variable.rs | |||
@@ -89,6 +89,7 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option<Assist> { | |||
89 | | (ast::Expr::ParenExpr(_), _) | 89 | | (ast::Expr::ParenExpr(_), _) |
90 | | (ast::Expr::PathExpr(_), _) | 90 | | (ast::Expr::PathExpr(_), _) |
91 | | (ast::Expr::BlockExpr(_), _) | 91 | | (ast::Expr::BlockExpr(_), _) |
92 | | (ast::Expr::EffectExpr(_), _) | ||
92 | | (_, ast::Expr::CallExpr(_)) | 93 | | (_, ast::Expr::CallExpr(_)) |
93 | | (_, ast::Expr::TupleExpr(_)) | 94 | | (_, ast::Expr::TupleExpr(_)) |
94 | | (_, ast::Expr::ArrayExpr(_)) | 95 | | (_, ast::Expr::ArrayExpr(_)) |
diff --git a/crates/ra_assists/src/handlers/introduce_variable.rs b/crates/ra_assists/src/handlers/introduce_variable.rs index eda9ac296..39c656305 100644 --- a/crates/ra_assists/src/handlers/introduce_variable.rs +++ b/crates/ra_assists/src/handlers/introduce_variable.rs | |||
@@ -111,7 +111,7 @@ fn valid_target_expr(node: SyntaxNode) -> Option<ast::Expr> { | |||
111 | /// expression like a lambda or match arm. | 111 | /// expression like a lambda or match arm. |
112 | fn anchor_stmt(expr: ast::Expr) -> Option<(SyntaxNode, bool)> { | 112 | fn anchor_stmt(expr: ast::Expr) -> Option<(SyntaxNode, bool)> { |
113 | expr.syntax().ancestors().find_map(|node| { | 113 | expr.syntax().ancestors().find_map(|node| { |
114 | if let Some(expr) = node.parent().and_then(ast::Block::cast).and_then(|it| it.expr()) { | 114 | if let Some(expr) = node.parent().and_then(ast::BlockExpr::cast).and_then(|it| it.expr()) { |
115 | if expr.syntax() == &node { | 115 | if expr.syntax() == &node { |
116 | tested_by!(test_introduce_var_last_expr); | 116 | tested_by!(test_introduce_var_last_expr); |
117 | return Some((node, false)); | 117 | return Some((node, false)); |
diff --git a/crates/ra_assists/src/handlers/move_guard.rs b/crates/ra_assists/src/handlers/move_guard.rs index d5ccdd91c..b084dd9ee 100644 --- a/crates/ra_assists/src/handlers/move_guard.rs +++ b/crates/ra_assists/src/handlers/move_guard.rs | |||
@@ -113,9 +113,9 @@ pub(crate) fn move_arm_cond_to_match_guard(ctx: AssistCtx) -> Option<Assist> { | |||
113 | "Move condition to match guard", | 113 | "Move condition to match guard", |
114 | |edit| { | 114 | |edit| { |
115 | edit.target(if_expr.syntax().text_range()); | 115 | edit.target(if_expr.syntax().text_range()); |
116 | let then_only_expr = then_block.block().and_then(|it| it.statements().next()).is_none(); | 116 | let then_only_expr = then_block.statements().next().is_none(); |
117 | 117 | ||
118 | match &then_block.block().and_then(|it| it.expr()) { | 118 | match &then_block.expr() { |
119 | Some(then_expr) if then_only_expr => { | 119 | Some(then_expr) if then_only_expr => { |
120 | edit.replace(if_expr.syntax().text_range(), then_expr.syntax().text()) | 120 | edit.replace(if_expr.syntax().text_range(), then_expr.syntax().text()) |
121 | } | 121 | } |
diff --git a/crates/ra_assists/src/handlers/replace_if_let_with_match.rs b/crates/ra_assists/src/handlers/replace_if_let_with_match.rs index 0a0a88f3d..9841f6980 100644 --- a/crates/ra_assists/src/handlers/replace_if_let_with_match.rs +++ b/crates/ra_assists/src/handlers/replace_if_let_with_match.rs | |||
@@ -1,11 +1,10 @@ | |||
1 | use ra_fmt::unwrap_trivial_block; | 1 | use ra_fmt::unwrap_trivial_block; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | ast::{self, make}, | 3 | ast::{self, edit::IndentLevel, make}, |
4 | AstNode, | 4 | AstNode, |
5 | }; | 5 | }; |
6 | 6 | ||
7 | use crate::{Assist, AssistCtx, AssistId}; | 7 | use crate::{utils::TryEnum, Assist, AssistCtx, AssistId}; |
8 | use ast::edit::IndentLevel; | ||
9 | 8 | ||
10 | // Assist: replace_if_let_with_match | 9 | // Assist: replace_if_let_with_match |
11 | // | 10 | // |
@@ -44,15 +43,21 @@ pub(crate) fn replace_if_let_with_match(ctx: AssistCtx) -> Option<Assist> { | |||
44 | ast::ElseBranch::IfExpr(_) => return None, | 43 | ast::ElseBranch::IfExpr(_) => return None, |
45 | }; | 44 | }; |
46 | 45 | ||
47 | ctx.add_assist(AssistId("replace_if_let_with_match"), "Replace with match", |edit| { | 46 | let sema = ctx.sema; |
47 | ctx.add_assist(AssistId("replace_if_let_with_match"), "Replace with match", move |edit| { | ||
48 | let match_expr = { | 48 | let match_expr = { |
49 | let then_arm = { | 49 | let then_arm = { |
50 | let then_expr = unwrap_trivial_block(then_block); | 50 | let then_expr = unwrap_trivial_block(then_block); |
51 | make::match_arm(vec![pat], then_expr) | 51 | make::match_arm(vec![pat.clone()], then_expr) |
52 | }; | 52 | }; |
53 | let else_arm = { | 53 | let else_arm = { |
54 | let pattern = sema | ||
55 | .type_of_pat(&pat) | ||
56 | .and_then(|ty| TryEnum::from_ty(sema, &ty)) | ||
57 | .map(|it| it.sad_pattern()) | ||
58 | .unwrap_or_else(|| make::placeholder_pat().into()); | ||
54 | let else_expr = unwrap_trivial_block(else_block); | 59 | let else_expr = unwrap_trivial_block(else_block); |
55 | make::match_arm(vec![make::placeholder_pat().into()], else_expr) | 60 | make::match_arm(vec![pattern], else_expr) |
56 | }; | 61 | }; |
57 | make::expr_match(expr, make::match_arm_list(vec![then_arm, else_arm])) | 62 | make::expr_match(expr, make::match_arm_list(vec![then_arm, else_arm])) |
58 | }; | 63 | }; |
@@ -68,6 +73,7 @@ pub(crate) fn replace_if_let_with_match(ctx: AssistCtx) -> Option<Assist> { | |||
68 | #[cfg(test)] | 73 | #[cfg(test)] |
69 | mod tests { | 74 | mod tests { |
70 | use super::*; | 75 | use super::*; |
76 | |||
71 | use crate::helpers::{check_assist, check_assist_target}; | 77 | use crate::helpers::{check_assist, check_assist_target}; |
72 | 78 | ||
73 | #[test] | 79 | #[test] |
@@ -145,4 +151,64 @@ impl VariantData { | |||
145 | }", | 151 | }", |
146 | ); | 152 | ); |
147 | } | 153 | } |
154 | |||
155 | #[test] | ||
156 | fn special_case_option() { | ||
157 | check_assist( | ||
158 | replace_if_let_with_match, | ||
159 | r#" | ||
160 | enum Option<T> { Some(T), None } | ||
161 | use Option::*; | ||
162 | |||
163 | fn foo(x: Option<i32>) { | ||
164 | <|>if let Some(x) = x { | ||
165 | println!("{}", x) | ||
166 | } else { | ||
167 | println!("none") | ||
168 | } | ||
169 | } | ||
170 | "#, | ||
171 | r#" | ||
172 | enum Option<T> { Some(T), None } | ||
173 | use Option::*; | ||
174 | |||
175 | fn foo(x: Option<i32>) { | ||
176 | <|>match x { | ||
177 | Some(x) => println!("{}", x), | ||
178 | None => println!("none"), | ||
179 | } | ||
180 | } | ||
181 | "#, | ||
182 | ); | ||
183 | } | ||
184 | |||
185 | #[test] | ||
186 | fn special_case_result() { | ||
187 | check_assist( | ||
188 | replace_if_let_with_match, | ||
189 | r#" | ||
190 | enum Result<T, E> { Ok(T), Err(E) } | ||
191 | use Result::*; | ||
192 | |||
193 | fn foo(x: Result<i32, ()>) { | ||
194 | <|>if let Ok(x) = x { | ||
195 | println!("{}", x) | ||
196 | } else { | ||
197 | println!("none") | ||
198 | } | ||
199 | } | ||
200 | "#, | ||
201 | r#" | ||
202 | enum Result<T, E> { Ok(T), Err(E) } | ||
203 | use Result::*; | ||
204 | |||
205 | fn foo(x: Result<i32, ()>) { | ||
206 | <|>match x { | ||
207 | Ok(x) => println!("{}", x), | ||
208 | Err(_) => println!("none"), | ||
209 | } | ||
210 | } | ||
211 | "#, | ||
212 | ); | ||
213 | } | ||
148 | } | 214 | } |
diff --git a/crates/ra_assists/src/handlers/replace_let_with_if_let.rs b/crates/ra_assists/src/handlers/replace_let_with_if_let.rs index bdbaae389..0cf23b754 100644 --- a/crates/ra_assists/src/handlers/replace_let_with_if_let.rs +++ b/crates/ra_assists/src/handlers/replace_let_with_if_let.rs | |||
@@ -1,6 +1,5 @@ | |||
1 | use std::iter::once; | 1 | use std::iter::once; |
2 | 2 | ||
3 | use hir::Adt; | ||
4 | use ra_syntax::{ | 3 | use ra_syntax::{ |
5 | ast::{ | 4 | ast::{ |
6 | self, | 5 | self, |
@@ -12,6 +11,7 @@ use ra_syntax::{ | |||
12 | 11 | ||
13 | use crate::{ | 12 | use crate::{ |
14 | assist_ctx::{Assist, AssistCtx}, | 13 | assist_ctx::{Assist, AssistCtx}, |
14 | utils::TryEnum, | ||
15 | AssistId, | 15 | AssistId, |
16 | }; | 16 | }; |
17 | 17 | ||
@@ -45,20 +45,10 @@ pub(crate) fn replace_let_with_if_let(ctx: AssistCtx) -> Option<Assist> { | |||
45 | let init = let_stmt.initializer()?; | 45 | let init = let_stmt.initializer()?; |
46 | let original_pat = let_stmt.pat()?; | 46 | let original_pat = let_stmt.pat()?; |
47 | let ty = ctx.sema.type_of_expr(&init)?; | 47 | let ty = ctx.sema.type_of_expr(&init)?; |
48 | let enum_ = match ty.as_adt() { | 48 | let happy_variant = TryEnum::from_ty(ctx.sema, &ty).map(|it| it.happy_case()); |
49 | Some(Adt::Enum(it)) => it, | ||
50 | _ => return None, | ||
51 | }; | ||
52 | let happy_case = | ||
53 | [("Result", "Ok"), ("Option", "Some")].iter().find_map(|(known_type, happy_case)| { | ||
54 | if &enum_.name(ctx.db).to_string() == known_type { | ||
55 | return Some(happy_case); | ||
56 | } | ||
57 | None | ||
58 | }); | ||
59 | 49 | ||
60 | ctx.add_assist(AssistId("replace_let_with_if_let"), "Replace with if-let", |edit| { | 50 | ctx.add_assist(AssistId("replace_let_with_if_let"), "Replace with if-let", |edit| { |
61 | let with_placeholder: ast::Pat = match happy_case { | 51 | let with_placeholder: ast::Pat = match happy_variant { |
62 | None => make::placeholder_pat().into(), | 52 | None => make::placeholder_pat().into(), |
63 | Some(var_name) => make::tuple_struct_pat( | 53 | Some(var_name) => make::tuple_struct_pat( |
64 | make::path_unqualified(make::path_segment(make::name_ref(var_name))), | 54 | make::path_unqualified(make::path_segment(make::name_ref(var_name))), |
diff --git a/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs b/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs index 2f02df303..ff2463c77 100644 --- a/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs | |||
@@ -27,7 +27,7 @@ pub(crate) fn replace_qualified_name_with_use(ctx: AssistCtx) -> Option<Assist> | |||
27 | return None; | 27 | return None; |
28 | } | 28 | } |
29 | 29 | ||
30 | let hir_path = hir::Path::from_ast(path.clone())?; | 30 | let hir_path = ctx.sema.lower_path(&path)?; |
31 | let segments = collect_hir_path_segments(&hir_path)?; | 31 | let segments = collect_hir_path_segments(&hir_path)?; |
32 | if segments.len() < 2 { | 32 | if segments.len() < 2 { |
33 | return None; | 33 | return None; |
@@ -38,7 +38,7 @@ pub(crate) fn replace_qualified_name_with_use(ctx: AssistCtx) -> Option<Assist> | |||
38 | "Replace qualified path with use", | 38 | "Replace qualified path with use", |
39 | |edit| { | 39 | |edit| { |
40 | let path_to_import = hir_path.mod_path().clone(); | 40 | let path_to_import = hir_path.mod_path().clone(); |
41 | insert_use_statement(path.syntax(), &path_to_import, edit.text_edit_builder()); | 41 | insert_use_statement(path.syntax(), &path_to_import, edit); |
42 | 42 | ||
43 | if let Some(last) = path.segment() { | 43 | if let Some(last) = path.segment() { |
44 | // Here we are assuming the assist will provide a correct use statement | 44 | // Here we are assuming the assist will provide a correct use statement |
diff --git a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs index 62cb7a763..62d4ea522 100644 --- a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs +++ b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs | |||
@@ -1,12 +1,11 @@ | |||
1 | use std::iter; | 1 | use std::iter; |
2 | 2 | ||
3 | use ra_syntax::{ | 3 | use ra_syntax::{ |
4 | ast::{self, make}, | 4 | ast::{self, edit::IndentLevel, make}, |
5 | AstNode, | 5 | AstNode, |
6 | }; | 6 | }; |
7 | 7 | ||
8 | use crate::{Assist, AssistCtx, AssistId}; | 8 | use crate::{utils::TryEnum, Assist, AssistCtx, AssistId}; |
9 | use ast::edit::IndentLevel; | ||
10 | 9 | ||
11 | // Assist: replace_unwrap_with_match | 10 | // Assist: replace_unwrap_with_match |
12 | // | 11 | // |
@@ -38,42 +37,27 @@ pub(crate) fn replace_unwrap_with_match(ctx: AssistCtx) -> Option<Assist> { | |||
38 | } | 37 | } |
39 | let caller = method_call.expr()?; | 38 | let caller = method_call.expr()?; |
40 | let ty = ctx.sema.type_of_expr(&caller)?; | 39 | let ty = ctx.sema.type_of_expr(&caller)?; |
40 | let happy_variant = TryEnum::from_ty(ctx.sema, &ty)?.happy_case(); | ||
41 | 41 | ||
42 | let type_name = ty.as_adt()?.name(ctx.sema.db).to_string(); | 42 | ctx.add_assist(AssistId("replace_unwrap_with_match"), "Replace unwrap with match", |edit| { |
43 | let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant))); | ||
44 | let it = make::bind_pat(make::name("a")).into(); | ||
45 | let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into(); | ||
43 | 46 | ||
44 | for (unwrap_type, variant_name) in [("Result", "Ok"), ("Option", "Some")].iter() { | 47 | let bind_path = make::path_unqualified(make::path_segment(make::name_ref("a"))); |
45 | if &type_name == unwrap_type { | 48 | let ok_arm = make::match_arm(iter::once(ok_tuple), make::expr_path(bind_path)); |
46 | return ctx.add_assist( | ||
47 | AssistId("replace_unwrap_with_match"), | ||
48 | "Replace unwrap with match", | ||
49 | |edit| { | ||
50 | let ok_path = | ||
51 | make::path_unqualified(make::path_segment(make::name_ref(variant_name))); | ||
52 | let it = make::bind_pat(make::name("a")).into(); | ||
53 | let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into(); | ||
54 | 49 | ||
55 | let bind_path = make::path_unqualified(make::path_segment(make::name_ref("a"))); | 50 | let unreachable_call = make::unreachable_macro_call().into(); |
56 | let ok_arm = make::match_arm(iter::once(ok_tuple), make::expr_path(bind_path)); | 51 | let err_arm = make::match_arm(iter::once(make::placeholder_pat().into()), unreachable_call); |
57 | 52 | ||
58 | let unreachable_call = make::unreachable_macro_call().into(); | 53 | let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]); |
59 | let err_arm = make::match_arm( | 54 | let match_expr = make::expr_match(caller.clone(), match_arm_list); |
60 | iter::once(make::placeholder_pat().into()), | 55 | let match_expr = IndentLevel::from_node(method_call.syntax()).increase_indent(match_expr); |
61 | unreachable_call, | ||
62 | ); | ||
63 | 56 | ||
64 | let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]); | 57 | edit.target(method_call.syntax().text_range()); |
65 | let match_expr = make::expr_match(caller.clone(), match_arm_list); | 58 | edit.set_cursor(caller.syntax().text_range().start()); |
66 | let match_expr = | 59 | edit.replace_ast::<ast::Expr>(method_call.into(), match_expr); |
67 | IndentLevel::from_node(method_call.syntax()).increase_indent(match_expr); | 60 | }) |
68 | |||
69 | edit.target(method_call.syntax().text_range()); | ||
70 | edit.set_cursor(caller.syntax().text_range().start()); | ||
71 | edit.replace_ast::<ast::Expr>(method_call.into(), match_expr); | ||
72 | }, | ||
73 | ); | ||
74 | } | ||
75 | } | ||
76 | None | ||
77 | } | 61 | } |
78 | 62 | ||
79 | #[cfg(test)] | 63 | #[cfg(test)] |
diff --git a/crates/ra_assists/src/handlers/unwrap_block.rs b/crates/ra_assists/src/handlers/unwrap_block.rs new file mode 100644 index 000000000..859c70ad8 --- /dev/null +++ b/crates/ra_assists/src/handlers/unwrap_block.rs | |||
@@ -0,0 +1,349 @@ | |||
1 | use crate::{Assist, AssistCtx, AssistId}; | ||
2 | |||
3 | use ast::LoopBodyOwner; | ||
4 | use ra_fmt::unwrap_trivial_block; | ||
5 | use ra_syntax::{ast, match_ast, AstNode, TextRange, T}; | ||
6 | |||
7 | // Assist: unwrap_block | ||
8 | // | ||
9 | // This assist removes if...else, for, while and loop control statements to just keep the body. | ||
10 | // | ||
11 | // ``` | ||
12 | // fn foo() { | ||
13 | // if true {<|> | ||
14 | // println!("foo"); | ||
15 | // } | ||
16 | // } | ||
17 | // ``` | ||
18 | // -> | ||
19 | // ``` | ||
20 | // fn foo() { | ||
21 | // println!("foo"); | ||
22 | // } | ||
23 | // ``` | ||
24 | pub(crate) fn unwrap_block(ctx: AssistCtx) -> Option<Assist> { | ||
25 | let l_curly_token = ctx.find_token_at_offset(T!['{'])?; | ||
26 | let block = ast::BlockExpr::cast(l_curly_token.parent())?; | ||
27 | let parent = block.syntax().parent()?; | ||
28 | let (expr, expr_to_unwrap) = match_ast! { | ||
29 | match parent { | ||
30 | ast::IfExpr(if_expr) => { | ||
31 | let expr_to_unwrap = if_expr.blocks().find_map(|expr| extract_expr(ctx.frange.range, expr)); | ||
32 | let expr_to_unwrap = expr_to_unwrap?; | ||
33 | // Find if we are in a else if block | ||
34 | let ancestor = if_expr.syntax().parent().and_then(ast::IfExpr::cast); | ||
35 | |||
36 | match ancestor { | ||
37 | None => (ast::Expr::IfExpr(if_expr), expr_to_unwrap), | ||
38 | Some(ancestor) => (ast::Expr::IfExpr(ancestor), expr_to_unwrap), | ||
39 | } | ||
40 | }, | ||
41 | ast::ForExpr(for_expr) => { | ||
42 | let block_expr = for_expr.loop_body()?; | ||
43 | let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?; | ||
44 | (ast::Expr::ForExpr(for_expr), expr_to_unwrap) | ||
45 | }, | ||
46 | ast::WhileExpr(while_expr) => { | ||
47 | let block_expr = while_expr.loop_body()?; | ||
48 | let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?; | ||
49 | (ast::Expr::WhileExpr(while_expr), expr_to_unwrap) | ||
50 | }, | ||
51 | ast::LoopExpr(loop_expr) => { | ||
52 | let block_expr = loop_expr.loop_body()?; | ||
53 | let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?; | ||
54 | (ast::Expr::LoopExpr(loop_expr), expr_to_unwrap) | ||
55 | }, | ||
56 | _ => return None, | ||
57 | } | ||
58 | }; | ||
59 | |||
60 | ctx.add_assist(AssistId("unwrap_block"), "Unwrap block", |edit| { | ||
61 | edit.set_cursor(expr.syntax().text_range().start()); | ||
62 | edit.target(expr_to_unwrap.syntax().text_range()); | ||
63 | |||
64 | let pat_start: &[_] = &[' ', '{', '\n']; | ||
65 | let expr_to_unwrap = expr_to_unwrap.to_string(); | ||
66 | let expr_string = expr_to_unwrap.trim_start_matches(pat_start); | ||
67 | let mut expr_string_lines: Vec<&str> = expr_string.lines().collect(); | ||
68 | expr_string_lines.pop(); // Delete last line | ||
69 | |||
70 | let expr_string = expr_string_lines | ||
71 | .into_iter() | ||
72 | .map(|line| line.replacen(" ", "", 1)) // Delete indentation | ||
73 | .collect::<Vec<String>>() | ||
74 | .join("\n"); | ||
75 | |||
76 | edit.replace(expr.syntax().text_range(), expr_string); | ||
77 | }) | ||
78 | } | ||
79 | |||
80 | fn extract_expr(cursor_range: TextRange, block: ast::BlockExpr) -> Option<ast::Expr> { | ||
81 | let cursor_in_range = block.l_curly_token()?.text_range().contains_range(cursor_range); | ||
82 | |||
83 | if cursor_in_range { | ||
84 | Some(unwrap_trivial_block(block)) | ||
85 | } else { | ||
86 | None | ||
87 | } | ||
88 | } | ||
89 | |||
90 | #[cfg(test)] | ||
91 | mod tests { | ||
92 | use crate::helpers::{check_assist, check_assist_not_applicable}; | ||
93 | |||
94 | use super::*; | ||
95 | |||
96 | #[test] | ||
97 | fn simple_if() { | ||
98 | check_assist( | ||
99 | unwrap_block, | ||
100 | r#" | ||
101 | fn main() { | ||
102 | bar(); | ||
103 | if true {<|> | ||
104 | foo(); | ||
105 | |||
106 | //comment | ||
107 | bar(); | ||
108 | } else { | ||
109 | println!("bar"); | ||
110 | } | ||
111 | } | ||
112 | "#, | ||
113 | r#" | ||
114 | fn main() { | ||
115 | bar(); | ||
116 | <|>foo(); | ||
117 | |||
118 | //comment | ||
119 | bar(); | ||
120 | } | ||
121 | "#, | ||
122 | ); | ||
123 | } | ||
124 | |||
125 | #[test] | ||
126 | fn simple_if_else() { | ||
127 | check_assist( | ||
128 | unwrap_block, | ||
129 | r#" | ||
130 | fn main() { | ||
131 | bar(); | ||
132 | if true { | ||
133 | foo(); | ||
134 | |||
135 | //comment | ||
136 | bar(); | ||
137 | } else {<|> | ||
138 | println!("bar"); | ||
139 | } | ||
140 | } | ||
141 | "#, | ||
142 | r#" | ||
143 | fn main() { | ||
144 | bar(); | ||
145 | <|>println!("bar"); | ||
146 | } | ||
147 | "#, | ||
148 | ); | ||
149 | } | ||
150 | |||
151 | #[test] | ||
152 | fn simple_if_else_if() { | ||
153 | check_assist( | ||
154 | unwrap_block, | ||
155 | r#" | ||
156 | fn main() { | ||
157 | //bar(); | ||
158 | if true { | ||
159 | println!("true"); | ||
160 | |||
161 | //comment | ||
162 | //bar(); | ||
163 | } else if false {<|> | ||
164 | println!("bar"); | ||
165 | } else { | ||
166 | println!("foo"); | ||
167 | } | ||
168 | } | ||
169 | "#, | ||
170 | r#" | ||
171 | fn main() { | ||
172 | //bar(); | ||
173 | <|>println!("bar"); | ||
174 | } | ||
175 | "#, | ||
176 | ); | ||
177 | } | ||
178 | |||
179 | #[test] | ||
180 | fn simple_if_bad_cursor_position() { | ||
181 | check_assist_not_applicable( | ||
182 | unwrap_block, | ||
183 | r#" | ||
184 | fn main() { | ||
185 | bar();<|> | ||
186 | if true { | ||
187 | foo(); | ||
188 | |||
189 | //comment | ||
190 | bar(); | ||
191 | } else { | ||
192 | println!("bar"); | ||
193 | } | ||
194 | } | ||
195 | "#, | ||
196 | ); | ||
197 | } | ||
198 | |||
199 | #[test] | ||
200 | fn simple_for() { | ||
201 | check_assist( | ||
202 | unwrap_block, | ||
203 | r#" | ||
204 | fn main() { | ||
205 | for i in 0..5 {<|> | ||
206 | if true { | ||
207 | foo(); | ||
208 | |||
209 | //comment | ||
210 | bar(); | ||
211 | } else { | ||
212 | println!("bar"); | ||
213 | } | ||
214 | } | ||
215 | } | ||
216 | "#, | ||
217 | r#" | ||
218 | fn main() { | ||
219 | <|>if true { | ||
220 | foo(); | ||
221 | |||
222 | //comment | ||
223 | bar(); | ||
224 | } else { | ||
225 | println!("bar"); | ||
226 | } | ||
227 | } | ||
228 | "#, | ||
229 | ); | ||
230 | } | ||
231 | |||
232 | #[test] | ||
233 | fn simple_if_in_for() { | ||
234 | check_assist( | ||
235 | unwrap_block, | ||
236 | r#" | ||
237 | fn main() { | ||
238 | for i in 0..5 { | ||
239 | if true {<|> | ||
240 | foo(); | ||
241 | |||
242 | //comment | ||
243 | bar(); | ||
244 | } else { | ||
245 | println!("bar"); | ||
246 | } | ||
247 | } | ||
248 | } | ||
249 | "#, | ||
250 | r#" | ||
251 | fn main() { | ||
252 | for i in 0..5 { | ||
253 | <|>foo(); | ||
254 | |||
255 | //comment | ||
256 | bar(); | ||
257 | } | ||
258 | } | ||
259 | "#, | ||
260 | ); | ||
261 | } | ||
262 | |||
263 | #[test] | ||
264 | fn simple_loop() { | ||
265 | check_assist( | ||
266 | unwrap_block, | ||
267 | r#" | ||
268 | fn main() { | ||
269 | loop {<|> | ||
270 | if true { | ||
271 | foo(); | ||
272 | |||
273 | //comment | ||
274 | bar(); | ||
275 | } else { | ||
276 | println!("bar"); | ||
277 | } | ||
278 | } | ||
279 | } | ||
280 | "#, | ||
281 | r#" | ||
282 | fn main() { | ||
283 | <|>if true { | ||
284 | foo(); | ||
285 | |||
286 | //comment | ||
287 | bar(); | ||
288 | } else { | ||
289 | println!("bar"); | ||
290 | } | ||
291 | } | ||
292 | "#, | ||
293 | ); | ||
294 | } | ||
295 | |||
296 | #[test] | ||
297 | fn simple_while() { | ||
298 | check_assist( | ||
299 | unwrap_block, | ||
300 | r#" | ||
301 | fn main() { | ||
302 | while true {<|> | ||
303 | if true { | ||
304 | foo(); | ||
305 | |||
306 | //comment | ||
307 | bar(); | ||
308 | } else { | ||
309 | println!("bar"); | ||
310 | } | ||
311 | } | ||
312 | } | ||
313 | "#, | ||
314 | r#" | ||
315 | fn main() { | ||
316 | <|>if true { | ||
317 | foo(); | ||
318 | |||
319 | //comment | ||
320 | bar(); | ||
321 | } else { | ||
322 | println!("bar"); | ||
323 | } | ||
324 | } | ||
325 | "#, | ||
326 | ); | ||
327 | } | ||
328 | |||
329 | #[test] | ||
330 | fn simple_if_in_while_bad_cursor_position() { | ||
331 | check_assist_not_applicable( | ||
332 | unwrap_block, | ||
333 | r#" | ||
334 | fn main() { | ||
335 | while true { | ||
336 | if true { | ||
337 | foo();<|> | ||
338 | |||
339 | //comment | ||
340 | bar(); | ||
341 | } else { | ||
342 | println!("bar"); | ||
343 | } | ||
344 | } | ||
345 | } | ||
346 | "#, | ||
347 | ); | ||
348 | } | ||
349 | } | ||
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 64bd87afb..c5df86600 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -143,6 +143,7 @@ mod handlers { | |||
143 | mod split_import; | 143 | mod split_import; |
144 | mod add_from_impl_for_enum; | 144 | mod add_from_impl_for_enum; |
145 | mod reorder_fields; | 145 | mod reorder_fields; |
146 | mod unwrap_block; | ||
146 | 147 | ||
147 | pub(crate) fn all() -> &'static [AssistHandler] { | 148 | pub(crate) fn all() -> &'static [AssistHandler] { |
148 | &[ | 149 | &[ |
@@ -181,6 +182,7 @@ mod handlers { | |||
181 | replace_unwrap_with_match::replace_unwrap_with_match, | 182 | replace_unwrap_with_match::replace_unwrap_with_match, |
182 | split_import::split_import, | 183 | split_import::split_import, |
183 | add_from_impl_for_enum::add_from_impl_for_enum, | 184 | add_from_impl_for_enum::add_from_impl_for_enum, |
185 | unwrap_block::unwrap_block, | ||
184 | // These are manually sorted for better priorities | 186 | // These are manually sorted for better priorities |
185 | add_missing_impl_members::add_missing_impl_members, | 187 | add_missing_impl_members::add_missing_impl_members, |
186 | add_missing_impl_members::add_missing_default_members, | 188 | add_missing_impl_members::add_missing_default_members, |
diff --git a/crates/ra_assists/src/marks.rs b/crates/ra_assists/src/marks.rs index 6c2a2b8b6..8d910205f 100644 --- a/crates/ra_assists/src/marks.rs +++ b/crates/ra_assists/src/marks.rs | |||
@@ -8,4 +8,5 @@ test_utils::marks![ | |||
8 | test_not_inline_mut_variable | 8 | test_not_inline_mut_variable |
9 | test_not_applicable_if_variable_unused | 9 | test_not_applicable_if_variable_unused |
10 | change_visibility_field_false_positive | 10 | change_visibility_field_false_positive |
11 | test_add_from_impl_already_exists | ||
11 | ]; | 12 | ]; |
diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index 3d6c59bda..2f15a3f15 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs | |||
@@ -1,7 +1,9 @@ | |||
1 | //! Assorted functions shared by several assists. | 1 | //! Assorted functions shared by several assists. |
2 | pub(crate) mod insert_use; | 2 | pub(crate) mod insert_use; |
3 | 3 | ||
4 | use hir::Semantics; | 4 | use std::iter; |
5 | |||
6 | use hir::{Adt, Crate, Semantics, Trait, Type}; | ||
5 | use ra_ide_db::RootDatabase; | 7 | use ra_ide_db::RootDatabase; |
6 | use ra_syntax::{ | 8 | use ra_syntax::{ |
7 | ast::{self, make, NameOwner}, | 9 | ast::{self, make, NameOwner}, |
@@ -9,9 +11,9 @@ use ra_syntax::{ | |||
9 | }; | 11 | }; |
10 | use rustc_hash::FxHashSet; | 12 | use rustc_hash::FxHashSet; |
11 | 13 | ||
12 | pub use insert_use::insert_use_statement; | 14 | pub(crate) use insert_use::insert_use_statement; |
13 | 15 | ||
14 | pub fn get_missing_impl_items( | 16 | pub fn get_missing_assoc_items( |
15 | sema: &Semantics<RootDatabase>, | 17 | sema: &Semantics<RootDatabase>, |
16 | impl_def: &ast::ImplDef, | 18 | impl_def: &ast::ImplDef, |
17 | ) -> Vec<hir::AssocItem> { | 19 | ) -> Vec<hir::AssocItem> { |
@@ -21,21 +23,21 @@ pub fn get_missing_impl_items( | |||
21 | let mut impl_type = FxHashSet::default(); | 23 | let mut impl_type = FxHashSet::default(); |
22 | 24 | ||
23 | if let Some(item_list) = impl_def.item_list() { | 25 | if let Some(item_list) = impl_def.item_list() { |
24 | for item in item_list.impl_items() { | 26 | for item in item_list.assoc_items() { |
25 | match item { | 27 | match item { |
26 | ast::ImplItem::FnDef(f) => { | 28 | ast::AssocItem::FnDef(f) => { |
27 | if let Some(n) = f.name() { | 29 | if let Some(n) = f.name() { |
28 | impl_fns_consts.insert(n.syntax().to_string()); | 30 | impl_fns_consts.insert(n.syntax().to_string()); |
29 | } | 31 | } |
30 | } | 32 | } |
31 | 33 | ||
32 | ast::ImplItem::TypeAliasDef(t) => { | 34 | ast::AssocItem::TypeAliasDef(t) => { |
33 | if let Some(n) = t.name() { | 35 | if let Some(n) = t.name() { |
34 | impl_type.insert(n.syntax().to_string()); | 36 | impl_type.insert(n.syntax().to_string()); |
35 | } | 37 | } |
36 | } | 38 | } |
37 | 39 | ||
38 | ast::ImplItem::ConstDef(c) => { | 40 | ast::AssocItem::ConstDef(c) => { |
39 | if let Some(n) = c.name() { | 41 | if let Some(n) = c.name() { |
40 | impl_fns_consts.insert(n.syntax().to_string()); | 42 | impl_fns_consts.insert(n.syntax().to_string()); |
41 | } | 43 | } |
@@ -99,3 +101,109 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> { | |||
99 | _ => None, | 101 | _ => None, |
100 | } | 102 | } |
101 | } | 103 | } |
104 | |||
105 | #[derive(Clone, Copy)] | ||
106 | pub(crate) enum TryEnum { | ||
107 | Result, | ||
108 | Option, | ||
109 | } | ||
110 | |||
111 | impl TryEnum { | ||
112 | const ALL: [TryEnum; 2] = [TryEnum::Option, TryEnum::Result]; | ||
113 | |||
114 | pub(crate) fn from_ty(sema: &Semantics<RootDatabase>, ty: &Type) -> Option<TryEnum> { | ||
115 | let enum_ = match ty.as_adt() { | ||
116 | Some(Adt::Enum(it)) => it, | ||
117 | _ => return None, | ||
118 | }; | ||
119 | TryEnum::ALL.iter().find_map(|&var| { | ||
120 | if &enum_.name(sema.db).to_string() == var.type_name() { | ||
121 | return Some(var); | ||
122 | } | ||
123 | None | ||
124 | }) | ||
125 | } | ||
126 | |||
127 | pub(crate) fn happy_case(self) -> &'static str { | ||
128 | match self { | ||
129 | TryEnum::Result => "Ok", | ||
130 | TryEnum::Option => "Some", | ||
131 | } | ||
132 | } | ||
133 | |||
134 | pub(crate) fn sad_pattern(self) -> ast::Pat { | ||
135 | match self { | ||
136 | TryEnum::Result => make::tuple_struct_pat( | ||
137 | make::path_unqualified(make::path_segment(make::name_ref("Err"))), | ||
138 | iter::once(make::placeholder_pat().into()), | ||
139 | ) | ||
140 | .into(), | ||
141 | TryEnum::Option => make::bind_pat(make::name("None")).into(), | ||
142 | } | ||
143 | } | ||
144 | |||
145 | fn type_name(self) -> &'static str { | ||
146 | match self { | ||
147 | TryEnum::Result => "Result", | ||
148 | TryEnum::Option => "Option", | ||
149 | } | ||
150 | } | ||
151 | } | ||
152 | |||
153 | /// Helps with finding well-know things inside the standard library. This is | ||
154 | /// somewhat similar to the known paths infra inside hir, but it different; We | ||
155 | /// want to make sure that IDE specific paths don't become interesting inside | ||
156 | /// the compiler itself as well. | ||
157 | pub(crate) struct FamousDefs<'a, 'b>(pub(crate) &'a Semantics<'b, RootDatabase>, pub(crate) Crate); | ||
158 | |||
159 | #[allow(non_snake_case)] | ||
160 | impl FamousDefs<'_, '_> { | ||
161 | #[cfg(test)] | ||
162 | pub(crate) const FIXTURE: &'static str = r#" | ||
163 | //- /libcore.rs crate:core | ||
164 | pub mod convert{ | ||
165 | pub trait From<T> { | ||
166 | fn from(T) -> Self; | ||
167 | } | ||
168 | } | ||
169 | |||
170 | pub mod prelude { pub use crate::convert::From } | ||
171 | #[prelude_import] | ||
172 | pub use prelude::*; | ||
173 | "#; | ||
174 | |||
175 | pub(crate) fn core_convert_From(&self) -> Option<Trait> { | ||
176 | self.find_trait("core:convert:From") | ||
177 | } | ||
178 | |||
179 | fn find_trait(&self, path: &str) -> Option<Trait> { | ||
180 | let db = self.0.db; | ||
181 | let mut path = path.split(':'); | ||
182 | let trait_ = path.next_back()?; | ||
183 | let std_crate = path.next()?; | ||
184 | let std_crate = self | ||
185 | .1 | ||
186 | .dependencies(db) | ||
187 | .into_iter() | ||
188 | .find(|dep| &dep.name.to_string() == std_crate)? | ||
189 | .krate; | ||
190 | |||
191 | let mut module = std_crate.root_module(db)?; | ||
192 | for segment in path { | ||
193 | module = module.children(db).find_map(|child| { | ||
194 | let name = child.name(db)?; | ||
195 | if &name.to_string() == segment { | ||
196 | Some(child) | ||
197 | } else { | ||
198 | None | ||
199 | } | ||
200 | })?; | ||
201 | } | ||
202 | let def = | ||
203 | module.scope(db, None).into_iter().find(|(name, _def)| &name.to_string() == trait_)?.1; | ||
204 | match def { | ||
205 | hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it), | ||
206 | _ => None, | ||
207 | } | ||
208 | } | ||
209 | } | ||
diff --git a/crates/ra_assists/src/utils/insert_use.rs b/crates/ra_assists/src/utils/insert_use.rs index c507e71e0..c1f447efe 100644 --- a/crates/ra_assists/src/utils/insert_use.rs +++ b/crates/ra_assists/src/utils/insert_use.rs | |||
@@ -2,6 +2,7 @@ | |||
2 | // FIXME: rewrite according to the plan, outlined in | 2 | // FIXME: rewrite according to the plan, outlined in |
3 | // https://github.com/rust-analyzer/rust-analyzer/issues/3301#issuecomment-592931553 | 3 | // https://github.com/rust-analyzer/rust-analyzer/issues/3301#issuecomment-592931553 |
4 | 4 | ||
5 | use crate::assist_ctx::ActionBuilder; | ||
5 | use hir::{self, ModPath}; | 6 | use hir::{self, ModPath}; |
6 | use ra_syntax::{ | 7 | use ra_syntax::{ |
7 | ast::{self, NameOwner}, | 8 | ast::{self, NameOwner}, |
@@ -14,14 +15,14 @@ use ra_text_edit::TextEditBuilder; | |||
14 | /// Creates and inserts a use statement for the given path to import. | 15 | /// Creates and inserts a use statement for the given path to import. |
15 | /// The use statement is inserted in the scope most appropriate to the | 16 | /// The use statement is inserted in the scope most appropriate to the |
16 | /// the cursor position given, additionally merged with the existing use imports. | 17 | /// the cursor position given, additionally merged with the existing use imports. |
17 | pub fn insert_use_statement( | 18 | pub(crate) fn insert_use_statement( |
18 | // Ideally the position of the cursor, used to | 19 | // Ideally the position of the cursor, used to |
19 | position: &SyntaxNode, | 20 | position: &SyntaxNode, |
20 | path_to_import: &ModPath, | 21 | path_to_import: &ModPath, |
21 | edit: &mut TextEditBuilder, | 22 | edit: &mut ActionBuilder, |
22 | ) { | 23 | ) { |
23 | let target = path_to_import.to_string().split("::").map(SmolStr::new).collect::<Vec<_>>(); | 24 | let target = path_to_import.to_string().split("::").map(SmolStr::new).collect::<Vec<_>>(); |
24 | let container = position.ancestors().find_map(|n| { | 25 | let container = edit.ctx().sema.ancestors_with_macros(position.clone()).find_map(|n| { |
25 | if let Some(module) = ast::Module::cast(n.clone()) { | 26 | if let Some(module) = ast::Module::cast(n.clone()) { |
26 | return module.item_list().map(|it| it.syntax().clone()); | 27 | return module.item_list().map(|it| it.syntax().clone()); |
27 | } | 28 | } |
@@ -30,7 +31,7 @@ pub fn insert_use_statement( | |||
30 | 31 | ||
31 | if let Some(container) = container { | 32 | if let Some(container) = container { |
32 | let action = best_action_for_target(container, position.clone(), &target); | 33 | let action = best_action_for_target(container, position.clone(), &target); |
33 | make_assist(&action, &target, edit); | 34 | make_assist(&action, &target, edit.text_edit_builder()); |
34 | } | 35 | } |
35 | } | 36 | } |
36 | 37 | ||