Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove type error comments, fix types in ts module #1617

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
53 changes: 32 additions & 21 deletions lua/neorg/modules/core/integrations/treesitter/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -392,15 +392,15 @@ module.public = {
local range1 = module.public.node_to_lsp_range(node1)
local range2 = module.public.node_to_lsp_range(node2)

local text1 = module.public.get_node_text(node1, bufnr)
local text2 = module.public.get_node_text(node2, bufnr)
local _text1 = module.public.get_node_text(node1, bufnr)
local _text2 = module.public.get_node_text(node2, bufnr)

if not text1 or not text2 then
if not _text1 or not _text2 then
return
end

text1 = vim.split(text1, "\n")
text2 = vim.split(text2, "\n")
local text1 = vim.split(_text1, "\n")
local text2 = vim.split(_text2, "\n")

---remove trailing blank lines from the text, and update the corresponding range appropriately
---@param text string[]
Expand Down Expand Up @@ -468,6 +468,7 @@ module.public = {
end,

--- Returns the first node of given type if present
---@deprecated use get_first_node_recursive instead
---@param type string #The type of node to search for
---@param buf number #The buffer to search in
---@param parent userdata #The node to start searching in
Expand All @@ -489,14 +490,16 @@ module.public = {
end

vim.treesitter.get_parser(buf, "norg"):for_each_tree(function(tree)
-- FIXME: this return value doesn't do what the original author thinks it does
-- Iterate over all top-level children and attempt to find a match
return iterate(tree:root()) ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
end)
end,
--- Recursively attempts to locate a node of a given type
---@param type string #The type of node to look for
---@param opts table #A table of two options: `buf` and `ft`, for the buffer and format respectively
---@return any ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
---@param opts {buf: number?, ft: string?, parent: TSNode?} # Buffer, filetype (for TS parsing),
---parent, defaults to root node
---@return TSNode?
get_first_node_recursive = function(type, opts)
opts = opts or {}
local result
Expand All @@ -519,11 +522,13 @@ module.public = {
root = tree:root()
end

if not root then return end

--- Recursively searches for a node of a given type
---@param node TSNode #The starting point for the search
local function descend(node)
-- Iterate over all children of the node and try to match their type
for child, _ in node:iter_children() do ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
for child, _ in node:iter_children() do
if child:type() == type then
return child
else
Expand All @@ -543,6 +548,7 @@ module.public = {

return result
end,

--- Given a node this function will break down the AST elements and return the corresponding text for certain nodes
--- @param tag_node TSNode - a node of type tag/carryover_tag
--- @param throw boolean - when true, throw an error instead of logging and returning on failure
Expand Down Expand Up @@ -576,12 +582,12 @@ module.public = {
end
elseif child:type() == "tag_name" then
-- If we're dealing with the tag name then append the text of the tag_name node to this table
table.insert(resulting_name, vim.split(module.public.get_node_text(child), "\n")[1]) ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
table.insert(resulting_name, vim.split(module.public.get_node_text(child), "\n")[1])
elseif child:type() == "tag_parameters" then
table.insert(params, vim.split(module.public.get_node_text(child), "\n")[1]) ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
table.insert(params, vim.split(module.public.get_node_text(child), "\n")[1])
elseif child:type() == "ranged_verbatim_tag_content" then
-- If we're dealing with tag content then retrieve that content
content = vim.split(module.public.get_node_text(child), "\n") ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
content = vim.split(module.public.get_node_text(child), "\n")
_, content_start_column = child:range()
end
end
Expand Down Expand Up @@ -618,8 +624,8 @@ module.public = {
["end"] = { row = end_row, column = end_column },
}
end,
--- Gets the range of a given node
---@param node userdata #The node to get the range of
--- Gets the range of the given node
---@param node TSNode
---@return { row_start: number, column_start: number, row_end: number, column_end: number } range
get_node_range = function(node)
if not node then
Expand All @@ -636,7 +642,7 @@ module.public = {
local _, _, ere, ece = node[#node]:range()
return brs, bcs, ere, ece
end, function()
local a, b, c, d = node:range() ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
local a, b, c, d = node:range()
return a, b, c, d
end)

Expand Down Expand Up @@ -669,29 +675,31 @@ module.public = {

return tree:root()
end,

--- Attempts to find a parent of a node recursively
---@param node userdata #The node to start at
---@param node TSNode #The node to start at
---@param types table|string #If `types` is a table, this function will attempt to match any of the types present in the table.
-- If the type is a string, the function will attempt to pattern match the `types` value with the node type.
find_parent = function(node, types)
---@type TSNode?
local _node = node

while _node do
if type(types) == "string" then
if _node:type():match(types) then ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
if _node:type():match(types) then
return _node
end
elseif vim.tbl_contains(types, _node:type()) then ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
elseif vim.tbl_contains(types, _node:type()) then
return _node
end

_node = _node:parent() ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
_node = _node:parent()
end
end,

--- Retrieves the first node at a specific line
---@param buf number #The buffer to search in (0 for current)
---@param line number #The line number (0-indexed) to get the node from
-- the same line as `line`.
---@param line number #The line number (0-indexed) to get the node from the same line as `line`.
---@param stop_type string|table? #Don't recurse to the provided type(s)
---@return TSNode|nil #The first node on `line`
get_first_node_on_line = function(buf, line, stop_type)
Expand All @@ -708,7 +716,7 @@ module.public = {
local first_char = (vim.api.nvim_buf_get_lines(buf, line, line + 1, true)[1] or ""):match("^(%s+)[^%s]")
first_char = first_char and first_char:len() or 0

local descendant = document_root:descendant_for_range(line, first_char, line, first_char + 1) ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
local descendant = document_root:descendant_for_range(line, first_char, line, first_char + 1)

if not descendant then
return
Expand All @@ -725,6 +733,8 @@ module.public = {
break
end

if not parent then return end

descendant = parent
end

Expand Down Expand Up @@ -870,6 +880,7 @@ module.public = {

return result
end,

--- Parses a query and automatically executes it for Norg
---@param query_string string #The query string
---@param callback function #The callback to execute with all values returned by
Expand Down
Loading