Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docgen/docgen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ local lib, modules, utils, log = neorg.lib, neorg.modules, neorg.utils, neorg.lo
-- Start neorg
neorg.org_file_entered(false)

-- Extract treesitter utility functions provided by Neorg and nvim-treesitter.ts_utils
-- Extract treesitter utility functions provided by Neorg
---@type core.integrations.treesitter
local ts = modules.get_module("core.integrations.treesitter")
assert(ts, "treesitter not available")
Expand Down
26 changes: 12 additions & 14 deletions lua/neorg/modules/core/completion/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ link completions are smart about closing `:` and `}`.
local neorg = require("neorg.core")
local Path = require("pathlib")
local log, modules, utils = neorg.log, neorg.modules, neorg.utils
local dirutils, dirman, link_utils, treesitter
local dirutils, dirman, link_utils, ts

local module = modules.create("core.completion")

Expand Down Expand Up @@ -101,7 +101,7 @@ module.private = {
---@param link_type "generic" | "definition" | "footnote" | string
get_linkables = function(source, link_type)
local query_str = link_utils.get_link_target_query_string(link_type)
local norg_parser, iter_src = treesitter.get_ts_parser(source)
local norg_parser, iter_src = ts.get_ts_parser(source)
if not norg_parser then
return {}
end
Expand All @@ -111,7 +111,7 @@ module.private = {
for id, node in query:iter_captures(norg_tree:root(), iter_src, 0, -1) do
local capture = query.captures[id]
if capture == "title" then
local original_title = treesitter.get_node_text(node, iter_src)
local original_title = ts.get_node_text(node, iter_src)
if original_title then
local title = original_title:gsub("\\", "")
title = title:gsub("%s+", " ")
Expand Down Expand Up @@ -230,7 +230,7 @@ module.private = {
module.private.foreign_link_names = function(_context, _prev, _saved, match)
local file, target = match[2], match[3]
local path = dirutils.expand_pathlib(file)
local meta = treesitter.get_document_metadata(path)
local meta = ts.get_document_metadata(path)
local suggestions = {}
if meta then
table.insert(suggestions, meta.title)
Expand All @@ -253,10 +253,10 @@ module.private.anchor_suggestions = function(_context, _prev, _saved, _match)
text: (paragraph) @anchor_name ))
]]

treesitter.execute_query(anchor_query_string, function(query, id, node, _metadata)
ts.execute_query(anchor_query_string, function(query, id, node, _metadata)
local capture_name = query.captures[id]
if capture_name == "anchor_name" then
table.insert(suggestions, treesitter.get_node_text(node, 0))
table.insert(suggestions, ts.get_node_text(node, 0))
end
end, 0)
return suggestions
Expand Down Expand Up @@ -303,7 +303,8 @@ module.load = function()
dirutils = module.required["core.dirman.utils"]
dirman = module.required["core.dirman"]
link_utils = module.required["core.links"]
treesitter = module.required["core.integrations.treesitter"]
---@type core.integrations.treesitter
ts = module.required["core.integrations.treesitter"]

-- Set a special function in the integration module to allow it to communicate with us
module.private.engine.invoke_completion_engine = function(context) ---@diagnostic disable-line
Expand Down Expand Up @@ -668,9 +669,6 @@ module.public = {

-- If the completion data has a node variable then attempt to match the current node too!
if completion_data.node then
-- Grab the treesitter utilities
local ts = treesitter.get_ts_utils()

-- If the type of completion data we're dealing with is a string then attempt to parse it
if type(completion_data.node) == "string" then
-- Split the completion node string down every pipe character
Expand All @@ -689,7 +687,7 @@ module.public = {
-- Is our other value "prev"? If so, compare the current node in the syntax tree with the previous node
if split[2] == "prev" then
-- Get the previous node
local current_node = ts.get_node_at_cursor()
local current_node = vim.treesitter.get_node()

if not current_node then
return { items = {}, options = {} }
Expand Down Expand Up @@ -722,7 +720,7 @@ module.public = {
-- Else if our second split is equal to "next" then it's time to inspect the next node in the AST
elseif split[2] == "next" then
-- Grab the next node
local current_node = ts.get_node_at_cursor()
local current_node = vim.treesitter.get_node()

if not current_node then
return { items = {}, options = {} }
Expand Down Expand Up @@ -753,7 +751,7 @@ module.public = {
end
end
else -- If we haven't defined a split (no pipe was found) then compare the current node
if ts.get_node_at_cursor():type() == split[1] then
if vim.treesitter.get_node():type() == split[1] then
-- If we're not negating then return completions
if not negate then
return ret_completions
Expand All @@ -765,7 +763,7 @@ module.public = {
-- If our completion data type is not a string but rather it is a function then
elseif type(completion_data.node) == "function" then
-- Grab all the necessary variables (current node, previous node, next node)
local current_node = ts.get_node_at_cursor()
local current_node = vim.treesitter.get_node()

-- The file is blank, return completions
if not current_node then
Expand Down
41 changes: 18 additions & 23 deletions lua/neorg/modules/core/esupports/hop/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,7 @@ module.public = {
--- Locate a `link` or `anchor` node under the cursor
---@return TSNode? #A `link` or `anchor` node if present under the cursor, else `nil`
extract_link_node = function()
local ts_utils = module.required["core.integrations.treesitter"].get_ts_utils()

if not ts_utils then
return
end

local current_node = ts_utils.get_node_at_cursor()
local current_node = vim.treesitter.get_node()
local found_node = module.required["core.integrations.treesitter"].find_parent(
current_node,
{ "link", "anchor_declaration", "anchor_definition" }
Expand All @@ -367,8 +361,6 @@ module.public = {
--- Attempts to locate a `link` or `anchor` node after the cursor on the same line
---@return TSNode? #A `link` or `anchor` node if present on the current line, else `nil`
lookahead_link_node = function()
local ts_utils = module.required["core.integrations.treesitter"].get_ts_utils()

local line = vim.api.nvim_get_current_line()
local current_cursor_pos = vim.api.nvim_win_get_cursor(0)
local current_line = current_cursor_pos[1]
Expand All @@ -395,7 +387,10 @@ module.public = {
smaller_value - 1,
})

local node_under_cursor = ts_utils.get_node_at_cursor()
local node_under_cursor = vim.treesitter.get_node()
if not node_under_cursor then
return
end

if vim.tbl_contains({ "link_location", "link_description" }, node_under_cursor:type()) then
resulting_node = node_under_cursor:parent()
Expand Down Expand Up @@ -963,19 +958,19 @@ module.private = {
end
callback(
"{"
.. lib.when(
parsed_link_information.link_file_text --[[@as boolean]],
lib.lazy_string_concat(":", parsed_link_information.link_file_text, ":"),
""
)
.. prefix
.. most_similar.text
.. "}"
.. lib.when(
parsed_link_information.link_description --[[@as boolean]],
lib.lazy_string_concat("[", parsed_link_information.link_description, "]"),
""
)
.. lib.when(
parsed_link_information.link_file_text --[[@as boolean]],
lib.lazy_string_concat(":", parsed_link_information.link_file_text, ":"),
""
)
.. prefix
.. most_similar.text
.. "}"
.. lib.when(
parsed_link_information.link_description --[[@as boolean]],
lib.lazy_string_concat("[", parsed_link_information.link_description, "]"),
""
)
)
end,
}
Expand Down
4 changes: 2 additions & 2 deletions lua/neorg/modules/core/export/markdown/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ local function todo_item_recollector()
end

local function handle_heading_newlines()
return function(output, _, node, ts_utils)
local prev = ts_utils.get_previous_node(node, true, true)
return function(output, _, node, ts)
local prev = ts.get_previous_node(node, true, true)

if
prev
Expand Down
5 changes: 2 additions & 3 deletions lua/neorg/modules/core/export/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ module.public = {
-- Initialize the state. The state is a table that exists throughout the entire duration
-- of the export, and can be used to e.g. retain indent levels and/or keep references.
local state = converter.export.init_state and converter.export.init_state() or {}
local ts_utils = ts.get_ts_utils()

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

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

return recollector and table.concat(recollector(output, state, start, ts_utils) or {})
return recollector and table.concat(recollector(output, state, start, ts) or {})
or (not vim.tbl_isempty(output) and table.concat(output))
end

Expand Down
120 changes: 105 additions & 15 deletions lua/neorg/modules/core/integrations/treesitter/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ local lib, log, modules, utils = neorg.lib, neorg.log, neorg.modules, neorg.util
local module = modules.create("core.integrations.treesitter")

module.private = {
ts_utils = nil,
link_query = [[
(link) @next-segment
(anchor_declaration) @next-segment
Expand Down Expand Up @@ -58,14 +57,14 @@ module.setup = function()
end

module.load = function()
local success, ts_utils = pcall(require, "nvim-treesitter.ts_utils")

assert(success, "Unable to load nvim-treesitter.ts_utils :(")

if module.config.public.configure_parsers then
-- luacheck: push ignore

local parser_configs = require("nvim-treesitter.parsers").get_parser_configs()
-- compat: nvim-treesitter master requires the extra function call, main does not
local parser_configs = require("nvim-treesitter.parsers")
if parser_configs.get_parser_configs then
parser_configs = parser_configs.get_parser_configs()
end

parser_configs.norg = {
install_info = module.config.public.parser_configs.norg,
Expand Down Expand Up @@ -109,8 +108,6 @@ module.load = function()
})
end

module.private.ts_utils = ts_utils

vim.keymap.set(
"",
"<Plug>(neorg.treesitter.next.heading)",
Expand Down Expand Up @@ -164,11 +161,7 @@ module.config.public = {
---@class core.integrations.treesitter
module.public = {
parser_path = nil,
--- Gives back an instance of `nvim-treesitter.ts_utils`
---@return table #`nvim-treesitter.ts_utils`
get_ts_utils = function()
return module.private.ts_utils
end,

--- Jumps to the next match of a query in the current buffer
---@param query_string string Query with `@next-segment` captures
goto_next_query_match = function(query_string)
Expand All @@ -194,7 +187,7 @@ module.public = {

-- Find and go to the first matching node that starts after the current cursor position.
if (start_line == line_number and start_col > col_number) or start_line > line_number then
module.private.ts_utils.goto_node(node) ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
module.public.goto_node(node)
return
end
end
Expand Down Expand Up @@ -236,7 +229,7 @@ module.public = {
::continue::
end
if final_node then
module.private.ts_utils.goto_node(final_node) ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
module.public.goto_node(final_node)
end
end,
--- Gets all nodes of a given type from the AST
Expand Down Expand Up @@ -945,6 +938,103 @@ module.public = {
end,
}


--[[
-- attribution notice:
-- The below public functions are originally licensed under Apache v2 taken from:
-- https://github.com/nvim-treesitter/nvim-treesitter/blob/master/lua/nvim-treesitter/ts_utils.lua
--]]

-- Get previous node with same parent
---@param node TSNode
---@param allow_switch_parents? boolean allow switching parents if first node
---@param allow_previous_parent? boolean allow previous parent if first node and previous parent without children
module.public.get_previous_node = function(node, allow_switch_parents, allow_previous_parent)
local destination_node ---@type TSNode?
local parent = node:parent()
if not parent then
return
end

local found_pos = 0
for i = 0, parent:named_child_count() - 1, 1 do
if parent:named_child(i) == node then
found_pos = i
break
end
end
if 0 < found_pos then
destination_node = parent:named_child(found_pos - 1)
elseif allow_switch_parents then
local previous_node = module.private.get_previous_node(node:parent())
if previous_node and previous_node:named_child_count() > 0 then
destination_node = previous_node:named_child(previous_node:named_child_count() - 1)
elseif previous_node and allow_previous_parent then
destination_node = previous_node
end
end
return destination_node
end

module.public.goto_node = function(node, goto_end, avoid_set_jump)
if not node then
return
end
if not avoid_set_jump then
vim.cmd("normal! m'")
end
local range = module.public.get_node_range(node)

---@type table<number>
local position
if not goto_end then
position = { range.row_start, range.column_start }
else
position = { range.row_end, range.column_end }
end

-- Enter visual mode if we are in operator pending mode
-- If we don't do this, it will miss the last character.
local mode = vim.api.nvim_get_mode()
if mode.mode == "no" then
vim.cmd("normal! v")
end

vim.api.nvim_win_set_cursor(0, { position[1] + 1, position[2] })
end

-- Get next node with same parent
---@param node TSNode
---@param allow_switch_parents? boolean allow switching parents if last node
---@param allow_next_parent? boolean allow next parent if last node and next parent without children
module.public.get_next_node = function(node, allow_switch_parents, allow_next_parent)
local destination_node ---@type TSNode?
local parent = node:parent()

if not parent then
return
end
local found_pos = 0
for i = 0, parent:named_child_count() - 1, 1 do
if parent:named_child(i) == node then
found_pos = i
break
end
end
if parent:named_child_count() > found_pos + 1 then
destination_node = parent:named_child(found_pos + 1)
elseif allow_switch_parents then
local next_node = module.public.get_next_node(parent)
if next_node and next_node:named_child_count() > 0 then
destination_node = next_node:named_child(0)
elseif next_node and allow_next_parent then
destination_node = next_node
end
end

return destination_node
end

module.on_event = function(event)
if event.split_type[2] == "sync-parsers" then
local install = require("nvim-treesitter.install")
Expand Down
Loading
Loading