@@ -392,15 +392,15 @@ module.public = {
392392 local range1 = module .public .node_to_lsp_range (node1 )
393393 local range2 = module .public .node_to_lsp_range (node2 )
394394
395- local text1 = module .public .get_node_text (node1 , bufnr )
396- local text2 = module .public .get_node_text (node2 , bufnr )
395+ local _text1 = module .public .get_node_text (node1 , bufnr )
396+ local _text2 = module .public .get_node_text (node2 , bufnr )
397397
398- if not text1 or not text2 then
398+ if not _text1 or not _text2 then
399399 return
400400 end
401401
402- text1 = vim .split (text1 , " \n " )
403- text2 = vim .split (text2 , " \n " )
402+ local text1 = vim .split (_text1 , " \n " )
403+ local text2 = vim .split (_text2 , " \n " )
404404
405405 --- remove trailing blank lines from the text, and update the corresponding range appropriately
406406 --- @param text string[]
@@ -468,6 +468,7 @@ module.public = {
468468 end ,
469469
470470 --- Returns the first node of given type if present
471+ --- @deprecated use get_first_node_recursive instead
471472 --- @param type string #The type of node to search for
472473 --- @param buf number #The buffer to search in
473474 --- @param parent userdata #The node to start searching in
@@ -489,13 +490,15 @@ module.public = {
489490 end
490491
491492 vim .treesitter .get_parser (buf , " norg" ):for_each_tree (function (tree )
493+ -- FIXME: this return value doesn't do what the original author thinks it does
492494 -- Iterate over all top-level children and attempt to find a match
493495 return iterate (tree :root ()) --- @diagnostic disable-line -- TODO : type error workaround <pysan3 >
494496 end )
495497 end ,
496498 --- Recursively attempts to locate a node of a given type
497499 --- @param type string #The type of node to look for
498- --- @param opts table #A table of two options: `buf` and `ft`, for the buffer and format respectively
500+ --- @param opts { buf : number ?, ft : string ?, parent : TSNode ?} # Buffer, filetype (for TS parsing),
501+ --- parent, defaults to root node
499502 --- @return TSNode ?
500503 get_first_node_recursive = function (type , opts )
501504 opts = opts or {}
@@ -519,11 +522,13 @@ module.public = {
519522 root = tree :root ()
520523 end
521524
525+ if not root then return end
526+
522527 --- Recursively searches for a node of a given type
523528 --- @param node TSNode #The starting point for the search
524529 local function descend (node )
525530 -- Iterate over all children of the node and try to match their type
526- for child , _ in node :iter_children () do --- @diagnostic disable-line -- TODO : type error workaround < pysan3 >
531+ for child , _ in node :iter_children () do
527532 if child :type () == type then
528533 return child
529534 else
@@ -543,6 +548,7 @@ module.public = {
543548
544549 return result
545550 end ,
551+
546552 --- Given a node this function will break down the AST elements and return the corresponding text for certain nodes
547553 --- @param tag_node TSNode - a node of type tag /carryover_tag
548554 --- @param throw boolean - when true , throw an error instead of logging and returning on failure
@@ -576,12 +582,12 @@ module.public = {
576582 end
577583 elseif child :type () == " tag_name" then
578584 -- If we're dealing with the tag name then append the text of the tag_name node to this table
579- table.insert (resulting_name , vim .split (module .public .get_node_text (child ), " \n " )[1 ]) --- @diagnostic disable-line -- TODO : type error workaround < pysan3 >
585+ table.insert (resulting_name , vim .split (module .public .get_node_text (child ), " \n " )[1 ])
580586 elseif child :type () == " tag_parameters" then
581- table.insert (params , vim .split (module .public .get_node_text (child ), " \n " )[1 ]) --- @diagnostic disable-line -- TODO : type error workaround < pysan3 >
587+ table.insert (params , vim .split (module .public .get_node_text (child ), " \n " )[1 ])
582588 elseif child :type () == " ranged_verbatim_tag_content" then
583589 -- If we're dealing with tag content then retrieve that content
584- content = vim .split (module .public .get_node_text (child ), " \n " ) --- @diagnostic disable-line -- TODO : type error workaround < pysan3 >
590+ content = vim .split (module .public .get_node_text (child ), " \n " )
585591 _ , content_start_column = child :range ()
586592 end
587593 end
@@ -618,8 +624,8 @@ module.public = {
618624 [" end" ] = { row = end_row , column = end_column },
619625 }
620626 end ,
621- --- Gets the range of a given node
622- --- @param node userdata #The node to get the range of
627+ --- Gets the range of the given node
628+ --- @param node TSNode
623629 --- @return { row_start : number , column_start : number , row_end : number , column_end : number } range
624630 get_node_range = function (node )
625631 if not node then
@@ -636,7 +642,7 @@ module.public = {
636642 local _ , _ , ere , ece = node [# node ]:range ()
637643 return brs , bcs , ere , ece
638644 end , function ()
639- local a , b , c , d = node :range () --- @diagnostic disable-line -- TODO : type error workaround < pysan3 >
645+ local a , b , c , d = node :range ()
640646 return a , b , c , d
641647 end )
642648
@@ -672,29 +678,31 @@ module.public = {
672678
673679 return tree :root ()
674680 end ,
681+
675682 --- Attempts to find a parent of a node recursively
676- --- @param node userdata #The node to start at
683+ --- @param node TSNode #The node to start at
677684 --- @param types table | string #If `types` is a table, this function will attempt to match any of the types present in the table.
678685 -- If the type is a string, the function will attempt to pattern match the `types` value with the node type.
679686 find_parent = function (node , types )
687+ --- @type TSNode ?
680688 local _node = node
681689
682690 while _node do
683691 if type (types ) == " string" then
684- if _node :type ():match (types ) then --- @diagnostic disable-line -- TODO : type error workaround < pysan3 >
692+ if _node :type ():match (types ) then
685693 return _node
686694 end
687- elseif vim .tbl_contains (types , _node :type ()) then --- @diagnostic disable-line -- TODO : type error workaround < pysan3 >
695+ elseif vim .tbl_contains (types , _node :type ()) then
688696 return _node
689697 end
690698
691- _node = _node :parent () --- @diagnostic disable-line -- TODO : type error workaround < pysan3 >
699+ _node = _node :parent ()
692700 end
693701 end ,
702+
694703 --- Retrieves the first node at a specific line
695704 --- @param buf number #The buffer to search in (0 for current)
696- --- @param line number #The line number (0-indexed) to get the node from
697- -- the same line as `line`.
705+ --- @param line number #The line number (0-indexed) to get the node from the same line as `line`.
698706 --- @param stop_type string | table ? #Don't recurse to the provided type(s)
699707 --- @return TSNode | nil #The first node on `line`
700708 get_first_node_on_line = function (buf , line , stop_type )
@@ -711,7 +719,7 @@ module.public = {
711719 local first_char = (vim .api .nvim_buf_get_lines (buf , line , line + 1 , true )[1 ] or " " ):match (" ^(%s+)[^%s]" )
712720 first_char = first_char and first_char :len () or 0
713721
714- local descendant = document_root :descendant_for_range (line , first_char , line , first_char + 1 ) --- @diagnostic disable-line -- TODO : type error workaround < pysan3 >
722+ local descendant = document_root :descendant_for_range (line , first_char , line , first_char + 1 )
715723
716724 if not descendant then
717725 return
@@ -728,6 +736,8 @@ module.public = {
728736 break
729737 end
730738
739+ if not parent then return end
740+
731741 descendant = parent
732742 end
733743
@@ -873,6 +883,7 @@ module.public = {
873883
874884 return result
875885 end ,
886+
876887 --- Parses a query and automatically executes it for Norg
877888 --- @param query_string string #The query string
878889 --- @param callback function #The callback to execute with all values returned by
0 commit comments