ms-jpq / coq_nvim

Fast as FUCK nvim completion. SQLite, concurrent scheduler, hundreds of hours of optimization.
GNU General Public License v3.0
3.49k stars 96 forks source link

request of help for custom source #470

Open euglevi opened 2 years ago

euglevi commented 2 years ago

First of all, thank you so much for Coq_nvim. I think it is a brilliant autocompletion system and I prefer it over cmp for simplicity and speed.

I am trying to create a source that would use a specific filetype's syntax. There is no LSP for that filetype for now. The way I was doing this with cmp was with the cmp-omni plugin and letting omnifunc be syntaxcomplete#Complete for that filetype.

Is it possible to do something similar for Coq_nvim? I saw that in cow.thirdparty there is an interface for omnifunc that is used by the Vimtex source, but I really would not know how to use it to build a source for omnifunc's syntax. I am too ignorant of lua, I am afraid. Is there someone willing to help? Thanks.

mendes-davi commented 2 years ago

Hi, it's quite simple.

I am too ignorant of lua

Take a look at nanotee/nvim-lua-guide

Expanding the interface from coq.thirdparty into a snippet for omnifunc completion...

local utils = require "coq_3p.utils"
local omnifunc = require "coq_3p.omnifunc"

COQsources = COQsources or {}
COQsources[utils.new_uid(COQsources)] = {
    name = "Vimtex",
    fn = omnifunc {
        use_cache = true,
        omnifunc = "vimtex#complete#omnifunc",
        filetypes = { "tex", "plaintex" },
    },
}
euglevi commented 2 years ago

Thank you very much! I had been trying with the nanotee's guide but without much luck I guess...

Based on your example, I have tried the following:

local utils = require "coq_3p.utils"
local omnifunc = require "coq_3p.omnifunc"

COQsources = COQsources or {}
COQsources[utils.new_uid(COQsources)] = {
    name = "Stata",
    fn = omnifunc {
        use_cache = true,
        omnifunc = "syntaxcomplete#Complete",
        filetypes = {"stata", "python"},
    },
}

I added python to the list of filetypes just to try if the source was working out. However, no luck. "Stata" suggestions do not come in the list of suggested autocompletions.

mendes-davi commented 2 years ago

You're welcome :)

If you type :lua print(vim.inspect(COQsources)) you can see the current registered sources for coq. Check this to see if your custom source show up.

euglevi commented 2 years ago

It is indeed registered as a source. It is even listed twice, not sure why. But yet, no suggested autocompletion appears from that source. With cmp it was working fine, so I am not sure what is not working with Coq.

mendes-davi commented 2 years ago

It is even listed twice, not sure why.

Each time the source is registered it gets a random uid so it might be registered twice in your config.

So, i took some time to investigate and i've come to the conclusion that the omnifunc syntaxcomplete#Complete is only returning a list of matches. After reading :help complete-functions it is clear that this is messing with the parser in coq_3p.omnifunc because it does not return a Dict as expected.

To fix this, you could setup the source from "scratch" using the same structure as before but fetching the matches and building a source like in the coq help for CUSTOM_SOURCES.md.

local utils = require("coq_3p.utils")

COQsources = COQsources or {}
COQsources[utils.new_uid(COQsources)] = {
    name = "Stata",
    fn = function(args, callback)
        local filetypes = { "stata" }

        local acc = false
        for _, ft in pairs(filetypes) do
            if ft == vim.bo.filetype then
                acc = true
            end
        end
        if not acc then
            callback(nil)
                        return
                end

        local omnifunc = function(...)
            return vim.call("syntaxcomplete#Complete", ...)
        end

        local pos = omnifunc(1, "")
        if pos == -2 or pos == -3 then
            callback(nil)
        else
            local cword = utils.cword(args.line, pos)
            local words = omnifunc(0, cword) or {}
            local items = {}
            for _, match in ipairs(words) do
                local item = {
                    label = match,
                    insertText = match,
                    kind = vim.lsp.protocol.CompletionItemKind.Keyword,
                    detail = nil,
                }
                table.insert(items, item)
            end
            callback({ isIncomplete = true, items = items })
        end
    end,
}

I've tested this here and it's working as expected. This is just bits and pieces of code from coq_3p as i'm also not that proficient in lua. I hope this helps!

euglevi commented 2 years ago

This is indeed working as expected. Thank you very much!

Wouldn't it be a good idea to have a wiki of custom sources? It seems to me that the syntax source, together with the spell one that you provided in a different thread, could be useful to many people.

mendes-davi commented 2 years ago

Cool!

A wiki would be nice! I might work on that later :)

Mte90 commented 2 years ago

Sadly this didn't helps with this old ticket... https://github.com/ms-jpq/coq_nvim/issues/358