Revert "Auto merge of #17073 - roife:better-inline-preview, r=Veykril"

af728741de6ad74517f34053efae864d7d75ac5d
This commit is contained in:
David Barsky 2024-04-15 18:24:15 -04:00
parent 90cfa8035f
commit ab8c112573
3 changed files with 47 additions and 70 deletions

View File

@ -1035,7 +1035,7 @@ fn main() {
fn postfix_drop_completion() { fn postfix_drop_completion() {
cov_mark::check!(postfix_drop_completion); cov_mark::check!(postfix_drop_completion);
check_edit( check_edit(
"x.drop", "drop",
r#" r#"
//- minicore: drop //- minicore: drop
struct Vec<T>(T); struct Vec<T>(T);

View File

@ -12,7 +12,6 @@ use ide_db::{
use stdx::never; use stdx::never;
use syntax::{ use syntax::{
ast::{self, make, AstNode, AstToken}, ast::{self, make, AstNode, AstToken},
format_smolstr,
SyntaxKind::{BLOCK_EXPR, EXPR_STMT, FOR_EXPR, IF_EXPR, LOOP_EXPR, STMT_LIST, WHILE_EXPR}, SyntaxKind::{BLOCK_EXPR, EXPR_STMT, FOR_EXPR, IF_EXPR, LOOP_EXPR, STMT_LIST, WHILE_EXPR},
TextRange, TextSize, TextRange, TextSize,
}; };
@ -56,8 +55,7 @@ pub(crate) fn complete_postfix(
None => return, None => return,
}; };
let postfix_snippet = let postfix_snippet = match build_postfix_snippet_builder(ctx, cap, dot_receiver) {
match build_postfix_snippet_builder(ctx, cap, dot_receiver, &receiver_text) {
Some(it) => it, Some(it) => it,
None => return, None => return,
}; };
@ -175,8 +173,7 @@ pub(crate) fn complete_postfix(
let (dot_receiver, node_to_replace_with) = include_references(dot_receiver); let (dot_receiver, node_to_replace_with) = include_references(dot_receiver);
let receiver_text = let receiver_text =
get_receiver_text(&node_to_replace_with, receiver_is_ambiguous_float_literal); get_receiver_text(&node_to_replace_with, receiver_is_ambiguous_float_literal);
let postfix_snippet = let postfix_snippet = match build_postfix_snippet_builder(ctx, cap, &dot_receiver) {
match build_postfix_snippet_builder(ctx, cap, &dot_receiver, &receiver_text) {
Some(it) => it, Some(it) => it,
None => return, None => return,
}; };
@ -320,7 +317,6 @@ fn build_postfix_snippet_builder<'ctx>(
ctx: &'ctx CompletionContext<'_>, ctx: &'ctx CompletionContext<'_>,
cap: SnippetCap, cap: SnippetCap,
receiver: &'ctx ast::Expr, receiver: &'ctx ast::Expr,
receiver_text: &'ctx str,
) -> Option<impl Fn(&str, &str, &str) -> Builder + 'ctx> { ) -> Option<impl Fn(&str, &str, &str) -> Builder + 'ctx> {
let receiver_range = ctx.sema.original_range_opt(receiver.syntax())?.range; let receiver_range = ctx.sema.original_range_opt(receiver.syntax())?.range;
if ctx.source_range().end() < receiver_range.start() { if ctx.source_range().end() < receiver_range.start() {
@ -336,16 +332,13 @@ fn build_postfix_snippet_builder<'ctx>(
fn build<'ctx>( fn build<'ctx>(
ctx: &'ctx CompletionContext<'_>, ctx: &'ctx CompletionContext<'_>,
cap: SnippetCap, cap: SnippetCap,
receiver_text: &'ctx str,
delete_range: TextRange, delete_range: TextRange,
) -> impl Fn(&str, &str, &str) -> Builder + 'ctx { ) -> impl Fn(&str, &str, &str) -> Builder + 'ctx {
move |label, detail, snippet| { move |label, detail, snippet| {
let edit = TextEdit::replace(delete_range, snippet.to_owned()); let edit = TextEdit::replace(delete_range, snippet.to_owned());
let mut item = CompletionItem::new(CompletionItemKind::Snippet, delete_range, label); let mut item =
CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), label);
item.detail(detail).snippet_edit(cap, edit); item.detail(detail).snippet_edit(cap, edit);
// Editors may filter completion item with the text within delete_range, so we need to
// include the receiver text in the lookup for editors to find the completion item.
item.lookup_by(format_smolstr!("{}.{}", receiver_text, label));
let postfix_match = if ctx.original_token.text() == label { let postfix_match = if ctx.original_token.text() == label {
cov_mark::hit!(postfix_exact_match_is_high_priority); cov_mark::hit!(postfix_exact_match_is_high_priority);
Some(CompletionRelevancePostfixMatch::Exact) Some(CompletionRelevancePostfixMatch::Exact)
@ -358,7 +351,7 @@ fn build_postfix_snippet_builder<'ctx>(
item item
} }
} }
Some(build(ctx, cap, receiver_text, delete_range)) Some(build(ctx, cap, delete_range))
} }
fn add_custom_postfix_completions( fn add_custom_postfix_completions(
@ -519,7 +512,7 @@ fn main() {
#[test] #[test]
fn option_iflet() { fn option_iflet() {
check_edit( check_edit(
"bar.ifl", "ifl",
r#" r#"
//- minicore: option //- minicore: option
fn main() { fn main() {
@ -541,7 +534,7 @@ fn main() {
#[test] #[test]
fn option_letelse() { fn option_letelse() {
check_edit( check_edit(
"bar.lete", "lete",
r#" r#"
//- minicore: option //- minicore: option
fn main() { fn main() {
@ -564,7 +557,7 @@ $0
#[test] #[test]
fn result_match() { fn result_match() {
check_edit( check_edit(
"bar.match", "match",
r#" r#"
//- minicore: result //- minicore: result
fn main() { fn main() {
@ -586,13 +579,13 @@ fn main() {
#[test] #[test]
fn postfix_completion_works_for_ambiguous_float_literal() { fn postfix_completion_works_for_ambiguous_float_literal() {
check_edit("42.refm", r#"fn main() { 42.$0 }"#, r#"fn main() { &mut 42 }"#) check_edit("refm", r#"fn main() { 42.$0 }"#, r#"fn main() { &mut 42 }"#)
} }
#[test] #[test]
fn works_in_simple_macro() { fn works_in_simple_macro() {
check_edit( check_edit(
"bar.dbg", "dbg",
r#" r#"
macro_rules! m { ($e:expr) => { $e } } macro_rules! m { ($e:expr) => { $e } }
fn main() { fn main() {
@ -612,10 +605,10 @@ fn main() {
#[test] #[test]
fn postfix_completion_for_references() { fn postfix_completion_for_references() {
check_edit("&&42.dbg", r#"fn main() { &&42.$0 }"#, r#"fn main() { dbg!(&&42) }"#); check_edit("dbg", r#"fn main() { &&42.$0 }"#, r#"fn main() { dbg!(&&42) }"#);
check_edit("42.refm", r#"fn main() { &&42.$0 }"#, r#"fn main() { &&&mut 42 }"#); check_edit("refm", r#"fn main() { &&42.$0 }"#, r#"fn main() { &&&mut 42 }"#);
check_edit( check_edit(
"bar.ifl", "ifl",
r#" r#"
//- minicore: option //- minicore: option
fn main() { fn main() {
@ -636,39 +629,35 @@ fn main() {
#[test] #[test]
fn postfix_completion_for_unsafe() { fn postfix_completion_for_unsafe() {
check_edit("foo.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() { { foo }.$0 }"#, r#"fn main() { unsafe { foo } }"#);
check_edit( check_edit(
"{ foo }.unsafe", "unsafe",
r#"fn main() { { foo }.$0 }"#,
r#"fn main() { unsafe { foo } }"#,
);
check_edit(
"if x { foo }.unsafe",
r#"fn main() { if x { foo }.$0 }"#, r#"fn main() { if x { foo }.$0 }"#,
r#"fn main() { unsafe { if x { foo } } }"#, r#"fn main() { unsafe { if x { foo } } }"#,
); );
check_edit( check_edit(
"loop { foo }.unsafe", "unsafe",
r#"fn main() { loop { foo }.$0 }"#, r#"fn main() { loop { foo }.$0 }"#,
r#"fn main() { unsafe { loop { foo } } }"#, r#"fn main() { unsafe { loop { foo } } }"#,
); );
check_edit( check_edit(
"if true {}.unsafe", "unsafe",
r#"fn main() { if true {}.$0 }"#, r#"fn main() { if true {}.$0 }"#,
r#"fn main() { unsafe { if true {} } }"#, r#"fn main() { unsafe { if true {} } }"#,
); );
check_edit( check_edit(
"while true {}.unsafe", "unsafe",
r#"fn main() { while true {}.$0 }"#, r#"fn main() { while true {}.$0 }"#,
r#"fn main() { unsafe { while true {} } }"#, r#"fn main() { unsafe { while true {} } }"#,
); );
check_edit( check_edit(
"for i in 0..10 {}.unsafe", "unsafe",
r#"fn main() { for i in 0..10 {}.$0 }"#, r#"fn main() { for i in 0..10 {}.$0 }"#,
r#"fn main() { unsafe { for i in 0..10 {} } }"#, r#"fn main() { unsafe { for i in 0..10 {} } }"#,
); );
check_edit( check_edit(
"if true {1} else {2}.unsafe", "unsafe",
r#"fn main() { let x = if true {1} else {2}.$0 }"#, r#"fn main() { let x = if true {1} else {2}.$0 }"#,
r#"fn main() { let x = unsafe { if true {1} else {2} } }"#, r#"fn main() { let x = unsafe { if true {1} else {2} } }"#,
); );
@ -698,7 +687,7 @@ fn main() {
check_edit_with_config( check_edit_with_config(
config.clone(), config.clone(),
"42.break", "break",
r#" r#"
//- minicore: try //- minicore: try
fn main() { 42.$0 } fn main() { 42.$0 }
@ -717,7 +706,7 @@ fn main() { ControlFlow::Break(42) }
// what users would see. Unescaping happens thereafter. // what users would see. Unescaping happens thereafter.
check_edit_with_config( check_edit_with_config(
config.clone(), config.clone(),
r#"'\\\\'.break"#, "break",
r#" r#"
//- minicore: try //- minicore: try
fn main() { '\\'.$0 } fn main() { '\\'.$0 }
@ -731,10 +720,7 @@ fn main() { ControlFlow::Break('\\\\') }
check_edit_with_config( check_edit_with_config(
config, config,
r#"match true { "break",
true => "\${1:placeholder}",
false => "\\\$",
}.break"#,
r#" r#"
//- minicore: try //- minicore: try
fn main() { fn main() {
@ -760,47 +746,39 @@ fn main() {
#[test] #[test]
fn postfix_completion_for_format_like_strings() { fn postfix_completion_for_format_like_strings() {
check_edit( check_edit(
r#""{some_var:?}".format"#, "format",
r#"fn main() { "{some_var:?}".$0 }"#, r#"fn main() { "{some_var:?}".$0 }"#,
r#"fn main() { format!("{some_var:?}") }"#, r#"fn main() { format!("{some_var:?}") }"#,
); );
check_edit( check_edit(
r#""Panic with {a}".panic"#, "panic",
r#"fn main() { "Panic with {a}".$0 }"#, r#"fn main() { "Panic with {a}".$0 }"#,
r#"fn main() { panic!("Panic with {a}") }"#, r#"fn main() { panic!("Panic with {a}") }"#,
); );
check_edit( check_edit(
r#""{ 2+2 } { SomeStruct { val: 1, other: 32 } :?}".println"#, "println",
r#"fn main() { "{ 2+2 } { SomeStruct { val: 1, other: 32 } :?}".$0 }"#, r#"fn main() { "{ 2+2 } { SomeStruct { val: 1, other: 32 } :?}".$0 }"#,
r#"fn main() { println!("{} {:?}", 2+2, SomeStruct { val: 1, other: 32 }) }"#, r#"fn main() { println!("{} {:?}", 2+2, SomeStruct { val: 1, other: 32 }) }"#,
); );
check_edit( check_edit(
r#""{2+2}".loge"#, "loge",
r#"fn main() { "{2+2}".$0 }"#, r#"fn main() { "{2+2}".$0 }"#,
r#"fn main() { log::error!("{}", 2+2) }"#, r#"fn main() { log::error!("{}", 2+2) }"#,
); );
check_edit( check_edit(
r#""{2+2}".logt"#, "logt",
r#"fn main() { "{2+2}".$0 }"#, r#"fn main() { "{2+2}".$0 }"#,
r#"fn main() { log::trace!("{}", 2+2) }"#, r#"fn main() { log::trace!("{}", 2+2) }"#,
); );
check_edit( check_edit(
r#""{2+2}".logd"#, "logd",
r#"fn main() { "{2+2}".$0 }"#, r#"fn main() { "{2+2}".$0 }"#,
r#"fn main() { log::debug!("{}", 2+2) }"#, r#"fn main() { log::debug!("{}", 2+2) }"#,
); );
check_edit("logi", r#"fn main() { "{2+2}".$0 }"#, r#"fn main() { log::info!("{}", 2+2) }"#);
check_edit("logw", r#"fn main() { "{2+2}".$0 }"#, r#"fn main() { log::warn!("{}", 2+2) }"#);
check_edit( check_edit(
r#""{2+2}".logi"#, "loge",
r#"fn main() { "{2+2}".$0 }"#,
r#"fn main() { log::info!("{}", 2+2) }"#,
);
check_edit(
r#""{2+2}".logw"#,
r#"fn main() { "{2+2}".$0 }"#,
r#"fn main() { log::warn!("{}", 2+2) }"#,
);
check_edit(
r#""{2+2}".loge"#,
r#"fn main() { "{2+2}".$0 }"#, r#"fn main() { "{2+2}".$0 }"#,
r#"fn main() { log::error!("{}", 2+2) }"#, r#"fn main() { log::error!("{}", 2+2) }"#,
); );
@ -822,21 +800,21 @@ fn main() {
check_edit_with_config( check_edit_with_config(
CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG }, CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG },
"&&42.ok", "ok",
r#"fn main() { &&42.o$0 }"#, r#"fn main() { &&42.o$0 }"#,
r#"fn main() { Ok(&&42) }"#, r#"fn main() { Ok(&&42) }"#,
); );
check_edit_with_config( check_edit_with_config(
CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG }, CompletionConfig { snippets: vec![snippet.clone()], ..TEST_CONFIG },
"&&42.ok", "ok",
r#"fn main() { &&42.$0 }"#, r#"fn main() { &&42.$0 }"#,
r#"fn main() { Ok(&&42) }"#, r#"fn main() { Ok(&&42) }"#,
); );
check_edit_with_config( check_edit_with_config(
CompletionConfig { snippets: vec![snippet], ..TEST_CONFIG }, CompletionConfig { snippets: vec![snippet], ..TEST_CONFIG },
"&a.a.ok", "ok",
r#" r#"
struct A { struct A {
a: i32, a: i32,

View File

@ -48,8 +48,7 @@ pub(crate) fn add_format_like_completions(
cap: SnippetCap, cap: SnippetCap,
receiver_text: &ast::String, receiver_text: &ast::String,
) { ) {
let postfix_snippet = let postfix_snippet = match build_postfix_snippet_builder(ctx, cap, dot_receiver) {
match build_postfix_snippet_builder(ctx, cap, dot_receiver, receiver_text.text()) {
Some(it) => it, Some(it) => it,
None => return, None => return,
}; };