Update "Advanced techniques" in the Wiki

Open Beginner friendly
#1,997 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
62/100
Issue type
Documentation
Clarity
Clearly specified
Activity status
Stale
Tech stack
lua
Domain
documentation

Research direction

Open the Wiki's Advanced techniques section, especially “Disabling completion in certain contexts, such as comments,” and compare it with the behavior described in this issue. Read the linked reply to #676 for the additive example. Done means the example preserves nvim-cmp's default prompt safeguards while disabling completion in comments, and explains that enabled replaces the default function.

Written by the indexing model from the issue text.

Description

bug
FAQ
  • I have checked the FAQ and it didn't resolve my problem.
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

Dominant language
Lua
Stars
9.5k
Forks
438
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from hrsh7th/nvim-cmp

All issues in hrsh7th/nvim-cmp

Similar issues

More Lua issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.