hrsh7th / nvim-cmp

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

Update "Advanced techniques" in the Wiki #1997

Open peppermilldetective opened 1 month ago

peppermilldetective commented 1 month ago

FAQ

Announcement

Minimal reproducible full config

if has('vim_starting')
  set encoding=utf-8
endif
scriptencoding utf-8

if &compatible
  set nocompatible
endif

let s:plug_dir = expand('/tmp/plugged/vim-plug')
if !filereadable(s:plug_dir .. '/plug.vim')
  execute printf('!curl -fLo %s/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim', s:plug_dir)
end

execute 'set runtimepath+=' . s:plug_dir
call plug#begin(s:plug_dir)
Plug 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'neovim/nvim-lspconfig'
Plug 'saadparwaiz1/cmp_luasnip'
Plug 'nvim-lua/plenary.nvim'
Plug 'nvim-telescope/telescope.nvim', { 'tag': '0.1.8' }
Plug 'rafamadriz/friendly-snippets'
Plug 'L3MON4D3/LuaSnip', {'tag': 'v2.*', 'do': 'make install_jsregexp'}
call plug#end()
PlugInstall | quit

" Setup global configuration. More on configuration below.
lua << EOF
require('telescope').setup{}
require('luasnip.loaders.from_vscode').lazy_load()

local cmp = require "cmp"
local luasnip = require "luasnip"
luasnip.config.setup({})

cmp.setup {
  snippet = {
    expand = function(args)
        luasnip.lsp_expand(args.body)
    end,
  },

  mapping = {
    ['<CR>'] = cmp.mapping.confirm({ select = true })
  },

  sources = cmp.config.sources({
    { name = "nvim_lsp" },
    { name = "luasnip" },
    { name = "buffer" },
  }),

  enabled = function()
    -- disable completion in comments
    local context = require 'cmp.config.context'
    -- keep command mode completion enabled when cursor is in a comment
    if vim.api.nvim_get_mode().mode == 'c' then
      return true
    else
      return not context.in_treesitter_capture("comment") 
        and not context.in_syntax_group("Comment")
    end
  end,
}
EOF

lua << EOF
local capabilities = require('cmp_nvim_lsp').default_capabilities()

require'lspconfig'.cssls.setup {
  capabilities = capabilities,
}
EOF

Description

The "Advanced technique" part of the wiki shows various capabilities and how to do them in nvim-cmp. The primary one I'm talking about here is "Disabling completion in certain contexts, such as comments".

Following the example given, nvim-cmp's ability to disable itself in prompt contexts will be removed outright due to the nature of the enabled opt being a replacement rather than an addition.

To address this, it would be best to change the example technique to be an additive example, showing nvim-cmp's default function with the addition of disabling in comments. This is shown by a reply to #676 here.

At minimum, the updated code (taken from the above reply), should be:

require('cmp').setup({
  enabled = function()
    local context = require('cmp.config.context')
    local disabled = false
    disabled = disabled or (vim.bo.bt == 'prompt')
    disabled = disabled or (vim.fn.reg_recording() ~= '')
    disabled = disabled or (vim.fn.reg_executing() ~= '')
    disabled = disabled or context.in_treesitter_capture('comment') -- Disable in comments
    return not disabled
  end,
})

I also feel like the docs should mention the replacement behavior, but it's more important to adjust the "Advanced techniques" part of the wiki, just in case.

If anyone knows of other parts of "Advanced techniques" that should also be updated, please feel free to post here.

Steps to reproduce

Using the above config:

  1. Open NeoVim.
  2. Execute :Telescope live_grep.
  3. Begin typing.
  4. Verify that auto-complete is active for the prompt.

Expected behavior

Autocomplete does not activate or show up in prompts such as command or Telescope.

Actual behavior

Autocomplete shows up and is active during prompts such as command or Telescope.

Additional context

No response

peppermilldetective commented 1 month ago

If there are any issues, please let me know.

I love the work you do and find nvim-cmp beyond useful! ❤️