From 30a0ea06b7ca9831e19bf0bb4743ade8d2607f53 Mon Sep 17 00:00:00 2001 From: benlubas Date: Fri, 17 May 2024 11:57:15 -0400 Subject: [PATCH 1/2] chore: refactor links query into separate module --- .../modules/core/esupports/hop/module.lua | 69 +------------- lua/neorg/modules/core/links/module.lua | 94 +++++++++++++++++++ 2 files changed, 98 insertions(+), 65 deletions(-) create mode 100644 lua/neorg/modules/core/links/module.lua diff --git a/lua/neorg/modules/core/esupports/hop/module.lua b/lua/neorg/modules/core/esupports/hop/module.lua index 1805f99ad..a701f4b75 100644 --- a/lua/neorg/modules/core/esupports/hop/module.lua +++ b/lua/neorg/modules/core/esupports/hop/module.lua @@ -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") @@ -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) @@ -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) diff --git a/lua/neorg/modules/core/links/module.lua b/lua/neorg/modules/core/links/module.lua new file mode 100644 index 000000000..a8f1a8461 --- /dev/null +++ b/lua/neorg/modules/core/links/module.lua @@ -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 From f4daaab2b6973db17c41074f4c4e534fbd82f38f Mon Sep 17 00:00:00 2001 From: benlubas Date: Fri, 17 May 2024 12:11:34 -0400 Subject: [PATCH 2/2] fix: mark core.links as internal --- lua/neorg/modules/core/links/module.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lua/neorg/modules/core/links/module.lua b/lua/neorg/modules/core/links/module.lua index a8f1a8461..5e0c49e46 100644 --- a/lua/neorg/modules/core/links/module.lua +++ b/lua/neorg/modules/core/links/module.lua @@ -2,6 +2,7 @@ file: Links title: Find links/target in the buffer description: Utility module to handle links/link targets in the buffer + internal: true --- This module provides utility functions that are used to find links and their targets in the buffer.