uga-rosa / cmp-dictionary

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

opt:dictionary and opt:thesaurus #30

Closed FOSSilizedDaemon closed 1 year ago

FOSSilizedDaemon commented 2 years ago

Howdy,

This just so happens to fill an exact need I have, but had a quick design question. I have my setup configured to add all dictionaries/wordlists to opt:dictionary as well as do something similar with opt:thesaurus (see here). I was curious if it is possible to just pull from those sources instead of having to mark them in the setup for this plugin?

uga-rosa commented 2 years ago

hi

I currently use lua to load dictionaries, but are you asking if we can use neovim's standard feature of completion for this? I thought it was not possible because I don't know of any function that can get candidate completions. If you can, I would like to know.

FOSSilizedDaemon commented 2 years ago

hi

I currently use lua to load dictionaries, but are you asking if we can use neovim's standard feature of completion for this? I thought it was not possible because I don't know of any function that can get candidate completions. If you can, I would like to know.

I don't know enough about Lua or neovim to know how this is done, but I use to use nvim's built-in completion and all I had to do was load my dictionary files like so:

´´´ -- Load all files from 'HOME/.usr/share/dict' and '/usr/share/dict/' to be used as dictionary files. local dictionary_directories = env.HOME .. '/.usr/share/dict/' .. ',' .. '/usr/share/dict/' local dictionary_files = fn.globpath(dictionary_directories, '*', 0, 1)

for _, file in pairs(dictionary_files) do opt.dictionary:append(file) end ´´´ then I could open a buffer, start typing, and hit say c-p and start cycling through all of the words from my dictionaries. I don't know how a plugin would pass those to nvim-cmp though.

uga-rosa commented 2 years ago

I'm not sure what the purpose of this issue is, although I suppose it would work just as well if you registered the dictionary in dic['*'] of the setup as well.

FOSSilizedDaemon commented 1 year ago

I'm not sure what the purpose of this issue is, although I suppose it would work just as well if you registered the dictionary in dic['*'] of the setup as well.

Aha, so I got that working with this but for some reason I don't get completion when I type ;-;

uga-rosa commented 1 year ago

Do you use lazy loading? Check help. https://github.com/uga-rosa/cmp-dictionary/blob/0ba3df56258b48a5a6a0130d31ae3cf4908c9567/doc/cmp-dictionary.txt#L250

FOSSilizedDaemon commented 1 year ago

Do you use lazy loading? Check help.

https://github.com/uga-rosa/cmp-dictionary/blob/0ba3df56258b48a5a6a0130d31ae3cf4908c9567/doc/cmp-dictionary.txt#L250

I don't use a plugin manager, but instead use git submodule to manage my plugins. I tried running ´CmpDictionaryUpdate´, but it still does not provide any suggestions sadly ;-;

uga-rosa commented 1 year ago

Cannot be reproduced. Please create a minimum setup and procedure to reproduce the issue.

FOSSilizedDaemon commented 1 year ago

Cannot be reproduced. Please create a minimum setup and procedure to reproduce the issue.

Install the latest version of neovim.

Allow vim to load all plugins in init.lua

-- The load.lua file contains all lua related to loading plugins. When this file is loaded the below
-- lua will do the following:
--    1. Configure settings for loading plugins.

local env = vim.env
local opt = vim.opt
local cmd = vim.cmd

-- Add the 'XDG_CONFIG_HOME/.config/nvim/' directory to the plugin path.
local config_directory = env.XDG_CONFIG_HOME
opt.packpath:append(config_directory .. '/nvim/pack')

-- Load all plugins found in the plugin path.
cmd([[ packloadall ]])

-- Load filetype plugins.
cmd([[ filetype plugin on ]])
cmd([[ filetype indent on ]])

-- Enable file type detection.
cmd([[ filetype on ]])

Clone needed plugins

mkdir -p ~/.config/nvim/pack/start
git submodule add https://github.com/hrsh7th/nvim-cmp.git
git submodule add https://github.com/uga-rosa/cmp-dictionary.git

Create needed plugin settings

mkdir -p ~/.config/nvim/plugin
-- add to nvim-cmp.lua:

local cmp = require('cmp')

cmp.setup {
    sources = {
        -- Provide completion from the current buffer, file system paths, and
        -- language server protocol.
        { name = "buffer",   keyword_length = 1 },,
    },

    completion = {
]],
    },

    window = {
        -- Wrap the completion menu window in a border.
        completion = cmp.config.window.bordered(),

        -- Wrap the completion menu's documentation window in a border.
        documentation = cmp.config.window.bordered(),
    },
}

-- add to cmp-dictionary.lua

local env = vim.env
local fn  = vim.fn

local cmp = require("cmp")
local cmp_dictionary = require("cmp_dictionary")

-- Load all files from 'HOME/.usr/share/dict' and '/usr/share/dict/' to be used as dictionary files.
local dictionary_directories = env.HOME .. '/.usr/share/dict/' .. ',' .. '/usr/share/dict/'
local dictionary_files = fn.globpath(dictionary_directories, '*', 0, 1)

cmp.setup({
    sources = {
        {
            -- An dictionaries to the Cmp sources list.
            name = "dictionary",
            keyword_length = 1,
        },
    }
})

require("cmp_dictionary").setup({
    dic = {
        ["*"] = dictionary_files,
    },

    -- Only show candidates with an exact prefix match.
    exact = -1,

    -- Do not ignore the case of the first character.
    first_case_insensitive = false,

    -- Do not ctivate document using external command.
    document = false,

    -- Use wn(1) for the above document tool.
    document_command = "wn %s -over",

    -- Perform the initialization in a separate thread.
    async = true,

    -- Cache at least 10 dictionaries.
    capacity = 10,

    -- Do not output debug messages.
    debug = false,
})

Then open nvim and run ´CmpDictionaryUpdate´. Start typing and you will see that no dictionary completion is offered, but if you type the word Hello on line one and then type He you will get Hello as a completion offer (therefore I know nvim-cmp is working).

uga-rosa commented 1 year ago
local env = vim.env
local opt = vim.opt
local cmd = vim.cmd
local fn  = vim.fn

local config_directory = env.XDG_CONFIG_HOME
opt.packpath:append(config_directory .. '/nvim/pack')

cmd([[
packloadall
filetype plugin indent on
]])

local cmp = require('cmp')

cmp.setup {
    sources = {
        {
            name = "dictionary",
            keyword_length = 1,
        },
    },
}

local cmp_dictionary = require("cmp_dictionary")

local dictionary_directories = env.HOME .. '/.usr/share/dict/' .. ',' .. '/usr/share/dict/'
local dictionary_files = fn.globpath(dictionary_directories, '*', 0, 1)

cmp_dictionary.setup({
    dic = {
        ["*"] = dictionary_files,
    },
    exact = -1,
    first_case_insensitive = false,
    document = false,
    document_command = "wn %s -over",
    async = true,
    capacity = 10,
    debug = false,
})

This is the very init.lua I used and it seems to work fine. Can you try it?

FOSSilizedDaemon commented 1 year ago
local env = vim.env
local opt = vim.opt
local cmd = vim.cmd
local fn  = vim.fn

local config_directory = env.XDG_CONFIG_HOME
opt.packpath:append(config_directory .. '/nvim/pack')

cmd([[
packloadall
filetype plugin indent on
]])

local cmp = require('cmp')

cmp.setup {
  sources = {
      {
          name = "dictionary",
          keyword_length = 1,
      },
  },
}

local cmp_dictionary = require("cmp_dictionary")

local dictionary_directories = env.HOME .. '/.usr/share/dict/' .. ',' .. '/usr/share/dict/'
local dictionary_files = fn.globpath(dictionary_directories, '*', 0, 1)

cmp_dictionary.setup({
  dic = {
      ["*"] = dictionary_files,
  },
  exact = -1,
  first_case_insensitive = false,
  document = false,
  document_command = "wn %s -over",
  async = true,
  capacity = 10,
  debug = false,
})

This is the very init.lua I used and it seems to work fine. Can you try it?

Yep! The solution was to merge the cmp-dictionary.lua and nvim-cmp.lua files into one file. Thank you so much!