Skip to content

Commit f8daaf3

Browse files
fix: rest of tests
1 parent 0accdf2 commit f8daaf3

File tree

6 files changed

+106
-76
lines changed

6 files changed

+106
-76
lines changed

crates/pgls_completions/src/providers/tables.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ mod tests {
273273
assert_complete_results(
274274
format!("update {}", QueryWithCursorPosition::cursor_marker()).as_str(),
275275
vec![CompletionAssertion::LabelAndKind(
276-
"public".into(),
277-
CompletionItemKind::Schema,
276+
"coos".into(),
277+
CompletionItemKind::Table,
278278
)],
279279
None,
280280
&pool,
@@ -354,10 +354,10 @@ mod tests {
354354

355355
assert_complete_results(
356356
format!("delete from {}", QueryWithCursorPosition::cursor_marker()).as_str(),
357-
vec![
358-
CompletionAssertion::LabelAndKind("public".into(), CompletionItemKind::Schema),
359-
CompletionAssertion::LabelAndKind("coos".into(), CompletionItemKind::Table),
360-
],
357+
vec![CompletionAssertion::LabelAndKind(
358+
"coos".into(),
359+
CompletionItemKind::Table,
360+
)],
361361
None,
362362
&pool,
363363
)
@@ -418,8 +418,13 @@ mod tests {
418418
)
419419
.as_str(),
420420
vec![
421-
CompletionAssertion::LabelAndKind("public".into(), CompletionItemKind::Schema),
422421
CompletionAssertion::LabelAndKind("auth".into(), CompletionItemKind::Schema),
422+
CompletionAssertion::LabelAndKind(
423+
"information_schema".into(),
424+
CompletionItemKind::Schema,
425+
),
426+
CompletionAssertion::LabelAndKind("pg_catalog".into(), CompletionItemKind::Schema),
427+
CompletionAssertion::LabelAndKind("pg_toast".into(), CompletionItemKind::Schema),
423428
CompletionAssertion::LabelAndKind("posts".into(), CompletionItemKind::Table), // self-join
424429
CompletionAssertion::LabelAndKind("users".into(), CompletionItemKind::Table),
425430
],
@@ -454,7 +459,6 @@ mod tests {
454459
assert_complete_results(
455460
format!("alter table {}", QueryWithCursorPosition::cursor_marker()).as_str(),
456461
vec![
457-
CompletionAssertion::LabelAndKind("public".into(), CompletionItemKind::Schema),
458462
CompletionAssertion::LabelAndKind("auth".into(), CompletionItemKind::Schema),
459463
CompletionAssertion::LabelAndKind("posts".into(), CompletionItemKind::Table),
460464
CompletionAssertion::LabelAndKind("users".into(), CompletionItemKind::Table),
@@ -471,7 +475,6 @@ mod tests {
471475
)
472476
.as_str(),
473477
vec![
474-
CompletionAssertion::LabelAndKind("public".into(), CompletionItemKind::Schema),
475478
CompletionAssertion::LabelAndKind("auth".into(), CompletionItemKind::Schema),
476479
CompletionAssertion::LabelAndKind("posts".into(), CompletionItemKind::Table),
477480
CompletionAssertion::LabelAndKind("users".into(), CompletionItemKind::Table),
@@ -484,7 +487,6 @@ mod tests {
484487
assert_complete_results(
485488
format!("drop table {}", QueryWithCursorPosition::cursor_marker()).as_str(),
486489
vec![
487-
CompletionAssertion::LabelAndKind("public".into(), CompletionItemKind::Schema),
488490
CompletionAssertion::LabelAndKind("auth".into(), CompletionItemKind::Schema),
489491
CompletionAssertion::LabelAndKind("posts".into(), CompletionItemKind::Table),
490492
CompletionAssertion::LabelAndKind("users".into(), CompletionItemKind::Table),
@@ -501,7 +503,6 @@ mod tests {
501503
)
502504
.as_str(),
503505
vec![
504-
CompletionAssertion::LabelAndKind("public".into(), CompletionItemKind::Schema),
505506
CompletionAssertion::LabelAndKind("auth".into(), CompletionItemKind::Schema),
506507
CompletionAssertion::LabelAndKind("posts".into(), CompletionItemKind::Table), // self-join
507508
CompletionAssertion::LabelAndKind("users".into(), CompletionItemKind::Table),
@@ -529,7 +530,6 @@ mod tests {
529530
assert_complete_results(
530531
format!("insert into {}", QueryWithCursorPosition::cursor_marker()).as_str(),
531532
vec![
532-
CompletionAssertion::LabelAndKind("public".into(), CompletionItemKind::Schema),
533533
CompletionAssertion::LabelAndKind("auth".into(), CompletionItemKind::Schema),
534534
CompletionAssertion::LabelAndKind("users".into(), CompletionItemKind::Table),
535535
],
@@ -561,7 +561,6 @@ mod tests {
561561
)
562562
.as_str(),
563563
vec![
564-
CompletionAssertion::LabelAndKind("public".into(), CompletionItemKind::Schema),
565564
CompletionAssertion::LabelAndKind("auth".into(), CompletionItemKind::Schema),
566565
CompletionAssertion::LabelAndKind("users".into(), CompletionItemKind::Table),
567566
],
@@ -686,8 +685,14 @@ mod tests {
686685
)
687686
.as_str(),
688687
vec![
689-
CompletionAssertion::LabelAndKind("public".into(), CompletionItemKind::Schema),
690688
CompletionAssertion::LabelAndKind("auth".into(), CompletionItemKind::Schema),
689+
CompletionAssertion::LabelAndKind(
690+
"information_schema".into(),
691+
CompletionItemKind::Schema,
692+
),
693+
CompletionAssertion::LabelAndKind("pg_catalog".into(), CompletionItemKind::Schema),
694+
CompletionAssertion::LabelAndKind("pg_toast".into(), CompletionItemKind::Schema),
695+
CompletionAssertion::LabelAndKind("public".into(), CompletionItemKind::Schema),
691696
],
692697
None,
693698
&pool,

crates/pgls_completions/src/relevance/filtering.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use pgls_schema_cache::ProcKind;
22
use pgls_treesitter::context::{TreesitterContext, WrappingClause, WrappingNode};
33

4-
54
use super::CompletionRelevanceData;
65

76
#[derive(Debug)]
@@ -144,17 +143,23 @@ impl CompletionFilter<'_> {
144143
]) && matches!(f.kind, ProcKind::Aggregate))
145144
}
146145

147-
CompletionRelevanceData::Table(_) => ctx.node_under_cursor_is_within_field(&[
148-
"object_reference_1of1",
149-
"object_reference_1of2",
150-
"object_reference_2of2",
151-
"object_reference_2of3",
152-
"table_reference_1of1",
153-
"column_reference_1of1",
154-
"column_reference_1of2",
155-
"column_reference_2of2",
156-
]),
157-
146+
CompletionRelevanceData::Table(_) => {
147+
ctx.node_under_cursor_is_within_field(&[
148+
"object_reference_1of1",
149+
"object_reference_1of2",
150+
"object_reference_2of2",
151+
"object_reference_2of3",
152+
"table_reference_1of1",
153+
"column_reference_1of1",
154+
"column_reference_1of2",
155+
"column_reference_2of2",
156+
]) && !ctx.history_ends_with(&[
157+
"update",
158+
"assignment",
159+
"column_reference",
160+
"any_identifier",
161+
])
162+
}
158163
_ => false,
159164
},
160165

@@ -170,7 +175,12 @@ impl CompletionFilter<'_> {
170175
.map(|clause| {
171176
match self.data {
172177
CompletionRelevanceData::Table(_) => match clause {
173-
WrappingClause::From | WrappingClause::Update => true,
178+
WrappingClause::From => true,
179+
180+
WrappingClause::Update => ctx
181+
.wrapping_node_kind
182+
.as_ref()
183+
.is_none_or(|n| n != &WrappingNode::Assignment),
174184

175185
WrappingClause::RevokeStatement | WrappingClause::GrantStatement => ctx
176186
.history_ends_with(&[

crates/pgls_completions/src/test_helper.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ pub(crate) async fn assert_complete_results(
186186
items.len()
187187
);
188188

189+
if !items.is_empty() {
190+
let max_len = std::cmp::min(items.len(), 5);
191+
println!("Completion Items: {:#?}", &items[..max_len]);
192+
}
193+
189194
for item in &items {
190195
for assertion in &not_existing {
191196
assertion.assert(item);

crates/pgls_treesitter/src/context/mod.rs

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,6 @@ impl<'a> TreesitterContext<'a> {
153153
ctx.gather_tree_context();
154154
ctx.gather_info_from_ts_queries();
155155

156-
println!("TreesitterContext: {:#?}", ctx);
157-
println!(
158-
"NodeUnderCursor: {:#?}",
159-
ctx.get_node_under_cursor_content()
160-
);
161-
162156
ctx
163157
}
164158

@@ -274,14 +268,14 @@ impl<'a> TreesitterContext<'a> {
274268

275269
if chars
276270
.nth(self.position)
277-
.is_some_and(|c| !c.is_ascii_whitespace() && !&[';', ')'].contains(&c))
271+
.is_some_and(|c| c.is_ascii_whitespace() || [';', ')', ',', '('].contains(&c))
278272
{
279-
self.position = cmp::min(self.position, self.text.len().saturating_sub(1));
280-
} else {
281273
self.position = cmp::min(
282274
self.position.saturating_sub(1),
283275
self.text.len().saturating_sub(1),
284276
);
277+
} else {
278+
self.position = cmp::min(self.position, self.text.len().saturating_sub(1));
285279
}
286280

287281
cursor.goto_first_child_for_byte(self.position);
@@ -380,17 +374,6 @@ impl<'a> TreesitterContext<'a> {
380374
if current_node.child_count() == 0
381375
|| current_node.first_child_for_byte(self.position).is_none()
382376
{
383-
// if the cursor is exactly at the start of a punctuation node,
384-
// prefer the previous sibling (e.g., when cursor is at "i|," prefer "i" over ",")
385-
let is_punctuation = matches!(current_node.kind(), "," | ")");
386-
if is_punctuation && current_node.start_byte() == self.position {
387-
if let Some(prev) = current_node.prev_sibling() {
388-
if prev.end_byte() == self.position {
389-
self.node_under_cursor = prev;
390-
return;
391-
}
392-
}
393-
}
394377
self.node_under_cursor = current_node;
395378
return;
396379
}
@@ -641,8 +624,8 @@ impl<'a> TreesitterContext<'a> {
641624
/// Verifies whether the node_under_cursor has the passed in ancestors in the right order.
642625
/// Note that you need to pass in the ancestors in the order as they would appear in the tree:
643626
///
644-
/// If the tree shows `relation > object_reference > any_identifier` and the "any_identifier" is a leaf node,
645-
/// you need to pass `&["relation", "object_reference"]`.
627+
/// If the tree shows `relation > object_reference > any_identifier`
628+
/// you need to pass `&["relation", "object_reference", "any_identifier"]`.
646629
pub fn history_ends_with(&self, expected_ancestors: &[&'static str]) -> bool {
647630
self.scope_tracker
648631
.current()
@@ -1115,4 +1098,58 @@ mod tests {
11151098
// should simply not panic
11161099
let _ = TreesitterContext::new(params);
11171100
}
1101+
1102+
#[test]
1103+
fn corrects_the_position_accordingly() {
1104+
let query = "select id, email from some_foo(param1, param2);";
1105+
// 01234567890123456789012345678901234567890123456
1106+
// 0 10 20 30 40
1107+
1108+
let tree = get_tree(query);
1109+
1110+
struct TestCase {
1111+
cursor_position: u32,
1112+
context_position: usize,
1113+
}
1114+
1115+
let cases: Vec<TestCase> = vec![
1116+
TestCase {
1117+
// moves from ',' to 'l' of 'email'
1118+
cursor_position: 9,
1119+
context_position: 8,
1120+
},
1121+
TestCase {
1122+
// stays on 'e' of 'email'
1123+
cursor_position: 11,
1124+
context_position: 11,
1125+
},
1126+
TestCase {
1127+
// moves from '(' to 'o' of 'some_foo'
1128+
cursor_position: 30,
1129+
context_position: 29,
1130+
},
1131+
TestCase {
1132+
// moves from ')' to '2' of 'param2'
1133+
cursor_position: 45,
1134+
context_position: 44,
1135+
},
1136+
];
1137+
1138+
for case in cases {
1139+
let params = TreeSitterContextParams {
1140+
position: case.cursor_position.into(),
1141+
text: &query,
1142+
tree: &tree,
1143+
};
1144+
1145+
// should simply not panic
1146+
let ctx = TreesitterContext::new(params);
1147+
1148+
assert_eq!(
1149+
ctx.position, case.context_position,
1150+
"received {} but expected {}",
1151+
ctx.position, case.context_position
1152+
);
1153+
}
1154+
}
11181155
}

crates/pgls_treesitter_grammar/grammar.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,8 +2241,6 @@ module.exports = grammar({
22412241
)
22422242
),
22432243

2244-
assignment_list: ($) => seq($.assignment, repeat(seq(",", $.assignment))),
2245-
22462244
_set_values: ($) => seq($.keyword_set, comma_list($.assignment, true)),
22472245

22482246
_column_list: ($) => paren_list(alias($._column, $.column), true),

crates/pgls_treesitter_grammar/src/grammar.json

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

0 commit comments

Comments
 (0)