Skip to content

Commit 6f5909e

Browse files
make postfix completion handle all references correctly
1 parent c904df0 commit 6f5909e

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;
1415
use stdx::never;
1516
use syntax::{
1617
SyntaxKind::{EXPR_STMT, STMT_LIST},
17-
T, TextRange, TextSize,
18+
T, TextRange, TextSize, ToSmolStr,
1819
ast::{self, AstNode, AstToken},
1920
match_ast,
2021
};
@@ -360,10 +361,18 @@ 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+
let last_child_or_token = parent_ref_element.syntax().last_child_or_token();
365+
prefix.insert_str(
366+
0,
367+
parent_ref_element
368+
.syntax()
369+
.children_with_tokens()
370+
.filter(|it| Some(it) != last_child_or_token.as_ref())
371+
.format("")
372+
.to_smolstr()
373+
.as_str(),
374+
);
364375
resulting_element = ast::Expr::from(parent_ref_element);
365-
366-
prefix.insert_str(0, if exclusive { "&mut " } else { "&" });
367376
}
368377

369378
if !found_ref_or_deref {
@@ -1005,6 +1014,20 @@ fn main() {
10051014
r#"fn main() { Ok(&&42) }"#,
10061015
);
10071016

1017+
check_edit_with_config(
1018+
CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG },
1019+
"ok",
1020+
r#"fn main() { &raw mut 42.$0 }"#,
1021+
r#"fn main() { Ok(&raw mut 42) }"#,
1022+
);
1023+
1024+
check_edit_with_config(
1025+
CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG },
1026+
"ok",
1027+
r#"fn main() { &raw const 42.$0 }"#,
1028+
r#"fn main() { Ok(&raw const 42) }"#,
1029+
);
1030+
10081031
check_edit_with_config(
10091032
CompletionConfig { snippets: vec![snippet], ..TEST_CONFIG },
10101033
"ok",
@@ -1031,6 +1054,55 @@ fn main() {
10311054
);
10321055
}
10331056

1057+
#[test]
1058+
fn postfix_custom_snippets_completion_for_reference_expr() {
1059+
// https://github.com/rust-lang/rust-analyzer/issues/21035
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() { &[1, 2, 3].g$0 }"#,
1074+
r#"fn main() { (&[1, 2, 3]) }"#,
1075+
);
1076+
1077+
check_edit_with_config(
1078+
CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG },
1079+
"group",
1080+
r#"fn main() { &&foo(a, b, 1+1).$0 }"#,
1081+
r#"fn main() { (&&foo(a, b, 1+1)) }"#,
1082+
);
1083+
1084+
check_edit_with_config(
1085+
CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG },
1086+
"group",
1087+
r#"fn main() { &mut Foo { a: 1, b: 2, c: 3 }.$0 }"#,
1088+
r#"fn main() { (&mut Foo { a: 1, b: 2, c: 3 }) }"#,
1089+
);
1090+
1091+
check_edit_with_config(
1092+
CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG },
1093+
"group",
1094+
r#"fn main() { &raw mut Foo::new().$0 }"#,
1095+
r#"fn main() { (&raw mut Foo::new()) }"#,
1096+
);
1097+
1098+
check_edit_with_config(
1099+
CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG },
1100+
"group",
1101+
r#"fn main() { &raw const Foo::bar::SOME_CONST.$0 }"#,
1102+
r#"fn main() { (&raw const Foo::bar::SOME_CONST) }"#,
1103+
);
1104+
}
1105+
10341106
#[test]
10351107
fn no_postfix_completions_in_if_block_that_has_an_else() {
10361108
check(

0 commit comments

Comments
 (0)