1
- use std:: borrow:: Cow ;
1
+ use std:: { borrow:: Cow , cmp :: max } ;
2
2
3
3
use pgt_text_size:: TextSize ;
4
4
24
24
if cursor_inbetween_nodes ( params. tree , params. position )
25
25
|| cursor_prepared_to_write_token_after_last_node ( params. tree , params. position )
26
26
|| cursor_before_semicolon ( params. tree , params. position )
27
+ || cursor_on_a_dot ( & params. text , params. position )
27
28
{
28
29
SanitizedCompletionParams :: with_adjusted_sql ( params)
29
30
} else {
@@ -44,12 +45,13 @@ where
44
45
45
46
let mut sql_iter = params. text . chars ( ) ;
46
47
47
- for idx in 0 ..cursor_pos + 1 {
48
+ let max = max ( cursor_pos + 1 , params. text . len ( ) ) ;
49
+
50
+ for idx in 0 ..max {
48
51
match sql_iter. next ( ) {
49
52
Some ( c) => {
50
53
if idx == cursor_pos {
51
54
sql. push_str ( SANITIZED_TOKEN ) ;
52
- sql. push ( ' ' ) ;
53
55
}
54
56
sql. push ( c) ;
55
57
}
@@ -149,6 +151,11 @@ fn cursor_prepared_to_write_token_after_last_node(
149
151
cursor_pos == tree. root_node ( ) . end_byte ( ) + 1
150
152
}
151
153
154
+ fn cursor_on_a_dot ( sql : & str , position : TextSize ) -> bool {
155
+ let position: usize = position. into ( ) ;
156
+ sql. chars ( ) . nth ( position - 1 ) . is_some_and ( |c| c == '.' )
157
+ }
158
+
152
159
fn cursor_before_semicolon ( tree : & tree_sitter:: Tree , position : TextSize ) -> bool {
153
160
let mut cursor = tree. walk ( ) ;
154
161
let mut leaf_node = tree. root_node ( ) ;
@@ -198,7 +205,7 @@ mod tests {
198
205
use pgt_text_size:: TextSize ;
199
206
200
207
use crate :: sanitization:: {
201
- cursor_before_semicolon, cursor_inbetween_nodes,
208
+ cursor_before_semicolon, cursor_inbetween_nodes, cursor_on_a_dot ,
202
209
cursor_prepared_to_write_token_after_last_node,
203
210
} ;
204
211
@@ -263,6 +270,20 @@ mod tests {
263
270
) ) ;
264
271
}
265
272
273
+ #[ test]
274
+ fn on_a_dot ( ) {
275
+ let input = "select * from private." ;
276
+
277
+ // select * from private.| <-- on a dot
278
+ assert ! ( cursor_on_a_dot( input, TextSize :: new( 22 ) ) ) ;
279
+
280
+ // select * from private|. <-- before the dot
281
+ assert ! ( !cursor_on_a_dot( input, TextSize :: new( 21 ) ) ) ;
282
+
283
+ // select * from private. | <-- too far off the dot
284
+ assert ! ( !cursor_on_a_dot( input, TextSize :: new( 23 ) ) ) ;
285
+ }
286
+
266
287
#[ test]
267
288
fn test_cursor_before_semicolon ( ) {
268
289
// Idx "13" is the exlusive end of `select * from` (first space after from)
0 commit comments