Skip to content

Commit 76a8a97

Browse files
committed
fix: better inline preview for postfix completion
1 parent 2cbc284 commit 76a8a97

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

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

+18-11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use ide_db::{
1212
use stdx::never;
1313
use syntax::{
1414
ast::{self, make, AstNode, AstToken},
15+
format_smolstr,
1516
SyntaxKind::{BLOCK_EXPR, EXPR_STMT, FOR_EXPR, IF_EXPR, LOOP_EXPR, STMT_LIST, WHILE_EXPR},
1617
TextRange, TextSize,
1718
};
@@ -55,10 +56,11 @@ pub(crate) fn complete_postfix(
5556
None => return,
5657
};
5758

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

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

181184
if !ctx.config.snippets.is_empty() {
182185
add_custom_postfix_completions(acc, ctx, &postfix_snippet, &receiver_text);
@@ -317,6 +320,7 @@ fn build_postfix_snippet_builder<'ctx>(
317320
ctx: &'ctx CompletionContext<'_>,
318321
cap: SnippetCap,
319322
receiver: &'ctx ast::Expr,
323+
receiver_text: &'ctx str,
320324
) -> Option<impl Fn(&str, &str, &str) -> Builder + 'ctx> {
321325
let receiver_range = ctx.sema.original_range_opt(receiver.syntax())?.range;
322326
if ctx.source_range().end() < receiver_range.start() {
@@ -332,13 +336,16 @@ fn build_postfix_snippet_builder<'ctx>(
332336
fn build<'ctx>(
333337
ctx: &'ctx CompletionContext<'_>,
334338
cap: SnippetCap,
339+
receiver_text: &'ctx str,
335340
delete_range: TextRange,
336341
) -> impl Fn(&str, &str, &str) -> Builder + 'ctx {
337342
move |label, detail, snippet| {
338343
let edit = TextEdit::replace(delete_range, snippet.to_owned());
339-
let mut item =
340-
CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), label);
344+
let mut item = CompletionItem::new(CompletionItemKind::Snippet, delete_range, label);
341345
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));
342349
let postfix_match = if ctx.original_token.text() == label {
343350
cov_mark::hit!(postfix_exact_match_is_high_priority);
344351
Some(CompletionRelevancePostfixMatch::Exact)
@@ -351,7 +358,7 @@ fn build_postfix_snippet_builder<'ctx>(
351358
item
352359
}
353360
}
354-
Some(build(ctx, cap, delete_range))
361+
Some(build(ctx, cap, receiver_text, delete_range))
355362
}
356363

357364
fn add_custom_postfix_completions(

0 commit comments

Comments
 (0)