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