Skip to content

Commit 16c0873

Browse files
committed
Auto merge of rust-lang#17081 - davidbarsky:david/revert-17073, r=Veykril
Revert rust-lang#17073: Better inline preview for postfix completion See discussion on rust-lang/rust-analyzer#17077, but I strongly suspect that the changes to the `TextEdit` ranges caused VS Code's autocomplete to prefer the snippets over method completions. I explain why I think that [here](rust-lang/rust-analyzer#17077 (comment)).
2 parents d21a978 + 082bf07 commit 16c0873

File tree

3 files changed

+47
-70
lines changed

3 files changed

+47
-70
lines changed

Diff for: src/tools/rust-analyzer/crates/ide-completion/src/completions/dot.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ fn main() {
10351035
fn postfix_drop_completion() {
10361036
cov_mark::check!(postfix_drop_completion);
10371037
check_edit(
1038-
"x.drop",
1038+
"drop",
10391039
r#"
10401040
//- minicore: drop
10411041
struct Vec<T>(T);

Diff for: src/tools/rust-analyzer/crates/ide-completion/src/completions/postfix.rs

+42-64
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use ide_db::{
1212
use stdx::never;
1313
use syntax::{
1414
ast::{self, make, AstNode, AstToken},
15-
format_smolstr,
1615
SyntaxKind::{BLOCK_EXPR, EXPR_STMT, FOR_EXPR, IF_EXPR, LOOP_EXPR, STMT_LIST, WHILE_EXPR},
1716
TextRange, TextSize,
1817
};
@@ -56,11 +55,10 @@ pub(crate) fn complete_postfix(
5655
None => return,
5756
};
5857

59-
let postfix_snippet =
60-
match build_postfix_snippet_builder(ctx, cap, dot_receiver, &receiver_text) {
61-
Some(it) => it,
62-
None => return,
63-
};
58+
let postfix_snippet = match build_postfix_snippet_builder(ctx, cap, dot_receiver) {
59+
Some(it) => it,
60+
None => return,
61+
};
6462

6563
if let Some(drop_trait) = ctx.famous_defs().core_ops_Drop() {
6664
if receiver_ty.impls_trait(ctx.db, drop_trait, &[]) {
@@ -175,11 +173,10 @@ pub(crate) fn complete_postfix(
175173
let (dot_receiver, node_to_replace_with) = include_references(dot_receiver);
176174
let receiver_text =
177175
get_receiver_text(&node_to_replace_with, receiver_is_ambiguous_float_literal);
178-
let postfix_snippet =
179-
match build_postfix_snippet_builder(ctx, cap, &dot_receiver, &receiver_text) {
180-
Some(it) => it,
181-
None => return,
182-
};
176+
let postfix_snippet = match build_postfix_snippet_builder(ctx, cap, &dot_receiver) {
177+
Some(it) => it,
178+
None => return,
179+
};
183180

184181
if !ctx.config.snippets.is_empty() {
185182
add_custom_postfix_completions(acc, ctx, &postfix_snippet, &receiver_text);
@@ -320,7 +317,6 @@ fn build_postfix_snippet_builder<'ctx>(
320317
ctx: &'ctx CompletionContext<'_>,
321318
cap: SnippetCap,
322319
receiver: &'ctx ast::Expr,
323-
receiver_text: &'ctx str,
324320
) -> Option<impl Fn(&str, &str, &str) -> Builder + 'ctx> {
325321
let receiver_range = ctx.sema.original_range_opt(receiver.syntax())?.range;
326322
if ctx.source_range().end() < receiver_range.start() {
@@ -336,16 +332,13 @@ fn build_postfix_snippet_builder<'ctx>(
336332
fn build<'ctx>(
337333
ctx: &'ctx CompletionContext<'_>,
338334
cap: SnippetCap,
339-
receiver_text: &'ctx str,
340335
delete_range: TextRange,
341336
) -> impl Fn(&str, &str, &str) -> Builder + 'ctx {
342337
move |label, detail, snippet| {
343338
let edit = TextEdit::replace(delete_range, snippet.to_owned());
344-
let mut item = CompletionItem::new(CompletionItemKind::Snippet, delete_range, label);
339+
let mut item =
340+
CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), label);
345341
item.detail(detail).snippet_edit(cap, edit);
346-
// Editors may filter completion item with the text within delete_range, so we need to
347-
// include the receiver text in the lookup for editors to find the completion item.
348-
item.lookup_by(format_smolstr!("{}.{}", receiver_text, label));
349342
let postfix_match = if ctx.original_token.text() == label {
350343
cov_mark::hit!(postfix_exact_match_is_high_priority);
351344
Some(CompletionRelevancePostfixMatch::Exact)
@@ -358,7 +351,7 @@ fn build_postfix_snippet_builder<'ctx>(
358351
item
359352
}
360353
}
361-
Some(build(ctx, cap, receiver_text, delete_range))
354+
Some(build(ctx, cap, delete_range))
362355
}
363356

364357
fn add_custom_postfix_completions(
@@ -519,7 +512,7 @@ fn main() {
519512
#[test]
520513
fn option_iflet() {
521514
check_edit(
522-
"bar.ifl",
515+
"ifl",
523516
r#"
524517
//- minicore: option
525518
fn main() {
@@ -541,7 +534,7 @@ fn main() {
541534
#[test]
542535
fn option_letelse() {
543536
check_edit(
544-
"bar.lete",
537+
"lete",
545538
r#"
546539
//- minicore: option
547540
fn main() {
@@ -564,7 +557,7 @@ $0
564557
#[test]
565558
fn result_match() {
566559
check_edit(
567-
"bar.match",
560+
"match",
568561
r#"
569562
//- minicore: result
570563
fn main() {
@@ -586,13 +579,13 @@ fn main() {
586579

587580
#[test]
588581
fn postfix_completion_works_for_ambiguous_float_literal() {
589-
check_edit("42.refm", r#"fn main() { 42.$0 }"#, r#"fn main() { &mut 42 }"#)
582+
check_edit("refm", r#"fn main() { 42.$0 }"#, r#"fn main() { &mut 42 }"#)
590583
}
591584

592585
#[test]
593586
fn works_in_simple_macro() {
594587
check_edit(
595-
"bar.dbg",
588+
"dbg",
596589
r#"
597590
macro_rules! m { ($e:expr) => { $e } }
598591
fn main() {
@@ -612,10 +605,10 @@ fn main() {
612605

613606
#[test]
614607
fn postfix_completion_for_references() {
615-
check_edit("&&42.dbg", r#"fn main() { &&42.$0 }"#, r#"fn main() { dbg!(&&42) }"#);
616-
check_edit("42.refm", r#"fn main() { &&42.$0 }"#, r#"fn main() { &&&mut 42 }"#);
608+
check_edit("dbg", r#"fn main() { &&42.$0 }"#, r#"fn main() { dbg!(&&42) }"#);
609+
check_edit("refm", r#"fn main() { &&42.$0 }"#, r#"fn main() { &&&mut 42 }"#);
617610
check_edit(
618-
"bar.ifl",
611+
"ifl",
619612
r#"
620613
//- minicore: option
621614
fn main() {
@@ -636,39 +629,35 @@ fn main() {
636629

637630
#[test]
638631
fn postfix_completion_for_unsafe() {
639-
check_edit("foo.unsafe", r#"fn main() { foo.$0 }"#, r#"fn main() { unsafe { foo } }"#);
640-
check_edit(
641-
"{ foo }.unsafe",
642-
r#"fn main() { { foo }.$0 }"#,
643-
r#"fn main() { unsafe { foo } }"#,
644-
);
632+
check_edit("unsafe", r#"fn main() { foo.$0 }"#, r#"fn main() { unsafe { foo } }"#);
633+
check_edit("unsafe", r#"fn main() { { foo }.$0 }"#, r#"fn main() { unsafe { foo } }"#);
645634
check_edit(
646-
"if x { foo }.unsafe",
635+
"unsafe",
647636
r#"fn main() { if x { foo }.$0 }"#,
648637
r#"fn main() { unsafe { if x { foo } } }"#,
649638
);
650639
check_edit(
651-
"loop { foo }.unsafe",
640+
"unsafe",
652641
r#"fn main() { loop { foo }.$0 }"#,
653642
r#"fn main() { unsafe { loop { foo } } }"#,
654643
);
655644
check_edit(
656-
"if true {}.unsafe",
645+
"unsafe",
657646
r#"fn main() { if true {}.$0 }"#,
658647
r#"fn main() { unsafe { if true {} } }"#,
659648
);
660649
check_edit(
661-
"while true {}.unsafe",
650+
"unsafe",
662651
r#"fn main() { while true {}.$0 }"#,
663652
r#"fn main() { unsafe { while true {} } }"#,
664653
);
665654
check_edit(
666-
"for i in 0..10 {}.unsafe",
655+
"unsafe",
667656
r#"fn main() { for i in 0..10 {}.$0 }"#,
668657
r#"fn main() { unsafe { for i in 0..10 {} } }"#,
669658
);
670659
check_edit(
671-
"if true {1} else {2}.unsafe",
660+
"unsafe",
672661
r#"fn main() { let x = if true {1} else {2}.$0 }"#,
673662
r#"fn main() { let x = unsafe { if true {1} else {2} } }"#,
674663
);
@@ -698,7 +687,7 @@ fn main() {
698687

699688
check_edit_with_config(
700689
config.clone(),
701-
"42.break",
690+
"break",
702691
r#"
703692
//- minicore: try
704693
fn main() { 42.$0 }
@@ -717,7 +706,7 @@ fn main() { ControlFlow::Break(42) }
717706
// what users would see. Unescaping happens thereafter.
718707
check_edit_with_config(
719708
config.clone(),
720-
r#"'\\\\'.break"#,
709+
"break",
721710
r#"
722711
//- minicore: try
723712
fn main() { '\\'.$0 }
@@ -731,10 +720,7 @@ fn main() { ControlFlow::Break('\\\\') }
731720

732721
check_edit_with_config(
733722
config,
734-
r#"match true {
735-
true => "\${1:placeholder}",
736-
false => "\\\$",
737-
}.break"#,
723+
"break",
738724
r#"
739725
//- minicore: try
740726
fn main() {
@@ -760,47 +746,39 @@ fn main() {
760746
#[test]
761747
fn postfix_completion_for_format_like_strings() {
762748
check_edit(
763-
r#""{some_var:?}".format"#,
749+
"format",
764750
r#"fn main() { "{some_var:?}".$0 }"#,
765751
r#"fn main() { format!("{some_var:?}") }"#,
766752
);
767753
check_edit(
768-
r#""Panic with {a}".panic"#,
754+
"panic",
769755
r#"fn main() { "Panic with {a}".$0 }"#,
770756
r#"fn main() { panic!("Panic with {a}") }"#,
771757
);
772758
check_edit(
773-
r#""{ 2+2 } { SomeStruct { val: 1, other: 32 } :?}".println"#,
759+
"println",
774760
r#"fn main() { "{ 2+2 } { SomeStruct { val: 1, other: 32 } :?}".$0 }"#,
775761
r#"fn main() { println!("{} {:?}", 2+2, SomeStruct { val: 1, other: 32 }) }"#,
776762
);
777763
check_edit(
778-
r#""{2+2}".loge"#,
764+
"loge",
779765
r#"fn main() { "{2+2}".$0 }"#,
780766
r#"fn main() { log::error!("{}", 2+2) }"#,
781767
);
782768
check_edit(
783-
r#""{2+2}".logt"#,
769+
"logt",
784770
r#"fn main() { "{2+2}".$0 }"#,
785771
r#"fn main() { log::trace!("{}", 2+2) }"#,
786772
);
787773
check_edit(
788-
r#""{2+2}".logd"#,
774+
"logd",
789775
r#"fn main() { "{2+2}".$0 }"#,
790776
r#"fn main() { log::debug!("{}", 2+2) }"#,
791777
);
778+
check_edit("logi", r#"fn main() { "{2+2}".$0 }"#, r#"fn main() { log::info!("{}", 2+2) }"#);
779+
check_edit("logw", r#"fn main() { "{2+2}".$0 }"#, r#"fn main() { log::warn!("{}", 2+2) }"#);
792780
check_edit(
793-
r#""{2+2}".logi"#,
794-
r#"fn main() { "{2+2}".$0 }"#,
795-
r#"fn main() { log::info!("{}", 2+2) }"#,
796-
);
797-
check_edit(
798-
r#""{2+2}".logw"#,
799-
r#"fn main() { "{2+2}".$0 }"#,
800-
r#"fn main() { log::warn!("{}", 2+2) }"#,
801-
);
802-
check_edit(
803-
r#""{2+2}".loge"#,
781+
"loge",
804782
r#"fn main() { "{2+2}".$0 }"#,
805783
r#"fn main() { log::error!("{}", 2+2) }"#,
806784
);
@@ -822,21 +800,21 @@ fn main() {
822800

823801
check_edit_with_config(
824802
CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG },
825-
"&&42.ok",
803+
"ok",
826804
r#"fn main() { &&42.o$0 }"#,
827805
r#"fn main() { Ok(&&42) }"#,
828806
);
829807

830808
check_edit_with_config(
831809
CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG },
832-
"&&42.ok",
810+
"ok",
833811
r#"fn main() { &&42.$0 }"#,
834812
r#"fn main() { Ok(&&42) }"#,
835813
);
836814

837815
check_edit_with_config(
838816
CompletionConfig { snippets: vec![snippet], ..TEST_CONFIG },
839-
"&a.a.ok",
817+
"ok",
840818
r#"
841819
struct A {
842820
a: i32,

Diff for: src/tools/rust-analyzer/crates/ide-completion/src/completions/postfix/format_like.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,10 @@ pub(crate) fn add_format_like_completions(
4848
cap: SnippetCap,
4949
receiver_text: &ast::String,
5050
) {
51-
let postfix_snippet =
52-
match build_postfix_snippet_builder(ctx, cap, dot_receiver, receiver_text.text()) {
53-
Some(it) => it,
54-
None => return,
55-
};
51+
let postfix_snippet = match build_postfix_snippet_builder(ctx, cap, dot_receiver) {
52+
Some(it) => it,
53+
None => return,
54+
};
5655

5756
if let Ok((mut out, mut exprs)) = parse_format_exprs(receiver_text.text()) {
5857
// Escape any snippet bits in the out text and any of the exprs.

0 commit comments

Comments
 (0)