hrsh7th / nvim-cmp

A completion plugin for neovim coded in Lua.
MIT License
7.89k stars 395 forks source link

Custom "menu" option for complete item #98

Closed kristijanhusak closed 3 years ago

kristijanhusak commented 3 years ago

In nvim-compe, I could usually differentiate from where the completion item is coming by menu value on the complete-item (Buffer, Tag, Lsp, etc). Here it seems that values are always used from lsp configuration, and defaults to Text. Could these non-lsp sources somehow have their own menu option?

hrsh7th commented 3 years ago

I think it is impossible to maintain consistent values for each source, so I've decided to remove the feature.

If the user want to show the source names for v:complete_item.menu. I think it should be the following config.

cmp.setup {
  formatting = {
    format = function(entry, vim_item)
      vim_item.menu = ({
        nvim_lsp = '[LSP]',
        buffer = '[Buf]',
        path = '[Path]',
      })[entry.source.name]
    end
  }
}
kristijanhusak commented 3 years ago

Why do you need that value to be consistent? Is some logic depending on it? To me that's just like an additional info that could be basically set to anything that user wants.

hrsh7th commented 3 years ago

For example, the user might want to show CompletionItem.detail to menu field.

cmp.setup {
  formatting = {
    format = function(entry, vim_item)
      vim_item.menu = entry:get_completion_item().detail
      return vim_item
    end
  }
}

I can think of a lot of wants for menu appearance so I thought it shouldn't set the default value. What do you think about it?

kristijanhusak commented 3 years ago

I'm ok for user having an option to override it, but I think there should be a way to set some default on source level. I have two custom sources for cmp (orgmode and vim-dadbod-completion), and I'm not able to let user know that completion is coming from these sources. Is there maybe some other way to provide custom menu value, through other property maybe?

hrsh7th commented 3 years ago

Actually, the menu field is already in use, Sorry... 🥲

https://github.com/hrsh7th/nvim-cmp/blob/main/lua/cmp/entry.lua#L219

It is an introduced by LSP. I will annotate auto-import like information.

hrsh7th commented 3 years ago

The spec included by next version of LSP spec. Currently, no. But I think it is potential breaking change.

kristijanhusak commented 3 years ago

I guess adding this to the item adds some more information to it:

labelDetails = {
  type = item.menu,
},

which for db completion it returns Text [DB] by default. That's also fine by me, if you don't plan to change these labelDetails.

hrsh7th commented 3 years ago

Sorry... it might be already changed. TT

/**
 * Additional details for a completion item label.
 *
 * @since 3.17.0 - proposed state
 */
export interface CompletionItemLabelDetails {

    /**
     * An optional string which is rendered less prominently directly after
     * {@link CompletionItemLabel.label label}, without any spacing. Should be
     * used for function signatures or type annotations.
     */
    detail?: string;

    /**
     * An optional string which is rendered less prominently after
     * {@link CompletionItemLabel.detail}. Should be used for fully qualified
     * names or file path.
     */
    description?: string;
}
kristijanhusak commented 3 years ago

Ok, I'll update it accordingly once that's set up. For now I'm using labelDetails.type for my sources.

hrsh7th commented 3 years ago

@kristijanhusak I will update to the latest spec as soon as possible. Please wait!

kristijanhusak commented 3 years ago

Ok, thanks. Let me know when it's done.

hrsh7th commented 3 years ago

@kristijanhusak done. Thank you for your work!

kristijanhusak commented 3 years ago

That was fast, thanks!

hrsh7th commented 3 years ago

I trust you will keep up with changes in LSP specifications. However, if possible, it may be better to avoid using undecided specifications.

I will write this note in the README.md.

(google translated)

kristijanhusak commented 3 years ago

Hopefully it will not change so much. Even if it does, i don't think anything will break, it will just stop showing additional data.

hrsh7th commented 3 years ago

Yeah. I think so too and pray it.