Disable forceful sorting of the slash command argument completions (#16240)

Also bubble up the current active tab's path in the \tab argument
completions.

Release Notes:

- N/A
This commit is contained in:
Kirill Bulatov 2024-08-14 22:34:25 +03:00 committed by GitHub
parent b55e8383c8
commit e8bae839ed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 113 additions and 68 deletions

View file

@ -900,6 +900,7 @@ enum ContextMenuOrigin {
#[derive(Clone)]
struct CompletionsMenu {
id: CompletionId,
sort_completions: bool,
initial_position: Anchor,
buffer: Model<Buffer>,
completions: Arc<RwLock<Box<[Completion]>>>,
@ -1225,55 +1226,57 @@ impl CompletionsMenu {
}
let completions = self.completions.read();
matches.sort_unstable_by_key(|mat| {
// We do want to strike a balance here between what the language server tells us
// to sort by (the sort_text) and what are "obvious" good matches (i.e. when you type
// `Creat` and there is a local variable called `CreateComponent`).
// So what we do is: we bucket all matches into two buckets
// - Strong matches
// - Weak matches
// Strong matches are the ones with a high fuzzy-matcher score (the "obvious" matches)
// and the Weak matches are the rest.
//
// For the strong matches, we sort by the language-servers score first and for the weak
// matches, we prefer our fuzzy finder first.
//
// The thinking behind that: it's useless to take the sort_text the language-server gives
// us into account when it's obviously a bad match.
if self.sort_completions {
matches.sort_unstable_by_key(|mat| {
// We do want to strike a balance here between what the language server tells us
// to sort by (the sort_text) and what are "obvious" good matches (i.e. when you type
// `Creat` and there is a local variable called `CreateComponent`).
// So what we do is: we bucket all matches into two buckets
// - Strong matches
// - Weak matches
// Strong matches are the ones with a high fuzzy-matcher score (the "obvious" matches)
// and the Weak matches are the rest.
//
// For the strong matches, we sort by the language-servers score first and for the weak
// matches, we prefer our fuzzy finder first.
//
// The thinking behind that: it's useless to take the sort_text the language-server gives
// us into account when it's obviously a bad match.
#[derive(PartialEq, Eq, PartialOrd, Ord)]
enum MatchScore<'a> {
Strong {
sort_text: Option<&'a str>,
score: Reverse<OrderedFloat<f64>>,
sort_key: (usize, &'a str),
},
Weak {
score: Reverse<OrderedFloat<f64>>,
sort_text: Option<&'a str>,
sort_key: (usize, &'a str),
},
}
let completion = &completions[mat.candidate_id];
let sort_key = completion.sort_key();
let sort_text = completion.lsp_completion.sort_text.as_deref();
let score = Reverse(OrderedFloat(mat.score));
if mat.score >= 0.2 {
MatchScore::Strong {
sort_text,
score,
sort_key,
#[derive(PartialEq, Eq, PartialOrd, Ord)]
enum MatchScore<'a> {
Strong {
sort_text: Option<&'a str>,
score: Reverse<OrderedFloat<f64>>,
sort_key: (usize, &'a str),
},
Weak {
score: Reverse<OrderedFloat<f64>>,
sort_text: Option<&'a str>,
sort_key: (usize, &'a str),
},
}
} else {
MatchScore::Weak {
score,
sort_text,
sort_key,
let completion = &completions[mat.candidate_id];
let sort_key = completion.sort_key();
let sort_text = completion.lsp_completion.sort_text.as_deref();
let score = Reverse(OrderedFloat(mat.score));
if mat.score >= 0.2 {
MatchScore::Strong {
sort_text,
score,
sort_key,
}
} else {
MatchScore::Weak {
score,
sort_text,
sort_key,
}
}
}
});
});
}
for mat in &mut matches {
let completion = &completions[mat.candidate_id];
@ -4105,6 +4108,7 @@ impl Editor {
trigger_kind,
};
let completions = provider.completions(&buffer, buffer_position, completion_context, cx);
let sort_completions = provider.sort_completions();
let id = post_inc(&mut self.next_completion_id);
let task = cx.spawn(|this, mut cx| {
@ -4116,6 +4120,7 @@ impl Editor {
let menu = if let Some(completions) = completions {
let mut menu = CompletionsMenu {
id,
sort_completions,
initial_position: position,
match_candidates: completions
.iter()
@ -12045,6 +12050,10 @@ pub trait CompletionProvider {
trigger_in_words: bool,
cx: &mut ViewContext<Editor>,
) -> bool;
fn sort_completions(&self) -> bool {
true
}
}
fn snippet_completions(