@@ -23,7 +23,12 @@ pub fn complete_columns<'a>(ctx: &CompletionContext<'a>, builder: &mut Completio
2323 } ;
2424
2525 // autocomplete with the alias in a join clause if we find one
26- if matches ! ( ctx. wrapping_clause_type, Some ( WrappingClause :: Join { .. } ) ) {
26+ if matches ! (
27+ ctx. wrapping_clause_type,
28+ Some ( WrappingClause :: Join { .. } )
29+ | Some ( WrappingClause :: Where )
30+ | Some ( WrappingClause :: Select )
31+ ) {
2732 item. completion_text = find_matching_alias_for_table ( ctx, col. table_name . as_str ( ) )
2833 . and_then ( |alias| {
2934 get_completion_text_with_schema_or_alias ( ctx, col. name . as_str ( ) , alias. as_str ( ) )
@@ -36,6 +41,8 @@ pub fn complete_columns<'a>(ctx: &CompletionContext<'a>, builder: &mut Completio
3641
3742#[ cfg( test) ]
3843mod tests {
44+ use std:: vec;
45+
3946 use crate :: {
4047 CompletionItem , CompletionItemKind , complete,
4148 test_helper:: {
@@ -643,4 +650,81 @@ mod tests {
643650 )
644651 . await ;
645652 }
653+
654+ #[ tokio:: test]
655+ async fn suggests_columns_in_where_clause ( ) {
656+ let setup = r#"
657+ create table instruments (
658+ id bigint primary key generated always as identity,
659+ name text not null,
660+ z text,
661+ created_at timestamp with time zone default now()
662+ );
663+
664+ create table others (
665+ a text,
666+ b text,
667+ c text
668+ );
669+ "# ;
670+
671+ assert_complete_results (
672+ format ! ( "select name from instruments where {} " , CURSOR_POS ) . as_str ( ) ,
673+ vec ! [
674+ CompletionAssertion :: Label ( "created_at" . into( ) ) ,
675+ CompletionAssertion :: Label ( "id" . into( ) ) ,
676+ CompletionAssertion :: Label ( "name" . into( ) ) ,
677+ CompletionAssertion :: Label ( "z" . into( ) ) ,
678+ ] ,
679+ setup,
680+ )
681+ . await ;
682+
683+ assert_complete_results (
684+ format ! (
685+ "select name from instruments where z = 'something' and created_at > {}" ,
686+ CURSOR_POS
687+ )
688+ . as_str ( ) ,
689+ // simply do not complete columns + schemas; functions etc. are ok
690+ vec ! [
691+ CompletionAssertion :: KindNotExists ( CompletionItemKind :: Column ) ,
692+ CompletionAssertion :: KindNotExists ( CompletionItemKind :: Schema ) ,
693+ ] ,
694+ setup,
695+ )
696+ . await ;
697+
698+ // prefers not mentioned columns
699+ assert_complete_results (
700+ format ! (
701+ "select name from instruments where id = 'something' and {}" ,
702+ CURSOR_POS
703+ )
704+ . as_str ( ) ,
705+ vec ! [
706+ CompletionAssertion :: Label ( "created_at" . into( ) ) ,
707+ CompletionAssertion :: Label ( "name" . into( ) ) ,
708+ CompletionAssertion :: Label ( "z" . into( ) ) ,
709+ ] ,
710+ setup,
711+ )
712+ . await ;
713+
714+ // // uses aliases
715+ assert_complete_results (
716+ format ! (
717+ "select name from instruments i join others o on i.z = o.a where i.{}" ,
718+ CURSOR_POS
719+ )
720+ . as_str ( ) ,
721+ vec ! [
722+ CompletionAssertion :: Label ( "created_at" . into( ) ) ,
723+ CompletionAssertion :: Label ( "id" . into( ) ) ,
724+ CompletionAssertion :: Label ( "name" . into( ) ) ,
725+ ] ,
726+ setup,
727+ )
728+ . await ;
729+ }
646730}
0 commit comments