uga-rosa / cmp-dictionary

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

Is it possible to change loaded dictionary dynamically based `filetype` change? #23

Closed tzws closed 2 years ago

tzws commented 2 years ago

Is it possible to change loaded dictionary dynamically based filetype change?

I've 2 workflows.

  1. use tabnnew to create a scratch window/buffer to do some work, such as edit/format a message before sending, drop the buffer after paste the message to IM. In this case, the &filetype is an empty string. Is it possible to define some dict for &filetype is empty string in the config something like:
    dic = {
      [""] =  string.format('%s/site/pack/packer/start/vim-dict/dict/text.dict',vim.fn.stdpath('data')),
      ["text"] =  string.format('%s/site/pack/packer/start/vim-dict/dict/text.dict',vim.fn.stdpath('data')),
      ["javascript,typescript"] = { string.format('%s/site/pack/packer/start/vim-dict/dict/javascript.dict',vim.fn.stdpath('data')) },
      ["lua"] = { string.format('%s/site/pack/packer/start/vim-dict/dict/lua.dict',vim.fn.stdpath('data')) },
      ["vim"] = { string.format('%s/site/pack/packer/start/vim-dict/dict/vim.dict',vim.fn.stdpath('data')) },
    }
  2. I'm working on some combined file type project, such as AWS cloudformation YAML file, it can contain shell/json code fragments. I have defined some key binding to change the &filetype on the fly
    
        nnoremap <leader>1 :set filetype=html<CR>
        nnoremap <leader>2 :set filetype=javascript<CR>
        nnoremap <leader>3 :set filetype=json<CR>
        nnoremap <leader>4 :set filetype=sh<CR>
        nnoremap <leader>5 :set filetype=sql<CR>
        nnoremap <leader>6 :set filetype=vim<CR>
        nnoremap <leader>7 :set filetype=txt<CR>
        nnoremap <leader>8 :set filetype=lua<CR>
        nnoremap <leader>9 :set filetype=tdx<CR>
        nnoremap <leader>f1 :set filetype=log<CR>
        nnoremap <leader>f2 :set filetype=txt<CR>
        nnoremap <leader>f3 :set filetype=markdown<CR>
        " Set buffer filetype
        nnoremap <Leader>f= :set filetype=


For example, when I'm editing a yaml file which contains a lot of shell code, I press <leader>4 to change the filetype to shell, take advantage of shell syntax highlighting.

After the &filetype changed, is it possible to detect the change, and load the specific dicts based on the changed filetype?
![5](https://user-images.githubusercontent.com/23232136/149911588-3c39f2f8-b06d-450f-ace0-e8782f3ba898.gif)
uga-rosa commented 2 years ago

I think lua table can work fine with empty strings, since you can use anything as a key. The second issue can also be solved. Since this is the default dictionary update, you can set it for au FileType * as well.

autocmd FileType * lua require("cmp_dictionary.caches").update()
tzws commented 2 years ago

Hi, thanks for the suggestion. Both are working. Only a little glitch:

image But it doesn't affect normal use.

uga-rosa commented 2 years ago

I can't reproduce. Is the problem caused by the above settings? In other words, is it a phenomenon that does not occur when you update to the latest version and disable the above settings, but occurs when you enable them?

tzws commented 2 years ago

Hi,

I saw the screenshot in Readme.md, it's seems it's designed behavior. image image

uga-rosa commented 2 years ago

I know that the display is not working properly. I can't help you if I don't know if it's a bug in this plugin, a hacky setting that caused it, a problem with cmp itself, or a problem with your settings that I don't know about.

uga-rosa commented 2 years ago

No reply, so I'll close it.

tzws commented 2 years ago

Thanks. I think the 3rd column 'belong to javascript.dict' is the "preview" of nvim-cmp. Since a dict entry is itself, nothing behind this dict entry, nothing need to preview, unless we can define something for it to preview, like wordnet or wordweb, organize the dict into a json format like:

{ "child": { "plural": "children" , "singular": "" , "antonym": "parent" , "synonym": "kid" , "meaning": "a son or daughter" } }