diff --git a/lua/neorg/modules/core/completion/module.lua b/lua/neorg/modules/core/completion/module.lua index fff65662d..5e7eb8b36 100644 --- a/lua/neorg/modules/core/completion/module.lua +++ b/lua/neorg/modules/core/completion/module.lua @@ -7,7 +7,7 @@ This module is an intermediary between Neorg and the completion engine of your choice. After setting up this module (this usually just involves setting the `engine` field in the [configuration](#configuration) section), please read the corresponding wiki page for the engine you selected ([`nvim-cmp`](@core.integrations.nvim-cmp) -or [`nvim-compe`](@core.integrations.nvim-compe)) to complete setup. +[`coq_nvim`](@core.integrations.coq_nvim) or [`nvim-compe`](@core.integrations.nvim-compe)) to complete setup. Completions are provided in the following cases (examples in (), `|` represents the cursor location): - TODO items (`- (|`) @@ -37,6 +37,7 @@ module.config.public = { -- -- Possible values: -- - [`"nvim-cmp"`](@core.integrations.nvim-cmp) + -- - [`"coq_nvim"`](@core.integrations.coq_nvim) -- - [`"nvim-compe"`](@core.integrations.nvim-compe) engine = nil, @@ -248,6 +249,9 @@ module.load = function() elseif module.config.public.engine == "nvim-cmp" and modules.load_module("core.integrations.nvim-cmp") then modules.load_module_as_dependency("core.integrations.nvim-cmp", module.name, {}) module.private.engine = modules.get_module("core.integrations.nvim-cmp") + elseif module.config.public.engine == "coq_nvim" and modules.load_module("core.integrations.coq_nvim") then + modules.load_module_as_dependency("core.integrations.coq_nvim", module.name, {}) + module.private.engine = modules.get_module("core.integrations.coq_nvim") else log.error("Unable to load completion module -", module.config.public.engine, "is not a recognized engine.") return diff --git a/lua/neorg/modules/core/integrations/coq_nvim/module.lua b/lua/neorg/modules/core/integrations/coq_nvim/module.lua new file mode 100644 index 000000000..9cf2f5138 --- /dev/null +++ b/lua/neorg/modules/core/integrations/coq_nvim/module.lua @@ -0,0 +1,104 @@ +--[[ + file: Coq_nvim + title: Integrating Neorg with `coq_nvim` + summary: A module for integrating coq_nvim with Neorg. + internal: true + --- +This module works with the [`core.completion`](@core.completion) module to attempt to provide +intelligent completions. Note that integrations like this are second-class citizens and may not work in 100% +of scenarios. If they don't then please file a bug report! +--]] + +local neorg = require("neorg.core") +local log, modules = neorg.log, neorg.modules + +local module = modules.create("core.integrations.coq_nvim") + +module.private = { + ---@param map table + new_uid = function(map) + vim.validate({ + map = { map, "table" }, + }) + + local key ---@type integer|nil + while true do + if not key or map[key] then + key = math.floor(math.random() * 10000) + else + return key + end + end + end, +} + +module.load = function() + local success = pcall(require, "coq") + + if not success then + log.fatal("coq_nvim not found, aborting...") + return + end +end + +---@class core.integrations.coq_nvim +module.public = { + create_source = function() + module.private.completion_item_mapping = { + Directive = vim.lsp.protocol.CompletionItemKind.Keyword, + Tag = vim.lsp.protocol.CompletionItemKind.Keyword, + Language = vim.lsp.protocol.CompletionItemKind.Property, + TODO = vim.lsp.protocol.CompletionItemKind.Event, + Property = vim.lsp.protocol.CompletionItemKind.Property, + Format = vim.lsp.protocol.CompletionItemKind.Property, + Embed = vim.lsp.protocol.CompletionItemKind.Property, + Reference = vim.lsp.protocol.CompletionItemKind.Reference, + File = vim.lsp.protocol.CompletionItemKind.File, + } + + -- luacheck: push ignore 111 + -- luacheck: push ignore 112 + -- luacheck: push ignore 113 + COQsources = COQsources or {} ---@diagnostic disable undefined-global + COQsources[module.private.new_uid(COQsources)] = { + name = "Neorg", + fn = function(args, callback) + if vim.bo.filetype ~= "norg" then + return callback() + end + + local completion_cache = module.public.invoke_completion_engine(args) + + if completion_cache.options.pre then + completion_cache.options.pre(args) + end + + local completions = vim.deepcopy(completion_cache.items) + + for index, element in ipairs(completions) do + local word = element + local label = element + if type(element) == "table" then + word = element[1] + label = element.label + end + completions[index] = { + word = word, + label = label, + kind = module.private.completion_item_mapping[completion_cache.options.type], + } + end + + callback({ + isIncomplete = false, + items = completions, + }) + end, + } + -- luacheck: pop + -- luacheck: pop + -- luacheck: pop + end, +} + +return module