diff --git a/lua/neorg/modules/core/completion/module.lua b/lua/neorg/modules/core/completion/module.lua index 62e53d2dd..ab0ec6164 100644 --- a/lua/neorg/modules/core/completion/module.lua +++ b/lua/neorg/modules/core/completion/module.lua @@ -219,6 +219,33 @@ module.private = { end, } +---Suggest common link names for the given link. Suggests: +--- - target name if the link point to a heading/footer/etc. +--- - metadata `title` field +--- - file description +---@return string[] +module.private.foreign_link_names = function(context, _prev, _saved, match) + print("foreign") + local file, target = match[2], match[3] + local path = dirutils.expand_pathlib(file) + print("Context") + P(context) + print("path:", path) + print("target:", target) + return {} +end + +--- suggest the link target name +---@return string[] +module.private.local_link_names = function(context, _prev, _saved, match) + local target = match[2] + if target then + target = target:gsub("^%s+", "") + target = target:gsub("%s+$", "") + end + return { target } +end + module.load = function() -- If we have not defined an engine then bail if not module.config.public.engine then @@ -519,6 +546,30 @@ module.public = { completion_start = "^", }, }, + { -- foreign link name suggestions `{:path:target}[|]` + regex = "^(.*){:([^:]*):([^}]*)}%[", + + complete = module.private.foreign_link_names, + + node = module.private.normal_norg, + + options = { + type = "Reference", + completion_start = "[", + }, + }, + { -- local link name suggestions `{target}[|]` for `#`, `$`, `^`, `*` link targets + regex = "^(.*){[#$*%^]+ ([^}]*)}%[", + + complete = module.private.local_link_names, + + node = module.private.normal_norg, + + options = { + type = "Reference", + completion_start = "[", + }, + }, }, --- Parses the public completion table and attempts to find all valid matches diff --git a/lua/neorg/modules/core/integrations/nvim-cmp/module.lua b/lua/neorg/modules/core/integrations/nvim-cmp/module.lua index ce4137601..397fbe8a7 100644 --- a/lua/neorg/modules/core/integrations/nvim-cmp/module.lua +++ b/lua/neorg/modules/core/integrations/nvim-cmp/module.lua @@ -87,7 +87,7 @@ module.public = { end function module.private.source:get_trigger_characters() - return { "@", "-", "(", " ", ".", ":", "#", "*", "^" } + return { "@", "-", "(", " ", ".", ":", "#", "*", "^", "[" } end function module.private.source:is_available() diff --git a/lua/neorg/modules/core/integrations/treesitter/module.lua b/lua/neorg/modules/core/integrations/treesitter/module.lua index da7329020..38df98d02 100644 --- a/lua/neorg/modules/core/integrations/treesitter/module.lua +++ b/lua/neorg/modules/core/integrations/treesitter/module.lua @@ -772,7 +772,7 @@ module.public = { local meta_node for id, node in norg_query:iter_captures(norg_tree:root(), source) do if norg_query.captures[id] == "tag_name" then - local tag_name = trim(module.public.get_node_text(node, buf)) + local tag_name = trim(module.public.get_node_text(node, source)) if tag_name == "document.meta" then meta_node = node:next_named_sibling() or vim.NIL break @@ -784,6 +784,7 @@ module.public = { return result end + local meta_source = module.public.get_node_text(meta_node, source) local norg_meta_parser = vim.treesitter.get_string_parser(meta_source, "norg_meta") ---@diagnostic disable-line -- TODO: type error workaround @@ -813,18 +814,21 @@ module.public = { 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 - ---@param buffer number #The buffer ID for the query + ---@param callback function #The callback to execute with all values returned by + ---`Query:iter_captures()`. When callback returns true, this function returns early + ---@param source number | string | PathlibPath #buf number, or file path or 0 for current buffer ---@param start number? #The start line for the query ---@param finish number? #The end line for the query - execute_query = function(query_string, callback, buffer, start, finish) + execute_query = function(query_string, callback, source, start, finish) local query = utils.ts_parse_query("norg", query_string) - local root = module.public.get_document_root(buffer) + local norg_parser, iter_src = module.public.get_ts_parser(source) - if not root then + if not norg_parser then return false end + local root = norg_parser:parse()[1]:root() + for id, node, metadata in query:iter_captures(root, iter_src, start, finish) do if callback(query, id, node, metadata) == true then return true end