Skip to content

Commit 87242d4

Browse files
committed
fix: compatibility with nvim-treesitter main branch
This doesn't break compatibility with the master branch
1 parent 65524c8 commit 87242d4

File tree

7 files changed

+139
-59
lines changed

7 files changed

+139
-59
lines changed

docgen/docgen.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ local lib, modules, utils, log = neorg.lib, neorg.modules, neorg.utils, neorg.lo
3636
-- Start neorg
3737
neorg.org_file_entered(false)
3838

39-
-- Extract treesitter utility functions provided by Neorg and nvim-treesitter.ts_utils
39+
-- Extract treesitter utility functions provided by Neorg
4040
---@type core.integrations.treesitter
4141
local ts = modules.get_module("core.integrations.treesitter")
4242
assert(ts, "treesitter not available")

lua/neorg/modules/core/completion/module.lua

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ module.load = function()
303303
dirutils = module.required["core.dirman.utils"]
304304
dirman = module.required["core.dirman"]
305305
link_utils = module.required["core.links"]
306+
---@type core.integrations.treesitter
306307
treesitter = module.required["core.integrations.treesitter"]
307308

308309
-- Set a special function in the integration module to allow it to communicate with us
@@ -668,9 +669,6 @@ module.public = {
668669

669670
-- If the completion data has a node variable then attempt to match the current node too!
670671
if completion_data.node then
671-
-- Grab the treesitter utilities
672-
local ts = treesitter.get_ts_utils()
673-
674672
-- If the type of completion data we're dealing with is a string then attempt to parse it
675673
if type(completion_data.node) == "string" then
676674
-- Split the completion node string down every pipe character
@@ -689,13 +687,13 @@ module.public = {
689687
-- Is our other value "prev"? If so, compare the current node in the syntax tree with the previous node
690688
if split[2] == "prev" then
691689
-- Get the previous node
692-
local current_node = ts.get_node_at_cursor()
690+
local current_node = vim.treesitter.get_node()
693691

694692
if not current_node then
695693
return { items = {}, options = {} }
696694
end
697695

698-
local previous_node = ts.get_previous_node(current_node, true, true)
696+
local previous_node = treesitter.get_previous_node(current_node, true, true)
699697

700698
-- If the previous node is nil
701699
if not previous_node then
@@ -722,13 +720,13 @@ module.public = {
722720
-- Else if our second split is equal to "next" then it's time to inspect the next node in the AST
723721
elseif split[2] == "next" then
724722
-- Grab the next node
725-
local current_node = ts.get_node_at_cursor()
723+
local current_node = vim.treesitter.get_node()
726724

727725
if not current_node then
728726
return { items = {}, options = {} }
729727
end
730728

731-
local next_node = ts.get_next_node(current_node, true, true)
729+
local next_node = treesitter.get_next_node(current_node, true, true)
732730

733731
-- If it's nil
734732
if not next_node then
@@ -753,7 +751,7 @@ module.public = {
753751
end
754752
end
755753
else -- If we haven't defined a split (no pipe was found) then compare the current node
756-
if ts.get_node_at_cursor():type() == split[1] then
754+
if vim.treesitter.get_node():type() == split[1] then
757755
-- If we're not negating then return completions
758756
if not negate then
759757
return ret_completions
@@ -765,19 +763,19 @@ module.public = {
765763
-- If our completion data type is not a string but rather it is a function then
766764
elseif type(completion_data.node) == "function" then
767765
-- Grab all the necessary variables (current node, previous node, next node)
768-
local current_node = ts.get_node_at_cursor()
766+
local current_node = vim.treesitter.get_node()
769767

770768
-- The file is blank, return completions
771769
if not current_node then
772770
return ret_completions
773771
end
774772

775-
local next_node = ts.get_next_node(current_node, true, true)
776-
local previous_node = ts.get_previous_node(current_node, true, true)
773+
local next_node = treesitter.get_next_node(current_node, true, true)
774+
local previous_node = treesitter.get_previous_node(current_node, true, true)
777775

778776
-- Execute the callback function with all of our parameters.
779777
-- If it returns true then that means the match was successful, and so return completions
780-
if completion_data.node(current_node, previous_node, next_node, ts) then
778+
if completion_data.node(current_node, previous_node, next_node, treesitter) then
781779
return ret_completions
782780
end
783781

lua/neorg/modules/core/esupports/hop/module.lua

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,7 @@ module.public = {
345345
--- Locate a `link` or `anchor` node under the cursor
346346
---@return TSNode? #A `link` or `anchor` node if present under the cursor, else `nil`
347347
extract_link_node = function()
348-
local ts_utils = module.required["core.integrations.treesitter"].get_ts_utils()
349-
350-
if not ts_utils then
351-
return
352-
end
353-
354-
local current_node = ts_utils.get_node_at_cursor()
348+
local current_node = vim.treesitter.get_node()
355349
local found_node = module.required["core.integrations.treesitter"].find_parent(
356350
current_node,
357351
{ "link", "anchor_declaration", "anchor_definition" }
@@ -367,8 +361,6 @@ module.public = {
367361
--- Attempts to locate a `link` or `anchor` node after the cursor on the same line
368362
---@return TSNode? #A `link` or `anchor` node if present on the current line, else `nil`
369363
lookahead_link_node = function()
370-
local ts_utils = module.required["core.integrations.treesitter"].get_ts_utils()
371-
372364
local line = vim.api.nvim_get_current_line()
373365
local current_cursor_pos = vim.api.nvim_win_get_cursor(0)
374366
local current_line = current_cursor_pos[1]
@@ -395,7 +387,10 @@ module.public = {
395387
smaller_value - 1,
396388
})
397389

398-
local node_under_cursor = ts_utils.get_node_at_cursor()
390+
local node_under_cursor = vim.treesitter.get_node()
391+
if not node_under_cursor then
392+
return
393+
end
399394

400395
if vim.tbl_contains({ "link_location", "link_description" }, node_under_cursor:type()) then
401396
resulting_node = node_under_cursor:parent()
@@ -963,19 +958,19 @@ module.private = {
963958
end
964959
callback(
965960
"{"
966-
.. lib.when(
967-
parsed_link_information.link_file_text --[[@as boolean]],
968-
lib.lazy_string_concat(":", parsed_link_information.link_file_text, ":"),
969-
""
970-
)
971-
.. prefix
972-
.. most_similar.text
973-
.. "}"
974-
.. lib.when(
975-
parsed_link_information.link_description --[[@as boolean]],
976-
lib.lazy_string_concat("[", parsed_link_information.link_description, "]"),
977-
""
978-
)
961+
.. lib.when(
962+
parsed_link_information.link_file_text --[[@as boolean]],
963+
lib.lazy_string_concat(":", parsed_link_information.link_file_text, ":"),
964+
""
965+
)
966+
.. prefix
967+
.. most_similar.text
968+
.. "}"
969+
.. lib.when(
970+
parsed_link_information.link_description --[[@as boolean]],
971+
lib.lazy_string_concat("[", parsed_link_information.link_description, "]"),
972+
""
973+
)
979974
)
980975
end,
981976
}

lua/neorg/modules/core/export/markdown/module.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ local function todo_item_recollector()
108108
end
109109

110110
local function handle_heading_newlines()
111-
return function(output, _, node, ts_utils)
112-
local prev = ts_utils.get_previous_node(node, true, true)
111+
return function(output, _, node, ts)
112+
local prev = ts.get_previous_node(node, true, true)
113113

114114
if
115115
prev

lua/neorg/modules/core/export/module.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ module.public = {
148148
-- Initialize the state. The state is a table that exists throughout the entire duration
149149
-- of the export, and can be used to e.g. retain indent levels and/or keep references.
150150
local state = converter.export.init_state and converter.export.init_state() or {}
151-
local ts_utils = ts.get_ts_utils()
152151

153152
--- Descends down a node and its children
154153
---@param start table #The TS node to begin at
@@ -176,7 +175,7 @@ module.public = {
176175
-- `keep_descending` - if true will continue to recurse down the current node's children despite the current
177176
-- node already being parsed
178177
-- `state` - a modified version of the state that then gets merged into the main state table
179-
local result = exporter(vim.treesitter.get_node_text(node, source), node, state, ts_utils)
178+
local result = exporter(vim.treesitter.get_node_text(node, source), node, state, ts)
180179

181180
if type(result) == "table" then
182181
state = result.state and vim.tbl_extend("force", state, result.state) or state
@@ -233,7 +232,7 @@ module.public = {
233232
-- and rearrange its components to { "Term", ": ", "Definition" } to then achieve the desired result.
234233
local recollector = converter.export.recollectors[start:type()]
235234

236-
return recollector and table.concat(recollector(output, state, start, ts_utils) or {})
235+
return recollector and table.concat(recollector(output, state, start, ts) or {})
237236
or (not vim.tbl_isempty(output) and table.concat(output))
238237
end
239238

lua/neorg/modules/core/integrations/treesitter/module.lua

Lines changed: 105 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ local lib, log, modules, utils = neorg.lib, neorg.log, neorg.modules, neorg.util
2323
local module = modules.create("core.integrations.treesitter")
2424

2525
module.private = {
26-
ts_utils = nil,
2726
link_query = [[
2827
(link) @next-segment
2928
(anchor_declaration) @next-segment
@@ -58,14 +57,14 @@ module.setup = function()
5857
end
5958

6059
module.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
165162
module.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+
9481038
module.on_event = function(event)
9491039
if event.split_type[2] == "sync-parsers" then
9501040
local install = require("nvim-treesitter.install")

lua/neorg/modules/core/looking-glass/module.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ module.public = {
8585
-- Make sure that the cursor is within bounds of the code block
8686
if cursor_pos[1] > extmark_begin[1] and cursor_pos[1] <= (extmark_end[1] + 1) then
8787
-- For extra information grab the current node under the cursor
88-
local current_node = module.required["core.integrations.treesitter"]
89-
.get_ts_utils()
90-
.get_node_at_cursor(source_window, true)
88+
local current_node = vim.treesitter.get_node({ bufnr = source, ignore_injections = true })
9189

9290
if not current_node then
9391
vim.api.nvim_buf_delete(target, { force = true })

0 commit comments

Comments
 (0)