Skip to content

feat: add completion support for coq_nvim #258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ require('blink.cmp').setup({
})
```

## coq_nvim

```lua
require("render-markdown.integ.coq").setup()
```

# Setup

Checkout the [Wiki](https://github.com/MeanderingProgrammer/render-markdown.nvim/wiki)
Expand Down
68 changes: 68 additions & 0 deletions lua/render-markdown/integ/coq.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
local source = require('render-markdown.integ.source')

local M = {}

---@class coq.Args
---@field pos {[1]: integer, [2]:integer}
---@field line string

---@class coq.CallbackArgs
---@field isIncomplete boolean
---@field items vim.lsp.CompletionResult

---@class coq.Source
---@field name string
---@field fn fun(args: coq.Args, callback: fun(args?: coq.CallbackArgs)): fun()|nil

---@alias coq.Sources table<integer, coq.Source>

---@param map coq.Sources
local function new_uid(map)
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

local function complete(args, callback)
if not source.enabled() then
callback(nil)
return
end

local last_char = args.line:sub(#args.line, #args.line)
if not vim.list_contains(source:trigger_characters(), last_char) then
callback(nil)
return
end

local row, col = unpack(args.pos) ---@type integer, integer

local items = source.items(0, row, col)

if items == nil then
callback(nil)
return
end

callback(items)
end

---Should only be called by a user to enable the integration
function M.setup()
local has_coq = pcall(require, 'coq')
if not has_coq then
return
end
COQsources = COQsources or {} ---@type coq.Sources
COQsources[new_uid(COQsources)] = {
name = 'rMD',
fn = complete,
}
end

return M