aboutsummaryrefslogtreecommitdiff
path: root/crates/completion
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-11-29 19:21:25 +0000
committerGitHub <[email protected]>2020-11-29 19:21:25 +0000
commitf8726d781a971752a6918a20eea10cddca2114ed (patch)
treea790c011f73be0896e6e09ec2bd63a1a4289d0fb /crates/completion
parent68910d2f341ddd7635265c3847712cc7e44ef75e (diff)
parent474ebd60d1ae1057165e189bb539ba6a410e1d68 (diff)
Merge #6601
6601: add let and letm postfix to turn expressions into variables r=matklad a=bnjjj Partially resolve #6426 Co-authored-by: Benjamin Coenen <[email protected]>
Diffstat (limited to 'crates/completion')
-rw-r--r--crates/completion/src/completions/postfix.rs59
-rw-r--r--crates/completion/src/lib.rs2
2 files changed, 61 insertions, 0 deletions
diff --git a/crates/completion/src/completions/postfix.rs b/crates/completion/src/completions/postfix.rs
index 7fbda7a6b..1785794cc 100644
--- a/crates/completion/src/completions/postfix.rs
+++ b/crates/completion/src/completions/postfix.rs
@@ -5,6 +5,7 @@ mod format_like;
5use ide_db::ty_filter::TryEnum; 5use ide_db::ty_filter::TryEnum;
6use syntax::{ 6use syntax::{
7 ast::{self, AstNode, AstToken}, 7 ast::{self, AstNode, AstToken},
8 SyntaxKind::BLOCK_EXPR,
8 TextRange, TextSize, 9 TextRange, TextSize,
9}; 10};
10use text_edit::TextEdit; 11use text_edit::TextEdit;
@@ -220,6 +221,30 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
220 ) 221 )
221 .add_to(acc); 222 .add_to(acc);
222 223
224 let parent_node = dot_receiver.syntax().parent().and_then(|p| p.parent());
225 if let Some(parent) = parent_node {
226 if parent.kind() == BLOCK_EXPR {
227 postfix_snippet(
228 ctx,
229 cap,
230 &dot_receiver,
231 "let",
232 "let",
233 &format!("let $0 = {};", receiver_text),
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 }
247
223 if let ast::Expr::Literal(literal) = dot_receiver.clone() { 248 if let ast::Expr::Literal(literal) = dot_receiver.clone() {
224 if let Some(literal_text) = ast::String::cast(literal.token()) { 249 if let Some(literal_text) = ast::String::cast(literal.token()) {
225 add_format_like_completions(acc, ctx, &dot_receiver, cap, &literal_text); 250 add_format_like_completions(acc, ctx, &dot_receiver, cap, &literal_text);
@@ -296,6 +321,38 @@ fn main() {
296 sn dbg dbg!(expr) 321 sn dbg dbg!(expr)
297 sn dbgr dbg!(&expr) 322 sn dbgr dbg!(&expr)
298 sn if if expr {} 323 sn if if expr {}
324 sn let let
325 sn letm let mut
326 sn match match expr {}
327 sn not !expr
328 sn ok Ok(expr)
329 sn ref &expr
330 sn refm &mut expr
331 sn some Some(expr)
332 sn while while expr {}
333 "#]],
334 );
335 }
336
337 #[test]
338 fn postfix_completion_works_for_function_calln() {
339 check(
340 r#"
341fn foo(elt: bool) -> bool {
342 !elt
343}
344
345fn 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 {}
299 sn match match expr {} 356 sn match match expr {}
300 sn not !expr 357 sn not !expr
301 sn ok Ok(expr) 358 sn ok Ok(expr)
@@ -321,6 +378,8 @@ fn main() {
321 sn call function(expr) 378 sn call function(expr)
322 sn dbg dbg!(expr) 379 sn dbg dbg!(expr)
323 sn dbgr dbg!(&expr) 380 sn dbgr dbg!(&expr)
381 sn let let
382 sn letm let mut
324 sn match match expr {} 383 sn match match expr {}
325 sn ok Ok(expr) 384 sn ok Ok(expr)
326 sn ref &expr 385 sn ref &expr
diff --git a/crates/completion/src/lib.rs b/crates/completion/src/lib.rs
index aecc1378b..1ec2e9be7 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)`