diff --git a/crates/ide_completion/src/completions/postfix.rs b/crates/ide_completion/src/completions/postfix.rs index ead957a3f1a7..45ae590405ec 100644 --- a/crates/ide_completion/src/completions/postfix.rs +++ b/crates/ide_completion/src/completions/postfix.rs @@ -240,12 +240,12 @@ fn build_postfix_snippet_builder<'ctx>( let mut item = CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), label); item.detail(detail).snippet_edit(cap, edit); - if ctx.original_token.text() == label { - let relevance = - CompletionRelevance { exact_postfix_snippet_match: true, ..Default::default() }; - item.set_relevance(relevance); - } - + let relevance = if ctx.original_token.text() == label { + CompletionRelevance { exact_postfix_snippet_match: true, ..Default::default() } + } else { + CompletionRelevance { is_postfix: true, ..Default::default() } + }; + item.set_relevance(relevance); item } } diff --git a/crates/ide_completion/src/item.rs b/crates/ide_completion/src/item.rs index 4b9f7d17c311..e86a1af8ae00 100644 --- a/crates/ide_completion/src/item.rs +++ b/crates/ide_completion/src/item.rs @@ -152,6 +152,8 @@ pub struct CompletionRelevance { /// Basically, we want to guarantee that postfix snippets always takes /// precedence over everything else. pub exact_postfix_snippet_match: bool, + /// Set in cases when item is postfix, but not exact + pub is_postfix: bool, } #[derive(Debug, Clone, Copy, Eq, PartialEq)] @@ -179,7 +181,7 @@ pub enum CompletionRelevanceTypeMatch { } impl CompletionRelevance { - const BASE_LINE: u32 = 2; + const BASE_LINE: u32 = 3; /// Provides a relevance score. Higher values are more relevant. /// /// The absolute value of the relevance score is not meaningful, for @@ -199,6 +201,9 @@ impl CompletionRelevance { if self.is_private_editable { score -= 1; } + if self.is_postfix { + score -= 3; + } // score increases if self.exact_name_match { @@ -215,6 +220,7 @@ impl CompletionRelevance { if self.exact_postfix_snippet_match { score += 100; } + score } @@ -573,6 +579,10 @@ mod tests { // This test asserts that the relevance score for these items is ascending, and // that any items in the same vec have the same score. let expected_relevance_order = vec![ + vec![CompletionRelevance { + is_postfix: true, + ..CompletionRelevance::default() + }], vec![CompletionRelevance { is_op_method: true, is_private_editable: true, diff --git a/crates/ide_completion/src/render.rs b/crates/ide_completion/src/render.rs index 604f21744718..652cd52f808f 100644 --- a/crates/ide_completion/src/render.rs +++ b/crates/ide_completion/src/render.rs @@ -615,6 +615,7 @@ fn main() { let _: m::Spam = S$0 } is_op_method: false, is_private_editable: false, exact_postfix_snippet_match: false, + is_postfix: false, }, }, CompletionItem { @@ -636,6 +637,7 @@ fn main() { let _: m::Spam = S$0 } is_op_method: false, is_private_editable: false, exact_postfix_snippet_match: false, + is_postfix: false, }, }, ] @@ -723,6 +725,7 @@ fn foo() { A { the$0 } } is_op_method: false, is_private_editable: false, exact_postfix_snippet_match: false, + is_postfix: false, }, }, ]