Skip to content

Commit 265c15d

Browse files
awesome stuffings!
1 parent 68f3645 commit 265c15d

File tree

4 files changed

+57
-29
lines changed

4 files changed

+57
-29
lines changed

crates/pgls_completions/src/relevance/filtering.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,17 +308,25 @@ impl CompletionFilter<'_> {
308308
}
309309

310310
fn check_mentioned_schema_or_alias(&self, ctx: &TreesitterContext) -> Option<()> {
311-
let second_qualifier = match ctx.tail_qualifier_sanitized() {
311+
let tail_qualifier = match ctx.tail_qualifier_sanitized() {
312312
Some(q) => q,
313313
None => return Some(()), // no qualifier = this check passes
314314
};
315315

316316
let matches = match self.data {
317-
CompletionRelevanceData::Table(table) => table.schema == second_qualifier,
318-
CompletionRelevanceData::Function(f) => f.schema == second_qualifier,
319-
CompletionRelevanceData::Column(col) => ctx
320-
.get_mentioned_table_for_alias(&second_qualifier)
321-
.is_some_and(|t| t == &col.table_name),
317+
CompletionRelevanceData::Table(table) => table.schema == tail_qualifier,
318+
CompletionRelevanceData::Function(f) => f.schema == tail_qualifier,
319+
CompletionRelevanceData::Column(col) => {
320+
let table = ctx
321+
.get_mentioned_table_for_alias(&tail_qualifier)
322+
.unwrap_or(&tail_qualifier);
323+
324+
if let Some(schema) = ctx.head_qualifier_sanitized() {
325+
col.schema_name == schema.as_str() && col.table_name == table.as_str()
326+
} else {
327+
col.table_name == table.as_str()
328+
}
329+
}
322330

323331
// we should never allow schema suggestions if there already was one.
324332
CompletionRelevanceData::Schema(_) => false,

crates/pgls_completions/src/relevance/scoring.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,15 @@ impl CompletionScore<'_> {
149149
Some(wn) => wn,
150150
};
151151

152-
let has_single_qualifier = ctx.has_single_qualifier();
152+
let has_qualifier = ctx.has_any_qualifier();
153153
let has_node_text = ctx
154154
.get_node_under_cursor_content()
155155
.is_some_and(|txt| !sanitization::is_sanitized_token(txt.as_str()));
156156

157157
self.score += match self.data {
158158
CompletionRelevanceData::Table(_) => match wrapping_node {
159-
WrappingNode::Relation if has_single_qualifier => 15,
160-
WrappingNode::Relation if !has_single_qualifier => 10,
159+
WrappingNode::Relation if has_qualifier => 15,
160+
WrappingNode::Relation if !has_qualifier => 10,
161161
WrappingNode::BinaryExpression => 5,
162162
_ => -50,
163163
},
@@ -172,8 +172,8 @@ impl CompletionScore<'_> {
172172
_ => -15,
173173
},
174174
CompletionRelevanceData::Schema(_) => match wrapping_node {
175-
WrappingNode::Relation if !has_single_qualifier && !has_node_text => 15,
176-
WrappingNode::Relation if !has_single_qualifier && has_node_text => 0,
175+
WrappingNode::Relation if !has_qualifier && !has_node_text => 15,
176+
WrappingNode::Relation if !has_qualifier && has_node_text => 0,
177177
_ => -50,
178178
},
179179
CompletionRelevanceData::Policy(_) => 0,
@@ -191,11 +191,24 @@ impl CompletionScore<'_> {
191191
}
192192

193193
fn check_matches_schema(&mut self, ctx: &TreesitterContext) {
194-
let schema_from_qualifier = match ctx.tail_qualifier_sanitized() {
195-
Some(s) => s,
196-
None => return,
194+
let schema_from_qualifier = match self.data {
195+
CompletionRelevanceData::Table(_) | CompletionRelevanceData::Function(_) => {
196+
ctx.tail_qualifier_sanitized()
197+
}
198+
199+
CompletionRelevanceData::Column(_) | CompletionRelevanceData::Policy(_) => {
200+
ctx.head_qualifier_sanitized()
201+
}
202+
203+
CompletionRelevanceData::Schema(_) | CompletionRelevanceData::Role(_) => None,
197204
};
198205

206+
if schema_from_qualifier.is_none() {
207+
return;
208+
}
209+
210+
let schema_from_qualifier = schema_from_qualifier.unwrap();
211+
199212
let data_schema = match self.get_schema_name() {
200213
Some(s) => s,
201214
None => return,

crates/pgls_hover/src/hoverables/column.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fmt::Write;
1+
u>se std::fmt::Write;
22

33
use pgls_schema_cache::{Column, SchemaCache};
44
use pgls_treesitter::TreesitterContext;
@@ -74,12 +74,18 @@ impl ContextualPriority for Column {
7474
let mut score = 0.0;
7575

7676
// high score if we match the specific alias or table being referenced in the cursor context
77-
if let Some(table_or_alias) = ctx.identifier_qualifiers.1.as_ref() {
78-
if table_or_alias.replace('"', "") == self.table_name.as_str() {
77+
78+
79+
if let Some(table_or_alias) = ctx.tail_qualifier_sanitized() {
80+
let table = ctx.get_mentioned_table_for_alias(&table_or_alias).unwrap_or(&table_or_alias);
81+
82+
if table == self.table_name.as_str() {
7983
score += 250.0;
80-
} else if let Some(table_name) = ctx.get_mentioned_table_for_alias(table_or_alias) {
81-
if table_name == self.table_name.as_str() {
82-
score += 250.0;
84+
}
85+
86+
if let Some(schema) = ctx.head_qualifier_sanitized() {
87+
if schema == self.schema_name.as_str() {
88+
score += 50.0;
8389
}
8490
}
8591
}

crates/pgls_treesitter/src/context/mod.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -781,8 +781,8 @@ impl<'a> TreesitterContext<'a> {
781781
}
782782

783783
/// Returns the head qualifier (leftmost), sanitized (quotes removed)
784-
/// For `schema.table.column`: returns `Some("schema")`
785-
/// For `table.column`: returns `None`
784+
/// For `schema.table.<column>`: returns `Some("schema")`
785+
/// For `table.<column>`: returns `None`
786786
pub fn head_qualifier_sanitized(&self) -> Option<String> {
787787
self.identifier_qualifiers
788788
.0
@@ -791,8 +791,8 @@ impl<'a> TreesitterContext<'a> {
791791
}
792792

793793
/// Returns the tail qualifier (rightmost), sanitized (quotes removed)
794-
/// For `schema.table.column`: returns `Some("table")`
795-
/// For `table.column`: returns `Some("table")`
794+
/// For `schema.table.<column>`: returns `Some("table")`
795+
/// For `table.<column>`: returns `Some("table")`
796796
pub fn tail_qualifier_sanitized(&self) -> Option<String> {
797797
self.identifier_qualifiers
798798
.1
@@ -802,12 +802,13 @@ impl<'a> TreesitterContext<'a> {
802802

803803
/// Returns true if there is at least one qualifier present
804804
pub fn has_any_qualifier(&self) -> bool {
805-
self.identifier_qualifiers.1.is_some()
806-
}
805+
match self.identifier_qualifiers {
806+
(Some(_), Some(_)) => true,
807+
(None, Some(_)) => true,
808+
(None, None) => false,
807809

808-
/// Returns true if there is exactly one qualifier (tail only, no head)
809-
pub fn has_single_qualifier(&self) -> bool {
810-
matches!(self.identifier_qualifiers, (None, Some(_)))
810+
(Some(_), None) => unreachable!(),
811+
}
811812
}
812813
}
813814

0 commit comments

Comments
 (0)