hrsh7th / nvim-cmp

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

option to autocomplete your cursor's text on the first item too #1529

Closed Thanatermesis closed 1 year ago

Thanatermesis commented 1 year ago

I have the "autocomplete = false", so menu only opens when I hit "tab"

If you see, when the second item is selected, the text in the cursor is filled up (similar to ghost_text), but this doesn't happens with the first item

In the normal behaviour that's somewhat normal, where the menu always opens with the first item preselected, but only when you scroll to the next option your cursor's text is modified, the issue is that the first item is not really preselected just hilighted, but when you trigger cmp.complete(), the first item should be selected in the same way the next ones are

Shougo commented 1 year ago

It seems dup of https://github.com/hrsh7th/nvim-cmp/discussions/1424

hrsh7th commented 1 year ago

Sorry. I can't understand.

I guess you should check the following two points.

  1. The preselect option See :help cmp-faq

  2. Remove completeopt setting and set coompleteopt as vim option

set completeopt=menu,menuone,noselect
Thanatermesis commented 1 year ago

@hrsh7th tried but doesn't seems to work, I have recorded a video showing the issue: https://youtu.be/awc4BjLwf9M

hrsh7th commented 1 year ago

What do you get the result with the followings?

  1. :echomsg &completeopt
  2. :imap <Tab>
  3. :lua= require('cmp').get_config()
Shougo commented 1 year ago

It seems Vim completeopt menu behavior(manual completion). nvim-cmp default behavior is for auto completion.

Thanatermesis commented 1 year ago
  1. menu,preview

  2. i * <Lua 480: ~/.local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/keymap.lua:127> cmp.utils.keymap.set_map

  3. http://sprunge.us/bqzcZB

hrsh7th commented 1 year ago

@Thanatermesis Could you try with :set completeopt=menu,menuone,noselect?

hrsh7th commented 1 year ago

I can't determine the root cause. Sorry.

Shougo commented 1 year ago

@Thanatermesis You should create the minimal init.lua for it.

uga-rosa commented 1 year ago

Is nvim-cmp updated to the latest version? Also, please provide a stand alone init.lua that describes the minimal configuration necessary to reproduce this issue as @Shougo suggests. This is a good base to build on.

Thanatermesis commented 1 year ago

@Thanatermesis Could you try with :set completeopt=menu,menuone,noselect?

Same issue, pressed Tab and the menu appears but my text is not replaced with the first result 🤔

Is nvim-cmp updated to the latest version?

Yes, I'm using the NvChad framework which already included nvim-cmp integrated, so maybe this can be an easy way to reproduce it (in a test environment, install NvChad in a single command, everything goes installed and nvim-cmp included, then change the trigger like "autocomplete = false" and add Tab to open the menu, just like in the video, and the bug should happen)

Shougo commented 1 year ago

Please don't use the plugin distribution for it. It is not easy to reproduce for us.

hrsh7th commented 1 year ago

Could you try the following?

require('cmp').setup {
  completion = {
    completeopt = 'menu,menuone,noselect'
  }
}

My nvim-cmp config does not reproduce this problem so I think you should raise issue for the nvchad side.

Thanatermesis commented 1 year ago

There's a small screencast of all the process (installing the nvim NvChad framework from zero, and without extra plugins) from scratch, the modifications I did to disable the autopopup, and showing the issue, including a "set" of the parameters you told me, in a strange way even if I set the values you said, it seems like when running nvim they are replaced by "menu,preview" 🤔

My nvim-cmp config does not reproduce this problem so I think you should raise issue for the nvchad side.

Maybe @siduck knows why the value of completeopt is replaced

Note: the bug is on the minute 5:00, already included on the link:

https://asciinema.org/a/IlDwksvQPFlq3qWY3emVwnMlF?t=4:58

siduck commented 1 year ago

There's a small screencast of all the process (installing the nvim NvChad framework from zero, and without extra plugins) from scratch, the modifications I did to disable the autopopup, and showing the issue, including a "set" of the parameters you told me, in a strange way even if I set the values you said, it seems like when running nvim they are replaced by "menu,preview" thinking

My nvim-cmp config does not reproduce this problem so I think you should raise issue for the nvchad side.

Maybe @siduck knows why the value of completeopt is replaced

Note: the bug is on the minute 5:00, already included on the link:

https://asciinema.org/a/IlDwksvQPFlq3qWY3emVwnMlF?t=4:58

image

just override nvim-cmp opts in your custom plugins

Thanatermesis commented 1 year ago

@siduck yes that's what I do but it doesn't seems to take effect, I changed it to "menu,menuone,noselect" but it doesn't gets static (as you can see in the video), on the other hand I set it manually with :set to that value but the bug stills present 🤔

siduck commented 1 year ago

works here tho

https://user-images.githubusercontent.com/59060246/235274201-93a11d2e-c682-4670-aadd-b3c34321c54c.mp4

@Thanatermesis

{
    "hrsh7th/nvim-cmp",
    opts = {
      completion = {
        completeopt = "menu,menuone,noselect",
      },
    },
},
Thanatermesis commented 1 year ago

@siduck Mmmh, I don't think so, the issue is that when the menu is set to not automatically popup all the time but with manually triggering it, so by pressing tab, the text is not autocompleted to the first result, only to the next ones

This means, adding "autocomplete = false," on the completion section, and in the TAB mapping, changing the fallback line to "cmp.complete()"

More details on the first post and a demo on the video: https://youtu.be/awc4BjLwf9M

Thanatermesis commented 1 year ago

Ok so I think this code made it finally working, so first I need to have the entry to completeopt = "menu,menuone,noselect", otherwise without "noselect", the first selection will be made (without filling up my code with it, so what is this issue about),

Then, I need to replace the cmp.complete() entry (which is triggered with Tab) using this combination instead, which If im not wrong what it does is "create the menu" and "switch to the first item"

        cmp.complete({ behavior = cmp.SelectBehavior.Select })
        cmp.select_next_item({ behavior = cmp.SelectBehavior.Insert })

This looks very good! the menu only appears when I press Tab, at the same time my text has the added keyword, if I press "space" the keyword is kept (used) on my line, if i want to open a snippet I press "enter"... same behaviour with the menu automatically showing up (which is annoying for me at the moment because it doesn't lets me to use Copilot as I mentioned here)

srcrip commented 1 month ago

For anyone coming across this from google, you can improve it a little bit more by conditionally running complete based on whether the menu is already open. heres the full example I'm using right now:

          ["<C-k>"] = cmp.mapping(function(fallback)
            if not cmp.visible() then
              cmp.complete({ behavior = cmp.SelectBehavior.Select })
            end
            cmp.select_prev_item({ behavior = cmp.SelectBehavior.Insert })
          end, { 'i', 's' }),
          ["<C-j>"] = cmp.mapping(function(fallback)
            if not cmp.visible() then
              cmp.complete({ behavior = cmp.SelectBehavior.Select })
            end
            cmp.select_next_item({ behavior = cmp.SelectBehavior.Insert })
          end, { 'i', 's' }),