@@ -24,7 +24,12 @@ pub fn complete_columns<'a>(ctx: &CompletionContext<'a>, builder: &mut Completio
24
24
} ;
25
25
26
26
// autocomplete with the alias in a join clause if we find one
27
- if matches ! ( ctx. wrapping_clause_type, Some ( WrappingClause :: Join { .. } ) ) {
27
+ if matches ! (
28
+ ctx. wrapping_clause_type,
29
+ Some ( WrappingClause :: Join { .. } )
30
+ | Some ( WrappingClause :: Where )
31
+ | Some ( WrappingClause :: Select )
32
+ ) {
28
33
item. completion_text = find_matching_alias_for_table ( ctx, col. table_name . as_str ( ) )
29
34
. and_then ( |alias| {
30
35
get_completion_text_with_schema_or_alias ( ctx, col. name . as_str ( ) , alias. as_str ( ) )
@@ -37,11 +42,13 @@ pub fn complete_columns<'a>(ctx: &CompletionContext<'a>, builder: &mut Completio
37
42
38
43
#[ cfg( test) ]
39
44
mod tests {
45
+ use std:: vec;
46
+
40
47
use crate :: {
41
48
CompletionItem , CompletionItemKind , complete,
42
49
test_helper:: {
43
- CURSOR_POS , CompletionAssertion , InputQuery , assert_complete_results, get_test_deps ,
44
- get_test_params,
50
+ CURSOR_POS , CompletionAssertion , InputQuery , assert_complete_results,
51
+ assert_no_complete_results , get_test_deps , get_test_params,
45
52
} ,
46
53
} ;
47
54
@@ -574,4 +581,151 @@ mod tests {
574
581
)
575
582
. await ;
576
583
}
584
+
585
+ #[ tokio:: test]
586
+ async fn suggests_columns_in_insert_clause ( ) {
587
+ let setup = r#"
588
+ create table instruments (
589
+ id bigint primary key generated always as identity,
590
+ name text not null,
591
+ z text
592
+ );
593
+
594
+ create table others (
595
+ id serial primary key,
596
+ a text,
597
+ b text
598
+ );
599
+ "# ;
600
+
601
+ // We should prefer the instrument columns, even though they
602
+ // are lower in the alphabet
603
+
604
+ assert_complete_results (
605
+ format ! ( "insert into instruments ({})" , CURSOR_POS ) . as_str ( ) ,
606
+ vec ! [
607
+ CompletionAssertion :: Label ( "id" . to_string( ) ) ,
608
+ CompletionAssertion :: Label ( "name" . to_string( ) ) ,
609
+ CompletionAssertion :: Label ( "z" . to_string( ) ) ,
610
+ ] ,
611
+ setup,
612
+ )
613
+ . await ;
614
+
615
+ assert_complete_results (
616
+ format ! ( "insert into instruments (id, {})" , CURSOR_POS ) . as_str ( ) ,
617
+ vec ! [
618
+ CompletionAssertion :: Label ( "name" . to_string( ) ) ,
619
+ CompletionAssertion :: Label ( "z" . to_string( ) ) ,
620
+ ] ,
621
+ setup,
622
+ )
623
+ . await ;
624
+
625
+ assert_complete_results (
626
+ format ! ( "insert into instruments (id, {}, name)" , CURSOR_POS ) . as_str ( ) ,
627
+ vec ! [ CompletionAssertion :: Label ( "z" . to_string( ) ) ] ,
628
+ setup,
629
+ )
630
+ . await ;
631
+
632
+ // works with completed statement
633
+ assert_complete_results (
634
+ format ! (
635
+ "insert into instruments (name, {}) values ('my_bass');" ,
636
+ CURSOR_POS
637
+ )
638
+ . as_str ( ) ,
639
+ vec ! [
640
+ CompletionAssertion :: Label ( "id" . to_string( ) ) ,
641
+ CompletionAssertion :: Label ( "z" . to_string( ) ) ,
642
+ ] ,
643
+ setup,
644
+ )
645
+ . await ;
646
+
647
+ // no completions in the values list!
648
+ assert_no_complete_results (
649
+ format ! ( "insert into instruments (id, name) values ({})" , CURSOR_POS ) . as_str ( ) ,
650
+ setup,
651
+ )
652
+ . await ;
653
+ }
654
+
655
+ #[ tokio:: test]
656
+ async fn suggests_columns_in_where_clause ( ) {
657
+ let setup = r#"
658
+ create table instruments (
659
+ id bigint primary key generated always as identity,
660
+ name text not null,
661
+ z text,
662
+ created_at timestamp with time zone default now()
663
+ );
664
+
665
+ create table others (
666
+ a text,
667
+ b text,
668
+ c text
669
+ );
670
+ "# ;
671
+
672
+ assert_complete_results (
673
+ format ! ( "select name from instruments where {} " , CURSOR_POS ) . as_str ( ) ,
674
+ vec ! [
675
+ CompletionAssertion :: Label ( "created_at" . into( ) ) ,
676
+ CompletionAssertion :: Label ( "id" . into( ) ) ,
677
+ CompletionAssertion :: Label ( "name" . into( ) ) ,
678
+ CompletionAssertion :: Label ( "z" . into( ) ) ,
679
+ ] ,
680
+ setup,
681
+ )
682
+ . await ;
683
+
684
+ assert_complete_results (
685
+ format ! (
686
+ "select name from instruments where z = 'something' and created_at > {}" ,
687
+ CURSOR_POS
688
+ )
689
+ . as_str ( ) ,
690
+ // simply do not complete columns + schemas; functions etc. are ok
691
+ vec ! [
692
+ CompletionAssertion :: KindNotExists ( CompletionItemKind :: Column ) ,
693
+ CompletionAssertion :: KindNotExists ( CompletionItemKind :: Schema ) ,
694
+ ] ,
695
+ setup,
696
+ )
697
+ . await ;
698
+
699
+ // prefers not mentioned columns
700
+ assert_complete_results (
701
+ format ! (
702
+ "select name from instruments where id = 'something' and {}" ,
703
+ CURSOR_POS
704
+ )
705
+ . as_str ( ) ,
706
+ vec ! [
707
+ CompletionAssertion :: Label ( "created_at" . into( ) ) ,
708
+ CompletionAssertion :: Label ( "name" . into( ) ) ,
709
+ CompletionAssertion :: Label ( "z" . into( ) ) ,
710
+ ] ,
711
+ setup,
712
+ )
713
+ . await ;
714
+
715
+ // // uses aliases
716
+ assert_complete_results (
717
+ format ! (
718
+ "select name from instruments i join others o on i.z = o.a where i.{}" ,
719
+ CURSOR_POS
720
+ )
721
+ . as_str ( ) ,
722
+ vec ! [
723
+ CompletionAssertion :: Label ( "created_at" . into( ) ) ,
724
+ CompletionAssertion :: Label ( "id" . into( ) ) ,
725
+ CompletionAssertion :: Label ( "name" . into( ) ) ,
726
+ ] ,
727
+ setup,
728
+ )
729
+ . await ;
730
+ }
577
731
}
0 commit comments