@@ -23,7 +23,6 @@ local lib, log, modules, utils = neorg.lib, neorg.log, neorg.modules, neorg.util
2323local module = modules .create (" core.integrations.treesitter" )
2424
2525module .private = {
26- ts_utils = nil ,
2726 link_query = [[
2827 (link) @next-segment
2928 (anchor_declaration) @next-segment
@@ -58,14 +57,14 @@ module.setup = function()
5857end
5958
6059module .load = function ()
61- local success , ts_utils = pcall (require , " nvim-treesitter.ts_utils" )
62-
63- assert (success , " Unable to load nvim-treesitter.ts_utils :(" )
64-
6560 if module .config .public .configure_parsers then
6661 -- luacheck: push ignore
6762
68- local parser_configs = require (" nvim-treesitter.parsers" ).get_parser_configs ()
63+ -- compat: nvim-treesitter master requires the extra function call, main does not
64+ local parser_configs = require (" nvim-treesitter.parsers" )
65+ if parser_configs .get_parser_configs then
66+ parser_configs = parser_configs .get_parser_configs ()
67+ end
6968
7069 parser_configs .norg = {
7170 install_info = module .config .public .parser_configs .norg ,
@@ -109,8 +108,6 @@ module.load = function()
109108 })
110109 end
111110
112- module .private .ts_utils = ts_utils
113-
114111 vim .keymap .set (
115112 " " ,
116113 " <Plug>(neorg.treesitter.next.heading)" ,
@@ -164,11 +161,7 @@ module.config.public = {
164161--- @class core.integrations.treesitter
165162module .public = {
166163 parser_path = nil ,
167- --- Gives back an instance of `nvim-treesitter.ts_utils`
168- --- @return table #`nvim-treesitter.ts_utils`
169- get_ts_utils = function ()
170- return module .private .ts_utils
171- end ,
164+
172165 --- Jumps to the next match of a query in the current buffer
173166 --- @param query_string string Query with ` @next-segment` captures
174167 goto_next_query_match = function (query_string )
@@ -194,7 +187,7 @@ module.public = {
194187
195188 -- Find and go to the first matching node that starts after the current cursor position.
196189 if (start_line == line_number and start_col > col_number ) or start_line > line_number then
197- module .private . ts_utils . goto_node (node ) --- @diagnostic disable-line -- TODO : type error workaround < pysan3 >
190+ module .public . goto_node (node )
198191 return
199192 end
200193 end
@@ -236,7 +229,7 @@ module.public = {
236229 :: continue::
237230 end
238231 if final_node then
239- module .private . ts_utils . goto_node (final_node ) --- @diagnostic disable-line -- TODO : type error workaround < pysan3 >
232+ module .public . goto_node (final_node )
240233 end
241234 end ,
242235 --- Gets all nodes of a given type from the AST
@@ -945,6 +938,103 @@ module.public = {
945938 end ,
946939}
947940
941+
942+ --[[
943+ -- attribution notice:
944+ -- The below public functions are originally licensed under Apache v2 taken from:
945+ -- https://github.com/nvim-treesitter/nvim-treesitter/blob/master/lua/nvim-treesitter/ts_utils.lua
946+ --]]
947+
948+ -- Get previous node with same parent
949+ --- @param node TSNode
950+ --- @param allow_switch_parents ? boolean allow switching parents if first node
951+ --- @param allow_previous_parent ? boolean allow previous parent if first node and previous parent without children
952+ module .public .get_previous_node = function (node , allow_switch_parents , allow_previous_parent )
953+ local destination_node --- @type TSNode ?
954+ local parent = node :parent ()
955+ if not parent then
956+ return
957+ end
958+
959+ local found_pos = 0
960+ for i = 0 , parent :named_child_count () - 1 , 1 do
961+ if parent :named_child (i ) == node then
962+ found_pos = i
963+ break
964+ end
965+ end
966+ if 0 < found_pos then
967+ destination_node = parent :named_child (found_pos - 1 )
968+ elseif allow_switch_parents then
969+ local previous_node = module .private .get_previous_node (node :parent ())
970+ if previous_node and previous_node :named_child_count () > 0 then
971+ destination_node = previous_node :named_child (previous_node :named_child_count () - 1 )
972+ elseif previous_node and allow_previous_parent then
973+ destination_node = previous_node
974+ end
975+ end
976+ return destination_node
977+ end
978+
979+ module .public .goto_node = function (node , goto_end , avoid_set_jump )
980+ if not node then
981+ return
982+ end
983+ if not avoid_set_jump then
984+ vim .cmd (" normal! m'" )
985+ end
986+ local range = module .public .get_node_range (node )
987+
988+ --- @type table<number>
989+ local position
990+ if not goto_end then
991+ position = { range .row_start , range .column_start }
992+ else
993+ position = { range .row_end , range .column_end }
994+ end
995+
996+ -- Enter visual mode if we are in operator pending mode
997+ -- If we don't do this, it will miss the last character.
998+ local mode = vim .api .nvim_get_mode ()
999+ if mode .mode == " no" then
1000+ vim .cmd (" normal! v" )
1001+ end
1002+
1003+ vim .api .nvim_win_set_cursor (0 , { position [1 ] + 1 , position [2 ] })
1004+ end
1005+
1006+ -- Get next node with same parent
1007+ --- @param node TSNode
1008+ --- @param allow_switch_parents ? boolean allow switching parents if last node
1009+ --- @param allow_next_parent ? boolean allow next parent if last node and next parent without children
1010+ module .public .get_next_node = function (node , allow_switch_parents , allow_next_parent )
1011+ local destination_node --- @type TSNode ?
1012+ local parent = node :parent ()
1013+
1014+ if not parent then
1015+ return
1016+ end
1017+ local found_pos = 0
1018+ for i = 0 , parent :named_child_count () - 1 , 1 do
1019+ if parent :named_child (i ) == node then
1020+ found_pos = i
1021+ break
1022+ end
1023+ end
1024+ if parent :named_child_count () > found_pos + 1 then
1025+ destination_node = parent :named_child (found_pos + 1 )
1026+ elseif allow_switch_parents then
1027+ local next_node = module .public .get_next_node (parent )
1028+ if next_node and next_node :named_child_count () > 0 then
1029+ destination_node = next_node :named_child (0 )
1030+ elseif next_node and allow_next_parent then
1031+ destination_node = next_node
1032+ end
1033+ end
1034+
1035+ return destination_node
1036+ end
1037+
9481038module .on_event = function (event )
9491039 if event .split_type [2 ] == " sync-parsers" then
9501040 local install = require (" nvim-treesitter.install" )
0 commit comments