Auto merge of #14095 - dqkqd:master, r=Veykril

Add postfix completion for `unsafe`.

Fix #13779.

Hi, this is my first PR. Please tell me if there is anything I should do.
This commit is contained in:
bors 2023-02-08 09:44:22 +00:00
commit 6effb26062
4 changed files with 156 additions and 84 deletions

View file

@ -86,6 +86,7 @@ fn foo(a: A) { a.$0 }
sn match match expr {}
sn ref &expr
sn refm &mut expr
sn unsafe unsafe {}
"#]],
);
@ -110,6 +111,7 @@ fn foo() {
sn match match expr {}
sn ref &expr
sn refm &mut expr
sn unsafe unsafe {}
"#]],
);
}
@ -136,6 +138,7 @@ fn foo(a: A) { a.$0 }
sn match match expr {}
sn ref &expr
sn refm &mut expr
sn unsafe unsafe {}
"#]],
);
}

View file

@ -6,7 +6,7 @@ use hir::{Documentation, HasAttrs};
use ide_db::{imports::insert_use::ImportScope, ty_filter::TryEnum, SnippetCap};
use syntax::{
ast::{self, make, AstNode, AstToken},
SyntaxKind::{EXPR_STMT, STMT_LIST},
SyntaxKind::{BLOCK_EXPR, EXPR_STMT, FOR_EXPR, IF_EXPR, LOOP_EXPR, STMT_LIST, WHILE_EXPR},
TextRange, TextSize,
};
use text_edit::TextEdit;
@ -123,6 +123,22 @@ pub(crate) fn complete_postfix(
postfix_snippet("ref", "&expr", &format!("&{receiver_text}")).add_to(acc);
postfix_snippet("refm", "&mut expr", &format!("&mut {receiver_text}")).add_to(acc);
let mut unsafe_should_be_wrapped = true;
if dot_receiver.syntax().kind() == BLOCK_EXPR {
unsafe_should_be_wrapped = false;
if let Some(parent) = dot_receiver.syntax().parent() {
if matches!(parent.kind(), IF_EXPR | WHILE_EXPR | LOOP_EXPR | FOR_EXPR) {
unsafe_should_be_wrapped = true;
}
}
};
let unsafe_completion_string = if unsafe_should_be_wrapped {
format!("unsafe {{ {receiver_text} }}")
} else {
format!("unsafe {receiver_text}")
};
postfix_snippet("unsafe", "unsafe {}", &unsafe_completion_string).add_to(acc);
// The rest of the postfix completions create an expression that moves an argument,
// so it's better to consider references now to avoid breaking the compilation
@ -340,6 +356,7 @@ fn main() {
sn not !expr
sn ref &expr
sn refm &mut expr
sn unsafe unsafe {}
sn while while expr {}
"#]],
);
@ -368,6 +385,7 @@ fn main() {
sn not !expr
sn ref &expr
sn refm &mut expr
sn unsafe unsafe {}
sn while while expr {}
"#]],
);
@ -392,6 +410,7 @@ fn main() {
sn match match expr {}
sn ref &expr
sn refm &mut expr
sn unsafe unsafe {}
"#]],
)
}
@ -417,6 +436,7 @@ fn main() {
sn not !expr
sn ref &expr
sn refm &mut expr
sn unsafe unsafe {}
sn while while expr {}
"#]],
);
@ -517,6 +537,49 @@ fn main() {
)
}
#[test]
fn postfix_completion_for_unsafe() {
check_edit("unsafe", r#"fn main() { foo.$0 }"#, r#"fn main() { unsafe { foo } }"#);
check_edit("unsafe", r#"fn main() { { foo }.$0 }"#, r#"fn main() { unsafe { foo } }"#);
check_edit(
"unsafe",
r#"fn main() { if x { foo }.$0 }"#,
r#"fn main() { unsafe { if x { foo } } }"#,
);
check_edit(
"unsafe",
r#"fn main() { loop { foo }.$0 }"#,
r#"fn main() { unsafe { loop { foo } } }"#,
);
check_edit(
"unsafe",
r#"fn main() { if true {}.$0 }"#,
r#"fn main() { unsafe { if true {} } }"#,
);
check_edit(
"unsafe",
r#"fn main() { while true {}.$0 }"#,
r#"fn main() { unsafe { while true {} } }"#,
);
check_edit(
"unsafe",
r#"fn main() { for i in 0..10 {}.$0 }"#,
r#"fn main() { unsafe { for i in 0..10 {} } }"#,
);
check_edit(
"unsafe",
r#"fn main() { let x = if true {1} else {2}.$0 }"#,
r#"fn main() { let x = unsafe { if true {1} else {2} } }"#,
);
// completion will not be triggered
check_edit(
"unsafe",
r#"fn main() { let x = true else {panic!()}.$0}"#,
r#"fn main() { let x = true else {panic!()}.unsafe}"#,
);
}
#[test]
fn custom_postfix_completion() {
let config = CompletionConfig {

View file

@ -1691,6 +1691,7 @@ fn main() {
sn while []
sn ref []
sn refm []
sn unsafe []
sn match []
sn box []
sn dbg []
@ -1718,6 +1719,7 @@ fn main() {
me f() []
sn ref []
sn refm []
sn unsafe []
sn match []
sn box []
sn dbg []

View file

@ -34,6 +34,7 @@ fn main() {
sn match match expr {}
sn ref &expr
sn refm &mut expr
sn unsafe unsafe {}
"#]],
)
}
@ -64,6 +65,7 @@ fn main() {
sn match match expr {}
sn ref &expr
sn refm &mut expr
sn unsafe unsafe {}
"#]],
)
}
@ -96,6 +98,7 @@ fn main() {}
sn match match expr {}
sn ref &expr
sn refm &mut expr
sn unsafe unsafe {}
"#]],
)
}
@ -128,6 +131,7 @@ fn main() {}
sn match match expr {}
sn ref &expr
sn refm &mut expr
sn unsafe unsafe {}
"#]],
)
}