Skip to content

Commit

Permalink
refactor!: remove nvim-treesitter from the dependency list
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed May 28, 2024
1 parent aa47d93 commit a3e3bd2
Show file tree
Hide file tree
Showing 32 changed files with 259 additions and 272 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ The recommended installation method is via [rocks.nvim](https://github.com/nvim-
<summary>Installation snippet.</summary>

- Run `:Rocks install rocks-config.nvim` (if you don't have it already!).
- If you don't have `nvim-treesitter` installed run `:Rocks install rocks-treesitter.nvim`.
- Run `:Rocks install neorg`.
- Add the following to your config's `lua/plugins/neorg.lua`:
```lua
Expand Down
3 changes: 2 additions & 1 deletion build.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ vim.schedule(function()
"plenary.nvim == 0.1.4",
"nui.nvim == 0.3.0",
"pathlib.nvim ~> 2.2",
"tree-sitter-norg == 0.2.4",
"tree-sitter-norg-meta == 0.1.0",
})

package.loaded["neorg"] = nil

require("neorg").setup_after_build()
pcall(vim.cmd.Neorg, "sync-parsers")
end)
4 changes: 2 additions & 2 deletions docgen/docgen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ end)
require("neorg").setup({
load = {
["core.defaults"] = {},
["core.integrations.treesitter"] = {
["core.treesitter"] = {
config = {
configure_parsers = false,
},
Expand All @@ -37,7 +37,7 @@ local lib, modules, utils, log = neorg.lib, neorg.modules, neorg.utils, neorg.lo
neorg.org_file_entered(false)

-- Extract treesitter utility functions provided by Neorg and nvim-treesitter.ts_utils
local ts = modules.get_module("core.integrations.treesitter")
local ts = modules.get_module("core.treesitter")
assert(ts, "treesitter not available")

--- Aggregates all the available modules.
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/norg.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Activate treesitter highlighting (just in case it hasn't been activated beforehand)
vim.treesitter.start()
4 changes: 2 additions & 2 deletions lua/neorg/modules/core/clipboard/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ local module = modules.create("core.clipboard")
module.setup = function()
return {
requires = {
"core.integrations.treesitter",
"core.treesitter",
},
}
end
Expand All @@ -33,7 +33,7 @@ module.load = function()
range[2][1] = range[2][1] - 1

for i = range[1][1], range[2][1] do
local node = module.required["core.integrations.treesitter"].get_first_node_on_line(data.buf, i)
local node = module.required["core.treesitter"].get_first_node_on_line(data.buf, i)

while node:parent() do
if module.private.callbacks[node:type()] then
Expand Down
25 changes: 11 additions & 14 deletions lua/neorg/modules/core/completion/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.config.public = {
module.setup = function()
return {
success = true,
requires = { "core.dirman", "core.dirman.utils", "core.integrations.treesitter", "core.links" },
requires = { "core.dirman", "core.dirman.utils", "core.treesitter", "core.links" },
}
end

Expand Down Expand Up @@ -260,7 +260,7 @@ 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"]
treesitter = module.required["core.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 @@ -567,9 +567,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 @@ -588,13 +585,13 @@ 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 = {} }
end

local previous_node = ts.get_previous_node(current_node, true, true)
local previous_node = module.required["core.treesitter"].get_previous_node(current_node, true, true)

-- If the previous node is nil
if not previous_node then
Expand All @@ -621,13 +618,13 @@ 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 = {} }
end

local next_node = ts.get_next_node(current_node, true, true)
local next_node = module.required["core.treesitter"].get_next_node(current_node, true, true)

-- If it's nil
if not next_node then
Expand All @@ -652,7 +649,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 @@ -664,19 +661,19 @@ 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
return ret_completions
end

local next_node = ts.get_next_node(current_node, true, true)
local previous_node = ts.get_previous_node(current_node, true, true)
local next_node = module.required["core.treesitter"].get_next_node(current_node, true, true)
local previous_node = module.required["core.treesitter"].get_previous_node(current_node, true, true)

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

Expand Down
4 changes: 2 additions & 2 deletions lua/neorg/modules/core/concealer/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ module.setup = function()
success = true,
requires = {
"core.autocommands",
"core.integrations.treesitter",
"core.treesitter",
},
}
end
Expand Down Expand Up @@ -1136,7 +1136,7 @@ local function prettify_range(bufid, row_start_0b, row_end_0bex)
-- TODO: optimize
row_end_0bex = math.min(row_end_0bex + 1, vim.api.nvim_buf_line_count(bufid))

local treesitter_module = module.required["core.integrations.treesitter"]
local treesitter_module = module.required["core.treesitter"]
local document_root = treesitter_module.get_document_root(bufid)
assert(document_root)

Expand Down
2 changes: 1 addition & 1 deletion lua/neorg/modules/core/defaults/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ return modules.create_meta(
"core.esupports.hop",
"core.esupports.indent",
"core.esupports.metagen",
"core.integrations.treesitter",
"core.treesitter",
"core.itero",
"core.journal",
"core.keybinds",
Expand Down
44 changes: 18 additions & 26 deletions lua/neorg/modules/core/esupports/hop/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.setup = function()
return {
success = true,
requires = {
"core.integrations.treesitter",
"core.treesitter",
"core.ui",
"core.dirman.utils",
"core.links",
Expand Down Expand Up @@ -76,7 +76,7 @@ module.public = {
end

local range =
module.required["core.integrations.treesitter"].get_node_range(located_anchor_declaration.node)
module.required["core.treesitter"].get_node_range(located_anchor_declaration.node)

vim.cmd([[normal! m`]])
vim.api.nvim_win_set_cursor(0, { range.row_start + 1, range.column_start })
Expand Down Expand Up @@ -172,7 +172,7 @@ module.public = {
end

if located_link_information.node then
local range = module.required["core.integrations.treesitter"].get_node_range(
local range = module.required["core.treesitter"].get_node_range(
located_link_information.node
)

Expand Down Expand Up @@ -274,14 +274,8 @@ module.public = {
--- Locate a `link` or `anchor` node under the cursor
---@return userdata|nil #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 found_node = module.required["core.integrations.treesitter"].find_parent(
local current_node = vim.treesitter.get_node()
local found_node = module.required["core.treesitter"].find_parent(
current_node,
{ "link", "anchor_declaration", "anchor_definition" }
)
Expand All @@ -296,8 +290,6 @@ module.public = {
--- Attempts to locate a `link` or `anchor` node after the cursor on the same line
---@return userdata|nil #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 @@ -324,7 +316,7 @@ module.public = {
smaller_value - 1,
})

local node_under_cursor = ts_utils.get_node_at_cursor()
local node_under_cursor = vim.treesitter.get_node()

if vim.tbl_contains({ "link_location", "link_description" }, node_under_cursor:type()) then
resulting_node = node_under_cursor:parent()
Expand All @@ -345,7 +337,7 @@ module.public = {

local target = module
.required
["core.integrations.treesitter"]
["core.treesitter"]
.get_node_text(anchor_decl_node:named_child(0):named_child(0)) ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
:gsub("[%s\\]", "")

Expand All @@ -357,7 +349,7 @@ module.public = {
)
]]

local document_root = module.required["core.integrations.treesitter"].get_document_root()
local document_root = module.required["core.treesitter"].get_document_root()

if not document_root then
return
Expand All @@ -369,7 +361,7 @@ module.public = {
local capture = query.captures[id]

if capture == "text" then
local original_title = module.required["core.integrations.treesitter"].get_node_text(node)
local original_title = module.required["core.treesitter"].get_node_text(node)
local title = original_title:gsub("[%s\\]", "")

if title:lower() == target:lower() then
Expand Down Expand Up @@ -448,14 +440,14 @@ module.public = {
]
]]

local document_root = module.required["core.integrations.treesitter"].get_document_root(buf)
local document_root = module.required["core.treesitter"].get_document_root(buf)

if not document_root then
return
end

local query = utils.ts_parse_query("norg", query_text)
local range = module.required["core.integrations.treesitter"].get_node_range(link_node)
local range = module.required["core.treesitter"].get_node_range(link_node)

local parsed_link_information = {
link_node = link_node,
Expand All @@ -464,12 +456,12 @@ module.public = {
for id, node in query:iter_captures(document_root, buf, range.row_start, range.row_end + 1) do
local capture = query.captures[id]

local capture_node_range = module.required["core.integrations.treesitter"].get_node_range(node)
local capture_node_range = module.required["core.treesitter"].get_node_range(node)

-- Check whether the node captured node is in bounds.
-- There are certain rare cases where incorrect nodes would be parsed.
if range_contains(range, capture_node_range) then
local extract_node_text = lib.wrap(module.required["core.integrations.treesitter"].get_node_text, node)
local extract_node_text = lib.wrap(module.required["core.treesitter"].get_node_text, node)

parsed_link_information[capture] = parsed_link_information[capture]
or lib.match(capture)({
Expand Down Expand Up @@ -583,7 +575,7 @@ module.public = {

_ = function()
local query_str = links.get_link_target_query_string(parsed_link_information.link_type)
local document_root = module.required["core.integrations.treesitter"].get_document_root(buf_pointer)
local document_root = module.required["core.treesitter"].get_document_root(buf_pointer)

if not document_root then
return
Expand All @@ -596,7 +588,7 @@ module.public = {

if capture == "title" then
local original_title =
module.required["core.integrations.treesitter"].get_node_text(node, buf_pointer)
module.required["core.treesitter"].get_node_text(node, buf_pointer)

if original_title then
local title = original_title:gsub("[%s\\]", "")
Expand Down Expand Up @@ -791,7 +783,7 @@ module.private = {

local query = utils.ts_parse_query("norg", query_str)

local document_root = module.required["core.integrations.treesitter"].get_document_root(buffer)
local document_root = module.required["core.treesitter"].get_document_root(buffer)

if not document_root then
return ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
Expand All @@ -805,7 +797,7 @@ module.private = {
local capture_name = query.captures[id]

if capture_name == "title" then
local text = module.required["core.integrations.treesitter"].get_node_text(node, buffer)
local text = module.required["core.treesitter"].get_node_text(node, buffer)
local similarity = module.private.calculate_similarity(parsed_link_information.link_location_text, text)

-- If our match is similar enough then add it to the list
Expand Down Expand Up @@ -838,7 +830,7 @@ module.private = {
return
end

local range = module.required["core.integrations.treesitter"].get_node_range(link_node)
local range = module.required["core.treesitter"].get_node_range(link_node)

local prefix = lib.when(
parsed_link_information.link_type == "generic" and not force_type,
Expand Down
Loading

0 comments on commit a3e3bd2

Please sign in to comment.