improving startup time by using lazy loading or filetype loading

This commit is contained in:
Jeremie Fraeys 2024-07-21 20:57:00 -04:00
parent bdb655c5a1
commit 7f1b34fd6f
62 changed files with 1285 additions and 561 deletions

View file

@ -11,35 +11,45 @@ return {
-- Adds a number of user-friendly snippets
'rafamadriz/friendly-snippets',
-- Optional sources
'hrsh7th/cmp-path',
'hrsh7th/cmp-buffer',
},
event = { 'InsertEnter', 'CmdlineEnter' },
config = function()
-- See `:help cmp`
-- Set completion options
vim.opt.completeopt = { 'menu', 'menuone', 'noselect' }
-- Lazy load snippets from friendly-snippets
require('luasnip.loaders.from_vscode').lazy_load()
local cmp = require 'cmp'
local luasnip = require 'luasnip'
luasnip.config.setup {}
-- Import required modules
local cmp = require('cmp')
local luasnip = require('luasnip')
local select_opts = { behavior = cmp.SelectBehavior.Select }
-- Setup luasnip
luasnip.config.setup({})
cmp.setup {
-- Setup nvim-cmp
cmp.setup({
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
sources = {
sources = cmp.config.sources({
{ name = 'nvim_lsp' },
{ name = 'luasnip' },
{ name = 'buffer' },
{ name = 'path' },
{ name = 'nvim_lsp', keyword_length = 1 },
{ name = 'buffer', keyword_length = 3 },
{ name = 'luasnip', keyword_length = 2 },
},
}),
window = {
documentation = cmp.config.window.bordered()
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
formatting = {
fields = { 'menu', 'abbr', 'kind' },
fields = { 'abbr', 'kind', 'menu' },
format = function(entry, item)
local menu_icon = {
nvim_lsp = 'λ',
@ -47,72 +57,52 @@ return {
buffer = 'Ω',
path = '🖫',
}
item.menu = menu_icon[entry.source.name]
item.menu = menu_icon[entry.source.name] or entry.source.name
return item
end,
},
mapping = cmp.mapping.preset.insert {
mapping = cmp.mapping.preset.insert({
['<C-n>'] = cmp.mapping.select_next_item(),
['<C-p>'] = cmp.mapping.select_prev_item(),
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete {},
['<CR>'] = cmp.mapping.confirm {
behavior = cmp.ConfirmBehavior.Replace,
select = true,
},
['<Tab>'] = cmp.mapping(function(fallback)
['<C-Space>'] = cmp.mapping.complete(),
['<CR>'] = cmp.mapping.confirm({ select = true }),
['<C-l>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_locally_jumpable() then
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end, { 'i', 's' }),
['<S-Tab>'] = cmp.mapping(function(fallback)
['<C-h>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.locally_jumpable(-1) then
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { 'i', 's' }),
},
}
local sign = function(opts)
vim.fn.sign_define(opts.name, {
texthl = opts.name,
text = opts.text,
numhl = ''
})
end
sign({ name = 'DiagnosticSignError', text = '' })
sign({ name = 'DiagnosticSignWarn', text = '' })
sign({ name = 'DiagnosticSignHint', text = '' })
sign({ name = 'DiagnosticSignInfo', text = '»' })
vim.diagnostic.config({
virtual_text = false,
severity_sort = true,
float = {
border = 'rounded',
source = 'always',
},
['<C-e>'] = cmp.mapping.abort(),
}),
})
vim.lsp.handlers['textDocument/hover'] = vim.lsp.with(
vim.lsp.handlers.hover,
{ border = 'rounded' }
)
-- Additional luasnip configuration
luasnip.config.set_config({
history = true,
updateevents = 'TextChanged,TextChangedI',
})
vim.lsp.handlers['textDocument/signatureHelp'] = vim.lsp.with(
vim.lsp.handlers.signature_help,
{ border = 'rounded' }
)
-- Setup for SQL filetype with vim-dadbod-completion
cmp.setup.filetype('sql', {
sources = cmp.config.sources({
{ name = 'vim-dadbod-completion' },
{ name = 'buffer' },
}),
})
end,
}