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

V3 Treesitter Engine #1020

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
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
50 changes: 14 additions & 36 deletions lua/neorg/modules/core/concealer/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -721,24 +721,14 @@ module.config.public = {
list = {
icons = { "•" },
nodes = {
"unordered_list1_prefix",
"unordered_list2_prefix",
"unordered_list3_prefix",
"unordered_list4_prefix",
"unordered_list5_prefix",
"unordered_list6_prefix",
"unordered_list_prefix",
},
render = module.public.icon_renderers.multilevel_on_right,
},
ordered = {
icons = (not has_anticonceal) and { "⒈", "A", "a", "⑴", "Ⓐ", "ⓐ" } or nil,
nodes = {
"ordered_list1_prefix",
"ordered_list2_prefix",
"ordered_list3_prefix",
"ordered_list4_prefix",
"ordered_list5_prefix",
"ordered_list6_prefix",
"ordered_list_prefix",
},

--- A list of icon generators.
Expand All @@ -765,14 +755,10 @@ module.config.public = {
quote = {
icons = { "│" },
nodes = {
"quote1_prefix",
"quote2_prefix",
"quote3_prefix",
"quote4_prefix",
"quote5_prefix",
"quote6_prefix",
"quote_prefix",
},
highlights = {
-- TODO(vhyrro/rewrite): Update highlights
"@neorg.quotes.1.prefix",
"@neorg.quotes.2.prefix",
"@neorg.quotes.3.prefix",
Expand All @@ -793,55 +779,47 @@ module.config.public = {
"@neorg.headings.6.prefix",
},
nodes = {
"heading1_prefix",
"heading2_prefix",
"heading3_prefix",
"heading4_prefix",
"heading5_prefix",
"heading6_prefix",
"heading_prefix",
concealed = {
"link_target_heading1",
"link_target_heading2",
"link_target_heading3",
"link_target_heading4",
"link_target_heading5",
"link_target_heading6",
-- TODO(vhyrro/rewrite): this can't be done this way
-- because of `(heading)` being the node name.
"heading_link",
},
},
render = module.public.icon_renderers.multilevel_on_right,
},
definition = {
single = {
icon = "≡",
nodes = { "single_definition_prefix", concealed = { "link_target_definition" } },
nodes = { "definition_single_prefix", concealed = { "link_target_definition" } },
render = module.public.icon_renderers.on_left,
},
multi_prefix = {
icon = "⋙ ",
nodes = { "multi_definition_prefix" },
nodes = { "definition_multi_prefix" },
render = module.public.icon_renderers.on_left,
},
multi_suffix = {
icon = "⋘ ",
nodes = { "multi_definition_suffix" },
nodes = { "definition_multi_end" },
render = module.public.icon_renderers.on_left,
},
},

footnote = {
single = {
icon = "⁎",
nodes = { "single_footnote_prefix", concealed = { "link_target_footnote" } },
nodes = { "footnote_single_prefix", concealed = { "link_target_footnote" } },
render = module.public.icon_renderers.on_left,
},
multi_prefix = {
icon = "⁑ ",
nodes = { "multi_footnote_prefix" },
nodes = { "footnote_multi_prefix" },
render = module.public.icon_renderers.on_left,
},
multi_suffix = {
icon = "⁑ ",
nodes = { "multi_footnote_suffix" },
nodes = { "footnote_multi_end" },
render = module.public.icon_renderers.on_left,
},
},
Expand Down
2 changes: 1 addition & 1 deletion lua/neorg/modules/core/esupports/metagen/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ module.public = {
local query = utils.ts_parse_query(
"norg",
[[
(ranged_verbatim_tag
(verbatim_ranged_tag
(tag_name) @name
(#eq? @name "document.meta")
) @meta
Expand Down
73 changes: 19 additions & 54 deletions lua/neorg/modules/core/integrations/treesitter/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ module.config.public = {
parser_configs = {
-- Configuration for the mainline norg parser.
norg = {
url = "https://github.com/nvim-neorg/tree-sitter-norg",
url = "https://github.com/nvim-neorg/tree-sitter-norg3",
files = { "src/parser.c", "src/scanner.cc" },
branch = "main",
revision = "6348056b999f06c2c7f43bb0a5aa7cfde5302712",
revision = "69f3e05deb28e71cccc3caeb1e93db39976f0acf",
},
-- Configuration for the metadata parser (used to parse the contents
-- of `@document.meta` blocks).
Expand All @@ -138,11 +138,13 @@ 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 Down Expand Up @@ -176,6 +178,7 @@ module.public = {
::continue::
end
end,

--- Jumps to the previous match of a query in the current buffer
---@param query_string string Query with `@next-segment` captures
goto_previous_query_match = function(query_string)
Expand Down Expand Up @@ -213,6 +216,7 @@ module.public = {
module.private.ts_utils.goto_node(final_node)
end
end,

--- Gets all nodes of a given type from the AST
---@param type string #The type of node to filter out
---@param opts? table #A table of two options: `buf` and `ft`, for the buffer and format to use respectively.
Expand Down Expand Up @@ -254,6 +258,7 @@ module.public = {

return result
end,

--- Executes function callback on each child node of the root
---@param callback function
---@param ts_tree #Optional syntax tree
Expand All @@ -266,6 +271,7 @@ module.public = {
callback(child)
end
end,

--- Executes callback on each child recursive
---@param callback function Executes with each node as parameter, can return false to stop recursion
---@param ts_tree #Optional syntax tree
Expand All @@ -285,6 +291,7 @@ module.public = {

descend(root)
end,

get_node_text = function(node, source)
source = source or 0

Expand All @@ -306,6 +313,7 @@ module.public = {

return table.concat(lines, "\n")
end,

--- Returns the first node of given type if present
---@param type string #The type of node to search for
---@param buf number #The buffer to search in
Expand All @@ -332,6 +340,7 @@ module.public = {
return iterate(tree:root())
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
Expand Down Expand Up @@ -382,6 +391,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 (userdata/treesitter node) - a node of type tag/carryover_tag
get_tag_info = function(tag_node)
Expand Down Expand Up @@ -452,6 +462,7 @@ 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
---@return table #A table of `row_start`, `column_start`, `row_end` and `column_end` values
Expand Down Expand Up @@ -481,6 +492,7 @@ module.public = {
column_end = ce,
}
end,

--- Extracts the document root from the current document or from the string
---@param src number|string The number of the buffer to extract or string with code (can be nil)
---@param filetype string? #The filetype of the buffer or the string with code
Expand All @@ -503,6 +515,7 @@ module.public = {

return tree:root()
end,

--- Attempts to find a parent of a node recursively
---@param node userdata #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.
Expand All @@ -522,6 +535,7 @@ module.public = {
_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
Expand Down Expand Up @@ -665,6 +679,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 the value returned by iter_captures
Expand All @@ -689,47 +704,6 @@ module.public = {
end,
}

-- this fixes the problem of installing neorg ts parsers on macOS without resorting to using gcc
local function install_norg_ts()
if vim.fn.has("macunix") == 1 then
-- https://github.com/nvim-neorg/tree-sitter-norg/issues/7
-- (we have to force clang to c++11 mode on macOS manually)

local shell = require("nvim-treesitter.shell_command_selectors")
local install = require("nvim-treesitter.install")

-- save the original functions
local select_executable = shell.select_executable
local compilers = install.compilers

-- temporarily patch treesitter install logic
local cc = "clang++ -std=c++11"
---@diagnostic disable-next-line: duplicate-set-field
shell.select_executable = function(executables)
return vim.tbl_filter(function(c) ---@param c string
return c ~= vim.NIL and (vim.fn.executable(c) == 1 or c == cc)
end, executables)[1]
end
install.compilers = { cc }

-- install norg parsers
local ok, err = pcall(function()
install.commands.TSInstallSync["run!"]("norg")
end)

-- no matter what, restore the defaults back
shell.select_executable = select_executable
install.compilers = compilers

-- if an error occurred during install, propagate it up
if not ok then
error(err)
end
else
vim.cmd([[ TSInstall! norg ]])
end
end

module.on_event = function(event)
if event.split_type[1] == "core.keybinds" then
if event.split_type[2] == "core.integrations.treesitter.next.heading" then
Expand All @@ -742,17 +716,8 @@ module.on_event = function(event)
module.public.goto_previous_query_match(module.private.link_query)
end
elseif event.split_type[2] == "sync-parsers" then
local ok = pcall(install_norg_ts)

if not ok then
utils.notify(
[[Unable to install norg parser.
]],
vim.log.levels.WARN
)
end

pcall(vim.cmd, "TSInstall! norg_meta")
pcall(vim.cmd, [[TSInstall! norg]])
pcall(vim.cmd, [[TSInstall! norg_meta]])
end
end

Expand Down
13 changes: 3 additions & 10 deletions queries/norg/folds.scm
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
(ranged_verbatim_tag
name: (tag_name) @_name
(verbatim_ranged_tag
(tag_name) @_name
(#eq? @_name "document.meta")
) @fold

[
(heading1)
(heading2)
(heading3)
(heading4)
(heading5)
(heading6)
] @fold
(heading) @fold
Loading
Loading