From 20859a8fc3cf1977d73614697a32d50398bf4267 Mon Sep 17 00:00:00 2001 From: Julian Date: Wed, 7 May 2025 08:35:29 +0200 Subject: [PATCH 1/4] ok --- crates/pgt_completions/src/providers/columns.rs | 14 ++++++++++++-- crates/pgt_completions/src/providers/helper.rs | 12 ++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/crates/pgt_completions/src/providers/columns.rs b/crates/pgt_completions/src/providers/columns.rs index bd573430..90c9b26e 100644 --- a/crates/pgt_completions/src/providers/columns.rs +++ b/crates/pgt_completions/src/providers/columns.rs @@ -1,17 +1,19 @@ use crate::{ CompletionItemKind, builder::{CompletionBuilder, PossibleCompletionItem}, - context::CompletionContext, + context::{CompletionContext, WrappingClause}, relevance::{CompletionRelevanceData, filtering::CompletionFilter, scoring::CompletionScore}, }; +use super::helper::{find_matching_alias_for_table, get_completion_text_with_schema}; + pub fn complete_columns<'a>(ctx: &CompletionContext<'a>, builder: &mut CompletionBuilder<'a>) { let available_columns = &ctx.schema_cache.columns; for col in available_columns { let relevance = CompletionRelevanceData::Column(col); - let item = PossibleCompletionItem { + let mut item = PossibleCompletionItem { label: col.name.clone(), score: CompletionScore::from(relevance.clone()), filter: CompletionFilter::from(relevance), @@ -20,6 +22,14 @@ pub fn complete_columns<'a>(ctx: &CompletionContext<'a>, builder: &mut Completio completion_text: None, }; + // autocomplete with the alias in a join clause if we find one + if matches!(ctx.wrapping_clause_type, Some(WrappingClause::Join { .. })) { + item.completion_text = find_matching_alias_for_table(ctx, col.table_name.as_str()) + .and_then(|alias| { + get_completion_text_with_schema(ctx, col.name.as_str(), alias.as_str()) + }); + } + builder.add_item(item); } } diff --git a/crates/pgt_completions/src/providers/helper.rs b/crates/pgt_completions/src/providers/helper.rs index c0fe5869..e575518e 100644 --- a/crates/pgt_completions/src/providers/helper.rs +++ b/crates/pgt_completions/src/providers/helper.rs @@ -2,6 +2,18 @@ use pgt_text_size::{TextRange, TextSize}; use crate::{CompletionText, context::CompletionContext}; +pub(crate) fn find_matching_alias_for_table( + ctx: &CompletionContext, + table_name: &str, +) -> Option { + for (alias, table) in ctx.mentioned_table_aliases.iter() { + if table == table_name { + return Some(alias.to_string()); + } + } + None +} + pub(crate) fn get_completion_text_with_schema( ctx: &CompletionContext, item_name: &str, From 6a1b2106abe68d6242b0f2f8c5bb3c900dfd9f54 Mon Sep 17 00:00:00 2001 From: Julian Date: Wed, 7 May 2025 08:40:34 +0200 Subject: [PATCH 2/4] rename --- crates/pgt_completions/src/providers/columns.rs | 4 ++-- crates/pgt_completions/src/providers/functions.rs | 4 ++-- crates/pgt_completions/src/providers/helper.rs | 2 +- crates/pgt_completions/src/providers/tables.rs | 8 ++++++-- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/crates/pgt_completions/src/providers/columns.rs b/crates/pgt_completions/src/providers/columns.rs index 90c9b26e..07351b35 100644 --- a/crates/pgt_completions/src/providers/columns.rs +++ b/crates/pgt_completions/src/providers/columns.rs @@ -5,7 +5,7 @@ use crate::{ relevance::{CompletionRelevanceData, filtering::CompletionFilter, scoring::CompletionScore}, }; -use super::helper::{find_matching_alias_for_table, get_completion_text_with_schema}; +use super::helper::{find_matching_alias_for_table, get_completion_text_with_schema_or_alias}; pub fn complete_columns<'a>(ctx: &CompletionContext<'a>, builder: &mut CompletionBuilder<'a>) { let available_columns = &ctx.schema_cache.columns; @@ -26,7 +26,7 @@ pub fn complete_columns<'a>(ctx: &CompletionContext<'a>, builder: &mut Completio if matches!(ctx.wrapping_clause_type, Some(WrappingClause::Join { .. })) { item.completion_text = find_matching_alias_for_table(ctx, col.table_name.as_str()) .and_then(|alias| { - get_completion_text_with_schema(ctx, col.name.as_str(), alias.as_str()) + get_completion_text_with_schema_or_alias(ctx, col.name.as_str(), alias.as_str()) }); } diff --git a/crates/pgt_completions/src/providers/functions.rs b/crates/pgt_completions/src/providers/functions.rs index 4241da92..d71dece0 100644 --- a/crates/pgt_completions/src/providers/functions.rs +++ b/crates/pgt_completions/src/providers/functions.rs @@ -5,7 +5,7 @@ use crate::{ relevance::{CompletionRelevanceData, filtering::CompletionFilter, scoring::CompletionScore}, }; -use super::helper::get_completion_text_with_schema; +use super::helper::get_completion_text_with_schema_or_alias; pub fn complete_functions<'a>(ctx: &'a CompletionContext, builder: &mut CompletionBuilder<'a>) { let available_functions = &ctx.schema_cache.functions; @@ -19,7 +19,7 @@ pub fn complete_functions<'a>(ctx: &'a CompletionContext, builder: &mut Completi filter: CompletionFilter::from(relevance), description: format!("Schema: {}", func.schema), kind: CompletionItemKind::Function, - completion_text: get_completion_text_with_schema(ctx, &func.name, &func.schema), + completion_text: get_completion_text_with_schema_or_alias(ctx, &func.name, &func.schema), }; builder.add_item(item); diff --git a/crates/pgt_completions/src/providers/helper.rs b/crates/pgt_completions/src/providers/helper.rs index e575518e..c5ed231b 100644 --- a/crates/pgt_completions/src/providers/helper.rs +++ b/crates/pgt_completions/src/providers/helper.rs @@ -14,7 +14,7 @@ pub(crate) fn find_matching_alias_for_table( None } -pub(crate) fn get_completion_text_with_schema( +pub(crate) fn get_completion_text_with_schema_or_alias( ctx: &CompletionContext, item_name: &str, item_schema_name: &str, diff --git a/crates/pgt_completions/src/providers/tables.rs b/crates/pgt_completions/src/providers/tables.rs index cbedc55b..57195da7 100644 --- a/crates/pgt_completions/src/providers/tables.rs +++ b/crates/pgt_completions/src/providers/tables.rs @@ -5,7 +5,7 @@ use crate::{ relevance::{CompletionRelevanceData, filtering::CompletionFilter, scoring::CompletionScore}, }; -use super::helper::get_completion_text_with_schema; +use super::helper::get_completion_text_with_schema_or_alias; pub fn complete_tables<'a>(ctx: &'a CompletionContext, builder: &mut CompletionBuilder<'a>) { let available_tables = &ctx.schema_cache.tables; @@ -19,7 +19,11 @@ pub fn complete_tables<'a>(ctx: &'a CompletionContext, builder: &mut CompletionB filter: CompletionFilter::from(relevance), description: format!("Schema: {}", table.schema), kind: CompletionItemKind::Table, - completion_text: get_completion_text_with_schema(ctx, &table.name, &table.schema), + completion_text: get_completion_text_with_schema_or_alias( + ctx, + &table.name, + &table.schema, + ), }; builder.add_item(item); From c4bddf33462611761f46b86b7dc59bf2c42d71aa Mon Sep 17 00:00:00 2001 From: Julian Date: Wed, 7 May 2025 08:41:23 +0200 Subject: [PATCH 3/4] rename param --- crates/pgt_completions/src/providers/helper.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/pgt_completions/src/providers/helper.rs b/crates/pgt_completions/src/providers/helper.rs index c5ed231b..999d6b37 100644 --- a/crates/pgt_completions/src/providers/helper.rs +++ b/crates/pgt_completions/src/providers/helper.rs @@ -17,9 +17,9 @@ pub(crate) fn find_matching_alias_for_table( pub(crate) fn get_completion_text_with_schema_or_alias( ctx: &CompletionContext, item_name: &str, - item_schema_name: &str, + schema_or_alias_name: &str, ) -> Option { - if item_schema_name == "public" || ctx.schema_or_alias_name.is_some() { + if schema_or_alias_name == "public" || ctx.schema_or_alias_name.is_some() { None } else { let node = ctx.node_under_cursor.unwrap(); @@ -30,7 +30,7 @@ pub(crate) fn get_completion_text_with_schema_or_alias( ); Some(CompletionText { - text: format!("{}.{}", item_schema_name, item_name), + text: format!("{}.{}", schema_or_alias_name, item_name), range, }) } From d59f8b19fbd4921158f6db93e59c53aba8161673 Mon Sep 17 00:00:00 2001 From: Julian Date: Wed, 7 May 2025 09:28:06 +0200 Subject: [PATCH 4/4] fmt --- crates/pgt_completions/src/providers/functions.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/pgt_completions/src/providers/functions.rs b/crates/pgt_completions/src/providers/functions.rs index d71dece0..6bc04deb 100644 --- a/crates/pgt_completions/src/providers/functions.rs +++ b/crates/pgt_completions/src/providers/functions.rs @@ -19,7 +19,11 @@ pub fn complete_functions<'a>(ctx: &'a CompletionContext, builder: &mut Completi filter: CompletionFilter::from(relevance), description: format!("Schema: {}", func.schema), kind: CompletionItemKind::Function, - completion_text: get_completion_text_with_schema_or_alias(ctx, &func.name, &func.schema), + completion_text: get_completion_text_with_schema_or_alias( + ctx, + &func.name, + &func.schema, + ), }; builder.add_item(item);