|
| 1 | +local source = require('render-markdown.integ.source') |
| 2 | + |
| 3 | +local M = {} |
| 4 | + |
| 5 | +---@class coq.Args |
| 6 | +---@field pos {[1]: integer, [2]:integer} |
| 7 | +---@field line string |
| 8 | + |
| 9 | +---@class coq.CallbackArgs |
| 10 | +---@field isIncomplete boolean |
| 11 | +---@field items vim.lsp.CompletionResult |
| 12 | + |
| 13 | +---@class coq.Source |
| 14 | +---@field name string |
| 15 | +---@field fn fun(args: coq.Args, callback: fun(args?: coq.CallbackArgs)): fun()|nil |
| 16 | + |
| 17 | +---@alias coq.Sources table<integer, coq.Source> |
| 18 | + |
| 19 | +---@param map coq.Sources |
| 20 | +local function new_uid(map) |
| 21 | + local key ---@type integer|nil |
| 22 | + while true do |
| 23 | + if not key or map[key] then |
| 24 | + key = math.floor(math.random() * 10000) |
| 25 | + else |
| 26 | + return key |
| 27 | + end |
| 28 | + end |
| 29 | +end |
| 30 | + |
| 31 | +local function complete(args, callback) |
| 32 | + if not source.enabled() then |
| 33 | + callback(nil) |
| 34 | + return |
| 35 | + end |
| 36 | + |
| 37 | + local last_char = args.line:sub(#args.line, #args.line) |
| 38 | + if not vim.list_contains(source:trigger_characters(), last_char) then |
| 39 | + callback(nil) |
| 40 | + return |
| 41 | + end |
| 42 | + |
| 43 | + local row, col = unpack(args.pos) ---@type integer, integer |
| 44 | + |
| 45 | + local items = source.items(0, row, col) |
| 46 | + |
| 47 | + if items == nil then |
| 48 | + callback(nil) |
| 49 | + return |
| 50 | + end |
| 51 | + |
| 52 | + callback(items) |
| 53 | +end |
| 54 | + |
| 55 | +---Should only be called by a user to enable the integration |
| 56 | +function M.setup() |
| 57 | + local has_coq = pcall(require, 'coq') |
| 58 | + if not has_coq then |
| 59 | + return |
| 60 | + end |
| 61 | + COQsources = COQsources or {} ---@type coq.Sources |
| 62 | + COQsources[new_uid(COQsources)] = { |
| 63 | + name = 'rMD', |
| 64 | + fn = complete, |
| 65 | + } |
| 66 | +end |
| 67 | + |
| 68 | +return M |
0 commit comments