Skip to content

Commit 7c279d5

Browse files
completion context getting ahead
1 parent 337687f commit 7c279d5

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

crates/pg_completions/src/context.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,48 @@
1+
use pg_schema_cache::SchemaCache;
2+
13
use crate::CompletionParams;
24

3-
pub struct CompletionContext {}
5+
pub struct CompletionContext<'a> {
6+
pub ts_node: Option<tree_sitter::Node<'a>>,
7+
pub tree: Option<&'a tree_sitter::Tree>,
8+
pub text: &'a str,
9+
pub schema_cache: &'a SchemaCache,
10+
pub original_token: Option<char>,
11+
}
412

5-
impl CompletionContext {
6-
pub fn new(params: &CompletionParams) -> Self {
7-
todo!()
13+
impl<'a> CompletionContext<'a> {
14+
pub fn new(params: &'a CompletionParams) -> Self {
15+
Self {
16+
ts_node: find_ts_node(params),
17+
tree: params.tree,
18+
text: params.text,
19+
schema_cache: params.schema,
20+
original_token: find_original_token(params),
21+
}
822
}
923
}
24+
25+
fn find_original_token<'a>(params: &'a CompletionParams) -> Option<char> {
26+
let idx = usize::from(params.position);
27+
params.text.chars().nth(idx)
28+
}
29+
30+
fn find_ts_node<'a>(params: &'a CompletionParams) -> Option<tree_sitter::Node<'a>> {
31+
let tree = params.tree?;
32+
33+
let mut node = tree.root_node().named_descendant_for_byte_range(
34+
usize::from(params.position),
35+
usize::from(params.position),
36+
)?;
37+
38+
let node_range = node.range();
39+
while let Some(parent) = node.parent() {
40+
if parent.range() != node_range {
41+
break;
42+
}
43+
44+
node = parent;
45+
}
46+
47+
Some(node)
48+
}

0 commit comments

Comments
 (0)