@@ -302,32 +302,11 @@ impl<'a> TreesitterContext<'a> {
302302 return ;
303303 }
304304
305- match parent_node_kind {
305+ match current_node_kind {
306306 "statement" | "subquery" => {
307- self . wrapping_clause_type =
308- self . get_wrapping_clause_from_current_node ( current_node, & mut cursor) ;
309-
310- self . wrapping_statement_range = Some ( parent_node. range ( ) ) ;
311- }
312- "invocation" => self . is_invocation = true ,
313- _ => { }
314- }
315-
316- // try to gather context from the siblings if we're within an error node.
317- if parent_node_kind == "ERROR" {
318- if let Some ( clause_type) = self . get_wrapping_clause_from_error_node_child ( current_node)
319- {
320- self . wrapping_clause_type = Some ( clause_type) ;
321- }
322- if let Some ( wrapping_node) = self . get_wrapping_node_from_error_node_child ( current_node)
323- {
324- self . wrapping_node_kind = Some ( wrapping_node)
307+ self . wrapping_statement_range = Some ( current_node. range ( ) ) ;
325308 }
326309
327- self . get_info_from_error_node_child ( current_node) ;
328- }
329-
330- match current_node_kind {
331310 "object_reference" | "column_reference" => {
332311 if let Some ( ( head, middle, _) ) = parts_of_reference_query ( current_node, self . text ) {
333312 self . identifier_qualifiers = (
@@ -361,6 +340,8 @@ impl<'a> TreesitterContext<'a> {
361340 self . wrapping_node_kind = Some ( WrappingNode :: List ) ;
362341 }
363342
343+ "invocation" => self . is_invocation = true ,
344+
364345 _ => {
365346 if let Some ( clause_type) =
366347 self . get_wrapping_clause_from_current_node ( current_node, & mut cursor)
@@ -382,189 +363,6 @@ impl<'a> TreesitterContext<'a> {
382363 self . gather_context_from_node ( cursor, current_node) ;
383364 }
384365
385- fn get_first_sibling ( & self , node : tree_sitter:: Node < ' a > ) -> tree_sitter:: Node < ' a > {
386- let mut first_sibling = node;
387- while let Some ( n) = first_sibling. prev_sibling ( ) {
388- first_sibling = n;
389- }
390- first_sibling
391- }
392-
393- fn get_wrapping_node_from_error_node_child (
394- & self ,
395- node : tree_sitter:: Node < ' a > ,
396- ) -> Option < WrappingNode > {
397- self . wrapping_clause_type
398- . as_ref ( )
399- . and_then ( |clause| match clause {
400- WrappingClause :: Insert => {
401- let mut first_sib = self . get_first_sibling ( node) ;
402-
403- let mut after_opening_bracket = false ;
404- let mut before_closing_bracket = false ;
405-
406- while let Some ( next_sib) = first_sib. next_sibling ( ) {
407- if next_sib. kind ( ) == "("
408- && next_sib. end_position ( ) <= node. start_position ( )
409- {
410- after_opening_bracket = true ;
411- }
412-
413- if next_sib. kind ( ) == ")"
414- && next_sib. start_position ( ) >= node. end_position ( )
415- {
416- before_closing_bracket = true ;
417- }
418-
419- first_sib = next_sib;
420- }
421-
422- if after_opening_bracket && before_closing_bracket {
423- Some ( WrappingNode :: List )
424- } else {
425- None
426- }
427- }
428- _ => None ,
429- } )
430- }
431-
432- fn get_wrapping_clause_from_error_node_child (
433- & self ,
434- node : tree_sitter:: Node < ' a > ,
435- ) -> Option < WrappingClause < ' a > > {
436- let clause_combinations: Vec < ( WrappingClause , & [ & ' static str ] ) > = vec ! [
437- ( WrappingClause :: Where , & [ "where" ] ) ,
438- ( WrappingClause :: Update , & [ "update" ] ) ,
439- ( WrappingClause :: Select , & [ "select" ] ) ,
440- ( WrappingClause :: Delete , & [ "delete" ] ) ,
441- ( WrappingClause :: Insert , & [ "insert" , "into" ] ) ,
442- ( WrappingClause :: From , & [ "from" ] ) ,
443- ( WrappingClause :: Join { on_node: None } , & [ "join" ] ) ,
444- ( WrappingClause :: AlterTable , & [ "alter" , "table" ] ) ,
445- ( WrappingClause :: AlterColumn , & [ "alter" , "table" , "alter" ] ) ,
446- ( WrappingClause :: RenameColumn , & [ "alter" , "table" , "rename" ] ) ,
447- (
448- WrappingClause :: AlterTable ,
449- & [ "alter" , "table" , "if" , "exists" ] ,
450- ) ,
451- ( WrappingClause :: DropTable , & [ "drop" , "table" ] ) ,
452- (
453- WrappingClause :: DropTable ,
454- & [ "drop" , "table" , "if" , "exists" ] ,
455- ) ,
456- ] ;
457-
458- let first_sibling = self . get_first_sibling ( node) ;
459-
460- /*
461- * For each clause, we'll iterate from first_sibling to the next ones,
462- * either until the end or until we land on the node under the cursor.
463- * We'll score the `WrappingClause` by how many tokens it matches in order.
464- */
465- let mut clauses_with_score: Vec < ( WrappingClause , usize ) > = clause_combinations
466- . into_iter ( )
467- . map ( |( clause, tokens) | {
468- let mut idx = 0 ;
469-
470- let mut sibling = Some ( first_sibling) ;
471- while let Some ( sib) = sibling {
472- if sib. end_byte ( ) >= node. end_byte ( ) || idx >= tokens. len ( ) {
473- break ;
474- }
475-
476- if let Some ( sibling_content) = self . get_ts_node_content ( & sib) {
477- if sibling_content == tokens[ idx] {
478- idx += 1 ;
479- }
480- } else {
481- break ;
482- }
483-
484- sibling = sib. next_sibling ( ) ;
485- }
486-
487- ( clause, idx)
488- } )
489- . collect ( ) ;
490-
491- clauses_with_score. sort_by ( |( _, score_a) , ( _, score_b) | score_b. cmp ( score_a) ) ;
492- clauses_with_score
493- . iter ( )
494- . find ( |( _, score) | * score > 0 )
495- . map ( |c| c. 0 . clone ( ) )
496- }
497-
498- fn get_info_from_error_node_child ( & mut self , node : tree_sitter:: Node < ' a > ) {
499- let mut first_sibling = self . get_first_sibling ( node) ;
500-
501- if let Some ( clause) = self . wrapping_clause_type . as_ref ( ) {
502- match * clause {
503- WrappingClause :: Insert => {
504- while let Some ( sib) = first_sibling. next_sibling ( ) {
505- match sib. kind ( ) {
506- "object_reference" => {
507- if let Some ( txt) = self . get_ts_node_content ( & sib) {
508- let mut iter = txt. split ( '.' ) . rev ( ) ;
509- let table = iter. next ( ) . unwrap ( ) . to_string ( ) ;
510- let schema = iter. next ( ) . map ( |s| s. to_string ( ) ) ;
511- self . mentioned_relations
512- . entry ( schema)
513- . and_modify ( |s| {
514- s. insert ( table. clone ( ) ) ;
515- } )
516- . or_insert ( HashSet :: from ( [ table] ) ) ;
517- }
518- }
519-
520- "column" => {
521- if let Some ( txt) = self . get_ts_node_content ( & sib) {
522- let entry = MentionedColumn {
523- column : txt,
524- alias : None ,
525- } ;
526-
527- self . mentioned_columns
528- . entry ( Some ( WrappingClause :: Insert ) )
529- . and_modify ( |s| {
530- s. insert ( entry. clone ( ) ) ;
531- } )
532- . or_insert ( HashSet :: from ( [ entry] ) ) ;
533- }
534- }
535-
536- _ => { }
537- }
538-
539- first_sibling = sib;
540- }
541- }
542-
543- WrappingClause :: AlterColumn => {
544- while let Some ( sib) = first_sibling. next_sibling ( ) {
545- if sib. kind ( ) == "object_reference" {
546- if let Some ( txt) = self . get_ts_node_content ( & sib) {
547- let mut iter = txt. split ( '.' ) . rev ( ) ;
548- let table = iter. next ( ) . unwrap ( ) . to_string ( ) ;
549- let schema = iter. next ( ) . map ( |s| s. to_string ( ) ) ;
550- self . mentioned_relations
551- . entry ( schema)
552- . and_modify ( |s| {
553- s. insert ( table. clone ( ) ) ;
554- } )
555- . or_insert ( HashSet :: from ( [ table] ) ) ;
556- }
557- }
558-
559- first_sibling = sib;
560- }
561- }
562-
563- _ => { }
564- }
565- }
566- }
567-
568366 fn get_wrapping_clause_from_current_node (
569367 & self ,
570368 node : tree_sitter:: Node < ' a > ,
0 commit comments