@@ -28,7 +28,10 @@ pub fn complete_columns<'a>(ctx: &CompletionContext<'a>, builder: &mut Completio
28
28
mod tests {
29
29
use crate :: {
30
30
CompletionItem , CompletionItemKind , complete,
31
- test_helper:: { CURSOR_POS , InputQuery , get_test_deps, get_test_params} ,
31
+ test_helper:: {
32
+ CURSOR_POS , CompletionAssertion , InputQuery , assert_complete_results, get_test_deps,
33
+ get_test_params,
34
+ } ,
32
35
} ;
33
36
34
37
struct TestCase {
@@ -168,9 +171,9 @@ mod tests {
168
171
( "name" , "Table: public.users" ) ,
169
172
( "narrator" , "Table: public.audio_books" ) ,
170
173
( "narrator_id" , "Table: private.audio_books" ) ,
174
+ ( "id" , "Table: public.audio_books" ) ,
171
175
( "name" , "Schema: pg_catalog" ) ,
172
176
( "nameconcatoid" , "Schema: pg_catalog" ) ,
173
- ( "nameeq" , "Schema: pg_catalog" ) ,
174
177
]
175
178
. into_iter ( )
176
179
. map ( |( label, schema) | LabelAndDesc {
@@ -325,4 +328,107 @@ mod tests {
325
328
) ;
326
329
}
327
330
}
331
+
332
+ #[ tokio:: test]
333
+ async fn filters_out_by_aliases ( ) {
334
+ let setup = r#"
335
+ create schema auth;
336
+
337
+ create table auth.users (
338
+ uid serial primary key,
339
+ name text not null,
340
+ email text unique not null
341
+ );
342
+
343
+ create table auth.posts (
344
+ pid serial primary key,
345
+ user_id int not null references auth.users(uid),
346
+ title text not null,
347
+ content text,
348
+ created_at timestamp default now()
349
+ );
350
+ "# ;
351
+
352
+ // test in SELECT clause
353
+ assert_complete_results (
354
+ format ! (
355
+ "select u.id, p.{} from auth.users u join auth.posts p on u.id = p.user_id;" ,
356
+ CURSOR_POS
357
+ )
358
+ . as_str ( ) ,
359
+ vec ! [
360
+ CompletionAssertion :: LabelNotExists ( "uid" . to_string( ) ) ,
361
+ CompletionAssertion :: LabelNotExists ( "name" . to_string( ) ) ,
362
+ CompletionAssertion :: LabelNotExists ( "email" . to_string( ) ) ,
363
+ CompletionAssertion :: Label ( "content" . to_string( ) ) ,
364
+ CompletionAssertion :: Label ( "created_at" . to_string( ) ) ,
365
+ CompletionAssertion :: Label ( "pid" . to_string( ) ) ,
366
+ CompletionAssertion :: Label ( "title" . to_string( ) ) ,
367
+ CompletionAssertion :: Label ( "user_id" . to_string( ) ) ,
368
+ ] ,
369
+ setup,
370
+ )
371
+ . await ;
372
+
373
+ // test in JOIN clause
374
+ assert_complete_results (
375
+ format ! (
376
+ "select u.id, p.content from auth.users u join auth.posts p on u.id = p.{};" ,
377
+ CURSOR_POS
378
+ )
379
+ . as_str ( ) ,
380
+ vec ! [
381
+ CompletionAssertion :: LabelNotExists ( "uid" . to_string( ) ) ,
382
+ CompletionAssertion :: LabelNotExists ( "name" . to_string( ) ) ,
383
+ CompletionAssertion :: LabelNotExists ( "email" . to_string( ) ) ,
384
+ // primary keys are preferred
385
+ CompletionAssertion :: Label ( "pid" . to_string( ) ) ,
386
+ CompletionAssertion :: Label ( "content" . to_string( ) ) ,
387
+ CompletionAssertion :: Label ( "created_at" . to_string( ) ) ,
388
+ CompletionAssertion :: Label ( "title" . to_string( ) ) ,
389
+ CompletionAssertion :: Label ( "user_id" . to_string( ) ) ,
390
+ ] ,
391
+ setup,
392
+ )
393
+ . await ;
394
+ }
395
+
396
+ #[ tokio:: test]
397
+ async fn does_not_complete_cols_in_join_clauses ( ) {
398
+ let setup = r#"
399
+ create schema auth;
400
+
401
+ create table auth.users (
402
+ uid serial primary key,
403
+ name text not null,
404
+ email text unique not null
405
+ );
406
+
407
+ create table auth.posts (
408
+ pid serial primary key,
409
+ user_id int not null references auth.users(uid),
410
+ title text not null,
411
+ content text,
412
+ created_at timestamp default now()
413
+ );
414
+ "# ;
415
+
416
+ /*
417
+ * We are not in the "ON" part of the JOIN clause, so we should not complete columns.
418
+ */
419
+ assert_complete_results (
420
+ format ! (
421
+ "select u.id, p.content from auth.users u join auth.{}" ,
422
+ CURSOR_POS
423
+ )
424
+ . as_str ( ) ,
425
+ vec ! [
426
+ CompletionAssertion :: KindNotExists ( CompletionItemKind :: Column ) ,
427
+ CompletionAssertion :: LabelAndKind ( "posts" . to_string( ) , CompletionItemKind :: Table ) ,
428
+ CompletionAssertion :: LabelAndKind ( "users" . to_string( ) , CompletionItemKind :: Table ) ,
429
+ ] ,
430
+ setup,
431
+ )
432
+ . await ;
433
+ }
328
434
}
0 commit comments