uga-rosa / cmp-dictionary

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

Error in decode when opening a file #17

Closed andriati-alex closed 2 years ago

andriati-alex commented 2 years ago

The plugin was working fine but suddenly I got the following error msg:

Error executing luv callback:                                                                                                                                    
...acker/start/cmp-dictionary/lua/cmp_dictionary/caches.lua:139: bad argument #1 to 'decode' (string expected, got nil)
stack traceback:
        [C]: in function 'decode'
        ...acker/start/cmp-dictionary/lua/cmp_dictionary/caches.lua:139: in function <...acker/start/cmp-dictionary/lua/cmp_dictionary/caches.lua:138>

I tried to use different dictionaries and reduce to few words but the error still persists.

uga-rosa commented 2 years ago

Let me see your config.

andriati-alex commented 2 years ago

The completion setup

cmp.setup {
  snippet = {
    expand = function(args)
      luasnip.lsp_expand(args.body)
    end,
  },
  mapping = {
    ['<C-p>'] = cmp.mapping.select_prev_item(),
    ['<C-n>'] = cmp.mapping.select_next_item(),
    ['<C-b>'] = cmp.mapping.scroll_docs(-1),
    ['<C-f>'] = cmp.mapping.scroll_docs(1),
    ['<C-Space>'] = cmp.mapping.complete(),
    ['<C-e>'] = cmp.mapping.close(),
    ['<CR>'] = cmp.mapping.confirm {
      behavior = cmp.ConfirmBehavior.Replace,
      select = false, -- If true, take the first option if none is selected
    },
    ['<Tab>'] = function(fallback)
      if cmp.visible() then
        cmp.select_next_item()
      elseif luasnip.expand_or_jumpable() then
        luasnip.expand_or_jump()
      elseif check_backspace() then
        fallback()
      else
        fallback()
      end
    end,
    ['<S-Tab>'] = function(fallback)
      if cmp.visible() then
        cmp.select_prev_item()
      elseif luasnip.jumpable(-1) then
        luasnip.jump(-1)
      else
        fallback()
      end
    end,
  },
  sources = {
    { name = 'nvim_lsp' },
    { name = 'luasnip' },
    { name = 'dictionary', keyword_length = 2 },
    { name = 'path' },
    { name = 'nvim_lua' },
    { name = 'buffer' },
  },
  documentation = {
    border = { "╭", "─", "╮", "│", "╯", "─", "╰", "│" },
    maxwidth = 50,
    maxheight = 8,
  },
  formatting = {
    format = function(entry, vim_item)
      -- Kind icons with names of the completion field
      vim_item.kind = string.format('%s %s', kind_icons[vim_item.kind], vim_item.kind)
      -- Sources
      vim_item.menu = ({
        nvim_lsp = "[LSP]",
        luasnip = "[Snippet]",
        dictionary = "[Dict]",
        path = "[Path]",
        nvim_lua = "[Vim]",
        buffer = "[Buffer]",
      })[entry.source.name]
      return vim_item
    end
  },
  experimental = {ghost_text = true},
}

The second relevant part is the dictionary configuration, with the snippet below

-- The source for the dictionary must be a plain text file. A way to generate
-- such a file is with the following command:
-- aspell -d en_US dump master | aspell -l en expand > american_english.dic
local dict_path = vim.fn.expand("~/") .. ".config/dicts/american_english.dic"
require("cmp_dictionary").setup({
    dic = {
        ["tex"] = dict_path,
    },
    -- The following are default values, so you don't need to write them if you don't want to change them
    exact = 2,
    async = true,
    capacity = 5,
    debug = false,
})

Besides these, there is only the plugin installation with packer.

uga-rosa commented 2 years ago

Hmm. I can't reproduce it. I want you to create a minimum configuration.

andriati-alex commented 2 years ago

Well I think I find the problem. I got some free time this Sunday and wrote a minimal configuration file as below:

--[[ Set all basic stuff --]]

vim.g.mapleader = " "
vim.opt.clipboard = "unnamedplus" -- allows neovim to access the system clipboard
vim.opt.confirm = true -- confirm to exit if there are unsave changes
vim.opt.hlsearch = false -- after hit enter stop highlighting
vim.opt.hidden = true -- let you change buffer without saving changes
vim.opt.wrap = false -- do not break long lines
vim.opt.cursorline = true -- highlight cursor line
vim.opt.fileencoding = "UTF-8"
vim.opt.number = true
vim.opt.relativenumber = true
vim.opt.expandtab = true
vim.opt.tabstop = 4
vim.opt.shiftwidth = 4
vim.opt.softtabstop = 4
vim.opt.autoindent = true
vim.opt.smartindent = true
vim.opt.smarttab = true
vim.opt.expandtab = true
-- Finish tab and identation config
vim.opt.incsearch = true -- jump to result as you type
vim.opt.scrolloff = 8 -- minimum 8 lines ahead while scrolling
vim.opt.sidescrolloff = 8 -- minimum 8 columns scrolling
vim.opt.showmode = false
-- handle backup
vim.opt.backup = false
vim.opt.writebackup = false
-- mostly only due to completion
vim.opt.completeopt = { "menuone", "noselect" }
vim.opt.updatetime = 200

--[[ Packer bootstrap and plugins installation --]]

local fn = vim.fn
local install_path = fn.stdpath("data") .. "/site/pack/packer/start/packer.nvim"
if fn.empty(fn.glob(install_path)) > 0 then
    PACKER_BOOTSTRAP = fn.system({
        "git",
        "clone",
        "--depth",
        "1",
        "https://github.com/wbthomason/packer.nvim",
        install_path,
    })
    print("Installing packer close and reopen Neovim...")
end

-- Use a protected call so we don't get errors on first use
local status_ok, packer = pcall(require, "packer")
if not status_ok then
    vim.notify("Problem to initiate Packer")
    return
end

packer.startup(function(use)
    use("wbthomason/packer.nvim") -- Packer manage itself
    use("nvim-lua/popup.nvim")
    use("nvim-lua/plenary.nvim")
    use("neovim/nvim-lspconfig")
    use("hrsh7th/nvim-cmp")
    use("hrsh7th/cmp-nvim-lsp")
    use("hrsh7th/cmp-path")
    use("hrsh7th/cmp-buffer")
    use("hrsh7th/cmp-cmdline")
    use("uga-rosa/cmp-dictionary")

    -- Automatically set up your configuration after cloning packer.nvim
    -- Put this at the end after all plugins
    if PACKER_BOOTSTRAP then
        require("packer").sync()
    end
end)

local cmp_status_ok, cmp = pcall(require, "cmp")
if not cmp_status_ok then
    return
end

cmp.setup({
    mapping = {
        ["<C-p>"] = cmp.mapping.select_prev_item(),
        ["<C-n>"] = cmp.mapping.select_next_item(),
        ["<C-b>"] = cmp.mapping.scroll_docs(-1),
        ["<C-f>"] = cmp.mapping.scroll_docs(1),
        ["<C-Space>"] = cmp.mapping.complete(),
        ["<C-e>"] = cmp.mapping.close(),
        ["<CR>"] = cmp.mapping.confirm({
            behavior = cmp.ConfirmBehavior.Replace,
            select = false, -- If true, take the first option if none is selected
        }),
        ["<Tab>"] = function(fallback)
            if cmp.visible() then
                cmp.select_next_item()
            else
                fallback()
            end
        end,
        ["<S-Tab>"] = function(fallback)
            if cmp.visible() then
                cmp.select_prev_item()
            else
                fallback()
            end
        end,
    },
    sources = {
        { name = "dictionary", keyword_length = 2 },
        { name = "path" },
        { name = "buffer" },
    },
    formatting = {
        format = function(entry, vim_item)
            -- Kind icons with names of the completion field
            vim_item.kind = string.format("%s", vim_item.kind)
            -- Sources
            vim_item.menu = ({
                dictionary = "[Dict]",
                path = "[Path]",
                buffer = "[Buffer]",
            })[entry.source.name]
            return vim_item
        end,
    },
})

-- The source for the dictionary must be a plain text file. A way to generate
-- such a file is with the following command:
-- aspell -d en_US dump master | aspell -l en expand > american_english.dic
local dict_path = vim.fn.expand("~/test_dictionary.dic")
require("cmp_dictionary").setup({
    dic = {
        ["*"] = dict_path,
    },
    -- The following are default values, so you don't need to write them if you don't want to change them
    exact = 2,
    async = false, -- THIS IS CAUSING THE PROBLEM WHEN SET TO TRUE
    capacity = 5,
    debug = false,
})

I wrote a very simple text file containing some words in my home dir named test_dictionary.dic. The problem is when I try to set async option to true, then I get the error described above. I them figure out that I have lua53-mpack instead of lua lua51-mpack as suggest in the README. Now that I installed the correct version of mpack is working well with both options for async. Sorry for the inconvenience, but the issue is solved.