Skip to content

Commit 195c706

Browse files
do not prefer public schema
1 parent 359a8ff commit 195c706

File tree

2 files changed

+47
-35
lines changed

2 files changed

+47
-35
lines changed

crates/pgls_completions/src/relevance/scoring.rs

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl CompletionScore<'_> {
6363
if case {
6464
println!("{} after clause type check", self.score);
6565
}
66-
self.check_matching_wrapping_node(ctx);
66+
self.check_without_content(ctx);
6767
if case {
6868
println!("{} after wrapping node check", self.score);
6969
}
@@ -247,37 +247,49 @@ impl CompletionScore<'_> {
247247
}
248248
}
249249

250-
fn check_matching_wrapping_node(&mut self, ctx: &TreesitterContext) {
251-
let wrapping_node = match ctx.wrapping_node_kind.as_ref() {
252-
None => return,
253-
Some(wn) => wn,
254-
};
250+
// ok i think we need a rule set first.
251+
// generally, we want to prefer columns that match a mentioned relation. that's already handled elsewhere. same with schema matches.
255252

256-
let has_qualifier = ctx.has_any_qualifier();
253+
// so, what here? we want to handle the *no content* case.
254+
// in that case, we want to check the current node_kind.
257255

258-
self.score += match self.data {
259-
CompletionRelevanceData::Table(_) => match wrapping_node {
260-
WrappingNode::Relation if has_qualifier => 15,
261-
WrappingNode::Relation if !has_qualifier => 10,
262-
WrappingNode::BinaryExpression => 5,
263-
_ => -50,
264-
},
265-
CompletionRelevanceData::Function(_) => match wrapping_node {
266-
WrappingNode::BinaryExpression => 15,
267-
WrappingNode::Relation => 10,
268-
_ => -50,
269-
},
270-
CompletionRelevanceData::Column(_) => match wrapping_node {
271-
WrappingNode::BinaryExpression => 15,
272-
WrappingNode::Assignment => 15,
273-
_ => -15,
274-
},
275-
CompletionRelevanceData::Schema(_) => match wrapping_node {
276-
WrappingNode::Relation if !has_qualifier => 15,
277-
_ => -50,
256+
fn check_without_content(&mut self, ctx: &TreesitterContext) {
257+
// the function is only concerned with cases where the user hasn't typed anything yet.
258+
if ctx
259+
.get_node_under_cursor_content()
260+
.is_some_and(|c| !sanitization::is_sanitized_token(c.as_str()))
261+
{
262+
return;
263+
}
264+
265+
match ctx.node_under_cursor.kind() {
266+
"function_identifier" | "table_identifier"
267+
if self.get_schema_name().is_some_and(|s| s == "public") =>
268+
{
269+
self.score += 10;
270+
}
271+
272+
"schema_identifier" if self.get_schema_name().is_some_and(|s| s == "public") => {
273+
self.score += 10;
274+
}
275+
276+
"any_identifier" => match self.data {
277+
CompletionRelevanceData::Table(table) => {
278+
if table.schema == "public" {
279+
self.score += 10;
280+
}
281+
}
282+
CompletionRelevanceData::Schema(schema) => {
283+
if schema.name != "public" {
284+
self.score += 10;
285+
} else {
286+
self.score -= 20;
287+
}
288+
}
289+
_ => {}
278290
},
279-
CompletionRelevanceData::Policy(_) => 0,
280-
CompletionRelevanceData::Role(_) => 0,
291+
292+
_ => return,
281293
}
282294
}
283295

crates/pgls_completions/src/snapshots/pgls_completions__test_helper__completes_quoted_columns.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,33 +66,33 @@ select "email" f|
6666
select "email" from |
6767

6868
Results:
69-
public - public (Schema)
7069
private - private (Schema)
71-
private.users - private.users (Table)
7270
information_schema - information_schema (Schema)
7371
pg_catalog - pg_catalog (Schema)
72+
pg_toast - pg_toast (Schema)
73+
private.users - private.users (Table)
7474

7575
--------------
7676

7777
select "email" from "|
7878

7979
Results:
80-
public - public (Schema)
8180
private - private (Schema)
82-
private"."users" - private.users (Table)
8381
information_schema - information_schema (Schema)
8482
pg_catalog - pg_catalog (Schema)
83+
pg_toast - pg_toast (Schema)
84+
private"."users" - private.users (Table)
8585

8686
--------------
8787

8888
select "email" from "|"
8989

9090
Results:
91-
public - public (Schema)
9291
private - private (Schema)
93-
private"."users - private.users (Table)
9492
information_schema - information_schema (Schema)
9593
pg_catalog - pg_catalog (Schema)
94+
pg_toast - pg_toast (Schema)
95+
private"."users - private.users (Table)
9696

9797
--------------
9898

0 commit comments

Comments
 (0)