diff options
Diffstat (limited to 'crates/ide_assists')
-rw-r--r-- | crates/ide_assists/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/ide_assists/src/handlers/add_turbo_fish.rs | 33 | ||||
-rw-r--r-- | crates/ide_assists/src/handlers/auto_import.rs | 14 | ||||
-rw-r--r-- | crates/ide_assists/src/handlers/qualify_path.rs | 18 |
4 files changed, 43 insertions, 24 deletions
diff --git a/crates/ide_assists/Cargo.toml b/crates/ide_assists/Cargo.toml index 3bf0099a9..dd9aa27c6 100644 --- a/crates/ide_assists/Cargo.toml +++ b/crates/ide_assists/Cargo.toml | |||
@@ -21,7 +21,7 @@ text_edit = { path = "../text_edit", version = "0.0.0" } | |||
21 | profile = { path = "../profile", version = "0.0.0" } | 21 | profile = { path = "../profile", version = "0.0.0" } |
22 | ide_db = { path = "../ide_db", version = "0.0.0" } | 22 | ide_db = { path = "../ide_db", version = "0.0.0" } |
23 | hir = { path = "../hir", version = "0.0.0" } | 23 | hir = { path = "../hir", version = "0.0.0" } |
24 | test_utils = { path = "../test_utils", version = "0.0.0" } | ||
25 | 24 | ||
26 | [dev-dependencies] | 25 | [dev-dependencies] |
26 | test_utils = { path = "../test_utils" } | ||
27 | expect-test = "1.1" | 27 | expect-test = "1.1" |
diff --git a/crates/ide_assists/src/handlers/add_turbo_fish.rs b/crates/ide_assists/src/handlers/add_turbo_fish.rs index 3b6efbab4..ee879c151 100644 --- a/crates/ide_assists/src/handlers/add_turbo_fish.rs +++ b/crates/ide_assists/src/handlers/add_turbo_fish.rs | |||
@@ -56,13 +56,20 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext) -> Option<( | |||
56 | if let Some(let_stmt) = ctx.find_node_at_offset::<ast::LetStmt>() { | 56 | if let Some(let_stmt) = ctx.find_node_at_offset::<ast::LetStmt>() { |
57 | if let_stmt.colon_token().is_none() { | 57 | if let_stmt.colon_token().is_none() { |
58 | let type_pos = let_stmt.pat()?.syntax().last_token()?.text_range().end(); | 58 | let type_pos = let_stmt.pat()?.syntax().last_token()?.text_range().end(); |
59 | let semi_pos = let_stmt.syntax().last_token()?.text_range().end(); | ||
60 | |||
59 | acc.add( | 61 | acc.add( |
60 | AssistId("add_type_ascription", AssistKind::RefactorRewrite), | 62 | AssistId("add_type_ascription", AssistKind::RefactorRewrite), |
61 | "Add `: _` before assignment operator", | 63 | "Add `: _` before assignment operator", |
62 | ident.text_range(), | 64 | ident.text_range(), |
63 | |builder| match ctx.config.snippet_cap { | 65 | |builder| { |
64 | Some(cap) => builder.insert_snippet(cap, type_pos, ": ${0:_}"), | 66 | if let_stmt.semicolon_token().is_none() { |
65 | None => builder.insert(type_pos, ": _"), | 67 | builder.insert(semi_pos, ";"); |
68 | } | ||
69 | match ctx.config.snippet_cap { | ||
70 | Some(cap) => builder.insert_snippet(cap, type_pos, ": ${0:_}"), | ||
71 | None => builder.insert(type_pos, ": _"), | ||
72 | } | ||
66 | }, | 73 | }, |
67 | )? | 74 | )? |
68 | } else { | 75 | } else { |
@@ -265,4 +272,24 @@ fn main() { | |||
265 | "#, | 272 | "#, |
266 | ); | 273 | ); |
267 | } | 274 | } |
275 | |||
276 | #[test] | ||
277 | fn add_type_ascription_append_semicolon() { | ||
278 | check_assist_by_label( | ||
279 | add_turbo_fish, | ||
280 | r#" | ||
281 | fn make<T>() -> T {} | ||
282 | fn main() { | ||
283 | let x = make$0() | ||
284 | } | ||
285 | "#, | ||
286 | r#" | ||
287 | fn make<T>() -> T {} | ||
288 | fn main() { | ||
289 | let x: ${0:_} = make(); | ||
290 | } | ||
291 | "#, | ||
292 | "Add `: _` before assignment operator", | ||
293 | ); | ||
294 | } | ||
268 | } | 295 | } |
diff --git a/crates/ide_assists/src/handlers/auto_import.rs b/crates/ide_assists/src/handlers/auto_import.rs index 7caee8df0..7019039b9 100644 --- a/crates/ide_assists/src/handlers/auto_import.rs +++ b/crates/ide_assists/src/handlers/auto_import.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | use ide_db::helpers::{ | 1 | use ide_db::helpers::{ |
2 | import_assets::{ImportAssets, ImportCandidate}, | 2 | import_assets::{ImportAssets, ImportCandidate}, |
3 | insert_use::{insert_use, ImportScope}, | 3 | insert_use::{insert_use, ImportScope}, |
4 | item_name, mod_path_to_ast, | 4 | mod_path_to_ast, |
5 | }; | 5 | }; |
6 | use syntax::{ast, AstNode, SyntaxNode}; | 6 | use syntax::{ast, AstNode, SyntaxNode}; |
7 | 7 | ||
@@ -90,17 +90,13 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> | |||
90 | } | 90 | } |
91 | 91 | ||
92 | let range = ctx.sema.original_range(&syntax_under_caret).range; | 92 | let range = ctx.sema.original_range(&syntax_under_caret).range; |
93 | let group = import_group_message(import_assets.import_candidate()); | 93 | let group_label = group_label(import_assets.import_candidate()); |
94 | let scope = ImportScope::find_insert_use_container(&syntax_under_caret, &ctx.sema)?; | 94 | let scope = ImportScope::find_insert_use_container(&syntax_under_caret, &ctx.sema)?; |
95 | for import in proposed_imports { | 95 | for import in proposed_imports { |
96 | let name = match item_name(ctx.db(), import.original_item) { | ||
97 | Some(name) => name, | ||
98 | None => continue, | ||
99 | }; | ||
100 | acc.add_group( | 96 | acc.add_group( |
101 | &group, | 97 | &group_label, |
102 | AssistId("auto_import", AssistKind::QuickFix), | 98 | AssistId("auto_import", AssistKind::QuickFix), |
103 | format!("Import `{}`", name), | 99 | format!("Import `{}`", import.import_path), |
104 | range, | 100 | range, |
105 | |builder| { | 101 | |builder| { |
106 | let rewriter = | 102 | let rewriter = |
@@ -126,7 +122,7 @@ pub(super) fn find_importable_node(ctx: &AssistContext) -> Option<(ImportAssets, | |||
126 | } | 122 | } |
127 | } | 123 | } |
128 | 124 | ||
129 | fn import_group_message(import_candidate: &ImportCandidate) -> GroupLabel { | 125 | fn group_label(import_candidate: &ImportCandidate) -> GroupLabel { |
130 | let name = match import_candidate { | 126 | let name = match import_candidate { |
131 | ImportCandidate::Path(candidate) => format!("Import {}", candidate.name.text()), | 127 | ImportCandidate::Path(candidate) => format!("Import {}", candidate.name.text()), |
132 | ImportCandidate::TraitAssocItem(candidate) => { | 128 | ImportCandidate::TraitAssocItem(candidate) => { |
diff --git a/crates/ide_assists/src/handlers/qualify_path.rs b/crates/ide_assists/src/handlers/qualify_path.rs index 272874ae3..30b23da6c 100644 --- a/crates/ide_assists/src/handlers/qualify_path.rs +++ b/crates/ide_assists/src/handlers/qualify_path.rs | |||
@@ -3,7 +3,7 @@ use std::iter; | |||
3 | use hir::AsAssocItem; | 3 | use hir::AsAssocItem; |
4 | use ide_db::helpers::{ | 4 | use ide_db::helpers::{ |
5 | import_assets::{ImportCandidate, LocatedImport}, | 5 | import_assets::{ImportCandidate, LocatedImport}, |
6 | item_name, mod_path_to_ast, | 6 | mod_path_to_ast, |
7 | }; | 7 | }; |
8 | use ide_db::RootDatabase; | 8 | use ide_db::RootDatabase; |
9 | use syntax::{ | 9 | use syntax::{ |
@@ -78,7 +78,7 @@ pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext) -> Option<()> | |||
78 | acc.add_group( | 78 | acc.add_group( |
79 | &group_label, | 79 | &group_label, |
80 | AssistId("qualify_path", AssistKind::QuickFix), | 80 | AssistId("qualify_path", AssistKind::QuickFix), |
81 | label(ctx.db(), candidate, &import), | 81 | label(candidate, &import), |
82 | range, | 82 | range, |
83 | |builder| { | 83 | |builder| { |
84 | qualify_candidate.qualify( | 84 | qualify_candidate.qualify( |
@@ -199,21 +199,17 @@ fn group_label(candidate: &ImportCandidate) -> GroupLabel { | |||
199 | GroupLabel(format!("Qualify {}", name)) | 199 | GroupLabel(format!("Qualify {}", name)) |
200 | } | 200 | } |
201 | 201 | ||
202 | fn label(db: &RootDatabase, candidate: &ImportCandidate, import: &LocatedImport) -> String { | 202 | fn label(candidate: &ImportCandidate, import: &LocatedImport) -> String { |
203 | let display_path = match item_name(db, import.original_item) { | ||
204 | Some(display_path) => display_path.to_string(), | ||
205 | None => "{unknown}".to_string(), | ||
206 | }; | ||
207 | match candidate { | 203 | match candidate { |
208 | ImportCandidate::Path(candidate) => { | 204 | ImportCandidate::Path(candidate) => { |
209 | if candidate.qualifier.is_some() { | 205 | if candidate.qualifier.is_some() { |
210 | format!("Qualify with `{}`", display_path) | 206 | format!("Qualify with `{}`", import.import_path) |
211 | } else { | 207 | } else { |
212 | format!("Qualify as `{}`", display_path) | 208 | format!("Qualify as `{}`", import.import_path) |
213 | } | 209 | } |
214 | } | 210 | } |
215 | ImportCandidate::TraitAssocItem(_) => format!("Qualify `{}`", display_path), | 211 | ImportCandidate::TraitAssocItem(_) => format!("Qualify `{}`", import.import_path), |
216 | ImportCandidate::TraitMethod(_) => format!("Qualify with cast as `{}`", display_path), | 212 | ImportCandidate::TraitMethod(_) => format!("Qualify with cast as `{}`", import.import_path), |
217 | } | 213 | } |
218 | } | 214 | } |
219 | 215 | ||