Skip to content

Commit 985bc71

Browse files
intermediary
1 parent 9d3df7c commit 985bc71

File tree

3 files changed

+66
-14
lines changed

3 files changed

+66
-14
lines changed

crates/pgls_completions/src/providers/keywords.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,12 @@ mod tests {
409409
use pgls_test_utils::QueryWithCursorPosition;
410410
use sqlx::PgPool;
411411

412-
use crate::test_helper::{
413-
CompletionAssertion, TestCompletionsCase, TestCompletionsSuite, assert_complete_results,
414-
assert_no_complete_results,
412+
use crate::{
413+
CompletionItemKind,
414+
test_helper::{
415+
CompletionAssertion, TestCompletionsCase, TestCompletionsSuite,
416+
assert_complete_results, assert_no_complete_results,
417+
},
415418
};
416419

417420
#[sqlx::test]
@@ -490,6 +493,28 @@ mod tests {
490493
.await;
491494
}
492495

496+
#[sqlx::test]
497+
async fn completes_columsn_after_select(pool: PgPool) {
498+
let setup = r#"
499+
create table public.users (
500+
id serial primary key,
501+
email varchar(255)
502+
);
503+
"#;
504+
505+
let query = format!("select {}", QueryWithCursorPosition::cursor_marker());
506+
507+
assert_complete_results(
508+
query.as_str(),
509+
vec![CompletionAssertion::KindNotExists(
510+
CompletionItemKind::Keyword,
511+
)],
512+
Some(setup),
513+
&pool,
514+
)
515+
.await;
516+
}
517+
493518
#[sqlx::test]
494519
async fn completes_after_from_clause(pool: PgPool) {
495520
let setup = r#"

crates/pgls_completions/src/relevance/filtering.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use async_std::task::current;
12
use pgls_schema_cache::ProcKind;
23
use pgls_treesitter::{
34
context::{TreesitterContext, WrappingClause, WrappingNode},
@@ -496,12 +497,6 @@ impl CompletionFilter<'_> {
496497
start_position,
497498
});
498499

499-
let (clause_to_investigate, clause_completed) = if ctx.node_under_cursor.kind() != "ERROR" {
500-
(ctx.current_clause, ctx.current_clause_completed)
501-
} else {
502-
(ctx.previous_clause, ctx.previous_clause_completed)
503-
};
504-
505500
if tree.root_node().has_error() {
506501
return None;
507502
} else if ctx.previous_clause.is_some_and(|n| n.kind() == "ERROR") {
@@ -510,6 +505,12 @@ impl CompletionFilter<'_> {
510505
return Some(());
511506
}
512507

508+
let (clause_to_investigate, clause_completed) = if ctx.node_under_cursor.kind() != "ERROR" {
509+
(ctx.current_clause, ctx.current_clause_completed)
510+
} else {
511+
(ctx.previous_clause, ctx.previous_clause_completed)
512+
};
513+
513514
if clause_to_investigate.is_none() {
514515
if keyword.starts_statement {
515516
return Some(());
@@ -529,15 +530,22 @@ impl CompletionFilter<'_> {
529530
&& Some(current_parent.start_byte())
530531
== clause_to_investigate.map(|n| n.start_byte())
531532
{
533+
println!("does not change the clause");
532534
return Some(());
533535
}
534536
}
535537
}
536538

537-
// will allow those nodes that fully exchange the cluase
539+
// will allow those nodes that fully exchange the clause
538540
if let Some(current_node) = goto_node_at_position(&tree, start_byte) {
539541
// replacing the start byte means full exchanging
540542
if Some(current_node.start_byte()) == clause_to_investigate.map(|n| n.start_byte()) {
543+
println!("fully exchange the clause.");
544+
println!("current_node {}", current_node.kind());
545+
println!(
546+
"investigated {}",
547+
clause_to_investigate.map(|n| n.kind()).unwrap_or("here")
548+
);
541549
return Some(());
542550
}
543551
}

crates/pgls_treesitter/src/context/mod.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ impl<'a> TreesitterContext<'a> {
171171
ctx.check_previous_clause_completed();
172172
ctx.check_current_clause_completed();
173173

174+
println!("{:#?}", ctx);
175+
println!("{:#?}", ctx.previous_clause);
176+
println!("{:#?}", ctx.current_clause);
177+
174178
ctx
175179
}
176180

@@ -427,9 +431,17 @@ impl<'a> TreesitterContext<'a> {
427431
fn check_current_clause_completed(&mut self) {
428432
if let Some(closest_parent) = helper::goto_closest_parent_clause(self.node_under_cursor) {
429433
self.current_clause = Some(closest_parent);
430-
self.current_clause_completed = closest_parent
431-
.child_by_field_name("end")
432-
.is_some_and(|child| child != self.node_under_cursor)
434+
self.current_clause_completed =
435+
closest_parent
436+
.child_by_field_name("end")
437+
.is_some_and(|child| {
438+
let is_node_under_cursor = child == self.node_under_cursor;
439+
let matches_node_under_cursor_range = child.start_byte()
440+
== self.node_under_cursor.start_byte()
441+
&& child.end_byte() == self.node_under_cursor.end_byte();
442+
443+
!is_node_under_cursor && !matches_node_under_cursor_range
444+
})
433445
};
434446
}
435447

@@ -439,7 +451,14 @@ impl<'a> TreesitterContext<'a> {
439451
self.previous_clause = Some(closest_parent);
440452
self.previous_clause_completed = closest_parent
441453
.child_by_field_name("end")
442-
.is_some_and(|child| child != self.node_under_cursor)
454+
.is_some_and(|child| {
455+
let is_node_under_cursor = child == self.node_under_cursor;
456+
let matches_node_under_cursor_range = child.start_byte()
457+
== self.node_under_cursor.start_byte()
458+
&& child.end_byte() == self.node_under_cursor.end_byte();
459+
460+
!is_node_under_cursor && !matches_node_under_cursor_range
461+
})
443462
}
444463
};
445464
}

0 commit comments

Comments
 (0)