mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-25 19:35:06 +00:00
Merge #11857
11857: Lower postfix suggestions in completions list r=Veykril a=avrong Fixes #11850 Adds a parameter for postfix suggestions in `CompletionRelevance`, and basing on it, decreases relevance score of such items in completion list Co-authored-by: Aleksei Trifonov <avrong@avrong.me>
This commit is contained in:
commit
a9ae0b0855
5 changed files with 57 additions and 30 deletions
|
@ -12,9 +12,11 @@ use syntax::{
|
||||||
use text_edit::TextEdit;
|
use text_edit::TextEdit;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
completions::postfix::format_like::add_format_like_completions, context::CompletionContext,
|
completions::postfix::format_like::add_format_like_completions,
|
||||||
item::Builder, patterns::ImmediateLocation, CompletionItem, CompletionItemKind,
|
context::CompletionContext,
|
||||||
CompletionRelevance, Completions, SnippetScope,
|
item::{Builder, CompletionRelevancePostfixMatch},
|
||||||
|
patterns::ImmediateLocation,
|
||||||
|
CompletionItem, CompletionItemKind, CompletionRelevance, Completions, SnippetScope,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
|
@ -240,12 +242,13 @@ fn build_postfix_snippet_builder<'ctx>(
|
||||||
let mut item =
|
let mut item =
|
||||||
CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), label);
|
CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), label);
|
||||||
item.detail(detail).snippet_edit(cap, edit);
|
item.detail(detail).snippet_edit(cap, edit);
|
||||||
if ctx.original_token.text() == label {
|
let postfix_match = if ctx.original_token.text() == label {
|
||||||
let relevance =
|
Some(CompletionRelevancePostfixMatch::Exact)
|
||||||
CompletionRelevance { exact_postfix_snippet_match: true, ..Default::default() };
|
} else {
|
||||||
item.set_relevance(relevance);
|
Some(CompletionRelevancePostfixMatch::NonExact)
|
||||||
}
|
};
|
||||||
|
let relevance = CompletionRelevance { postfix_match, ..Default::default() };
|
||||||
|
item.set_relevance(relevance);
|
||||||
item
|
item
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ use syntax::{ast::Expr, T};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
patterns::ImmediateLocation, CompletionContext, CompletionItem, CompletionItemKind,
|
patterns::ImmediateLocation, CompletionContext, CompletionItem, CompletionItemKind,
|
||||||
CompletionRelevance, Completions,
|
CompletionRelevance, CompletionRelevancePostfixMatch, Completions,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
|
pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
|
||||||
|
@ -45,7 +45,7 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
|
||||||
let completion_text =
|
let completion_text =
|
||||||
completion_text.strip_prefix(ctx.token.text()).unwrap_or(completion_text);
|
completion_text.strip_prefix(ctx.token.text()).unwrap_or(completion_text);
|
||||||
item.insert_text(completion_text).set_relevance(CompletionRelevance {
|
item.insert_text(completion_text).set_relevance(CompletionRelevance {
|
||||||
exact_postfix_snippet_match: true,
|
postfix_match: Some(CompletionRelevancePostfixMatch::Exact),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
item.add_to(acc);
|
item.add_to(acc);
|
||||||
|
|
|
@ -143,15 +143,8 @@ pub struct CompletionRelevance {
|
||||||
pub is_op_method: bool,
|
pub is_op_method: bool,
|
||||||
/// Set for item completions that are private but in the workspace.
|
/// Set for item completions that are private but in the workspace.
|
||||||
pub is_private_editable: bool,
|
pub is_private_editable: bool,
|
||||||
/// This is set in cases like these:
|
/// Set for postfix snippet item completions
|
||||||
///
|
pub postfix_match: Option<CompletionRelevancePostfixMatch>,
|
||||||
/// ```
|
|
||||||
/// (a > b).not$0
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// Basically, we want to guarantee that postfix snippets always takes
|
|
||||||
/// precedence over everything else.
|
|
||||||
pub exact_postfix_snippet_match: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
||||||
|
@ -178,8 +171,23 @@ pub enum CompletionRelevanceTypeMatch {
|
||||||
Exact,
|
Exact,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
||||||
|
pub enum CompletionRelevancePostfixMatch {
|
||||||
|
/// Set in cases when item is postfix, but not exact
|
||||||
|
NonExact,
|
||||||
|
/// This is set in cases like these:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// (a > b).not$0
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// Basically, we want to guarantee that postfix snippets always takes
|
||||||
|
/// precedence over everything else.
|
||||||
|
Exact,
|
||||||
|
}
|
||||||
|
|
||||||
impl CompletionRelevance {
|
impl CompletionRelevance {
|
||||||
const BASE_LINE: u32 = 2;
|
const BASE_LINE: u32 = 3;
|
||||||
/// Provides a relevance score. Higher values are more relevant.
|
/// Provides a relevance score. Higher values are more relevant.
|
||||||
///
|
///
|
||||||
/// The absolute value of the relevance score is not meaningful, for
|
/// The absolute value of the relevance score is not meaningful, for
|
||||||
|
@ -199,6 +207,9 @@ impl CompletionRelevance {
|
||||||
if self.is_private_editable {
|
if self.is_private_editable {
|
||||||
score -= 1;
|
score -= 1;
|
||||||
}
|
}
|
||||||
|
if self.postfix_match.is_some() {
|
||||||
|
score -= 3;
|
||||||
|
}
|
||||||
|
|
||||||
// score increases
|
// score increases
|
||||||
if self.exact_name_match {
|
if self.exact_name_match {
|
||||||
|
@ -212,9 +223,10 @@ impl CompletionRelevance {
|
||||||
if self.is_local {
|
if self.is_local {
|
||||||
score += 1;
|
score += 1;
|
||||||
}
|
}
|
||||||
if self.exact_postfix_snippet_match {
|
if self.postfix_match == Some(CompletionRelevancePostfixMatch::Exact) {
|
||||||
score += 100;
|
score += 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
score
|
score
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -530,7 +542,9 @@ mod tests {
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use test_utils::assert_eq_text;
|
use test_utils::assert_eq_text;
|
||||||
|
|
||||||
use super::{CompletionRelevance, CompletionRelevanceTypeMatch};
|
use super::{
|
||||||
|
CompletionRelevance, CompletionRelevancePostfixMatch, CompletionRelevanceTypeMatch,
|
||||||
|
};
|
||||||
|
|
||||||
/// Check that these are CompletionRelevance are sorted in ascending order
|
/// Check that these are CompletionRelevance are sorted in ascending order
|
||||||
/// by their relevance score.
|
/// by their relevance score.
|
||||||
|
@ -573,6 +587,10 @@ mod tests {
|
||||||
// This test asserts that the relevance score for these items is ascending, and
|
// This test asserts that the relevance score for these items is ascending, and
|
||||||
// that any items in the same vec have the same score.
|
// that any items in the same vec have the same score.
|
||||||
let expected_relevance_order = vec![
|
let expected_relevance_order = vec![
|
||||||
|
vec![CompletionRelevance {
|
||||||
|
postfix_match: Some(CompletionRelevancePostfixMatch::NonExact),
|
||||||
|
..CompletionRelevance::default()
|
||||||
|
}],
|
||||||
vec![CompletionRelevance {
|
vec![CompletionRelevance {
|
||||||
is_op_method: true,
|
is_op_method: true,
|
||||||
is_private_editable: true,
|
is_private_editable: true,
|
||||||
|
@ -612,7 +630,7 @@ mod tests {
|
||||||
..CompletionRelevance::default()
|
..CompletionRelevance::default()
|
||||||
}],
|
}],
|
||||||
vec![CompletionRelevance {
|
vec![CompletionRelevance {
|
||||||
exact_postfix_snippet_match: true,
|
postfix_match: Some(CompletionRelevancePostfixMatch::Exact),
|
||||||
..CompletionRelevance::default()
|
..CompletionRelevance::default()
|
||||||
}],
|
}],
|
||||||
];
|
];
|
||||||
|
|
|
@ -28,7 +28,10 @@ use crate::{completions::Completions, context::CompletionContext};
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
config::CompletionConfig,
|
config::CompletionConfig,
|
||||||
item::{CompletionItem, CompletionItemKind, CompletionRelevance, ImportEdit},
|
item::{
|
||||||
|
CompletionItem, CompletionItemKind, CompletionRelevance, CompletionRelevancePostfixMatch,
|
||||||
|
ImportEdit,
|
||||||
|
},
|
||||||
snippet::{Snippet, SnippetScope},
|
snippet::{Snippet, SnippetScope},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -365,7 +365,7 @@ mod tests {
|
||||||
use crate::{
|
use crate::{
|
||||||
item::CompletionRelevanceTypeMatch,
|
item::CompletionRelevanceTypeMatch,
|
||||||
tests::{check_edit, do_completion, get_all_items, TEST_CONFIG},
|
tests::{check_edit, do_completion, get_all_items, TEST_CONFIG},
|
||||||
CompletionItem, CompletionItemKind, CompletionRelevance,
|
CompletionItem, CompletionItemKind, CompletionRelevance, CompletionRelevancePostfixMatch,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
|
@ -432,7 +432,10 @@ mod tests {
|
||||||
),
|
),
|
||||||
(relevance.exact_name_match, "name"),
|
(relevance.exact_name_match, "name"),
|
||||||
(relevance.is_local, "local"),
|
(relevance.is_local, "local"),
|
||||||
(relevance.exact_postfix_snippet_match, "snippet"),
|
(
|
||||||
|
relevance.postfix_match == Some(CompletionRelevancePostfixMatch::Exact),
|
||||||
|
"snippet",
|
||||||
|
),
|
||||||
(relevance.is_op_method, "op_method"),
|
(relevance.is_op_method, "op_method"),
|
||||||
]
|
]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@ -614,7 +617,7 @@ fn main() { let _: m::Spam = S$0 }
|
||||||
is_local: false,
|
is_local: false,
|
||||||
is_op_method: false,
|
is_op_method: false,
|
||||||
is_private_editable: false,
|
is_private_editable: false,
|
||||||
exact_postfix_snippet_match: false,
|
postfix_match: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
CompletionItem {
|
CompletionItem {
|
||||||
|
@ -635,7 +638,7 @@ fn main() { let _: m::Spam = S$0 }
|
||||||
is_local: false,
|
is_local: false,
|
||||||
is_op_method: false,
|
is_op_method: false,
|
||||||
is_private_editable: false,
|
is_private_editable: false,
|
||||||
exact_postfix_snippet_match: false,
|
postfix_match: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
@ -722,7 +725,7 @@ fn foo() { A { the$0 } }
|
||||||
is_local: false,
|
is_local: false,
|
||||||
is_op_method: false,
|
is_op_method: false,
|
||||||
is_private_editable: false,
|
is_private_editable: false,
|
||||||
exact_postfix_snippet_match: false,
|
postfix_match: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in a new issue