Skip to content

Commit c09f032

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

File tree

1 file changed

+76
-4
lines changed

1 file changed

+76
-4
lines changed

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

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ use ide_db::{
1111
text_edit::TextEdit,
1212
ty_filter::TryEnum,
1313
};
14+
use itertools::Itertools as _;
1415
use stdx::never;
1516
use syntax::{
1617
SyntaxKind::{EXPR_STMT, STMT_LIST},
17-
T, TextRange, TextSize,
18+
T, TextRange, TextSize, ToSmolStr as _,
1819
ast::{self, AstNode, AstToken},
1920
match_ast,
2021
};
@@ -360,10 +361,17 @@ fn include_references(initial_element: &ast::Expr) -> (ast::Expr, String) {
360361
resulting_element.syntax().parent().and_then(ast::RefExpr::cast)
361362
{
362363
found_ref_or_deref = true;
363-
let exclusive = parent_ref_element.mut_token().is_some();
364+
prefix.insert_str(
365+
0,
366+
parent_ref_element
367+
.syntax()
368+
.children_with_tokens()
369+
.filter(|it| Some(it) != parent_ref_element.syntax().last_child_or_token().as_ref())
370+
.format("")
371+
.to_smolstr()
372+
.as_str(),
373+
);
364374
resulting_element = ast::Expr::from(parent_ref_element);
365-
366-
prefix.insert_str(0, if exclusive { "&mut " } else { "&" });
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)