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: refactor links query into separate module #1428

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
69 changes: 4 additions & 65 deletions lua/neorg/modules/core/esupports/hop/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ prompted with a set of actions that you can perform on the broken link.

local neorg = require("neorg.core")
local config, lib, log, modules, utils = neorg.config, neorg.lib, neorg.log, neorg.modules, neorg.utils
local links

local module = modules.create("core.esupports.hop")

Expand All @@ -23,11 +24,13 @@ module.setup = function()
"core.integrations.treesitter",
"core.ui",
"core.dirman.utils",
"core.links",
},
}
end

module.load = function()
links = module.required["core.links"]
modules.await("core.keybinds", function(keybinds)
keybinds.register_keybind(module.name, "hop-link")
end)
Expand Down Expand Up @@ -579,71 +582,7 @@ module.public = {
end,

_ = function()
local query_str = lib.match(parsed_link_information.link_type)({
generic = [[
[(_
[(strong_carryover_set
(strong_carryover
name: (tag_name) @tag_name
(tag_parameters) @title
(#eq? @tag_name "name")))
(weak_carryover_set
(weak_carryover
name: (tag_name) @tag_name
(tag_parameters) @title
(#eq? @tag_name "name")))]?
title: (paragraph_segment) @title)
(inline_link_target
(paragraph) @title)]
]],

[{ "definition", "footnote" }] = string.format(
[[
(%s_list
(strong_carryover_set
(strong_carryover
name: (tag_name) @tag_name
(tag_parameters) @title
(#eq? @tag_name "name")))?
.
[(single_%s
(weak_carryover_set
(weak_carryover
name: (tag_name) @tag_name
(tag_parameters) @title
(#eq? @tag_name "name")))?
(single_%s_prefix)
title: (paragraph_segment) @title)
(multi_%s
(weak_carryover_set
(weak_carryover
name: (tag_name) @tag_name
(tag_parameters) @title
(#eq? @tag_name "name")))?
(multi_%s_prefix)
title: (paragraph_segment) @title)])
]],
lib.reparg(parsed_link_information.link_type, 5)
),
_ = string.format(
[[
(%s
[(strong_carryover_set
(strong_carryover
name: (tag_name) @tag_name
(tag_parameters) @title
(#eq? @tag_name "name")))
(weak_carryover_set
(weak_carryover
name: (tag_name) @tag_name
(tag_parameters) @title
(#eq? @tag_name "name")))]?
(%s_prefix)
title: (paragraph_segment) @title)
]],
lib.reparg(parsed_link_information.link_type, 2)
),
})
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)

Expand Down
94 changes: 94 additions & 0 deletions lua/neorg/modules/core/links/module.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
--[[
file: Links
title: Find links/target in the buffer
description: Utility module to handle links/link targets in the buffer
benlubas marked this conversation as resolved.
Show resolved Hide resolved
---

This module provides utility functions that are used to find links and their targets in the buffer.
--]]

local neorg = require("neorg.core")
local lib, modules = neorg.lib, neorg.modules

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

module.setup = function()
return {
success = true,
}
end

---@class core.links
module.public = {
-- TS query strings for different link targets
---@param link_type "generic" | "definition" | "footnote" | string
get_link_target_query_string = function(link_type)
return lib.match(link_type)({
generic = [[
[(_
[(strong_carryover_set
(strong_carryover
name: (tag_name) @tag_name
(tag_parameters) @title
(#eq? @tag_name "name")))
(weak_carryover_set
(weak_carryover
name: (tag_name) @tag_name
(tag_parameters) @title
(#eq? @tag_name "name")))]?
title: (paragraph_segment) @title)
(inline_link_target
(paragraph) @title)]
]],

[{ "definition", "footnote" }] = string.format(
[[
(%s_list
(strong_carryover_set
(strong_carryover
name: (tag_name) @tag_name
(tag_parameters) @title
(#eq? @tag_name "name")))?
.
[(single_%s
(weak_carryover_set
(weak_carryover
name: (tag_name) @tag_name
(tag_parameters) @title
(#eq? @tag_name "name")))?
(single_%s_prefix)
title: (paragraph_segment) @title)
(multi_%s
(weak_carryover_set
(weak_carryover
name: (tag_name) @tag_name
(tag_parameters) @title
(#eq? @tag_name "name")))?
(multi_%s_prefix)
title: (paragraph_segment) @title)])
]],
lib.reparg(link_type, 5)
),
_ = string.format(
[[
(%s
[(strong_carryover_set
(strong_carryover
name: (tag_name) @tag_name
(tag_parameters) @title
(#eq? @tag_name "name")))
(weak_carryover_set
(weak_carryover
name: (tag_name) @tag_name
(tag_parameters) @title
(#eq? @tag_name "name")))]?
(%s_prefix)
title: (paragraph_segment) @title)
]],
lib.reparg(link_type, 2)
),
})
end,
}

return module
Loading