Skip to content

Commit 2f18db5

Browse files
fix: algorithm handles treesitter error nodes
1 parent 7724b2f commit 2f18db5

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

crates/pg_completions/src/context.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ impl<'a> CompletionContext<'a> {
9191
// go to the statement node that matches the position
9292
let current_node_kind = cursor.node().kind();
9393

94+
dbg!(current_node_kind);
95+
dbg!(self.position);
96+
9497
cursor.goto_first_child_for_byte(self.position);
9598

9699
self.gather_context_from_node(cursor, current_node_kind);
@@ -104,6 +107,14 @@ impl<'a> CompletionContext<'a> {
104107
let current_node = cursor.node();
105108
let current_node_kind = current_node.kind();
106109

110+
println!("inside..");
111+
dbg!(current_node_kind);
112+
113+
if current_node_kind == previous_node_kind {
114+
self.ts_node = Some(current_node);
115+
return;
116+
}
117+
107118
match previous_node_kind {
108119
"statement" => self.wrapping_clause_type = current_node_kind.try_into().ok(),
109120
"invocation" => self.is_invocation = true,
@@ -267,4 +278,29 @@ mod tests {
267278
assert_eq!(ctx.is_invocation, is_invocation);
268279
}
269280
}
281+
282+
#[test]
283+
fn get_ts_node_content_does_not_fail_on_error_nodes() {
284+
let query = format!("select * from {}", CURSOR_POS);
285+
286+
let position = query.find(CURSOR_POS).unwrap();
287+
let text = query.replace(CURSOR_POS, "");
288+
289+
let tree = get_tree(text.as_str());
290+
291+
let params = crate::CompletionParams {
292+
position: (position as u32).into(),
293+
text: text,
294+
tree: Some(&tree),
295+
schema: &pg_schema_cache::SchemaCache::new(),
296+
};
297+
298+
let ctx = CompletionContext::new(&params);
299+
300+
let node = ctx.ts_node.map(|n| n.clone());
301+
302+
println!("node kind: {}", node.as_ref().unwrap().kind());
303+
304+
ctx.get_ts_node_content(node.unwrap());
305+
}
270306
}

crates/pg_completions/src/providers/tables.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ mod tests {
7676
(format!("select * from us{}", CURSOR_POS), "users"),
7777
(format!("select * from em{}", CURSOR_POS), "emails"),
7878
// TODO: Fix queries with tree-sitter errors.
79-
// (format!("select * from {}", CURSOR_POS), "addresses"),
79+
(format!("select * from {}", CURSOR_POS), "addresses"),
8080
];
8181

8282
for (query, expected_label) in test_cases {

crates/pg_test_utils/src/bin/tree_print.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ fn main() {
2929

3030
fn print_tree(node: &tree_sitter::Node, source: &str, level: usize) {
3131
let indent = " ".repeat(level);
32-
let node_text = node.utf8_text(source.as_bytes()).unwrap_or("NO_NAME");
32+
33+
let node_text = node
34+
.utf8_text(source.as_bytes())
35+
.unwrap_or("NO_NAME")
36+
.split_whitespace()
37+
.collect::<Vec<&str>>()
38+
.join(" ");
3339

3440
println!(
3541
"{}{} [{}..{}] '{}'",

justfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ new-crate name:
130130
cargo new --lib crates/{{snakecase(name)}}
131131
cargo run -p xtask_codegen -- new-crate --name={{snakecase(name)}}
132132

133+
# Prints the treesitter tree of the given SQL file
134+
print-tree file:
135+
cargo run --bin tree_print -- -f {{file}}
136+
133137
# Creates a new changeset for the final changelog
134138
# new-changeset:
135139
# knope document-change

0 commit comments

Comments
 (0)