diff --git a/README.md b/README.md index 3793155..c04cdf2 100644 --- a/README.md +++ b/README.md @@ -33,17 +33,19 @@ Setup ## Install -First, You should install `nvim-cmp` itself and completion sources by your -favourite plugin manager. +First, You should install `nvim-cmp` itself and completion sources and snippet engine by your favourite plugin manager. -The `nvim-cmp` sources can be found in -[here](https://github.com/topics/nvim-cmp). +The `nvim-cmp` sources can be found in [here](https://github.com/topics/nvim-cmp). Using [vim-plug](https://github.com/junegunn/vim-plug): ```viml " Install nvim-cmp Plug 'hrsh7th/nvim-cmp' + +" Install snippet engine (This example installs [hrsh7th/vim-vsnip](https://github.com/hrsh7th/vim-vsnip)) +Plug 'hrsh7th/vim-vsnip' + " Install the buffer completion source Plug 'hrsh7th/cmp-buffer' ``` @@ -55,6 +57,7 @@ Using [packer.nvim](https://github.com/wbthomason/packer.nvim): use { "hrsh7th/nvim-cmp", requires = { + "hrsh7th/vim-vsnip", "hrsh7th/cmp-buffer", } } @@ -62,44 +65,28 @@ use { ## Basic Configuration +First, You must set `snippet engine` up. See README.md of your choosen snippet engine. + To use `nvim-cmp` with the default configuration: ```viml lua <'] = cmp.mapping.confirm({ select = true }), + } + }) EOF ``` The default configuration can be found in [here](./lua/cmp/config/default.lua) -To configure with suggested key mappings and the `hrsh7th/cmp-buffer` source: - -```viml -lua <'] = cmp.mapping.select_prev_item(), - [''] = cmp.mapping.select_next_item(), - [''] = cmp.mapping.scroll_docs(-4), - [''] = cmp.mapping.scroll_docs(4), - [''] = cmp.mapping.complete(), - [''] = cmp.mapping.close(), - [''] = cmp.mapping.confirm({ - behavior = cmp.ConfirmBehavior.Insert, - select = true, - }) - }, - - -- You should specify your *installed* sources. - sources = { - { name = 'buffer' }, - }, - } -EOF -``` - Advanced Configuration ==================== @@ -152,8 +139,6 @@ You can configure `nvim-cmp` to use these `cmd.mappings` like this: ```lua mapping = { - [''] = cmp.mapping.select_prev_item(), - [''] = cmp.mapping.select_next_item(), [''] = cmp.mapping.scroll_docs(-4), [''] = cmp.mapping.scroll_docs(4), [''] = cmp.mapping.complete(), @@ -179,8 +164,8 @@ You can specify your own custom mapping function. ```lua local check_back_space = function() - local col = vim.fn.col('.') - 1 - return col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') + local col = vim.fn.col('.') - 1 + return col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') end mapping = { @@ -212,8 +197,8 @@ lua filetype. " Setup buffer configuration (nvim-lua source only enables in Lua filetype). autocmd FileType lua lua require'cmp'.setup.buffer { \ sources = { -\ { name = 'buffer' }, \ { name = 'nvim_lua' }, +\ { name = 'buffer' }, \ }, \ } ``` @@ -425,9 +410,6 @@ In such case, the time near the 100ms will be consumed just to parse payloads as This is supertab-like mapping for [LuaSnip](https://github.com/L3MON4D3/LuaSnip) ```lua -local t = function(str) - return vim.api.nvim_replace_termcodes(str, true, true, true) -end local check_back_space = function() local col = vim.fn.col '.' - 1 return col == 0 or vim.fn.getline('.'):sub(col, col):match '%s' ~= nil @@ -436,13 +418,13 @@ local luasnip = require("luasnip") -- supertab-like mapping mapping = { - [""] = cmp.mapping(function(fallback) + [""] = cmp.mapping(function(fallback) if vim.fn.pumvisible() == 1 then - vim.fn.feedkeys(t(""), "n") + vim.fn.feedkeys(vim.api.nvim_replace_termcodes(""), "n") elseif luasnip.expand_or_jumpable() then - vim.fn.feedkeys(t("luasnip-expand-or-jump"), "") + vim.fn.feedkeys(vim.api.nvim_replace_termcodes("luasnip-expand-or-jump"), "") elseif check_back_space() then - vim.fn.feedkeys(t(""), "n") + vim.fn.feedkeys(vim.api.nvim_replace_termcodes(""), "n") else fallback() end @@ -450,11 +432,11 @@ mapping = { "i", "s", }), - [""] = cmp.mapping(function(fallback) + [""] = cmp.mapping(function(fallback) if vim.fn.pumvisible() == 1 then - vim.fn.feedkeys(t(""), "n") + vim.fn.feedkeys(vim.api.nvim_replace_termcodes(""), "n") elseif luasnip.jumpable(-1) then - vim.fn.feedkeys(t("luasnip-jump-prev"), "") + vim.fn.feedkeys(vim.api.nvim_replace_termcodes("luasnip-jump-prev"), "") else fallback() end @@ -471,9 +453,8 @@ mapping = { formatting = { format = function(entry, vim_item) -- fancy icons and a name of kind - vim_item.kind = require("lspkind").presets.default[vim_item.kind] - .. " " - .. vim_item.kind + vim_item.kind = require("lspkind").presets.default[vim_item.kind] .. " " .. vim_item.kind + -- set a name for each source vim_item.menu = ({ buffer = "[Buffer]", @@ -487,6 +468,7 @@ formatting = { }, ``` + Source creation ====================