@@ -50,28 +50,12 @@ impl TryFrom<String> for ClauseType {
50
50
51
51
pub ( crate ) struct CompletionContext < ' a > {
52
52
pub node_under_cursor : Option < tree_sitter:: Node < ' a > > ,
53
- pub previous_node : Option < tree_sitter:: Node < ' a > > ,
54
53
55
54
pub tree : Option < & ' a tree_sitter:: Tree > ,
56
55
pub text : & ' a str ,
57
56
pub schema_cache : & ' a SchemaCache ,
58
57
pub position : usize ,
59
58
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
-
75
59
pub schema_name : Option < String > ,
76
60
pub wrapping_clause_type : Option < ClauseType > ,
77
61
pub is_invocation : bool ,
@@ -81,14 +65,12 @@ pub(crate) struct CompletionContext<'a> {
81
65
}
82
66
83
67
impl < ' 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 {
85
69
let mut ctx = Self {
86
- tree : params . tree ,
70
+ tree : usable_tree ,
87
71
text : & params. text ,
88
72
schema_cache : params. schema ,
89
73
position : usize:: from ( params. position ) ,
90
- cursor_offset_from_end : false ,
91
- previous_node : None ,
92
74
node_under_cursor : None ,
93
75
schema_name : None ,
94
76
wrapping_clause_type : None ,
@@ -97,7 +79,10 @@ impl<'a> CompletionContext<'a> {
97
79
mentioned_relations : HashMap :: new ( ) ,
98
80
} ;
99
81
82
+ tracing:: warn!( "gathering tree context" ) ;
100
83
ctx. gather_tree_context ( ) ;
84
+
85
+ tracing:: warn!( "gathering info from ts query" ) ;
101
86
ctx. gather_info_from_ts_queries ( ) ;
102
87
103
88
ctx
@@ -164,14 +149,10 @@ impl<'a> CompletionContext<'a> {
164
149
* `select * from use {}` becomes `select * from use{}`.
165
150
*/
166
151
let current_node = cursor. node ( ) ;
167
- let position_cache = self . position . clone ( ) ;
168
152
while cursor. goto_first_child_for_byte ( self . position ) . is_none ( ) && self . position > 0 {
169
153
self . position -= 1 ;
170
154
}
171
155
172
- let cursor_offset = position_cache - self . position ;
173
- self . cursor_offset_from_end = cursor_offset > 0 && cursor_offset <= 2 ;
174
-
175
156
self . gather_context_from_node ( cursor, current_node) ;
176
157
}
177
158
@@ -223,23 +204,11 @@ impl<'a> CompletionContext<'a> {
223
204
224
205
// We have arrived at the leaf node
225
206
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" {
227
208
self . node_under_cursor = None ;
228
- self . previous_node = Some ( current_node) ;
229
209
} 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
- } ;
239
210
self . node_under_cursor = Some ( current_node) ;
240
- self . previous_node = previous;
241
211
}
242
-
243
212
return ;
244
213
}
245
214
@@ -305,7 +274,7 @@ mod tests {
305
274
schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
306
275
} ;
307
276
308
- let ctx = CompletionContext :: new ( & params) ;
277
+ let ctx = CompletionContext :: new ( & params, Some ( & tree ) ) ;
309
278
310
279
assert_eq ! ( ctx. wrapping_clause_type, expected_clause. try_into( ) . ok( ) ) ;
311
280
}
@@ -337,7 +306,7 @@ mod tests {
337
306
schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
338
307
} ;
339
308
340
- let ctx = CompletionContext :: new ( & params) ;
309
+ let ctx = CompletionContext :: new ( & params, Some ( & tree ) ) ;
341
310
342
311
assert_eq ! ( ctx. schema_name, expected_schema. map( |f| f. to_string( ) ) ) ;
343
312
}
@@ -371,7 +340,7 @@ mod tests {
371
340
schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
372
341
} ;
373
342
374
- let ctx = CompletionContext :: new ( & params) ;
343
+ let ctx = CompletionContext :: new ( & params, Some ( & tree ) ) ;
375
344
376
345
assert_eq ! ( ctx. is_invocation, is_invocation) ;
377
346
}
@@ -396,7 +365,7 @@ mod tests {
396
365
schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
397
366
} ;
398
367
399
- let ctx = CompletionContext :: new ( & params) ;
368
+ let ctx = CompletionContext :: new ( & params, Some ( & tree ) ) ;
400
369
401
370
let node = ctx. node_under_cursor . unwrap ( ) ;
402
371
@@ -424,7 +393,7 @@ mod tests {
424
393
schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
425
394
} ;
426
395
427
- let ctx = CompletionContext :: new ( & params) ;
396
+ let ctx = CompletionContext :: new ( & params, Some ( & tree ) ) ;
428
397
429
398
let node = ctx. node_under_cursor . unwrap ( ) ;
430
399
@@ -450,7 +419,7 @@ mod tests {
450
419
schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
451
420
} ;
452
421
453
- let ctx = CompletionContext :: new ( & params) ;
422
+ let ctx = CompletionContext :: new ( & params, Some ( & tree ) ) ;
454
423
455
424
let node = ctx. node_under_cursor . unwrap ( ) ;
456
425
@@ -475,7 +444,7 @@ mod tests {
475
444
schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
476
445
} ;
477
446
478
- let ctx = CompletionContext :: new ( & params) ;
447
+ let ctx = CompletionContext :: new ( & params, Some ( & tree ) ) ;
479
448
480
449
let node = ctx. node_under_cursor . unwrap ( ) ;
481
450
0 commit comments