Skip to content

Commit

Permalink
feat: add integrations.coq_nvim as a new completion engine
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLeoP authored and vhyrro committed May 23, 2024
1 parent 936b051 commit b8f9f83
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lua/neorg/modules/core/completion/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 (`- (|`)
Expand Down Expand Up @@ -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,

Expand Down Expand Up @@ -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
Expand Down
104 changes: 104 additions & 0 deletions lua/neorg/modules/core/integrations/coq_nvim/module.lua
Original file line number Diff line number Diff line change
@@ -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<number, 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

0 comments on commit b8f9f83

Please sign in to comment.