aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/assists/auto_import.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/assists/auto_import.rs')
-rw-r--r--crates/ra_assists/src/assists/auto_import.rs939
1 files changed, 939 insertions, 0 deletions
diff --git a/crates/ra_assists/src/assists/auto_import.rs b/crates/ra_assists/src/assists/auto_import.rs
new file mode 100644
index 000000000..5aae98546
--- /dev/null
+++ b/crates/ra_assists/src/assists/auto_import.rs
@@ -0,0 +1,939 @@
1use hir::{self, db::HirDatabase};
2use ra_text_edit::TextEditBuilder;
3
4use crate::{
5 assist_ctx::{Assist, AssistCtx},
6 AssistId,
7};
8use ra_syntax::{
9 ast::{self, NameOwner},
10 AstNode, Direction, SmolStr,
11 SyntaxKind::{PATH, PATH_SEGMENT},
12 SyntaxNode, TextRange, T,
13};
14
15fn collect_path_segments_raw(
16 segments: &mut Vec<ast::PathSegment>,
17 mut path: ast::Path,
18) -> Option<usize> {
19 let oldlen = segments.len();
20 loop {
21 let mut children = path.syntax().children_with_tokens();
22 let (first, second, third) = (
23 children.next().map(|n| (n.clone(), n.kind())),
24 children.next().map(|n| (n.clone(), n.kind())),
25 children.next().map(|n| (n.clone(), n.kind())),
26 );
27 match (first, second, third) {
28 (Some((subpath, PATH)), Some((_, T![::])), Some((segment, PATH_SEGMENT))) => {
29 path = ast::Path::cast(subpath.as_node()?.clone())?;
30 segments.push(ast::PathSegment::cast(segment.as_node()?.clone())?);
31 }
32 (Some((segment, PATH_SEGMENT)), _, _) => {
33 segments.push(ast::PathSegment::cast(segment.as_node()?.clone())?);
34 break;
35 }
36 (_, _, _) => return None,
37 }
38 }
39 // We need to reverse only the new added segments
40 let only_new_segments = segments.split_at_mut(oldlen).1;
41 only_new_segments.reverse();
42 Some(segments.len() - oldlen)
43}
44
45fn fmt_segments(segments: &[SmolStr]) -> String {
46 let mut buf = String::new();
47 fmt_segments_raw(segments, &mut buf);
48 buf
49}
50
51fn fmt_segments_raw(segments: &[SmolStr], buf: &mut String) {
52 let mut iter = segments.iter();
53 if let Some(s) = iter.next() {
54 buf.push_str(s);
55 }
56 for s in iter {
57 buf.push_str("::");
58 buf.push_str(s);
59 }
60}
61
62// Returns the numeber of common segments.
63fn compare_path_segments(left: &[SmolStr], right: &[ast::PathSegment]) -> usize {
64 left.iter().zip(right).filter(|(l, r)| compare_path_segment(l, r)).count()
65}
66
67fn compare_path_segment(a: &SmolStr, b: &ast::PathSegment) -> bool {
68 if let Some(kb) = b.kind() {
69 match kb {
70 ast::PathSegmentKind::Name(nameref_b) => a == nameref_b.text(),
71 ast::PathSegmentKind::SelfKw => a == "self",
72 ast::PathSegmentKind::SuperKw => a == "super",
73 ast::PathSegmentKind::CrateKw => a == "crate",
74 ast::PathSegmentKind::Type { .. } => false, // not allowed in imports
75 }
76 } else {
77 false
78 }
79}
80
81fn compare_path_segment_with_name(a: &SmolStr, b: &ast::Name) -> bool {
82 a == b.text()
83}
84
85#[derive(Clone)]
86enum ImportAction {
87 Nothing,
88 // Add a brand new use statement.
89 AddNewUse {
90 anchor: Option<SyntaxNode>, // anchor node
91 add_after_anchor: bool,
92 },
93
94 // To split an existing use statement creating a nested import.
95 AddNestedImport {
96 // how may segments matched with the target path
97 common_segments: usize,
98 path_to_split: ast::Path,
99 // the first segment of path_to_split we want to add into the new nested list
100 first_segment_to_split: Option<ast::PathSegment>,
101 // Wether to add 'self' in addition to the target path
102 add_self: bool,
103 },
104 // To add the target path to an existing nested import tree list.
105 AddInTreeList {
106 common_segments: usize,
107 // The UseTreeList where to add the target path
108 tree_list: ast::UseTreeList,
109 add_self: bool,
110 },
111}
112
113impl ImportAction {
114 fn add_new_use(anchor: Option<SyntaxNode>, add_after_anchor: bool) -> Self {
115 ImportAction::AddNewUse { anchor, add_after_anchor }
116 }
117
118 fn add_nested_import(
119 common_segments: usize,
120 path_to_split: ast::Path,
121 first_segment_to_split: Option<ast::PathSegment>,
122 add_self: bool,
123 ) -> Self {
124 ImportAction::AddNestedImport {
125 common_segments,
126 path_to_split,
127 first_segment_to_split,
128 add_self,
129 }
130 }
131
132 fn add_in_tree_list(
133 common_segments: usize,
134 tree_list: ast::UseTreeList,
135 add_self: bool,
136 ) -> Self {
137 ImportAction::AddInTreeList { common_segments, tree_list, add_self }
138 }
139
140 fn better(left: ImportAction, right: ImportAction) -> ImportAction {
141 if left.is_better(&right) {
142 left
143 } else {
144 right
145 }
146 }
147
148 fn is_better(&self, other: &ImportAction) -> bool {
149 match (self, other) {
150 (ImportAction::Nothing, _) => true,
151 (ImportAction::AddInTreeList { .. }, ImportAction::Nothing) => false,
152 (
153 ImportAction::AddNestedImport { common_segments: n, .. },
154 ImportAction::AddInTreeList { common_segments: m, .. },
155 ) => n > m,
156 (
157 ImportAction::AddInTreeList { common_segments: n, .. },
158 ImportAction::AddNestedImport { common_segments: m, .. },
159 ) => n > m,
160 (ImportAction::AddInTreeList { .. }, _) => true,
161 (ImportAction::AddNestedImport { .. }, ImportAction::Nothing) => false,
162 (ImportAction::AddNestedImport { .. }, _) => true,
163 (ImportAction::AddNewUse { .. }, _) => false,
164 }
165 }
166}
167
168// Find out the best ImportAction to import target path against current_use_tree.
169// If current_use_tree has a nested import the function gets called recursively on every UseTree inside a UseTreeList.
170fn walk_use_tree_for_best_action(
171 current_path_segments: &mut Vec<ast::PathSegment>, // buffer containing path segments
172 current_parent_use_tree_list: Option<ast::UseTreeList>, // will be Some value if we are in a nested import
173 current_use_tree: ast::UseTree, // the use tree we are currently examinating
174 target: &[SmolStr], // the path we want to import
175) -> ImportAction {
176 // We save the number of segments in the buffer so we can restore the correct segments
177 // before returning. Recursive call will add segments so we need to delete them.
178 let prev_len = current_path_segments.len();
179
180 let tree_list = current_use_tree.use_tree_list();
181 let alias = current_use_tree.alias();
182
183 let path = match current_use_tree.path() {
184 Some(path) => path,
185 None => {
186 // If the use item don't have a path, it means it's broken (syntax error)
187 return ImportAction::add_new_use(
188 current_use_tree
189 .syntax()
190 .ancestors()
191 .find_map(ast::UseItem::cast)
192 .map(|it| it.syntax().clone()),
193 true,
194 );
195 }
196 };
197
198 // This can happen only if current_use_tree is a direct child of a UseItem
199 if let Some(name) = alias.and_then(|it| it.name()) {
200 if compare_path_segment_with_name(&target[0], &name) {
201 return ImportAction::Nothing;
202 }
203 }
204
205 collect_path_segments_raw(current_path_segments, path.clone());
206
207 // We compare only the new segments added in the line just above.
208 // The first prev_len segments were already compared in 'parent' recursive calls.
209 let left = target.split_at(prev_len).1;
210 let right = current_path_segments.split_at(prev_len).1;
211 let common = compare_path_segments(left, &right);
212 let mut action = match common {
213 0 => ImportAction::add_new_use(
214 // e.g: target is std::fmt and we can have
215 // use foo::bar
216 // We add a brand new use statement
217 current_use_tree
218 .syntax()
219 .ancestors()
220 .find_map(ast::UseItem::cast)
221 .map(|it| it.syntax().clone()),
222 true,
223 ),
224 common if common == left.len() && left.len() == right.len() => {
225 // e.g: target is std::fmt and we can have
226 // 1- use std::fmt;
227 // 2- use std::fmt:{ ... }
228 if let Some(list) = tree_list {
229 // In case 2 we need to add self to the nested list
230 // unless it's already there
231 let has_self = list.use_trees().map(|it| it.path()).any(|p| {
232 p.and_then(|it| it.segment())
233 .and_then(|it| it.kind())
234 .filter(|k| *k == ast::PathSegmentKind::SelfKw)
235 .is_some()
236 });
237
238 if has_self {
239 ImportAction::Nothing
240 } else {
241 ImportAction::add_in_tree_list(current_path_segments.len(), list, true)
242 }
243 } else {
244 // Case 1
245 ImportAction::Nothing
246 }
247 }
248 common if common != left.len() && left.len() == right.len() => {
249 // e.g: target is std::fmt and we have
250 // use std::io;
251 // We need to split.
252 let segments_to_split = current_path_segments.split_at(prev_len + common).1;
253 ImportAction::add_nested_import(
254 prev_len + common,
255 path,
256 Some(segments_to_split[0].clone()),
257 false,
258 )
259 }
260 common if common == right.len() && left.len() > right.len() => {
261 // e.g: target is std::fmt and we can have
262 // 1- use std;
263 // 2- use std::{ ... };
264
265 // fallback action
266 let mut better_action = ImportAction::add_new_use(
267 current_use_tree
268 .syntax()
269 .ancestors()
270 .find_map(ast::UseItem::cast)
271 .map(|it| it.syntax().clone()),
272 true,
273 );
274 if let Some(list) = tree_list {
275 // Case 2, check recursively if the path is already imported in the nested list
276 for u in list.use_trees() {
277 let child_action = walk_use_tree_for_best_action(
278 current_path_segments,
279 Some(list.clone()),
280 u,
281 target,
282 );
283 if child_action.is_better(&better_action) {
284 better_action = child_action;
285 if let ImportAction::Nothing = better_action {
286 return better_action;
287 }
288 }
289 }
290 } else {
291 // Case 1, split adding self
292 better_action = ImportAction::add_nested_import(prev_len + common, path, None, true)
293 }
294 better_action
295 }
296 common if common == left.len() && left.len() < right.len() => {
297 // e.g: target is std::fmt and we can have
298 // use std::fmt::Debug;
299 let segments_to_split = current_path_segments.split_at(prev_len + common).1;
300 ImportAction::add_nested_import(
301 prev_len + common,
302 path,
303 Some(segments_to_split[0].clone()),
304 true,
305 )
306 }
307 common if common < left.len() && common < right.len() => {
308 // e.g: target is std::fmt::nested::Debug
309 // use std::fmt::Display
310 let segments_to_split = current_path_segments.split_at(prev_len + common).1;
311 ImportAction::add_nested_import(
312 prev_len + common,
313 path,
314 Some(segments_to_split[0].clone()),
315 false,
316 )
317 }
318 _ => unreachable!(),
319 };
320
321 // If we are inside a UseTreeList adding a use statement become adding to the existing
322 // tree list.
323 action = match (current_parent_use_tree_list, action.clone()) {
324 (Some(use_tree_list), ImportAction::AddNewUse { .. }) => {
325 ImportAction::add_in_tree_list(prev_len, use_tree_list, false)
326 }
327 (_, _) => action,
328 };
329
330 // We remove the segments added
331 current_path_segments.truncate(prev_len);
332 action
333}
334
335fn best_action_for_target(
336 container: SyntaxNode,
337 anchor: SyntaxNode,
338 target: &[SmolStr],
339) -> ImportAction {
340 let mut storage = Vec::with_capacity(16); // this should be the only allocation
341 let best_action = container
342 .children()
343 .filter_map(ast::UseItem::cast)
344 .filter_map(|it| it.use_tree())
345 .map(|u| walk_use_tree_for_best_action(&mut storage, None, u, target))
346 .fold(None, |best, a| match best {
347 Some(best) => Some(ImportAction::better(best, a)),
348 None => Some(a),
349 });
350
351 match best_action {
352 Some(action) => action,
353 None => {
354 // We have no action and no UseItem was found in container so we find
355 // another item and we use it as anchor.
356 // If there are no items above, we choose the target path itself as anchor.
357 // todo: we should include even whitespace blocks as anchor candidates
358 let anchor = container
359 .children()
360 .find(|n| n.text_range().start() < anchor.text_range().start())
361 .or_else(|| Some(anchor));
362
363 ImportAction::add_new_use(anchor, false)
364 }
365 }
366}
367
368fn make_assist(action: &ImportAction, target: &[SmolStr], edit: &mut TextEditBuilder) {
369 match action {
370 ImportAction::AddNewUse { anchor, add_after_anchor } => {
371 make_assist_add_new_use(anchor, *add_after_anchor, target, edit)
372 }
373 ImportAction::AddInTreeList { common_segments, tree_list, add_self } => {
374 // We know that the fist n segments already exists in the use statement we want
375 // to modify, so we want to add only the last target.len() - n segments.
376 let segments_to_add = target.split_at(*common_segments).1;
377 make_assist_add_in_tree_list(tree_list, segments_to_add, *add_self, edit)
378 }
379 ImportAction::AddNestedImport {
380 common_segments,
381 path_to_split,
382 first_segment_to_split,
383 add_self,
384 } => {
385 let segments_to_add = target.split_at(*common_segments).1;
386 make_assist_add_nested_import(
387 path_to_split,
388 first_segment_to_split,
389 segments_to_add,
390 *add_self,
391 edit,
392 )
393 }
394 _ => {}
395 }
396}
397
398fn make_assist_add_new_use(
399 anchor: &Option<SyntaxNode>,
400 after: bool,
401 target: &[SmolStr],
402 edit: &mut TextEditBuilder,
403) {
404 if let Some(anchor) = anchor {
405 let indent = ra_fmt::leading_indent(anchor);
406 let mut buf = String::new();
407 if after {
408 buf.push_str("\n");
409 if let Some(spaces) = &indent {
410 buf.push_str(spaces);
411 }
412 }
413 buf.push_str("use ");
414 fmt_segments_raw(target, &mut buf);
415 buf.push_str(";");
416 if !after {
417 buf.push_str("\n\n");
418 if let Some(spaces) = &indent {
419 buf.push_str(&spaces);
420 }
421 }
422 let position = if after { anchor.text_range().end() } else { anchor.text_range().start() };
423 edit.insert(position, buf);
424 }
425}
426
427fn make_assist_add_in_tree_list(
428 tree_list: &ast::UseTreeList,
429 target: &[SmolStr],
430 add_self: bool,
431 edit: &mut TextEditBuilder,
432) {
433 let last = tree_list.use_trees().last();
434 if let Some(last) = last {
435 let mut buf = String::new();
436 let comma = last.syntax().siblings(Direction::Next).find(|n| n.kind() == T![,]);
437 let offset = if let Some(comma) = comma {
438 comma.text_range().end()
439 } else {
440 buf.push_str(",");
441 last.syntax().text_range().end()
442 };
443 if add_self {
444 buf.push_str(" self")
445 } else {
446 buf.push_str(" ");
447 }
448 fmt_segments_raw(target, &mut buf);
449 edit.insert(offset, buf);
450 } else {
451
452 }
453}
454
455fn make_assist_add_nested_import(
456 path: &ast::Path,
457 first_segment_to_split: &Option<ast::PathSegment>,
458 target: &[SmolStr],
459 add_self: bool,
460 edit: &mut TextEditBuilder,
461) {
462 let use_tree = path.syntax().ancestors().find_map(ast::UseTree::cast);
463 if let Some(use_tree) = use_tree {
464 let (start, add_colon_colon) = if let Some(first_segment_to_split) = first_segment_to_split
465 {
466 (first_segment_to_split.syntax().text_range().start(), false)
467 } else {
468 (use_tree.syntax().text_range().end(), true)
469 };
470 let end = use_tree.syntax().text_range().end();
471
472 let mut buf = String::new();
473 if add_colon_colon {
474 buf.push_str("::");
475 }
476 buf.push_str("{ ");
477 if add_self {
478 buf.push_str("self, ");
479 }
480 fmt_segments_raw(target, &mut buf);
481 if !target.is_empty() {
482 buf.push_str(", ");
483 }
484 edit.insert(start, buf);
485 edit.insert(end, "}".to_string());
486 }
487}
488
489fn apply_auto_import(
490 container: &SyntaxNode,
491 path: &ast::Path,
492 target: &[SmolStr],
493 edit: &mut TextEditBuilder,
494) {
495 let action = best_action_for_target(container.clone(), path.syntax().clone(), target);
496 make_assist(&action, target, edit);
497 if let Some(last) = path.segment() {
498 // Here we are assuming the assist will provide a correct use statement
499 // so we can delete the path qualifier
500 edit.delete(TextRange::from_to(
501 path.syntax().text_range().start(),
502 last.syntax().text_range().start(),
503 ));
504 }
505}
506
507pub fn collect_hir_path_segments(path: &hir::Path) -> Option<Vec<SmolStr>> {
508 let mut ps = Vec::<SmolStr>::with_capacity(10);
509 match path.kind {
510 hir::PathKind::Abs => ps.push("".into()),
511 hir::PathKind::Crate => ps.push("crate".into()),
512 hir::PathKind::Plain => {}
513 hir::PathKind::Self_ => ps.push("self".into()),
514 hir::PathKind::Super => ps.push("super".into()),
515 hir::PathKind::Type(_) => return None,
516 }
517 for s in path.segments.iter() {
518 ps.push(s.name.to_string().into());
519 }
520 Some(ps)
521}
522
523// This function produces sequence of text edits into edit
524// to import the target path in the most appropriate scope given
525// the cursor position
526pub fn auto_import_text_edit(
527 // Ideally the position of the cursor, used to
528 position: &SyntaxNode,
529 // The statement to use as anchor (last resort)
530 anchor: &SyntaxNode,
531 // The path to import as a sequence of strings
532 target: &[SmolStr],
533 edit: &mut TextEditBuilder,
534) {
535 let container = position.ancestors().find_map(|n| {
536 if let Some(module) = ast::Module::cast(n.clone()) {
537 return module.item_list().map(|it| it.syntax().clone());
538 }
539 ast::SourceFile::cast(n).map(|it| it.syntax().clone())
540 });
541
542 if let Some(container) = container {
543 let action = best_action_for_target(container, anchor.clone(), target);
544 make_assist(&action, target, edit);
545 }
546}
547
548pub(crate) fn auto_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
549 let path: ast::Path = ctx.node_at_offset()?;
550 // We don't want to mess with use statements
551 if path.syntax().ancestors().find_map(ast::UseItem::cast).is_some() {
552 return None;
553 }
554
555 let hir_path = hir::Path::from_ast(path.clone())?;
556 let segments = collect_hir_path_segments(&hir_path)?;
557 if segments.len() < 2 {
558 return None;
559 }
560
561 if let Some(module) = path.syntax().ancestors().find_map(ast::Module::cast) {
562 if let (Some(item_list), Some(name)) = (module.item_list(), module.name()) {
563 ctx.add_action(
564 AssistId("auto_import"),
565 format!("import {} in mod {}", fmt_segments(&segments), name.text()),
566 |edit| {
567 apply_auto_import(
568 item_list.syntax(),
569 &path,
570 &segments,
571 edit.text_edit_builder(),
572 );
573 },
574 );
575 }
576 } else {
577 let current_file = path.syntax().ancestors().find_map(ast::SourceFile::cast)?;
578 ctx.add_action(
579 AssistId("auto_import"),
580 format!("import {} in the current file", fmt_segments(&segments)),
581 |edit| {
582 apply_auto_import(
583 current_file.syntax(),
584 &path,
585 &segments,
586 edit.text_edit_builder(),
587 );
588 },
589 );
590 }
591
592 ctx.build()
593}
594
595#[cfg(test)]
596mod tests {
597 use super::*;
598 use crate::helpers::{check_assist, check_assist_not_applicable};
599
600 #[test]
601 fn test_auto_import_add_use_no_anchor() {
602 check_assist(
603 auto_import,
604 "
605std::fmt::Debug<|>
606 ",
607 "
608use std::fmt::Debug;
609
610Debug<|>
611 ",
612 );
613 }
614 #[test]
615 fn test_auto_import_add_use_no_anchor_with_item_below() {
616 check_assist(
617 auto_import,
618 "
619std::fmt::Debug<|>
620
621fn main() {
622}
623 ",
624 "
625use std::fmt::Debug;
626
627Debug<|>
628
629fn main() {
630}
631 ",
632 );
633 }
634
635 #[test]
636 fn test_auto_import_add_use_no_anchor_with_item_above() {
637 check_assist(
638 auto_import,
639 "
640fn main() {
641}
642
643std::fmt::Debug<|>
644 ",
645 "
646use std::fmt::Debug;
647
648fn main() {
649}
650
651Debug<|>
652 ",
653 );
654 }
655
656 #[test]
657 fn test_auto_import_add_use_no_anchor_2seg() {
658 check_assist(
659 auto_import,
660 "
661std::fmt<|>::Debug
662 ",
663 "
664use std::fmt;
665
666fmt<|>::Debug
667 ",
668 );
669 }
670
671 #[test]
672 fn test_auto_import_add_use() {
673 check_assist(
674 auto_import,
675 "
676use stdx;
677
678impl std::fmt::Debug<|> for Foo {
679}
680 ",
681 "
682use stdx;
683use std::fmt::Debug;
684
685impl Debug<|> for Foo {
686}
687 ",
688 );
689 }
690
691 #[test]
692 fn test_auto_import_file_use_other_anchor() {
693 check_assist(
694 auto_import,
695 "
696impl std::fmt::Debug<|> for Foo {
697}
698 ",
699 "
700use std::fmt::Debug;
701
702impl Debug<|> for Foo {
703}
704 ",
705 );
706 }
707
708 #[test]
709 fn test_auto_import_add_use_other_anchor_indent() {
710 check_assist(
711 auto_import,
712 "
713 impl std::fmt::Debug<|> for Foo {
714 }
715 ",
716 "
717 use std::fmt::Debug;
718
719 impl Debug<|> for Foo {
720 }
721 ",
722 );
723 }
724
725 #[test]
726 fn test_auto_import_split_different() {
727 check_assist(
728 auto_import,
729 "
730use std::fmt;
731
732impl std::io<|> for Foo {
733}
734 ",
735 "
736use std::{ io, fmt};
737
738impl io<|> for Foo {
739}
740 ",
741 );
742 }
743
744 #[test]
745 fn test_auto_import_split_self_for_use() {
746 check_assist(
747 auto_import,
748 "
749use std::fmt;
750
751impl std::fmt::Debug<|> for Foo {
752}
753 ",
754 "
755use std::fmt::{ self, Debug, };
756
757impl Debug<|> for Foo {
758}
759 ",
760 );
761 }
762
763 #[test]
764 fn test_auto_import_split_self_for_target() {
765 check_assist(
766 auto_import,
767 "
768use std::fmt::Debug;
769
770impl std::fmt<|> for Foo {
771}
772 ",
773 "
774use std::fmt::{ self, Debug};
775
776impl fmt<|> for Foo {
777}
778 ",
779 );
780 }
781
782 #[test]
783 fn test_auto_import_add_to_nested_self_nested() {
784 check_assist(
785 auto_import,
786 "
787use std::fmt::{Debug, nested::{Display}};
788
789impl std::fmt::nested<|> for Foo {
790}
791",
792 "
793use std::fmt::{Debug, nested::{Display, self}};
794
795impl nested<|> for Foo {
796}
797",
798 );
799 }
800
801 #[test]
802 fn test_auto_import_add_to_nested_self_already_included() {
803 check_assist(
804 auto_import,
805 "
806use std::fmt::{Debug, nested::{self, Display}};
807
808impl std::fmt::nested<|> for Foo {
809}
810",
811 "
812use std::fmt::{Debug, nested::{self, Display}};
813
814impl nested<|> for Foo {
815}
816",
817 );
818 }
819
820 #[test]
821 fn test_auto_import_add_to_nested_nested() {
822 check_assist(
823 auto_import,
824 "
825use std::fmt::{Debug, nested::{Display}};
826
827impl std::fmt::nested::Debug<|> for Foo {
828}
829",
830 "
831use std::fmt::{Debug, nested::{Display, Debug}};
832
833impl Debug<|> for Foo {
834}
835",
836 );
837 }
838
839 #[test]
840 fn test_auto_import_split_common_target_longer() {
841 check_assist(
842 auto_import,
843 "
844use std::fmt::Debug;
845
846impl std::fmt::nested::Display<|> for Foo {
847}
848",
849 "
850use std::fmt::{ nested::Display, Debug};
851
852impl Display<|> for Foo {
853}
854",
855 );
856 }
857
858 #[test]
859 fn test_auto_import_split_common_use_longer() {
860 check_assist(
861 auto_import,
862 "
863use std::fmt::nested::Debug;
864
865impl std::fmt::Display<|> for Foo {
866}
867",
868 "
869use std::fmt::{ Display, nested::Debug};
870
871impl Display<|> for Foo {
872}
873",
874 );
875 }
876
877 #[test]
878 fn test_auto_import_alias() {
879 check_assist(
880 auto_import,
881 "
882use std::fmt as foo;
883
884impl foo::Debug<|> for Foo {
885}
886",
887 "
888use std::fmt as foo;
889
890impl Debug<|> for Foo {
891}
892",
893 );
894 }
895
896 #[test]
897 fn test_auto_import_not_applicable_one_segment() {
898 check_assist_not_applicable(
899 auto_import,
900 "
901impl foo<|> for Foo {
902}
903",
904 );
905 }
906
907 #[test]
908 fn test_auto_import_not_applicable_in_use() {
909 check_assist_not_applicable(
910 auto_import,
911 "
912use std::fmt<|>;
913",
914 );
915 }
916
917 #[test]
918 fn test_auto_import_add_use_no_anchor_in_mod_mod() {
919 check_assist(
920 auto_import,
921 "
922mod foo {
923 mod bar {
924 std::fmt::Debug<|>
925 }
926}
927 ",
928 "
929mod foo {
930 mod bar {
931 use std::fmt::Debug;
932
933 Debug<|>
934 }
935}
936 ",
937 );
938 }
939}