@@ -50,28 +50,12 @@ impl TryFrom<String> for ClauseType {
5050
5151pub ( crate ) struct CompletionContext < ' a > {
5252 pub node_under_cursor : Option < tree_sitter:: Node < ' a > > ,
53- pub previous_node : Option < tree_sitter:: Node < ' a > > ,
5453
5554 pub tree : Option < & ' a tree_sitter:: Tree > ,
5655 pub text : & ' a str ,
5756 pub schema_cache : & ' a SchemaCache ,
5857 pub position : usize ,
5958
60- /// If the cursor of the user is offset to the right of the statement,
61- /// we'll have to move it back to the last node, otherwise, tree-sitter will break.
62- /// However, knowing that the user is typing on the "next" node lets us prioritize different completion results.
63- /// We consider an offset of up to two characters as valid.
64- ///
65- /// Example:
66- ///
67- /// ```
68- /// select * from {}
69- /// ```
70- ///
71- /// We'll adjust the cursor position so it lies on the "from" token – but we're looking
72- /// for table completions.
73- pub cursor_offset_from_end : bool ,
74-
7559 pub schema_name : Option < String > ,
7660 pub wrapping_clause_type : Option < ClauseType > ,
7761 pub is_invocation : bool ,
@@ -81,14 +65,12 @@ pub(crate) struct CompletionContext<'a> {
8165}
8266
8367impl < ' a > CompletionContext < ' a > {
84- pub fn new ( params : & ' a CompletionParams ) -> Self {
68+ pub fn new ( params : & ' a CompletionParams , usable_tree : Option < & ' a tree_sitter :: Tree > ) -> Self {
8569 let mut ctx = Self {
86- tree : params . tree ,
70+ tree : usable_tree ,
8771 text : & params. text ,
8872 schema_cache : params. schema ,
8973 position : usize:: from ( params. position ) ,
90- cursor_offset_from_end : false ,
91- previous_node : None ,
9274 node_under_cursor : None ,
9375 schema_name : None ,
9476 wrapping_clause_type : None ,
@@ -97,7 +79,10 @@ impl<'a> CompletionContext<'a> {
9779 mentioned_relations : HashMap :: new ( ) ,
9880 } ;
9981
82+ tracing:: warn!( "gathering tree context" ) ;
10083 ctx. gather_tree_context ( ) ;
84+
85+ tracing:: warn!( "gathering info from ts query" ) ;
10186 ctx. gather_info_from_ts_queries ( ) ;
10287
10388 ctx
@@ -164,14 +149,10 @@ impl<'a> CompletionContext<'a> {
164149 * `select * from use {}` becomes `select * from use{}`.
165150 */
166151 let current_node = cursor. node ( ) ;
167- let position_cache = self . position . clone ( ) ;
168152 while cursor. goto_first_child_for_byte ( self . position ) . is_none ( ) && self . position > 0 {
169153 self . position -= 1 ;
170154 }
171155
172- let cursor_offset = position_cache - self . position ;
173- self . cursor_offset_from_end = cursor_offset > 0 && cursor_offset <= 2 ;
174-
175156 self . gather_context_from_node ( cursor, current_node) ;
176157 }
177158
@@ -223,23 +204,11 @@ impl<'a> CompletionContext<'a> {
223204
224205 // We have arrived at the leaf node
225206 if current_node. child_count ( ) == 0 {
226- if self . cursor_offset_from_end {
207+ if self . get_ts_node_content ( current_node ) . unwrap ( ) == "REPLACED_TOKEN" {
227208 self . node_under_cursor = None ;
228- self . previous_node = Some ( current_node) ;
229209 } else {
230- // for the previous node, either select the previous sibling,
231- // or collect the parent's previous sibling's last child.
232- let previous = match current_node. prev_sibling ( ) {
233- Some ( n) => Some ( n) ,
234- None => {
235- let sib_of_parent = parent_node. prev_sibling ( ) ;
236- sib_of_parent. and_then ( |p| p. children ( & mut cursor) . last ( ) )
237- }
238- } ;
239210 self . node_under_cursor = Some ( current_node) ;
240- self . previous_node = previous;
241211 }
242-
243212 return ;
244213 }
245214
@@ -305,7 +274,7 @@ mod tests {
305274 schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
306275 } ;
307276
308- let ctx = CompletionContext :: new ( & params) ;
277+ let ctx = CompletionContext :: new ( & params, Some ( & tree ) ) ;
309278
310279 assert_eq ! ( ctx. wrapping_clause_type, expected_clause. try_into( ) . ok( ) ) ;
311280 }
@@ -337,7 +306,7 @@ mod tests {
337306 schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
338307 } ;
339308
340- let ctx = CompletionContext :: new ( & params) ;
309+ let ctx = CompletionContext :: new ( & params, Some ( & tree ) ) ;
341310
342311 assert_eq ! ( ctx. schema_name, expected_schema. map( |f| f. to_string( ) ) ) ;
343312 }
@@ -371,7 +340,7 @@ mod tests {
371340 schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
372341 } ;
373342
374- let ctx = CompletionContext :: new ( & params) ;
343+ let ctx = CompletionContext :: new ( & params, Some ( & tree ) ) ;
375344
376345 assert_eq ! ( ctx. is_invocation, is_invocation) ;
377346 }
@@ -396,7 +365,7 @@ mod tests {
396365 schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
397366 } ;
398367
399- let ctx = CompletionContext :: new ( & params) ;
368+ let ctx = CompletionContext :: new ( & params, Some ( & tree ) ) ;
400369
401370 let node = ctx. node_under_cursor . unwrap ( ) ;
402371
@@ -424,7 +393,7 @@ mod tests {
424393 schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
425394 } ;
426395
427- let ctx = CompletionContext :: new ( & params) ;
396+ let ctx = CompletionContext :: new ( & params, Some ( & tree ) ) ;
428397
429398 let node = ctx. node_under_cursor . unwrap ( ) ;
430399
@@ -450,7 +419,7 @@ mod tests {
450419 schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
451420 } ;
452421
453- let ctx = CompletionContext :: new ( & params) ;
422+ let ctx = CompletionContext :: new ( & params, Some ( & tree ) ) ;
454423
455424 let node = ctx. node_under_cursor . unwrap ( ) ;
456425
@@ -475,7 +444,7 @@ mod tests {
475444 schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
476445 } ;
477446
478- let ctx = CompletionContext :: new ( & params) ;
447+ let ctx = CompletionContext :: new ( & params, Some ( & tree ) ) ;
479448
480449 let node = ctx. node_under_cursor . unwrap ( ) ;
481450
0 commit comments