4545 ' larger : ' smaller ,
4646{
4747 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 )
5050 || cursor_before_semicolon ( params. tree , params. position )
5151 || cursor_on_a_dot ( & params. text , params. position )
5252 || cursor_between_double_quotes ( & params. text , params. position )
@@ -125,37 +125,17 @@ where
125125/// select |from users; -- cursor "touches" from node. returns false.
126126/// select | from users; -- cursor is between select and from nodes. returns true.
127127/// ```
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 ( ) ;
138131
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 ( ) ) ;
151135
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 ( ) ) ;
153137
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
159139}
160140
161141/// 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
166146/// select * from| -- user still needs to type a space
167147/// select * from | -- too far off.
168148/// ```
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 {
173150 let cursor_pos: usize = position. into ( ) ;
174- cursor_pos == tree . root_node ( ) . end_byte ( ) + 1
151+ cursor_pos == sql . len ( ) + 1
175152}
176153
177154fn cursor_on_a_dot ( sql : & str , position : TextSize ) -> bool {
@@ -243,58 +220,44 @@ mod tests {
243220 // note: two spaces between select and from.
244221 let input = "select from users;" ;
245222
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-
253223 // 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 ) ) ) ;
255225
256226 // 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 ) ) ) ;
258228
259229 // 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 ) ) ) ;
261231
262232 // select from users;|
263- assert ! ( !cursor_inbetween_nodes( & tree , TextSize :: new( 19 ) ) ) ;
233+ assert ! ( !cursor_inbetween_nodes( input , TextSize :: new( 19 ) ) ) ;
264234 }
265235
266236 #[ test]
267237 fn test_cursor_after_nodes ( ) {
268238 let input = "select * from" ;
269239
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-
277240 // select * from| <-- still on previous token
278241 assert ! ( !cursor_prepared_to_write_token_after_last_node(
279- & tree ,
242+ input ,
280243 TextSize :: new( 13 )
281244 ) ) ;
282245
283246 // select * from | <-- too far off, two spaces afterward
284247 assert ! ( !cursor_prepared_to_write_token_after_last_node(
285- & tree ,
248+ input ,
286249 TextSize :: new( 15 )
287250 ) ) ;
288251
289252 // select * |from <-- it's within
290253 assert ! ( !cursor_prepared_to_write_token_after_last_node(
291- & tree ,
254+ input ,
292255 TextSize :: new( 9 )
293256 ) ) ;
294257
295258 // select * from | <-- just right
296259 assert ! ( cursor_prepared_to_write_token_after_last_node(
297- & tree ,
260+ input ,
298261 TextSize :: new( 14 )
299262 ) ) ;
300263 }
@@ -353,5 +316,11 @@ mod tests {
353316
354317 // select * from "|" <-- between quotations
355318 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 ) ) ) ;
356325 }
357326}
0 commit comments