aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs
diff options
context:
space:
mode:
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.rs61
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 @@
1use hir; 1use hir;
2use ra_syntax::{ast, AstNode, SmolStr, TextRange}; 2use ra_syntax::{ast, AstNode, SmolStr, TextRange};
3 3
4use crate::{ 4use 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// ```
23pub(crate) fn replace_qualified_name_with_use(ctx: AssistCtx) -> Option<Assist> { 19pub(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)]
76mod tests { 77mod 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 "
89use std::fmt::Debug; 90use std::fmt::Debug;
90 91
91Debug<|> 92Debug
92 ", 93 ",
93 ); 94 );
94 } 95 }
@@ -105,7 +106,7 @@ fn main() {
105 " 106 "
106use std::fmt::Debug; 107use std::fmt::Debug;
107 108
108Debug<|> 109Debug
109 110
110fn main() { 111fn main() {
111} 112}
@@ -129,7 +130,7 @@ use std::fmt::Debug;
129fn main() { 130fn main() {
130} 131}
131 132
132Debug<|> 133Debug
133 ", 134 ",
134 ); 135 );
135 } 136 }
@@ -144,7 +145,7 @@ std::fmt<|>::Debug
144 " 145 "
145use std::fmt; 146use std::fmt;
146 147
147fmt<|>::Debug 148fmt::Debug
148 ", 149 ",
149 ); 150 );
150 } 151 }
@@ -163,7 +164,7 @@ impl std::fmt::Debug<|> for Foo {
163use stdx; 164use stdx;
164use std::fmt::Debug; 165use std::fmt::Debug;
165 166
166impl Debug<|> for Foo { 167impl Debug for Foo {
167} 168}
168 ", 169 ",
169 ); 170 );
@@ -180,7 +181,7 @@ impl std::fmt::Debug<|> for Foo {
180 " 181 "
181use std::fmt::Debug; 182use std::fmt::Debug;
182 183
183impl Debug<|> for Foo { 184impl 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 "
217use std::{io, fmt}; 218use std::{io, fmt};
218 219
219impl io<|> for Foo { 220impl io for Foo {
220} 221}
221 ", 222 ",
222 ); 223 );
@@ -235,7 +236,7 @@ impl std::fmt::Debug<|> for Foo {
235 " 236 "
236use std::fmt::{self, Debug, }; 237use std::fmt::{self, Debug, };
237 238
238impl Debug<|> for Foo { 239impl Debug for Foo {
239} 240}
240 ", 241 ",
241 ); 242 );
@@ -254,7 +255,7 @@ impl std::fmt<|> for Foo {
254 " 255 "
255use std::fmt::{self, Debug}; 256use std::fmt::{self, Debug};
256 257
257impl fmt<|> for Foo { 258impl fmt for Foo {
258} 259}
259 ", 260 ",
260 ); 261 );
@@ -273,7 +274,7 @@ impl std::fmt::nested<|> for Foo {
273 " 274 "
274use std::fmt::{Debug, nested::{Display, self}}; 275use std::fmt::{Debug, nested::{Display, self}};
275 276
276impl nested<|> for Foo { 277impl nested for Foo {
277} 278}
278", 279",
279 ); 280 );
@@ -292,7 +293,7 @@ impl std::fmt::nested<|> for Foo {
292 " 293 "
293use std::fmt::{Debug, nested::{self, Display}}; 294use std::fmt::{Debug, nested::{self, Display}};
294 295
295impl nested<|> for Foo { 296impl nested for Foo {
296} 297}
297", 298",
298 ); 299 );
@@ -311,7 +312,7 @@ impl std::fmt::nested::Debug<|> for Foo {
311 " 312 "
312use std::fmt::{Debug, nested::{Display, Debug}}; 313use std::fmt::{Debug, nested::{Display, Debug}};
313 314
314impl Debug<|> for Foo { 315impl Debug for Foo {
315} 316}
316", 317",
317 ); 318 );
@@ -330,7 +331,7 @@ impl std::fmt::nested::Display<|> for Foo {
330 " 331 "
331use std::fmt::{nested::Display, Debug}; 332use std::fmt::{nested::Display, Debug};
332 333
333impl Display<|> for Foo { 334impl Display for Foo {
334} 335}
335", 336",
336 ); 337 );
@@ -349,7 +350,7 @@ impl std::fmt::Display<|> for Foo {
349 " 350 "
350use std::fmt::{Display, nested::Debug}; 351use std::fmt::{Display, nested::Debug};
351 352
352impl Display<|> for Foo { 353impl Display for Foo {
353} 354}
354", 355",
355 ); 356 );
@@ -373,7 +374,7 @@ use crate::{
373 AssocItem, 374 AssocItem,
374}; 375};
375 376
376fn foo() { lower<|>::trait_env() } 377fn foo() { lower::trait_env() }
377", 378",
378 ); 379 );
379 } 380 }
@@ -391,7 +392,7 @@ impl foo::Debug<|> for Foo {
391 " 392 "
392use std::fmt as foo; 393use std::fmt as foo;
393 394
394impl Debug<|> for Foo { 395impl 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() {
457use std::fmt::Debug; 458use std::fmt::Debug;
458 459
459fn main() { 460fn main() {
460 Debug<|> 461 Debug
461} 462}
462 ", 463 ",
463 ); 464 );