Skip to content

Commit 2f27bee

Browse files
add comment, rename
1 parent 9cd19b0 commit 2f27bee

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-10
lines changed

crates/pgt_completions/src/context.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,25 @@ pub(crate) struct CompletionContext<'a> {
106106
pub schema_cache: &'a SchemaCache,
107107
pub position: usize,
108108

109-
pub schema_name: Option<String>,
109+
/// If the cursor is on a node that uses dot notation
110+
/// to specify an alias or schema, this will hold the schema's or
111+
/// alias's name.
112+
///
113+
/// Here, `auth` is a schema name:
114+
/// ```sql
115+
/// select * from auth.users;
116+
/// ```
117+
///
118+
/// Here, `u` is an alias name:
119+
/// ```sql
120+
/// select
121+
/// *
122+
/// from
123+
/// auth.users u
124+
/// left join identities i
125+
/// on u.id = i.user_id;
126+
/// ```
127+
pub schema_or_alias_name: Option<String>,
110128
pub wrapping_clause_type: Option<ClauseType>,
111129

112130
pub wrapping_node_kind: Option<WrappingNode>,
@@ -127,7 +145,7 @@ impl<'a> CompletionContext<'a> {
127145
schema_cache: params.schema,
128146
position: usize::from(params.position),
129147
node_under_cursor: None,
130-
schema_name: None,
148+
schema_or_alias_name: None,
131149
wrapping_clause_type: None,
132150
wrapping_node_kind: None,
133151
wrapping_statement_range: None,
@@ -258,7 +276,7 @@ impl<'a> CompletionContext<'a> {
258276
NodeText::Original(txt) => {
259277
let parts: Vec<&str> = txt.split('.').collect();
260278
if parts.len() == 2 {
261-
self.schema_name = Some(parts[0].to_string());
279+
self.schema_or_alias_name = Some(parts[0].to_string());
262280
}
263281
}
264282
NodeText::Replaced => {}
@@ -380,7 +398,10 @@ mod tests {
380398

381399
let ctx = CompletionContext::new(&params);
382400

383-
assert_eq!(ctx.schema_name, expected_schema.map(|f| f.to_string()));
401+
assert_eq!(
402+
ctx.schema_or_alias_name,
403+
expected_schema.map(|f| f.to_string())
404+
);
384405
}
385406
}
386407

crates/pgt_completions/src/providers/helper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub(crate) fn get_completion_text_with_schema(
77
item_name: &str,
88
item_schema_name: &str,
99
) -> Option<CompletionText> {
10-
if item_schema_name == "public" || ctx.schema_name.is_some() {
10+
if item_schema_name == "public" || ctx.schema_or_alias_name.is_some() {
1111
None
1212
} else {
1313
let node = ctx.node_under_cursor.unwrap();

crates/pgt_completions/src/relevance/filtering.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,38 @@ impl CompletionFilter<'_> {
8787
}
8888

8989
fn check_mentioned_schema(&self, ctx: &CompletionContext) -> Option<()> {
90-
if ctx.schema_name.is_none() {
90+
if ctx.schema_or_alias_name.is_none() {
9191
return Some(());
9292
}
9393

94-
let name = ctx.schema_name.as_ref().unwrap();
94+
let name = ctx.schema_or_alias_name.as_ref().unwrap();
95+
96+
let does_not_match = match self.data {
97+
CompletionRelevanceData::Table(table) => &table.schema != name,
98+
CompletionRelevanceData::Function(f) => &f.schema != name,
99+
CompletionRelevanceData::Column(_) => {
100+
// columns belong to tables, not schemas
101+
true
102+
}
103+
CompletionRelevanceData::Schema(_) => {
104+
// we should never allow schema suggestions if there already was one.
105+
true
106+
}
107+
};
108+
109+
if does_not_match {
110+
return None;
111+
}
112+
113+
Some(())
114+
}
115+
116+
fn check_mentioned_alias(&self, ctx: &CompletionContext) -> Option<()> {
117+
if ctx.schema_or_alias_name.is_none() {
118+
return Some(());
119+
}
120+
121+
let name = ctx.schema_or_alias_name.as_ref().unwrap();
95122

96123
let does_not_match = match self.data {
97124
CompletionRelevanceData::Table(table) => &table.schema != name,

crates/pgt_completions/src/relevance/scoring.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl CompletionScore<'_> {
6262
};
6363

6464
let has_mentioned_tables = !ctx.mentioned_relations.is_empty();
65-
let has_mentioned_schema = ctx.schema_name.is_some();
65+
let has_mentioned_schema = ctx.schema_or_alias_name.is_some();
6666

6767
self.score += match self.data {
6868
CompletionRelevanceData::Table(_) => match clause_type {
@@ -98,7 +98,7 @@ impl CompletionScore<'_> {
9898
Some(wn) => wn,
9999
};
100100

101-
let has_mentioned_schema = ctx.schema_name.is_some();
101+
let has_mentioned_schema = ctx.schema_or_alias_name.is_some();
102102
let has_node_text = ctx.get_node_under_cursor_content().is_some();
103103

104104
self.score += match self.data {
@@ -135,7 +135,7 @@ impl CompletionScore<'_> {
135135
}
136136

137137
fn check_matches_schema(&mut self, ctx: &CompletionContext) {
138-
let schema_name = match ctx.schema_name.as_ref() {
138+
let schema_name = match ctx.schema_or_alias_name.as_ref() {
139139
None => return,
140140
Some(n) => n,
141141
};

0 commit comments

Comments
 (0)