Skip to content

Commit 458e433

Browse files
feat(completions): consider invocations in score
1 parent 85c3ad4 commit 458e433

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

crates/pg_completions/src/providers/functions.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use pg_schema_cache::Function;
22

33
use crate::{
4-
builder::CompletionBuilder, context::CompletionContext, CompletionItem, CompletionItemKind,
4+
builder::CompletionBuilder, context::CompletionContext, relevance::CompletionRelevanceData,
5+
CompletionItem, CompletionItemKind,
56
};
67

78
pub fn complete_functions(ctx: &CompletionContext, builder: &mut CompletionBuilder) {
@@ -11,7 +12,7 @@ pub fn complete_functions(ctx: &CompletionContext, builder: &mut CompletionBuild
1112
.iter()
1213
.map(|foo| CompletionItem {
1314
label: foo.name.clone(),
14-
score: get_score(ctx, foo),
15+
score: CompletionRelevanceData::Function(foo).get_score(ctx),
1516
description: format!("Schema: {}", foo.schema),
1617
preselected: false,
1718
kind: CompletionItemKind::Function,
@@ -23,10 +24,6 @@ pub fn complete_functions(ctx: &CompletionContext, builder: &mut CompletionBuild
2324
}
2425
}
2526

26-
fn get_score(ctx: &CompletionContext, foo: &Function) -> i32 {
27-
0
28-
}
29-
3027
#[cfg(test)]
3128
mod tests {
3229
use crate::{

crates/pg_completions/src/relevance.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ impl<'a> CompletionRelevance<'a> {
3232
self.check_matches_schema(ctx);
3333
self.check_matches_query_input(ctx);
3434
self.check_if_catalog(ctx);
35+
self.check_is_invocation(ctx);
3536

3637
self.score
3738
}
@@ -59,6 +60,25 @@ impl<'a> CompletionRelevance<'a> {
5960
};
6061
}
6162

63+
fn check_is_invocation(&mut self, ctx: &CompletionContext) {
64+
self.score += match self.data {
65+
CompletionRelevanceData::Function(_) => {
66+
if ctx.is_invocation {
67+
30
68+
} else {
69+
-30
70+
}
71+
}
72+
_ => {
73+
if ctx.is_invocation {
74+
-10
75+
} else {
76+
0
77+
}
78+
}
79+
};
80+
}
81+
6282
fn check_matches_schema(&mut self, ctx: &CompletionContext) {
6383
let schema_name = match ctx.schema_name.as_ref() {
6484
None => return,

0 commit comments

Comments
 (0)