Skip to content

Commit 5707bb8

Browse files
pickin up fast
1 parent 5533414 commit 5707bb8

File tree

6 files changed

+440
-73
lines changed

6 files changed

+440
-73
lines changed

Cargo.lock

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pgls_completions/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ tokio = { version = "1.41.1", features = ["full"] }
3333
[dev-dependencies]
3434
criterion.workspace = true
3535
pgls_test_utils.workspace = true
36+
insta.workspace = true
37+
regex = "1.12.2"
38+
3639

3740
[lib]
3841
doctest = false

crates/pgls_completions/src/providers/columns.rs

Lines changed: 42 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ mod tests {
5858
use crate::{
5959
CompletionItem, CompletionItemKind, complete,
6060
test_helper::{
61-
CompletionAssertion, assert_complete_results, get_test_deps, get_test_params,
61+
CompletionAssertion, TestCompletionsBuilder, assert_complete_results, get_test_deps,
62+
get_test_params,
6263
},
6364
};
6465

@@ -381,7 +382,7 @@ mod tests {
381382
}
382383

383384
#[sqlx::test(migrator = "pgls_test_utils::MIGRATIONS")]
384-
async fn filters_out_by_aliases(pool: PgPool) {
385+
async fn filters_out_by_aliases_in_join_on(pool: PgPool) {
385386
let setup = r#"
386387
create schema auth;
387388
@@ -400,52 +401,41 @@ mod tests {
400401
);
401402
"#;
402403

403-
pool.execute(setup).await.unwrap();
404+
TestCompletionsBuilder::new(&pool, Some(setup))
405+
.prefix_static("select u.id, p.content from auth.users u join auth.posts p")
406+
.type_sql("on u<1>.id = p.<2>user_id")
407+
.comment("Should prefer primary indices here.")
408+
.comment("We should only get columns from the auth.posts table.")
409+
.snapshot()
410+
.await;
411+
}
404412

405-
// test in SELECT clause
406-
assert_complete_results(
407-
format!(
408-
"select u.id, p.{} from auth.users u join auth.posts p on u.id = p.user_id;",
409-
QueryWithCursorPosition::cursor_marker()
410-
)
411-
.as_str(),
412-
vec![
413-
CompletionAssertion::LabelNotExists("uid".to_string()),
414-
CompletionAssertion::LabelNotExists("name".to_string()),
415-
CompletionAssertion::LabelNotExists("email".to_string()),
416-
CompletionAssertion::Label("content".to_string()),
417-
CompletionAssertion::Label("created_at".to_string()),
418-
CompletionAssertion::Label("pid".to_string()),
419-
CompletionAssertion::Label("title".to_string()),
420-
CompletionAssertion::Label("user_id".to_string()),
421-
],
422-
None,
423-
&pool,
424-
)
425-
.await;
413+
#[sqlx::test(migrator = "pgls_test_utils::MIGRATIONS")]
414+
async fn filters_out_by_aliases_in_select(pool: PgPool) {
415+
let setup = r#"
416+
create schema auth;
426417
427-
// test in JOIN clause
428-
assert_complete_results(
429-
format!(
430-
"select u.id, p.content from auth.users u join auth.posts p on u.id = p.{};",
431-
QueryWithCursorPosition::cursor_marker()
432-
)
433-
.as_str(),
434-
vec![
435-
CompletionAssertion::LabelNotExists("uid".to_string()),
436-
CompletionAssertion::LabelNotExists("name".to_string()),
437-
CompletionAssertion::LabelNotExists("email".to_string()),
438-
// primary keys are preferred
439-
CompletionAssertion::Label("pid".to_string()),
440-
CompletionAssertion::Label("content".to_string()),
441-
CompletionAssertion::Label("created_at".to_string()),
442-
CompletionAssertion::Label("title".to_string()),
443-
CompletionAssertion::Label("user_id".to_string()),
444-
],
445-
None,
446-
&pool,
447-
)
448-
.await;
418+
create table auth.users (
419+
uid serial primary key,
420+
name text not null,
421+
email text unique not null
422+
);
423+
424+
create table auth.posts (
425+
pid serial primary key,
426+
user_id int not null references auth.users(uid),
427+
title text not null,
428+
content text,
429+
created_at timestamp default now()
430+
);
431+
"#;
432+
433+
TestCompletionsBuilder::new(&pool, Some(setup))
434+
.type_sql("select u.id, p.pid<1>")
435+
.comment("We should only get columns from the auth.posts table.")
436+
.append_static("from auth.users u join auth.posts p on u.id = p.user_id;")
437+
.snapshot()
438+
.await;
449439
}
450440

451441
#[sqlx::test(migrator = "pgls_test_utils::MIGRATIONS")]
@@ -468,24 +458,12 @@ mod tests {
468458
);
469459
"#;
470460

471-
/*
472-
* We are not in the "ON" part of the JOIN clause, so we should not complete columns.
473-
*/
474-
assert_complete_results(
475-
format!(
476-
"select u.id, p.content from auth.users u join auth.{}",
477-
QueryWithCursorPosition::cursor_marker()
478-
)
479-
.as_str(),
480-
vec![
481-
CompletionAssertion::KindNotExists(CompletionItemKind::Column),
482-
CompletionAssertion::LabelAndKind("posts".to_string(), CompletionItemKind::Table),
483-
CompletionAssertion::LabelAndKind("users".to_string(), CompletionItemKind::Table),
484-
],
485-
Some(setup),
486-
&pool,
487-
)
488-
.await;
461+
TestCompletionsBuilder::new(&pool, Some(setup))
462+
.type_sql("select u.uid, p.content from auth<1>.users<2> u join auth.posts p on u.uid = p.user_id")
463+
.comment("Schema suggestions should be prioritized, since we want to push users to specify them.")
464+
.comment("Here, we shouldn't have schema completions.")
465+
.snapshot()
466+
.await;
489467
}
490468

491469
#[sqlx::test(migrator = "pgls_test_utils::MIGRATIONS")]

crates/pgls_completions/src/relevance/filtering.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ impl CompletionFilter<'_> {
324324
WrappingClause::DropRole | WrappingClause::AlterRole => true,
325325

326326
WrappingClause::SetStatement => ctx
327-
.before_cursor_matches_kind(&["keyword_role", "keyword_authorization"]),
327+
.before_cursor_matches_kind(&["eyword_role", "keyword_authorization"]),
328328

329329
WrappingClause::RevokeStatement | WrappingClause::GrantStatement => {
330330
ctx.history_ends_with(&["role_specification", "any_identifier"])
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
source: crates/pgls_completions/src/test_helper.rs
3+
assertion_line: 415
4+
expression: snapshot_result
5+
---
6+
s|
7+
from auth.users u join auth.posts p on u.id = p.user_id;
8+
9+
No Results
10+
11+
--------------
12+
13+
select |
14+
from auth.users u join auth.posts p on u.id = p.user_id;
15+
16+
Results:
17+
p.content - auth.posts.content (Column)
18+
p.created_at - auth.posts.created_at (Column)
19+
u.email - auth.users.email (Column)
20+
u.name - auth.users.name (Column)
21+
p.pid - auth.posts.pid (Column)
22+
23+
--------------
24+
25+
select u.|
26+
from auth.users u join auth.posts p on u.id = p.user_id;
27+
28+
Results:
29+
email - auth.users.email (Column)
30+
name - auth.users.name (Column)
31+
uid - auth.users.uid (Column)
32+
33+
--------------
34+
35+
select u.i|
36+
from auth.users u join auth.posts p on u.id = p.user_id;
37+
38+
Results:
39+
email - auth.users.email (Column)
40+
uid - auth.users.uid (Column)
41+
name - auth.users.name (Column)
42+
43+
--------------
44+
45+
select u.id, |
46+
from auth.users u join auth.posts p on u.id = p.user_id;
47+
48+
Results:
49+
p.content - auth.posts.content (Column)
50+
p.created_at - auth.posts.created_at (Column)
51+
u.email - auth.users.email (Column)
52+
u.name - auth.users.name (Column)
53+
p.pid - auth.posts.pid (Column)
54+
55+
--------------
56+
57+
select u.id, p.|
58+
from auth.users u join auth.posts p on u.id = p.user_id;
59+
60+
**We should only get columns from the auth.posts table.**
61+
62+
Results:
63+
content - auth.posts.content (Column)
64+
created_at - auth.posts.created_at (Column)
65+
pid - auth.posts.pid (Column)
66+
title - auth.posts.title (Column)
67+
user_id - auth.posts.user_id (Column)
68+
69+
--------------
70+
71+
select u.id, p.p|
72+
from auth.users u join auth.posts p on u.id = p.user_id;
73+
74+
**We should only get columns from the auth.posts table.**
75+
76+
Results:
77+
pid - auth.posts.pid (Column)
78+
content - auth.posts.content (Column)
79+
created_at - auth.posts.created_at (Column)
80+
title - auth.posts.title (Column)
81+
user_id - auth.posts.user_id (Column)
82+
83+
--------------
84+
85+
select u.id, p.pid |
86+
from auth.users u join auth.posts p on u.id = p.user_id;
87+
88+
No Results
89+
90+
--------------

0 commit comments

Comments
 (0)