Skip to content

Commit

Permalink
chore: refactor links query into separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
benlubas authored and vhyrro committed May 20, 2024
1 parent 1e63f84 commit 792eddc
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 65 deletions.
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
---
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

0 comments on commit 792eddc

Please sign in to comment.