Skip to content

Commit

Permalink
feat: link name completions
Browse files Browse the repository at this point in the history
  • Loading branch information
benlubas authored and vhyrro committed Jun 7, 2024
1 parent 4ebb7c7 commit 8ec38e0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
51 changes: 51 additions & 0 deletions lua/neorg/modules/core/completion/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/neorg/modules/core/integrations/nvim-cmp/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ module.public = {
end

function module.private.source:get_trigger_characters()
return { "@", "-", "(", " ", ".", ":", "#", "*", "^" }
return { "@", "-", "(", " ", ".", ":", "#", "*", "^", "[" }
end

function module.private.source:is_available()
Expand Down
16 changes: 10 additions & 6 deletions lua/neorg/modules/core/integrations/treesitter/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <pysan3>

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8ec38e0

Please sign in to comment.