Giter Site home page Giter Site logo

Comments (29)

hrsh7th avatar hrsh7th commented on June 24, 2024 1

@ccshao nvim-cmp respects sources order. and the cmp-buffer source avoid duplicated items by default.

So you can use the following config for the problem.

cmp.setup {
  mapping = {
    ['<C-p>'] = cmp.mapping.prev_item(),
    ['<C-n>'] = cmp.mapping.next_item(),
    ['<C-d>'] = cmp.mapping.scroll(-4),
    ['<C-f>'] = cmp.mapping.scroll(4),
    ['<C-Space>'] = cmp.mapping.complete(),
    ['<C-e>'] = cmp.mapping.close(),
    ['<CR>'] = cmp.mapping.confirm({
      behavior = cmp.ConfirmBehavior.Replace,
      select = true,
    })
  },

  sources = {
      { name = 'nvim_lsp' },
      { name = 'path' },
      { name = 'buffer' },
  },

  documentation = false,
}

from nvim-cmp.

hrsh7th avatar hrsh7th commented on June 24, 2024 1

Note: nvim-lsp source set dup=1 by default.

from nvim-cmp.

hrsh7th avatar hrsh7th commented on June 24, 2024 1

Yes. It is understandable to me.

Your expected behavior can be realized with the following config maybe.

sources = {
   { name = 'buffer',
     opts = {
       get_bufnrs = function()
         local bufs = {}
         for _, win in ipairs(vim.api.nvim_list_wins()) do
           bufs[vim.api.nvim_win_get_buf(win)] = true
         end
         return vim.tbl_keys(bufs)
       end
     }
   },
   { name = 'nvim_lsp' },
   { name = 'path' },
 },

 formatting = {
   format = function(entry, vim_item)
     vim_item.menu = ({
       nvim_lsp = '[L]',
       emoji    = '[E]',
       path     = '[F]',
       calc     = '[C]',
       vsnip    = '[S]',
       buffer   = '[B]',
     })[entry.source.name]
     vim_item.dup = ({
       buffer = 1,
       path = 1,
       nvim_lsp = 0,
     })[entry.source.name] or 0
     return vim_item
   end
 },

from nvim-cmp.

hrsh7th avatar hrsh7th commented on June 24, 2024 1

I've fixed the bug! f3a5491

Now, You can fully customizable vim' completion item (See :help complete-items)

from nvim-cmp.

rhcher avatar rhcher commented on June 24, 2024

@ccshao nvim-cmp respects sources order. and the cmp-buffer source avoid duplicated items by default.

So you can use the following config for the problem.

cmp.setup {
  mapping = {
    ['<C-p>'] = cmp.mapping.prev_item(),
    ['<C-n>'] = cmp.mapping.next_item(),
    ['<C-d>'] = cmp.mapping.scroll(-4),
    ['<C-f>'] = cmp.mapping.scroll(4),
    ['<C-Space>'] = cmp.mapping.complete(),
    ['<C-e>'] = cmp.mapping.close(),
    ['<CR>'] = cmp.mapping.confirm({
      behavior = cmp.ConfirmBehavior.Replace,
      select = true,
    })
  },

  sources = {
      { name = 'nvim_lsp' },
      { name = 'path' },
      { name = 'buffer' },
  },

  documentation = false,
}

This looks like there is no difference

from nvim-cmp.

hrsh7th avatar hrsh7th commented on June 24, 2024

No. The sources order is changed.

from nvim-cmp.

ccshao avatar ccshao commented on June 24, 2024

Inddeed swapping the order works.

I just found something interesting, that buffer could find other candidates in other buffers.

In the example bellow, I have a few words in 1.R, and split 2.R in the same window. When I type libr, there are no candidates from 1.R, namely, libre, librart, but only from lsp. The order of buffer does not matter.

What do I need to add to the config to let nvim-cmp see other buffers?
Screenshot 2021-08-17 at 18 30 59

from nvim-cmp.

hrsh7th avatar hrsh7th commented on June 24, 2024

It is a source-related customization. See https://github.com/hrsh7th/cmp-buffer

cmp.setup {
  mapping = {
    ['<C-p>'] = cmp.mapping.prev_item(),
    ['<C-n>'] = cmp.mapping.next_item(),
    ['<C-d>'] = cmp.mapping.scroll(-4),
    ['<C-f>'] = cmp.mapping.scroll(4),
    ['<C-Space>'] = cmp.mapping.complete(),
    ['<C-e>'] = cmp.mapping.close(),
    ['<CR>'] = cmp.mapping.confirm({
      behavior = cmp.ConfirmBehavior.Replace,
      select = true,
    })
  },

  sources = {
      { name = 'nvim_lsp' },
      { name = 'path' },
      {
        name = 'buffer',
        opts = {
          get_bufnrs = function()
            local bufs = {}
            for _, win in ipairs(vim.api.nvim_list_wins()) do
              bufs[vim.api.nvim_win_get_buf(win)] = true
            end
            return vim.tbl_keys(bufs)
          end
        }
      },
  },
}

from nvim-cmp.

rhcher avatar rhcher commented on June 24, 2024

No. The sources order is changed.

Big shock, it's time to adjust my configuration again

from nvim-cmp.

hrsh7th avatar hrsh7th commented on June 24, 2024

Sorry for confusion. I thoughts it is intuitive way.... TT

from nvim-cmp.

ccshao avatar ccshao commented on June 24, 2024

Thanks!

May I bother you with another small issue? The reason I put buffer ahead of lsp is buffer candidates are usually more relevant.

Now the lsp are top candidates, how could I give more weights to buffer candiates? I think it is related to priority_weight but I don't know how to tweak it.

from nvim-cmp.

hrsh7th avatar hrsh7th commented on June 24, 2024

@ccshao It is not supported as suitable way but nvim-cmp provides fully customizable vim's completion item by the user.

cmp.setup {
  formatting = {
    format = function(entry, vim_item)
      if entry.source.name == 'nvim_lsp' then
        vim_item.dup = 0
      end
      return vim_item
    end
  }
}

from nvim-cmp.

ccshao avatar ccshao commented on June 24, 2024

Thanks for the suggestions, sorry I don't know much about vimscript/lua. The above formating codes seems not put the lsp candidates behind all buffer candidates.

In the example, I would like to get TEXT candidates in front of Function/Module/Feld from lsp.

Screenshot 2021-08-18 at 08 58 56

lua << EOF
local cmp = require'cmp'
cmp.setup {
  mapping = {
    ['<C-p>'] = cmp.mapping.prev_item(),
    ['<C-n>'] = cmp.mapping.next_item(),
    ['<C-d>'] = cmp.mapping.scroll(-4),
    ['<C-f>'] = cmp.mapping.scroll(4),
    ['<C-Space>'] = cmp.mapping.complete(),
    ['<C-e>'] = cmp.mapping.close(),
    ['<CR>'] = cmp.mapping.confirm({
      behavior = cmp.ConfirmBehavior.Replace,
      select = true,
    })
  },

  sources = {
        { name = 'nvim_lsp' },
        { name = 'path' },
        { name = 'buffer',
          opts = {
            get_bufnrs = function()
              local bufs = {}
              for _, win in ipairs(vim.api.nvim_list_wins()) do
                bufs[vim.api.nvim_win_get_buf(win)] = true
              end
              return vim.tbl_keys(bufs)
            end
          }
        },
  },

  formatting = {
    format = function(entry, vim_item)
      if entry.source.name == 'nvim_lsp' then
        vim_item.dup = 0
      end
      return vim_item
    end
  },

  documentation = false,
}

require('cmp_nvim_lsp').setup {}
EOF

from nvim-cmp.

hrsh7th avatar hrsh7th commented on June 24, 2024
  1. Prefer buffer source than nvim-lsp source
    You should change the sources option order. The first item will be show to the top.

  2. nvim-lsp source should hide if the buffer source's same word item exists.
    You can specify the above formatting.format option

from nvim-cmp.

ccshao avatar ccshao commented on June 24, 2024

I might get the logic, so the buffer is ahead of lsp in the source setting, and add the formation options.

  sources = {
      { name = 'buffer',
        opts = {
          get_bufnrs = function()
            local bufs = {}
            for _, win in ipairs(vim.api.nvim_list_wins()) do
              bufs[vim.api.nvim_win_get_buf(win)] = true
            end
            return vim.tbl_keys(bufs)
          end
        }
      },
      { name = 'path' },
      { name = 'nvim_lsp' },
  },

  formatting = {
    format = function(entry, vim_item)
      if entry.source.name == 'nvim_lsp' then
        vim_item.dup = 0
      end
      return vim_item
    end
  },

However, now I get duplicated candidates from buffer...

Screenshot_2021-08-18_09-58-11

from nvim-cmp.

rhcher avatar rhcher commented on June 24, 2024

However, now I get duplicated candidates from buffer...

If you don't want to have dup items you can delete formatting section. The cmp will hide dup items by default.

from nvim-cmp.

ccshao avatar ccshao commented on June 24, 2024

Strangely I still see the duplicated candidates, with a miminum setting:

lua << EOF
local cmp = require'cmp'
cmp.setup {
  mapping = {
    ['<C-p>'] = cmp.mapping.prev_item(),
    ['<C-n>'] = cmp.mapping.next_item(),
    ['<C-d>'] = cmp.mapping.scroll(-4),
    ['<C-f>'] = cmp.mapping.scroll(4),
    ['<C-Space>'] = cmp.mapping.complete(),
    ['<C-e>'] = cmp.mapping.close(),
    ['<CR>'] = cmp.mapping.confirm({
      behavior = cmp.ConfirmBehavior.Replace,
      select = true,
    })
  },

  sources = {
      { name = 'buffer' },
      { name = 'path' },
      { name = 'nvim_lsp' },

  },

  documentation = false,
}

require('cmp_nvim_lsp').setup {}
EOF

To get ride of duplicates, I have to put { name = 'nvim_lsp' },ahead of { name = 'buffer' },.

from nvim-cmp.

Shougo avatar Shougo commented on June 24, 2024

I think nvim_lsp candidates set dup, so you need to set dup=0 if don't like the dup.

from nvim-cmp.

hrsh7th avatar hrsh7th commented on June 24, 2024

Hm... Could you share your environment to testing?

Language Server
File content
etc

from nvim-cmp.

hrsh7th avatar hrsh7th commented on June 24, 2024

It's may be a bug. I will investigate it.

from nvim-cmp.

ccshao avatar ccshao commented on June 24, 2024

I am bit of lost in the settings, so here is the summary, with nvim 0.5.0, all latest plugins, and R language server.

setting 1. Works great, no redundant candidates from buffer and lsp.

sources = {
      { name = 'nvim_lsp' },
      { name = 'path' },
      { name = 'buffer',
        opts = {
          get_bufnrs = function()
            local bufs = {}
            for _, win in ipairs(vim.api.nvim_list_wins()) do
              bufs[vim.api.nvim_win_get_buf(win)] = true
            end
            return vim.tbl_keys(bufs)
          end
        }
      },
  },

setting 2. Duplicated items from buffer (indicated by "Text" in the popup)

sources = {
      { name = 'buffer' },
      { name = 'path' },
      { name = 'nvim_lsp' },

  },

" or with buffer config
  sources = {
          { name = 'buffer',
            opts = {
              get_bufnrs = function()
                local bufs = {}
                for _, win in ipairs(vim.api.nvim_list_wins()) do
                  bufs[vim.api.nvim_win_get_buf(win)] = true
                end
                return vim.tbl_keys(bufs)
              end
            }
          },
          { name = 'nvim_lsp' },
          { name = 'path' },
  },

from nvim-cmp.

hrsh7th avatar hrsh7th commented on June 24, 2024

Thank you for your information.

I guess the R language server returns items that contain the same duplicated Text entries.

I will provide debuggable configuration for it. Please wait.

from nvim-cmp.

hrsh7th avatar hrsh7th commented on June 24, 2024
    formatting = {
      format = function(entry, vim_item)
        vim_item.kind = lspkind.presets.default[vim_item.kind]
        vim_item.menu = ({
          nvim_lsp = '[L]',
          emoji    = '[E]',
          path     = '[F]',
          calc     = '[C]',
          vsnip    = '[S]',
          buffer   = '[B]',
        })[entry.source.name]
        return vim_item
      end
    }

@ccshao Could you test with this config?

It can be used to detect which source returned the item.

from nvim-cmp.

hrsh7th avatar hrsh7th commented on June 24, 2024

Then can you try this?

  formatting = {
    format = function(entry, vim_item)
      vim_item.menu = ({
        nvim_lsp = '[L]',
        emoji    = '[E]',
        path     = '[F]',
        calc     = '[C]',
        vsnip    = '[S]',
        buffer   = '[B]',
      })[entry.source.name]
      return vim_item
    end

from nvim-cmp.

ccshao avatar ccshao commented on June 24, 2024

Seems the duplilcated from both lsp and buffer.
Screenshot 2021-08-21 at 18 42 42

sources = {
   { name = 'buffer',
     opts = {
       get_bufnrs = function()
         local bufs = {}
         for _, win in ipairs(vim.api.nvim_list_wins()) do
           bufs[vim.api.nvim_win_get_buf(win)] = true
         end
         return vim.tbl_keys(bufs)
       end
     }
   },
   { name = 'nvim_lsp' },
   { name = 'path' },
 },

 formatting = {
   format = function(entry, vim_item)
     vim_item.menu = ({
       nvim_lsp = '[L]',
       emoji    = '[E]',
       path     = '[F]',
       calc     = '[C]',
       vsnip    = '[S]',
       buffer   = '[B]',
     })[entry.source.name]
     return vim_item
   end
 },

If I put the buffer at the end of source, then all comes from lsp.
Screenshot 2021-08-21 at 18 46 10

from nvim-cmp.

ccshao avatar ccshao commented on June 24, 2024

I still get the duplicated candidates from both lsp and buffer with exactly the above settings ...

from nvim-cmp.

hrsh7th avatar hrsh7th commented on June 24, 2024

Thsnk you and Sorry for annoying.

I think the above config dhould work so I will investigate it.

from nvim-cmp.

ccshao avatar ccshao commented on June 24, 2024

I just try with a python script with pylsp, again duplicated candidates. So might not be an issue from lsp.

from nvim-cmp.

ccshao avatar ccshao commented on June 24, 2024

Thank you very much for bing patience on this small issue!

from nvim-cmp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.