aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/assists
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2019-10-28 18:08:56 +0000
committerFlorian Diebold <[email protected]>2019-10-28 18:08:56 +0000
commit6d1b4ffa2e18838a6d025b9e38f95dfd4864afa7 (patch)
tree8e28aaae343b6c8c8586434c8b0a373d9b933312 /crates/ra_assists/src/assists
parent1ca41f2118fca17ee5ad0f586218489fa5d883af (diff)
Fix autoimport not choosing the deepest use tree in some situations
Diffstat (limited to 'crates/ra_assists/src/assists')
-rw-r--r--crates/ra_assists/src/assists/add_import.rs43
1 files changed, 37 insertions, 6 deletions
diff --git a/crates/ra_assists/src/assists/add_import.rs b/crates/ra_assists/src/assists/add_import.rs
index e87fae1af..c8b9809f4 100644
--- a/crates/ra_assists/src/assists/add_import.rs
+++ b/crates/ra_assists/src/assists/add_import.rs
@@ -124,9 +124,9 @@ fn fmt_segments_raw(segments: &[SmolStr], buf: &mut String) {
124 } 124 }
125} 125}
126 126
127// Returns the numeber of common segments. 127/// Returns the number of common segments.
128fn compare_path_segments(left: &[SmolStr], right: &[ast::PathSegment]) -> usize { 128fn compare_path_segments(left: &[SmolStr], right: &[ast::PathSegment]) -> usize {
129 left.iter().zip(right).filter(|(l, r)| compare_path_segment(l, r)).count() 129 left.iter().zip(right).take_while(|(l, r)| compare_path_segment(l, r)).count()
130} 130}
131 131
132fn compare_path_segment(a: &SmolStr, b: &ast::PathSegment) -> bool { 132fn compare_path_segment(a: &SmolStr, b: &ast::PathSegment) -> bool {
@@ -147,7 +147,7 @@ fn compare_path_segment_with_name(a: &SmolStr, b: &ast::Name) -> bool {
147 a == b.text() 147 a == b.text()
148} 148}
149 149
150#[derive(Clone)] 150#[derive(Clone, Debug)]
151enum ImportAction { 151enum ImportAction {
152 Nothing, 152 Nothing,
153 // Add a brand new use statement. 153 // Add a brand new use statement.
@@ -217,10 +217,18 @@ impl ImportAction {
217 ( 217 (
218 ImportAction::AddNestedImport { common_segments: n, .. }, 218 ImportAction::AddNestedImport { common_segments: n, .. },
219 ImportAction::AddInTreeList { common_segments: m, .. }, 219 ImportAction::AddInTreeList { common_segments: m, .. },
220 ) => n > m, 220 )
221 ( 221 | (
222 ImportAction::AddInTreeList { common_segments: n, .. }, 222 ImportAction::AddInTreeList { common_segments: n, .. },
223 ImportAction::AddNestedImport { common_segments: m, .. }, 223 ImportAction::AddNestedImport { common_segments: m, .. },
224 )
225 | (
226 ImportAction::AddInTreeList { common_segments: n, .. },
227 ImportAction::AddInTreeList { common_segments: m, .. },
228 )
229 | (
230 ImportAction::AddNestedImport { common_segments: n, .. },
231 ImportAction::AddNestedImport { common_segments: m, .. },
224 ) => n > m, 232 ) => n > m,
225 (ImportAction::AddInTreeList { .. }, _) => true, 233 (ImportAction::AddInTreeList { .. }, _) => true,
226 (ImportAction::AddNestedImport { .. }, ImportAction::Nothing) => false, 234 (ImportAction::AddNestedImport { .. }, ImportAction::Nothing) => false,
@@ -289,7 +297,7 @@ fn walk_use_tree_for_best_action(
289 common if common == left.len() && left.len() == right.len() => { 297 common if common == left.len() && left.len() == right.len() => {
290 // e.g: target is std::fmt and we can have 298 // e.g: target is std::fmt and we can have
291 // 1- use std::fmt; 299 // 1- use std::fmt;
292 // 2- use std::fmt:{ ... } 300 // 2- use std::fmt::{ ... }
293 if let Some(list) = tree_list { 301 if let Some(list) = tree_list {
294 // In case 2 we need to add self to the nested list 302 // In case 2 we need to add self to the nested list
295 // unless it's already there 303 // unless it's already there
@@ -868,6 +876,29 @@ impl Display<|> for Foo {
868 } 876 }
869 877
870 #[test] 878 #[test]
879 fn test_auto_import_use_nested_import() {
880 check_assist(
881 add_import,
882 "
883use crate::{
884 ty::{Substs, Ty},
885 AssocItem,
886};
887
888fn foo() { crate::ty::lower<|>::trait_env() }
889",
890 "
891use crate::{
892 ty::{Substs, Ty, lower},
893 AssocItem,
894};
895
896fn foo() { lower<|>::trait_env() }
897",
898 );
899 }
900
901 #[test]
871 fn test_auto_import_alias() { 902 fn test_auto_import_alias() {
872 check_assist( 903 check_assist(
873 add_import, 904 add_import,