diff options
Diffstat (limited to 'crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs')
-rw-r--r-- | crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs | 61 |
1 files changed, 31 insertions, 30 deletions
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 918e8dd8d..0197a8cf0 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 | |||
@@ -1,11 +1,7 @@ | |||
1 | use hir; | 1 | use hir; |
2 | use ra_syntax::{ast, AstNode, SmolStr, TextRange}; | 2 | use ra_syntax::{ast, AstNode, SmolStr, TextRange}; |
3 | 3 | ||
4 | use crate::{ | 4 | use crate::{utils::insert_use_statement, AssistContext, AssistId, Assists}; |
5 | assist_ctx::{Assist, AssistCtx}, | ||
6 | utils::insert_use_statement, | ||
7 | AssistId, | ||
8 | }; | ||
9 | 5 | ||
10 | // Assist: replace_qualified_name_with_use | 6 | // Assist: replace_qualified_name_with_use |
11 | // | 7 | // |
@@ -20,7 +16,10 @@ use crate::{ | |||
20 | // | 16 | // |
21 | // fn process(map: HashMap<String, String>) {} | 17 | // fn process(map: HashMap<String, String>) {} |
22 | // ``` | 18 | // ``` |
23 | pub(crate) fn replace_qualified_name_with_use(ctx: AssistCtx) -> Option<Assist> { | 19 | pub(crate) fn replace_qualified_name_with_use( |
20 | acc: &mut Assists, | ||
21 | ctx: &AssistContext, | ||
22 | ) -> Option<()> { | ||
24 | let path: ast::Path = ctx.find_node_at_offset()?; | 23 | let path: ast::Path = ctx.find_node_at_offset()?; |
25 | // We don't want to mess with use statements | 24 | // We don't want to mess with use statements |
26 | if path.syntax().ancestors().find_map(ast::UseItem::cast).is_some() { | 25 | if path.syntax().ancestors().find_map(ast::UseItem::cast).is_some() { |
@@ -33,17 +32,19 @@ pub(crate) fn replace_qualified_name_with_use(ctx: AssistCtx) -> Option<Assist> | |||
33 | return None; | 32 | return None; |
34 | } | 33 | } |
35 | 34 | ||
36 | ctx.add_assist( | 35 | let target = path.syntax().text_range(); |
36 | acc.add( | ||
37 | AssistId("replace_qualified_name_with_use"), | 37 | AssistId("replace_qualified_name_with_use"), |
38 | "Replace qualified path with use", | 38 | "Replace qualified path with use", |
39 | |edit| { | 39 | target, |
40 | |builder| { | ||
40 | let path_to_import = hir_path.mod_path().clone(); | 41 | let path_to_import = hir_path.mod_path().clone(); |
41 | insert_use_statement(path.syntax(), &path_to_import, edit.text_edit_builder()); | 42 | insert_use_statement(path.syntax(), &path_to_import, ctx, builder.text_edit_builder()); |
42 | 43 | ||
43 | if let Some(last) = path.segment() { | 44 | if let Some(last) = path.segment() { |
44 | // Here we are assuming the assist will provide a correct use statement | 45 | // Here we are assuming the assist will provide a correct use statement |
45 | // so we can delete the path qualifier | 46 | // so we can delete the path qualifier |
46 | edit.delete(TextRange::new( | 47 | builder.delete(TextRange::new( |
47 | path.syntax().text_range().start(), | 48 | path.syntax().text_range().start(), |
48 | last.syntax().text_range().start(), | 49 | last.syntax().text_range().start(), |
49 | )); | 50 | )); |
@@ -74,7 +75,7 @@ fn collect_hir_path_segments(path: &hir::Path) -> Option<Vec<SmolStr>> { | |||
74 | 75 | ||
75 | #[cfg(test)] | 76 | #[cfg(test)] |
76 | mod tests { | 77 | mod tests { |
77 | use crate::helpers::{check_assist, check_assist_not_applicable}; | 78 | use crate::tests::{check_assist, check_assist_not_applicable}; |
78 | 79 | ||
79 | use super::*; | 80 | use super::*; |
80 | 81 | ||
@@ -88,7 +89,7 @@ std::fmt::Debug<|> | |||
88 | " | 89 | " |
89 | use std::fmt::Debug; | 90 | use std::fmt::Debug; |
90 | 91 | ||
91 | Debug<|> | 92 | Debug |
92 | ", | 93 | ", |
93 | ); | 94 | ); |
94 | } | 95 | } |
@@ -105,7 +106,7 @@ fn main() { | |||
105 | " | 106 | " |
106 | use std::fmt::Debug; | 107 | use std::fmt::Debug; |
107 | 108 | ||
108 | Debug<|> | 109 | Debug |
109 | 110 | ||
110 | fn main() { | 111 | fn main() { |
111 | } | 112 | } |
@@ -129,7 +130,7 @@ use std::fmt::Debug; | |||
129 | fn main() { | 130 | fn main() { |
130 | } | 131 | } |
131 | 132 | ||
132 | Debug<|> | 133 | Debug |
133 | ", | 134 | ", |
134 | ); | 135 | ); |
135 | } | 136 | } |
@@ -144,7 +145,7 @@ std::fmt<|>::Debug | |||
144 | " | 145 | " |
145 | use std::fmt; | 146 | use std::fmt; |
146 | 147 | ||
147 | fmt<|>::Debug | 148 | fmt::Debug |
148 | ", | 149 | ", |
149 | ); | 150 | ); |
150 | } | 151 | } |
@@ -163,7 +164,7 @@ impl std::fmt::Debug<|> for Foo { | |||
163 | use stdx; | 164 | use stdx; |
164 | use std::fmt::Debug; | 165 | use std::fmt::Debug; |
165 | 166 | ||
166 | impl Debug<|> for Foo { | 167 | impl Debug for Foo { |
167 | } | 168 | } |
168 | ", | 169 | ", |
169 | ); | 170 | ); |
@@ -180,7 +181,7 @@ impl std::fmt::Debug<|> for Foo { | |||
180 | " | 181 | " |
181 | use std::fmt::Debug; | 182 | use std::fmt::Debug; |
182 | 183 | ||
183 | impl Debug<|> for Foo { | 184 | impl Debug for Foo { |
184 | } | 185 | } |
185 | ", | 186 | ", |
186 | ); | 187 | ); |
@@ -197,7 +198,7 @@ impl Debug<|> for Foo { | |||
197 | " | 198 | " |
198 | use std::fmt::Debug; | 199 | use std::fmt::Debug; |
199 | 200 | ||
200 | impl Debug<|> for Foo { | 201 | impl Debug for Foo { |
201 | } | 202 | } |
202 | ", | 203 | ", |
203 | ); | 204 | ); |
@@ -216,7 +217,7 @@ impl std::io<|> for Foo { | |||
216 | " | 217 | " |
217 | use std::{io, fmt}; | 218 | use std::{io, fmt}; |
218 | 219 | ||
219 | impl io<|> for Foo { | 220 | impl io for Foo { |
220 | } | 221 | } |
221 | ", | 222 | ", |
222 | ); | 223 | ); |
@@ -235,7 +236,7 @@ impl std::fmt::Debug<|> for Foo { | |||
235 | " | 236 | " |
236 | use std::fmt::{self, Debug, }; | 237 | use std::fmt::{self, Debug, }; |
237 | 238 | ||
238 | impl Debug<|> for Foo { | 239 | impl Debug for Foo { |
239 | } | 240 | } |
240 | ", | 241 | ", |
241 | ); | 242 | ); |
@@ -254,7 +255,7 @@ impl std::fmt<|> for Foo { | |||
254 | " | 255 | " |
255 | use std::fmt::{self, Debug}; | 256 | use std::fmt::{self, Debug}; |
256 | 257 | ||
257 | impl fmt<|> for Foo { | 258 | impl fmt for Foo { |
258 | } | 259 | } |
259 | ", | 260 | ", |
260 | ); | 261 | ); |
@@ -273,7 +274,7 @@ impl std::fmt::nested<|> for Foo { | |||
273 | " | 274 | " |
274 | use std::fmt::{Debug, nested::{Display, self}}; | 275 | use std::fmt::{Debug, nested::{Display, self}}; |
275 | 276 | ||
276 | impl nested<|> for Foo { | 277 | impl nested for Foo { |
277 | } | 278 | } |
278 | ", | 279 | ", |
279 | ); | 280 | ); |
@@ -292,7 +293,7 @@ impl std::fmt::nested<|> for Foo { | |||
292 | " | 293 | " |
293 | use std::fmt::{Debug, nested::{self, Display}}; | 294 | use std::fmt::{Debug, nested::{self, Display}}; |
294 | 295 | ||
295 | impl nested<|> for Foo { | 296 | impl nested for Foo { |
296 | } | 297 | } |
297 | ", | 298 | ", |
298 | ); | 299 | ); |
@@ -311,7 +312,7 @@ impl std::fmt::nested::Debug<|> for Foo { | |||
311 | " | 312 | " |
312 | use std::fmt::{Debug, nested::{Display, Debug}}; | 313 | use std::fmt::{Debug, nested::{Display, Debug}}; |
313 | 314 | ||
314 | impl Debug<|> for Foo { | 315 | impl Debug for Foo { |
315 | } | 316 | } |
316 | ", | 317 | ", |
317 | ); | 318 | ); |
@@ -330,7 +331,7 @@ impl std::fmt::nested::Display<|> for Foo { | |||
330 | " | 331 | " |
331 | use std::fmt::{nested::Display, Debug}; | 332 | use std::fmt::{nested::Display, Debug}; |
332 | 333 | ||
333 | impl Display<|> for Foo { | 334 | impl Display for Foo { |
334 | } | 335 | } |
335 | ", | 336 | ", |
336 | ); | 337 | ); |
@@ -349,7 +350,7 @@ impl std::fmt::Display<|> for Foo { | |||
349 | " | 350 | " |
350 | use std::fmt::{Display, nested::Debug}; | 351 | use std::fmt::{Display, nested::Debug}; |
351 | 352 | ||
352 | impl Display<|> for Foo { | 353 | impl Display for Foo { |
353 | } | 354 | } |
354 | ", | 355 | ", |
355 | ); | 356 | ); |
@@ -373,7 +374,7 @@ use crate::{ | |||
373 | AssocItem, | 374 | AssocItem, |
374 | }; | 375 | }; |
375 | 376 | ||
376 | fn foo() { lower<|>::trait_env() } | 377 | fn foo() { lower::trait_env() } |
377 | ", | 378 | ", |
378 | ); | 379 | ); |
379 | } | 380 | } |
@@ -391,7 +392,7 @@ impl foo::Debug<|> for Foo { | |||
391 | " | 392 | " |
392 | use std::fmt as foo; | 393 | use std::fmt as foo; |
393 | 394 | ||
394 | impl Debug<|> for Foo { | 395 | impl Debug for Foo { |
395 | } | 396 | } |
396 | ", | 397 | ", |
397 | ); | 398 | ); |
@@ -434,7 +435,7 @@ mod foo { | |||
434 | mod bar { | 435 | mod bar { |
435 | use std::fmt::Debug; | 436 | use std::fmt::Debug; |
436 | 437 | ||
437 | Debug<|> | 438 | Debug |
438 | } | 439 | } |
439 | } | 440 | } |
440 | ", | 441 | ", |
@@ -457,7 +458,7 @@ fn main() { | |||
457 | use std::fmt::Debug; | 458 | use std::fmt::Debug; |
458 | 459 | ||
459 | fn main() { | 460 | fn main() { |
460 | Debug<|> | 461 | Debug |
461 | } | 462 | } |
462 | ", | 463 | ", |
463 | ); | 464 | ); |