Skip to content

Instantly share code, notes, and snippets.

@AlixShahid
Created November 7, 2021 15:55
Show Gist options
  • Select an option

  • Save AlixShahid/44eba18e455a35de2c34e49cd1389915 to your computer and use it in GitHub Desktop.

Select an option

Save AlixShahid/44eba18e455a35de2c34e49cd1389915 to your computer and use it in GitHub Desktop.
Medium-Setting Up Neovim for Web Development - 8
-- Set completeopt to have a better completion experience
vim.o.completeopt = 'menuone,noselect'
local cmp = require'cmp'
cmp.setup({
completion = {
--completeopt = 'menu,menuone,noinsert',
},
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body)
end,
},
formatting = {
format = function(entry, vim_item)
-- fancy icons and a name of kind
vim_item.kind = require("lspkind").presets.default[vim_item.kind]
-- set a name for each source
vim_item.menu = ({
buffer = "[Buff]",
nvim_lsp = "[LSP]",
luasnip = "[LuaSnip]",
nvim_lua = "[Lua]",
latex_symbols = "[Latex]",
})[entry.source.name]
return vim_item
end,
},
sources = {
{ name = 'nvim_lsp' },
{ name = 'nvim_lua' },
{ name = 'path' },
{ name = 'luasnip' },
{ name = 'buffer', keyword_length=1 },
{ name = 'calc' },
},
experimental = {
-- ghost_text = true,
},
})
-- Require function for tab to work with LUA-SNIP
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
local luasnip = require("luasnip")
cmp.setup({
mapping = {
['<C-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.close(),
['<C-u>'] = cmp.mapping.scroll_docs(-4),
['<C-d>'] = cmp.mapping.scroll_docs(4),
['<CR>'] = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Replace,
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" }
),
}
})
@AlixShahid
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment