}
}
-// Returns the numeber of common segments.
+/// Returns the number of common segments.
fn compare_path_segments(left: &[SmolStr], right: &[ast::PathSegment]) -> usize {
- left.iter().zip(right).filter(|(l, r)| compare_path_segment(l, r)).count()
+ left.iter().zip(right).take_while(|(l, r)| compare_path_segment(l, r)).count()
}
fn compare_path_segment(a: &SmolStr, b: &ast::PathSegment) -> bool {
a == b.text()
}
-#[derive(Clone)]
+#[derive(Clone, Debug)]
enum ImportAction {
Nothing,
// Add a brand new use statement.
(
ImportAction::AddNestedImport { common_segments: n, .. },
ImportAction::AddInTreeList { common_segments: m, .. },
- ) => n > m,
- (
+ )
+ | (
ImportAction::AddInTreeList { common_segments: n, .. },
ImportAction::AddNestedImport { common_segments: m, .. },
+ )
+ | (
+ ImportAction::AddInTreeList { common_segments: n, .. },
+ ImportAction::AddInTreeList { common_segments: m, .. },
+ )
+ | (
+ ImportAction::AddNestedImport { common_segments: n, .. },
+ ImportAction::AddNestedImport { common_segments: m, .. },
) => n > m,
(ImportAction::AddInTreeList { .. }, _) => true,
(ImportAction::AddNestedImport { .. }, ImportAction::Nothing) => false,
common if common == left.len() && left.len() == right.len() => {
// e.g: target is std::fmt and we can have
// 1- use std::fmt;
- // 2- use std::fmt:{ ... }
+ // 2- use std::fmt::{ ... }
if let Some(list) = tree_list {
// In case 2 we need to add self to the nested list
// unless it's already there
);
}
+ #[test]
+ fn test_auto_import_use_nested_import() {
+ check_assist(
+ add_import,
+ "
+use crate::{
+ ty::{Substs, Ty},
+ AssocItem,
+};
+
+fn foo() { crate::ty::lower<|>::trait_env() }
+",
+ "
+use crate::{
+ ty::{Substs, Ty, lower},
+ AssocItem,
+};
+
+fn foo() { lower<|>::trait_env() }
+",
+ );
+ }
+
#[test]
fn test_auto_import_alias() {
check_assist(