Skip to content

Commit 20859a8

Browse files
ok
1 parent 6469ce3 commit 20859a8

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
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};
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(ctx, col.name.as_str(), alias.as_str())
30+
});
31+
}
32+
2333
builder.add_item(item);
2434
}
2535
}

crates/pgt_completions/src/providers/helper.rs

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

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

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+
517
pub(crate) fn get_completion_text_with_schema(
618
ctx: &CompletionContext,
719
item_name: &str,

0 commit comments

Comments
 (0)