uga-rosa / cmp-dictionary

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

How to show a word meaning as a document? #44

Closed ivan-volnov closed 1 year ago

ivan-volnov commented 1 year ago

Hi!

Thanks for the great plugin!

I use it with my anki word deck to use the vocabulary I'm learning. Extremely useful!

Please add LSP preview what can display a word meaning or translation.

image

ivan-volnov commented 1 year ago

Just a plain text. The dict format could be:

word1 meaning1
word2 meaning2
...

Or something another..

uga-rosa commented 1 year ago

Hi. Thanks for using this plugin. I can't support that grammar, sorry. Because I want to keep the dictionary format compatible with the 'dictionary' option (That is, in the form of a collection of words separated by spaces, line breaks, and other whitespace characters). But there is a way. See :h cmp-dictionary-document. Documentation can be attached using a program such as wordnet where you can get a description of the word from the word.

image

If you want to add your own description, create a program like this one.

require("cmp_dictionary").setup({
  document = true,
  document_command = "get_description.lua %s"
})

local dictionary = { word1 = "meaning1", word2 = "meaning2", }

local word = arg[1] if word then print(dictionary[word]) end


Of course, this is just an example and does not have to be written in lua.
ivan-volnov commented 1 year ago

Thanks for the reply!

Well, can you make document_command accept a lua function optionally without an external tool? A system command call is quite slow.

Or may be better add an alternative way we can inject words and "documentation" as a lua dict instead of a dict file and a separate call to document_command? So user can implement their own format and parsers.

And how to hide "belong to" record?

uga-rosa commented 1 year ago

It is true that external commands are slow, but I think this is not a practical problem since it is delayed until the candidate is selected. This feature originally came about because of requests to use wordnet.

You can create your own sources, so if you want to pass tables from lua, it is better to do so.

require('cmp').setup({
  ...
  sources = {
    ...
    { name = 'meaning' },
  },
})

local meaning = {}

function meaning:is_available()
  return true
end

function meaning:get_debug_name()
  return 'meaning'
end

function meaning:get_keyword_pattern()
  return [[\k\+]]
end

function meaning:get_trigger_characters()
  return { '.' }
end

function meaning:complete(params, callback)
  callback({
    { label = 'word1', documentation = 'meaning1' },
    { label = 'word2', documentation = 'meaning2' },
  })
end

function meaning:resolve(completion_item, callback)
  callback(completion_item)
end

function meaning:execute(completion_item, callback)
  callback(completion_item)
end

require('cmp').register_source('meaning', meaning)

Currently, it is not possible to set the belong to to be hidden. I will add the option.

ivan-volnov commented 1 year ago

Thanks a lot! Tested. It works.

uga-rosa commented 1 year ago

Good. Can you close this issue?