Skip to content

Commit be94461

Browse files
feat(completions): fill in alias for columns in join clauses (#392)
Given a query ```sql select email, id from auth.users u join auth.identities i on {} ``` if you pick `user_id` from the list, it will fill in `i.user_id` now.
1 parent 573e0ff commit be94461

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

crates/pgt_completions/src/providers/columns.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
use crate::{
22
CompletionItemKind,
33
builder::{CompletionBuilder, PossibleCompletionItem},
4-
context::CompletionContext,
4+
context::{CompletionContext, WrappingClause},
55
relevance::{CompletionRelevanceData, filtering::CompletionFilter, scoring::CompletionScore},
66
};
77

8+
use super::helper::{find_matching_alias_for_table, get_completion_text_with_schema_or_alias};
9+
810
pub fn complete_columns<'a>(ctx: &CompletionContext<'a>, builder: &mut CompletionBuilder<'a>) {
911
let available_columns = &ctx.schema_cache.columns;
1012

1113
for col in available_columns {
1214
let relevance = CompletionRelevanceData::Column(col);
1315

14-
let item = PossibleCompletionItem {
16+
let mut item = PossibleCompletionItem {
1517
label: col.name.clone(),
1618
score: CompletionScore::from(relevance.clone()),
1719
filter: CompletionFilter::from(relevance),
@@ -20,6 +22,14 @@ pub fn complete_columns<'a>(ctx: &CompletionContext<'a>, builder: &mut Completio
2022
completion_text: None,
2123
};
2224

25+
// autocomplete with the alias in a join clause if we find one
26+
if matches!(ctx.wrapping_clause_type, Some(WrappingClause::Join { .. })) {
27+
item.completion_text = find_matching_alias_for_table(ctx, col.table_name.as_str())
28+
.and_then(|alias| {
29+
get_completion_text_with_schema_or_alias(ctx, col.name.as_str(), alias.as_str())
30+
});
31+
}
32+
2333
builder.add_item(item);
2434
}
2535
}

crates/pgt_completions/src/providers/functions.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
relevance::{CompletionRelevanceData, filtering::CompletionFilter, scoring::CompletionScore},
66
};
77

8-
use super::helper::get_completion_text_with_schema;
8+
use super::helper::get_completion_text_with_schema_or_alias;
99

1010
pub fn complete_functions<'a>(ctx: &'a CompletionContext, builder: &mut CompletionBuilder<'a>) {
1111
let available_functions = &ctx.schema_cache.functions;
@@ -19,7 +19,11 @@ pub fn complete_functions<'a>(ctx: &'a CompletionContext, builder: &mut Completi
1919
filter: CompletionFilter::from(relevance),
2020
description: format!("Schema: {}", func.schema),
2121
kind: CompletionItemKind::Function,
22-
completion_text: get_completion_text_with_schema(ctx, &func.name, &func.schema),
22+
completion_text: get_completion_text_with_schema_or_alias(
23+
ctx,
24+
&func.name,
25+
&func.schema,
26+
),
2327
};
2428

2529
builder.add_item(item);

crates/pgt_completions/src/providers/helper.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@ use pgt_text_size::{TextRange, TextSize};
22

33
use crate::{CompletionText, context::CompletionContext};
44

5-
pub(crate) fn get_completion_text_with_schema(
5+
pub(crate) fn find_matching_alias_for_table(
6+
ctx: &CompletionContext,
7+
table_name: &str,
8+
) -> Option<String> {
9+
for (alias, table) in ctx.mentioned_table_aliases.iter() {
10+
if table == table_name {
11+
return Some(alias.to_string());
12+
}
13+
}
14+
None
15+
}
16+
17+
pub(crate) fn get_completion_text_with_schema_or_alias(
618
ctx: &CompletionContext,
719
item_name: &str,
8-
item_schema_name: &str,
20+
schema_or_alias_name: &str,
921
) -> Option<CompletionText> {
10-
if item_schema_name == "public" || ctx.schema_or_alias_name.is_some() {
22+
if schema_or_alias_name == "public" || ctx.schema_or_alias_name.is_some() {
1123
None
1224
} else {
1325
let node = ctx.node_under_cursor.unwrap();
@@ -18,7 +30,7 @@ pub(crate) fn get_completion_text_with_schema(
1830
);
1931

2032
Some(CompletionText {
21-
text: format!("{}.{}", item_schema_name, item_name),
33+
text: format!("{}.{}", schema_or_alias_name, item_name),
2234
range,
2335
})
2436
}

crates/pgt_completions/src/providers/tables.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
relevance::{CompletionRelevanceData, filtering::CompletionFilter, scoring::CompletionScore},
66
};
77

8-
use super::helper::get_completion_text_with_schema;
8+
use super::helper::get_completion_text_with_schema_or_alias;
99

1010
pub fn complete_tables<'a>(ctx: &'a CompletionContext, builder: &mut CompletionBuilder<'a>) {
1111
let available_tables = &ctx.schema_cache.tables;
@@ -19,7 +19,11 @@ pub fn complete_tables<'a>(ctx: &'a CompletionContext, builder: &mut CompletionB
1919
filter: CompletionFilter::from(relevance),
2020
description: format!("Schema: {}", table.schema),
2121
kind: CompletionItemKind::Table,
22-
completion_text: get_completion_text_with_schema(ctx, &table.name, &table.schema),
22+
completion_text: get_completion_text_with_schema_or_alias(
23+
ctx,
24+
&table.name,
25+
&table.schema,
26+
),
2327
};
2428

2529
builder.add_item(item);

0 commit comments

Comments
 (0)