diff options
author | Benjamin Coenen <[email protected]> | 2020-11-28 16:38:16 +0000 |
---|---|---|
committer | Benjamin Coenen <[email protected]> | 2020-11-28 16:38:16 +0000 |
commit | 474ebd60d1ae1057165e189bb539ba6a410e1d68 (patch) | |
tree | 3a20cc535a4d2428e2eaed5030cc88b9a0b0df48 /crates | |
parent | 775c69183cd6c7a2a5c9f27e23e4f4b7bc67f06f (diff) |
add let and letm postfix to turn expressions into variables
Signed-off-by: Benjamin Coenen <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/completion/src/completions/postfix.rs | 65 | ||||
-rw-r--r-- | crates/completion/src/lib.rs | 2 |
2 files changed, 56 insertions, 11 deletions
diff --git a/crates/completion/src/completions/postfix.rs b/crates/completion/src/completions/postfix.rs index b4fac74a0..1785794cc 100644 --- a/crates/completion/src/completions/postfix.rs +++ b/crates/completion/src/completions/postfix.rs | |||
@@ -5,6 +5,7 @@ mod format_like; | |||
5 | use ide_db::ty_filter::TryEnum; | 5 | use ide_db::ty_filter::TryEnum; |
6 | use syntax::{ | 6 | use syntax::{ |
7 | ast::{self, AstNode, AstToken}, | 7 | ast::{self, AstNode, AstToken}, |
8 | SyntaxKind::BLOCK_EXPR, | ||
8 | TextRange, TextSize, | 9 | TextRange, TextSize, |
9 | }; | 10 | }; |
10 | use text_edit::TextEdit; | 11 | use text_edit::TextEdit; |
@@ -220,17 +221,29 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { | |||
220 | ) | 221 | ) |
221 | .add_to(acc); | 222 | .add_to(acc); |
222 | 223 | ||
223 | postfix_snippet(ctx, cap, &dot_receiver, "let", "let", &format!("let $0 = {};", receiver_text)) | 224 | let parent_node = dot_receiver.syntax().parent().and_then(|p| p.parent()); |
224 | .add_to(acc); | 225 | if let Some(parent) = parent_node { |
225 | postfix_snippet( | 226 | if parent.kind() == BLOCK_EXPR { |
226 | ctx, | 227 | postfix_snippet( |
227 | cap, | 228 | ctx, |
228 | &dot_receiver, | 229 | cap, |
229 | "letm", | 230 | &dot_receiver, |
230 | "let mut", | 231 | "let", |
231 | &format!("let mut $0 = {};", receiver_text), | 232 | "let", |
232 | ) | 233 | &format!("let $0 = {};", receiver_text), |
233 | .add_to(acc); | 234 | ) |
235 | .add_to(acc); | ||
236 | postfix_snippet( | ||
237 | ctx, | ||
238 | cap, | ||
239 | &dot_receiver, | ||
240 | "letm", | ||
241 | "let mut", | ||
242 | &format!("let mut $0 = {};", receiver_text), | ||
243 | ) | ||
244 | .add_to(acc); | ||
245 | } | ||
246 | } | ||
234 | 247 | ||
235 | if let ast::Expr::Literal(literal) = dot_receiver.clone() { | 248 | if let ast::Expr::Literal(literal) = dot_receiver.clone() { |
236 | if let Some(literal_text) = ast::String::cast(literal.token()) { | 249 | if let Some(literal_text) = ast::String::cast(literal.token()) { |
@@ -322,6 +335,36 @@ fn main() { | |||
322 | } | 335 | } |
323 | 336 | ||
324 | #[test] | 337 | #[test] |
338 | fn postfix_completion_works_for_function_calln() { | ||
339 | check( | ||
340 | r#" | ||
341 | fn foo(elt: bool) -> bool { | ||
342 | !elt | ||
343 | } | ||
344 | |||
345 | fn main() { | ||
346 | let bar = true; | ||
347 | foo(bar.<|>) | ||
348 | } | ||
349 | "#, | ||
350 | expect![[r#" | ||
351 | sn box Box::new(expr) | ||
352 | sn call function(expr) | ||
353 | sn dbg dbg!(expr) | ||
354 | sn dbgr dbg!(&expr) | ||
355 | sn if if expr {} | ||
356 | sn match match expr {} | ||
357 | sn not !expr | ||
358 | sn ok Ok(expr) | ||
359 | sn ref &expr | ||
360 | sn refm &mut expr | ||
361 | sn some Some(expr) | ||
362 | sn while while expr {} | ||
363 | "#]], | ||
364 | ); | ||
365 | } | ||
366 | |||
367 | #[test] | ||
325 | fn postfix_type_filtering() { | 368 | fn postfix_type_filtering() { |
326 | check( | 369 | check( |
327 | r#" | 370 | r#" |
diff --git a/crates/completion/src/lib.rs b/crates/completion/src/lib.rs index cb6e0554e..ac57683fb 100644 --- a/crates/completion/src/lib.rs +++ b/crates/completion/src/lib.rs | |||
@@ -44,6 +44,8 @@ pub use crate::{ | |||
44 | // - `expr.while` -> `while expr {}` or `while let ... {}` for `Option` or `Result` | 44 | // - `expr.while` -> `while expr {}` or `while let ... {}` for `Option` or `Result` |
45 | // - `expr.ref` -> `&expr` | 45 | // - `expr.ref` -> `&expr` |
46 | // - `expr.refm` -> `&mut expr` | 46 | // - `expr.refm` -> `&mut expr` |
47 | // - `expr.let` -> `let <|> = expr;` | ||
48 | // - `expr.letm` -> `let mut <|> = expr;` | ||
47 | // - `expr.not` -> `!expr` | 49 | // - `expr.not` -> `!expr` |
48 | // - `expr.dbg` -> `dbg!(expr)` | 50 | // - `expr.dbg` -> `dbg!(expr)` |
49 | // - `expr.dbgr` -> `dbg!(&expr)` | 51 | // - `expr.dbgr` -> `dbg!(&expr)` |