uga-rosa / cmp-dictionary

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

Doubled entries when configuring vim.opt.dictionary #45

Closed martineausimon closed 1 year ago

martineausimon commented 1 year ago

Hi,

Thanks a lot for this plugin !

Since a recent update, I noticed that my entries were duplicated when setting up a dictionary with vim.opt.dictionary.

I tested with this minimal config. How can I correct this?

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

require("lazy").setup({
  'uga-rosa/cmp-dictionary',
  'hrsh7th/nvim-cmp',
})

local cmp = require'cmp'
local gl = vim.api.nvim_buf_get_lines
local has_words_before = function()
  unpack = unpack or table.unpack
  local line, col = unpack(vim.api.nvim_win_get_cursor(0))
  return col ~= 0 and 
    gl(0, line -1, line, true)[1]:sub(col, col):match("%s") == nil
end

cmp.setup({
  mapping = cmp.mapping.preset.insert({
    ["<CR>"] = cmp.mapping.confirm {
      behavior = cmp.ConfirmBehavior.Replace,
      select = false,
    },
    ["<Tab>"] = cmp.mapping(function(fallback)
      if cmp.visible() then
        cmp.select_next_item()
      elseif has_words_before() then
        cmp.complete()
      else
        fallback()
      end
    end, { "i", "s" }),
    ["<S-Tab>"] = cmp.mapping(function(fallback)
      if cmp.visible() then
        cmp.select_prev_item()
      else
        fallback()
      end
    end, { "i", "s" }),
  }),
  sources = cmp.config.sources({
    { name = 'dictionary' },
  })
})

vim.opt.dictionary = '$HOME/test.dict'
uga-rosa commented 1 year ago

I have reproduced it, but this has not happened in my normal environment. It could be a compatibility issue with the plugin manager. I will look into it.

martineausimon commented 1 year ago

Thanks !!

iotaiota commented 1 year ago

Thanks for the fix! I was experiencing this issue as well. Now resolved.