Skip to content

Commit dddc6da

Browse files
make postfix completion handle all references correctly
1 parent f3cbd0e commit dddc6da

File tree

1 file changed

+75
-3
lines changed

1 file changed

+75
-3
lines changed

crates/ide-completion/src/completions/postfix.rs

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,18 @@ fn include_references(initial_element: &ast::Expr) -> (ast::Expr, String) {
360360
resulting_element.syntax().parent().and_then(ast::RefExpr::cast)
361361
{
362362
found_ref_or_deref = true;
363-
let exclusive = parent_ref_element.mut_token().is_some();
363+
let is_mut = parent_ref_element.mut_token().is_some();
364+
let is_raw = parent_ref_element.raw_token().is_some();
364365
resulting_element = ast::Expr::from(parent_ref_element);
365-
366-
prefix.insert_str(0, if exclusive { "&mut " } else { "&" });
366+
prefix.insert_str(
367+
0,
368+
match (is_raw, is_mut) {
369+
(true, true) => "&raw mut ",
370+
(true, false) => "&raw const ",
371+
(false, true) => "&mut ",
372+
(false, false) => "&",
373+
},
374+
);
367375
}
368376

369377
if !found_ref_or_deref {
@@ -1005,6 +1013,20 @@ fn main() {
10051013
r#"fn main() { Ok(&&42) }"#,
10061014
);
10071015

1016+
check_edit_with_config(
1017+
CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG },
1018+
"ok",
1019+
r#"fn main() { &raw mut 42.$0 }"#,
1020+
r#"fn main() { Ok(&raw mut 42) }"#,
1021+
);
1022+
1023+
check_edit_with_config(
1024+
CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG },
1025+
"ok",
1026+
r#"fn main() { &raw const 42.$0 }"#,
1027+
r#"fn main() { Ok(&raw const 42) }"#,
1028+
);
1029+
10081030
check_edit_with_config(
10091031
CompletionConfig { snippets: vec![snippet], ..TEST_CONFIG },
10101032
"ok",
@@ -1031,6 +1053,56 @@ fn main() {
10311053
);
10321054
}
10331055

1056+
#[test]
1057+
fn postfix_custom_snippets_completion_for_row_references() {
1058+
// https://github.com/rust-lang/rust-analyzer/issues/21035
1059+
1060+
let snippet = Snippet::new(
1061+
&[],
1062+
&["group".into()],
1063+
&["(${receiver})".into()],
1064+
"",
1065+
&[],
1066+
crate::SnippetScope::Expr,
1067+
)
1068+
.unwrap();
1069+
1070+
check_edit_with_config(
1071+
CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG },
1072+
"group",
1073+
r#"fn main() { &42.g$0 }"#,
1074+
r#"fn main() { (&42) }"#,
1075+
);
1076+
1077+
check_edit_with_config(
1078+
CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG },
1079+
"group",
1080+
r#"fn main() { &&42.$0 }"#,
1081+
r#"fn main() { (&&42) }"#,
1082+
);
1083+
1084+
check_edit_with_config(
1085+
CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG },
1086+
"group",
1087+
r#"fn main() { &mut 42.$0 }"#,
1088+
r#"fn main() { (&mut 42) }"#,
1089+
);
1090+
1091+
check_edit_with_config(
1092+
CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG },
1093+
"group",
1094+
r#"fn main() { &raw mut 42.$0 }"#,
1095+
r#"fn main() { (&raw mut 42) }"#,
1096+
);
1097+
1098+
check_edit_with_config(
1099+
CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG },
1100+
"group",
1101+
r#"fn main() { &raw const 42.$0 }"#,
1102+
r#"fn main() { (&raw const 42) }"#,
1103+
);
1104+
}
1105+
10341106
#[test]
10351107
fn no_postfix_completions_in_if_block_that_has_an_else() {
10361108
check(

0 commit comments

Comments
 (0)