uga-rosa / cmp-dictionary

A dictionary completion source for nvim-cmp
MIT License
236 stars 16 forks source link

question: How to specify a dictionary for a special format in version 3? #60

Closed fcying closed 5 months ago

fcying commented 5 months ago

For example, how to specify a dictionary for an xmake file in the new version? file format is Lua, but it has a separate dictionary.

        filepath = {
            [".*xmake.lua"] = { dict_path .. "xmake.dict" },
        },
uga-rosa commented 5 months ago

See #59 Use autocmd.

local dict = {
  ["*"] = { "/usr/share/dict/words" },
  xmake = { dict_path .. "xmake.dict" },
}

vim.api.nvim_create_autocmd("BufEnter", {
  pattern = "*",
  callback = function(ev)
    local paths = dict["*"]
    if ev.match:find(".*xmake.lua") then
      paths = dict.xmake
    end
    require("cmp_dictionary").setup({
      paths = paths,
    })
  end,
})
fcying commented 5 months ago

@uga-rosa There is an issue when using autocmd. When setup cmp_dictionary in BufEnter , it calls nvim-cmp, and a bunch of plugins get associated started, not lazy load(event = "InsertEnter").

uga-rosa commented 5 months ago

Not an issue of this plugin, but I'll answer. Place the autocmd definition in lazy's config. config is a callback function that will be executed after the plugin is loaded.

{
  "hrsh7th/nvim-cmp",
  event = "InsertEnter",
  dependencies = {
    {
      "uga-rosa/cmp-dictionary",
      config = function()
        ...
      end
    }
  },
  ... 
}
fcying commented 5 months ago

This is how I have it set up now. If I use lazy load, when I first open a file and start typing for autocompletion with 'i', the dictionary complete doesn't take effect (BufEnter is before InsertEnter). It only works when I open a second file. disable nvim-cmp lazy load dictionary complete work fine on first file.

reproduce

nvim -u init.lua
i
--current dictionary complete can't work, path complete can work
<esc>
:e t.sh
i
--dictionary and path complete can work

test init.lua

local g, fn = vim.g, vim.fn
g.config_dir = fn.fnamemodify(fn.resolve(fn.expand('<sfile>:p')), ':h')
g.runtime_dir = g.config_dir .. "/.repro"
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = g.runtime_dir .. "/" .. name
end

local lazypath = g.config_dir .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "--single-branch",
        "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

local plugins = {
    "maxmx03/solarized.nvim",
    {
        "rcarriga/nvim-notify",
        config = function()
            require("notify").setup()
            vim.notify = require("notify")
        end
    },
    {
        "hrsh7th/nvim-cmp",
        event = "InsertEnter",    -- disable lazy load work fine
        version = false,
        dependencies = {
            { "hrsh7th/cmp-path" },
            {
                "uga-rosa/cmp-dictionary",
                config = function()
                    local dict_path = g.config_dir .. "/dict/"
                    local dict = {
                        ["*"] = { dict_path .. "dictionary" },
                        ["xmake"] = { dict_path .. "xmake.dict" },
                        ["go"] = { dict_path .. "go.dict" },
                    }

                    require("cmp_dictionary").setup({
                        paths = dict["*"],
                        exact_length = 2,
                        first_case_insensitive = false,
                        document = {
                            enable = true,
                            command = { "wn", "${label}", "-over" },
                        },
                    })

                    vim.api.nvim_create_autocmd("BufEnter", {
                        pattern = "*",
                        callback = function(ev)
                            local paths = {}
                            if ev.file:find(".*xmake.lua") then
                                paths = dict.xmake
                            else
                                paths = dict[vim.bo.filetype] or {}
                            end
                            vim.list_extend(paths, dict["*"])
                            require("cmp_dictionary").setup({
                                paths = paths,
                            })
                        end,
                    })
                end,
            },
        },
        config = function()
            local cmp = require("cmp")
            cmp.setup({
                sources = cmp.config.sources({
                    { name = "path" },
                    { name = "dictionary" },
                }),
            })
    end
    },
}

vim.opt.termguicolors = true
require("lazy").setup(plugins, {
    root = g.runtime_dir .. "/plugins",
})
vim.opt.background = "light"
vim.cmd.colorscheme("solarized")
uga-rosa commented 5 months ago

Edited to correct.

fcying commented 5 months ago

Understood, at initialization, setup cmp_dictionary once, and in subsequent BufEnter callback, setup only the paths. Thanks a lot~