|
| 1 | +use pg_schema_cache::SchemaCache; |
| 2 | + |
1 | 3 | use crate::CompletionParams;
|
2 | 4 |
|
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 | +} |
4 | 12 |
|
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 | + } |
8 | 22 | }
|
9 | 23 | }
|
| 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