45
45
' larger : ' smaller ,
46
46
{
47
47
fn from ( params : CompletionParams < ' larger > ) -> Self {
48
- if cursor_inbetween_nodes ( params. tree , params. position )
49
- || cursor_prepared_to_write_token_after_last_node ( params. tree , params. position )
48
+ if cursor_inbetween_nodes ( & params. text , params. position )
49
+ || cursor_prepared_to_write_token_after_last_node ( & params. text , params. position )
50
50
|| cursor_before_semicolon ( params. tree , params. position )
51
51
|| cursor_on_a_dot ( & params. text , params. position )
52
52
|| cursor_between_double_quotes ( & params. text , params. position )
@@ -125,37 +125,17 @@ where
125
125
/// select |from users; -- cursor "touches" from node. returns false.
126
126
/// select | from users; -- cursor is between select and from nodes. returns true.
127
127
/// ```
128
- fn cursor_inbetween_nodes ( tree : & tree_sitter:: Tree , position : TextSize ) -> bool {
129
- let mut cursor = tree. walk ( ) ;
130
- let mut leaf_node = tree. root_node ( ) ;
131
-
132
- let byte = position. into ( ) ;
133
-
134
- // if the cursor escapes the root node, it can't be between nodes.
135
- if byte < leaf_node. start_byte ( ) || byte >= leaf_node. end_byte ( ) {
136
- return false ;
137
- }
128
+ fn cursor_inbetween_nodes ( sql : & str , position : TextSize ) -> bool {
129
+ let position: usize = position. into ( ) ;
130
+ let mut chars = sql. chars ( ) ;
138
131
139
- /*
140
- * Get closer and closer to the leaf node, until
141
- * a) there is no more child *for the node* or
142
- * b) there is no more child *under the cursor*.
143
- */
144
- loop {
145
- let child_idx = cursor. goto_first_child_for_byte ( position. into ( ) ) ;
146
- if child_idx. is_none ( ) {
147
- break ;
148
- }
149
- leaf_node = cursor. node ( ) ;
150
- }
132
+ let previous_whitespace = chars
133
+ . nth ( position - 1 )
134
+ . is_some_and ( |c| c. is_ascii_whitespace ( ) ) ;
151
135
152
- let cursor_on_leafnode = byte >= leaf_node . start_byte ( ) && leaf_node . end_byte ( ) >= byte ;
136
+ let current_whitespace = chars . next ( ) . is_some_and ( |c| c . is_ascii_whitespace ( ) ) ;
153
137
154
- /*
155
- * The cursor is inbetween nodes if it is not within the range
156
- * of a leaf node.
157
- */
158
- !cursor_on_leafnode
138
+ previous_whitespace && current_whitespace
159
139
}
160
140
161
141
/// Checks if the cursor is positioned after the last node,
@@ -166,12 +146,9 @@ fn cursor_inbetween_nodes(tree: &tree_sitter::Tree, position: TextSize) -> bool
166
146
/// select * from| -- user still needs to type a space
167
147
/// select * from | -- too far off.
168
148
/// ```
169
- fn cursor_prepared_to_write_token_after_last_node (
170
- tree : & tree_sitter:: Tree ,
171
- position : TextSize ,
172
- ) -> bool {
149
+ fn cursor_prepared_to_write_token_after_last_node ( sql : & str , position : TextSize ) -> bool {
173
150
let cursor_pos: usize = position. into ( ) ;
174
- cursor_pos == tree . root_node ( ) . end_byte ( ) + 1
151
+ cursor_pos == sql . len ( ) + 1
175
152
}
176
153
177
154
fn cursor_on_a_dot ( sql : & str , position : TextSize ) -> bool {
@@ -243,58 +220,44 @@ mod tests {
243
220
// note: two spaces between select and from.
244
221
let input = "select from users;" ;
245
222
246
- let mut parser = tree_sitter:: Parser :: new ( ) ;
247
- parser
248
- . set_language ( tree_sitter_sql:: language ( ) )
249
- . expect ( "Error loading sql language" ) ;
250
-
251
- let tree = parser. parse ( input, None ) . unwrap ( ) ;
252
-
253
223
// select | from users; <-- just right, one space after select token, one space before from
254
- assert ! ( cursor_inbetween_nodes( & tree , TextSize :: new( 7 ) ) ) ;
224
+ assert ! ( cursor_inbetween_nodes( input , TextSize :: new( 7 ) ) ) ;
255
225
256
226
// select| from users; <-- still on select token
257
- assert ! ( !cursor_inbetween_nodes( & tree , TextSize :: new( 6 ) ) ) ;
227
+ assert ! ( !cursor_inbetween_nodes( input , TextSize :: new( 6 ) ) ) ;
258
228
259
229
// select |from users; <-- already on from token
260
- assert ! ( !cursor_inbetween_nodes( & tree , TextSize :: new( 8 ) ) ) ;
230
+ assert ! ( !cursor_inbetween_nodes( input , TextSize :: new( 8 ) ) ) ;
261
231
262
232
// select from users;|
263
- assert ! ( !cursor_inbetween_nodes( & tree , TextSize :: new( 19 ) ) ) ;
233
+ assert ! ( !cursor_inbetween_nodes( input , TextSize :: new( 19 ) ) ) ;
264
234
}
265
235
266
236
#[ test]
267
237
fn test_cursor_after_nodes ( ) {
268
238
let input = "select * from" ;
269
239
270
- let mut parser = tree_sitter:: Parser :: new ( ) ;
271
- parser
272
- . set_language ( tree_sitter_sql:: language ( ) )
273
- . expect ( "Error loading sql language" ) ;
274
-
275
- let tree = parser. parse ( input, None ) . unwrap ( ) ;
276
-
277
240
// select * from| <-- still on previous token
278
241
assert ! ( !cursor_prepared_to_write_token_after_last_node(
279
- & tree ,
242
+ input ,
280
243
TextSize :: new( 13 )
281
244
) ) ;
282
245
283
246
// select * from | <-- too far off, two spaces afterward
284
247
assert ! ( !cursor_prepared_to_write_token_after_last_node(
285
- & tree ,
248
+ input ,
286
249
TextSize :: new( 15 )
287
250
) ) ;
288
251
289
252
// select * |from <-- it's within
290
253
assert ! ( !cursor_prepared_to_write_token_after_last_node(
291
- & tree ,
254
+ input ,
292
255
TextSize :: new( 9 )
293
256
) ) ;
294
257
295
258
// select * from | <-- just right
296
259
assert ! ( cursor_prepared_to_write_token_after_last_node(
297
- & tree ,
260
+ input ,
298
261
TextSize :: new( 14 )
299
262
) ) ;
300
263
}
@@ -353,5 +316,11 @@ mod tests {
353
316
354
317
// select * from "|" <-- between quotations
355
318
assert ! ( cursor_between_double_quotes( input, TextSize :: new( 15 ) ) ) ;
319
+
320
+ // select * from "r|" <-- between quotations, but there's
321
+ // a letter inside
322
+ let input = "select * from \" r\" " ;
323
+
324
+ assert ! ( !cursor_between_double_quotes( input, TextSize :: new( 16 ) ) ) ;
356
325
}
357
326
}
0 commit comments