From 775c69183cd6c7a2a5c9f27e23e4f4b7bc67f06f Mon Sep 17 00:00:00 2001 From: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> Date: Sat, 21 Nov 2020 15:06:22 +0100 Subject: add let and letm postfix to turn expressions into variables Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> --- crates/completion/src/completions/postfix.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'crates/completion') diff --git a/crates/completion/src/completions/postfix.rs b/crates/completion/src/completions/postfix.rs index 7fbda7a6b..b4fac74a0 100644 --- a/crates/completion/src/completions/postfix.rs +++ b/crates/completion/src/completions/postfix.rs @@ -220,6 +220,18 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { ) .add_to(acc); + postfix_snippet(ctx, cap, &dot_receiver, "let", "let", &format!("let $0 = {};", receiver_text)) + .add_to(acc); + postfix_snippet( + ctx, + cap, + &dot_receiver, + "letm", + "let mut", + &format!("let mut $0 = {};", receiver_text), + ) + .add_to(acc); + if let ast::Expr::Literal(literal) = dot_receiver.clone() { if let Some(literal_text) = ast::String::cast(literal.token()) { add_format_like_completions(acc, ctx, &dot_receiver, cap, &literal_text); @@ -296,6 +308,8 @@ fn main() { sn dbg dbg!(expr) sn dbgr dbg!(&expr) sn if if expr {} + sn let let + sn letm let mut sn match match expr {} sn not !expr sn ok Ok(expr) @@ -321,6 +335,8 @@ fn main() { sn call function(expr) sn dbg dbg!(expr) sn dbgr dbg!(&expr) + sn let let + sn letm let mut sn match match expr {} sn ok Ok(expr) sn ref &expr -- cgit v1.2.3 From 474ebd60d1ae1057165e189bb539ba6a410e1d68 Mon Sep 17 00:00:00 2001 From: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> Date: Sat, 28 Nov 2020 17:38:16 +0100 Subject: add let and letm postfix to turn expressions into variables Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> --- crates/completion/src/completions/postfix.rs | 65 +++++++++++++++++++++++----- crates/completion/src/lib.rs | 2 + 2 files changed, 56 insertions(+), 11 deletions(-) (limited to 'crates/completion') 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; use ide_db::ty_filter::TryEnum; use syntax::{ ast::{self, AstNode, AstToken}, + SyntaxKind::BLOCK_EXPR, TextRange, TextSize, }; use text_edit::TextEdit; @@ -220,17 +221,29 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { ) .add_to(acc); - postfix_snippet(ctx, cap, &dot_receiver, "let", "let", &format!("let $0 = {};", receiver_text)) - .add_to(acc); - postfix_snippet( - ctx, - cap, - &dot_receiver, - "letm", - "let mut", - &format!("let mut $0 = {};", receiver_text), - ) - .add_to(acc); + let parent_node = dot_receiver.syntax().parent().and_then(|p| p.parent()); + if let Some(parent) = parent_node { + if parent.kind() == BLOCK_EXPR { + postfix_snippet( + ctx, + cap, + &dot_receiver, + "let", + "let", + &format!("let $0 = {};", receiver_text), + ) + .add_to(acc); + postfix_snippet( + ctx, + cap, + &dot_receiver, + "letm", + "let mut", + &format!("let mut $0 = {};", receiver_text), + ) + .add_to(acc); + } + } if let ast::Expr::Literal(literal) = dot_receiver.clone() { if let Some(literal_text) = ast::String::cast(literal.token()) { @@ -321,6 +334,36 @@ fn main() { ); } + #[test] + fn postfix_completion_works_for_function_calln() { + check( + r#" +fn foo(elt: bool) -> bool { + !elt +} + +fn main() { + let bar = true; + foo(bar.<|>) +} +"#, + expect![[r#" + sn box Box::new(expr) + sn call function(expr) + sn dbg dbg!(expr) + sn dbgr dbg!(&expr) + sn if if expr {} + sn match match expr {} + sn not !expr + sn ok Ok(expr) + sn ref &expr + sn refm &mut expr + sn some Some(expr) + sn while while expr {} + "#]], + ); + } + #[test] fn postfix_type_filtering() { check( 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::{ // - `expr.while` -> `while expr {}` or `while let ... {}` for `Option` or `Result` // - `expr.ref` -> `&expr` // - `expr.refm` -> `&mut expr` +// - `expr.let` -> `let <|> = expr;` +// - `expr.letm` -> `let mut <|> = expr;` // - `expr.not` -> `!expr` // - `expr.dbg` -> `dbg!(expr)` // - `expr.dbgr` -> `dbg!(&expr)` -- cgit v1.2.3