Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hm... I don't have any ideas how to use CoC, so here's my guide on how to setup it with `nvim-lspconfig`:
- If I'm seeing it right, you've already setup the lsp. All you wan now is
- basically just auto-completion, right?
- You need an auto-completion plugin for this! `nvim-lspconfig` just simplifies
- the lsp-configuration for you. You can see a list of auto-completion plugins [here](https://github.com/rockerBOO/awesome-neovim#completion).
- To simplify things I provide a quick guide how I set it up:
- 1. Prepare some directories first (I like to separate the settings for each
- plugin):
- ```bash
- mkdir -p ~/.config/nvim/lua/nvim_cmp_settings/
- mkdir -p ~/.config/nvim/lua/luasnip_settings/
- ```
- 2. Add the following to your [plugin list](https://github.com/hungLink/dotfiles-debian/blob/master/.config/nvim/lua/plugins.lua#L15-L20):
- ```lua
- -- the auto-completion plugin
- use({
- "hrsh7th/nvim-cmp",
- config = function()
- require("nvim_cmp_settings.settings")
- end,
- requires = {
- "hrsh7th/cmp-buffer",
- "hrsh7th/cmp-nvim-lsp",
- "hrsh7th/cmp-path",
- "hrsh7th/cmp-nvim-lua",
- "saadparwaiz1/cmp_luasnip",
- "onsails/lspkind-nvim",
- "hrsh7th/cmp-nvim-lsp-signature-help",
- "lukas-reineke/cmp-rg",
- },
- })
- -- the snippet plugin. You can choose another snippet plugin if you want.
- -- Here's a list of them: https://github.com/rockerBOO/awesome-neovim#snippet
- use({
- "L3MON4D3/LuaSnip",
- config = function()
- require("luasnip_settings.settings")
- end,
- })
- ```
- 3. Execute inside neovim `:PackerSync` wait until it finished and exit it
- afterwards.
- 4. Put the following into `~/.config/nvim/lua/nvim_cmp_settings/settings.lua`:
- ```lua
- local cmp = require("cmp")
- local lspkind = require("lspkind")
- local luasnip = require("luasnip")
- local has_words_before = function()
- local line, col = unpack(vim.api.nvim_win_get_cursor(0))
- return col ~= 0 and
- vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col,
- col)
- :match("%s") == nil
- end
- cmp.setup({
- preselect = cmp.PreselectMode.None,
- snippet = {
- expand = function(args) require('luasnip').lsp_expand(args.body) end
- },
- window = {
- completion = cmp.config.window.bordered(),
- documentation = cmp.config.window.bordered(),
- },
- mapping = {
- ['<C-Space>'] = cmp.mapping.complete(),
- ['<C-c>'] = cmp.mapping.close(),
- ['<CR>'] = cmp.mapping.confirm({select = false}),
- ["<Tab>"] = cmp.mapping(function(fallback)
- if cmp.visible() then
- cmp.select_next_item()
- elseif luasnip.expand_or_jumpable() then
- luasnip.expand_or_jump()
- elseif has_words_before() then
- cmp.complete()
- else
- fallback()
- end
- end, {"i", "s"}),
- ["<S-Tab>"] = cmp.mapping(function(fallback)
- if cmp.visible() then
- cmp.select_prev_item()
- elseif luasnip.jumpable(-1) then
- luasnip.jump(-1)
- else
- fallback()
- end
- end, { "i", "s" }),
- },
- formatting = {
- format = lspkind.cmp_format({
- with_text = false,
- maxwidth = 50,
- -- The function below will be called before any actual modifications from lspkind
- -- so that you can provide more controls on popup customization. (See [#30](https://github.com/onsails/lspkind-nvim/pull/30))
- before = function(entry, vim_item) return vim_item end
- })
- },
- sources = {
- -- {name = "rg", prority = 8},
- {name = 'nvim_lsp', priority = 10},
- {name = 'path', priority = 2},
- {name = 'buffer', priority = 4},
- {name = 'luasnip', priority = 3},
- -- {name = 'nvim_lsp_signature_help', priority = 11}
- },
- })
- ```
- 5. Put the following into `~/.config/nvim/lua/luasnip_settings/settings.lua`
- ```lua
- local function prequire(...)
- local status, lib = pcall(require, ...)
- if (status) then return lib end
- return nil
- end
- local luasnip = prequire('luasnip')
- local cmp = prequire("cmp")
- 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
- if col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') then
- return true
- else
- return false
- end
- end
- _G.tab_complete = function()
- if cmp and cmp.visible() then
- cmp.select_next_item()
- elseif luasnip and luasnip.expand_or_jumpable() then
- return t("<Plug>luasnip-expand-or-jump")
- elseif check_back_space() then
- return t "<Tab>"
- else
- cmp.complete()
- end
- return ""
- end
- _G.s_tab_complete = function()
- if cmp and cmp.visible() then
- cmp.select_prev_item()
- elseif luasnip and luasnip.jumpable(-1) then
- return t("<Plug>luasnip-jump-prev")
- else
- return t "<S-Tab>"
- end
- return ""
- end
- vim.api.nvim_set_keymap("i", "<Tab>", "v:lua.tab_complete()", {expr = true})
- vim.api.nvim_set_keymap("s", "<Tab>", "v:lua.tab_complete()", {expr = true})
- vim.api.nvim_set_keymap("i", "<S-Tab>", "v:lua.s_tab_complete()", {expr = true})
- vim.api.nvim_set_keymap("s", "<S-Tab>", "v:lua.s_tab_complete()", {expr = true})
- vim.api.nvim_set_keymap("i", "<C-E>", "<Plug>luasnip-next-choice", {})
- vim.api.nvim_set_keymap("s", "<C-E>", "<Plug>luasnip-next-choice", {})
- require("luasnip.loaders.from_lua").lazy_load({
- paths = "./lua/luasnip_settings/snippets"
- })
- ```
- Now you should have autocompletion.
- If it's too much for you, there's also a list of
- [neovim-"distributions"](https://github.com/rockerBOO/awesome-neovim#preconfigured-configuration)
- which basically preconfigured a lot for you. (Some of my friends are praising
- [LunarVim](https://github.com/LunarVim/LunarVim))
- They are mostly giving you an out-of-the-box experience as in VSCode and other
- editors/IDEs.
- Maybe [my configs](https://github.com/TornaxO7/my_configs/tree/master/nvim) can
- inspire you at some points?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement