nvim: Add support for using snippets

This commit is contained in:
Sanchayan Maity 2021-05-13 20:33:21 +05:30
parent ca46007f89
commit e24fc200ec
4 changed files with 42 additions and 1 deletions

View file

@ -0,0 +1,19 @@
" Expand
imap <expr> <C-j> vsnip#expandable() ? '<Plug>(vsnip-expand)' : '<C-j>'
smap <expr> <C-j> vsnip#expandable() ? '<Plug>(vsnip-expand)' : '<C-j>'
" Expand or jump
imap <expr> <C-l> vsnip#available(1) ? '<Plug>(vsnip-expand-or-jump)' : '<C-l>'
smap <expr> <C-l> vsnip#available(1) ? '<Plug>(vsnip-expand-or-jump)' : '<C-l>'
" Select or cut text to use as $TM_SELECTED_TEXT in the next snippet.
" See https://github.com/hrsh7th/vim-vsnip/pull/50
nmap s <Plug>(vsnip-select-text)
xmap s <Plug>(vsnip-select-text)
nmap S <Plug>(vsnip-cut-text)
xmap S <Plug>(vsnip-cut-text)
" If you want to use snippet for multiple filetypes, you can `g:vsnip_filetypes` for it.
let g:vsnip_filetypes = {}
let g:vsnip_filetypes.javascriptreact = ['javascript']
let g:vsnip_filetypes.typescriptreact = ['typescript']

View file

@ -116,9 +116,24 @@ local on_attach = function(client, bufnr)
}
end
function set_snippet_capabilities()
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.completion.completionItem.snippetSupport = true
capabilities.textDocument.completion.completionItem.resolveSupport = {
properties = {
'documentation',
'detail',
'additionalTextEdits',
}
}
return capabilities
end
local servers = { 'hls', 'rust_analyzer', 'pyls', 'tsserver' }
for _, lsp in ipairs(servers) do
nvim_lsp[lsp].setup {
on_attach = on_attach,
capabilities = set_snippet_capabilities(),
}
end

View file

@ -22,7 +22,7 @@ require'compe'.setup {
tags = true;
treesitter = true;
snippets_nvim = false;
vsnip = false;
vsnip = true;
};
}
@ -45,6 +45,8 @@ end
_G.tab_complete = function()
if vim.fn.pumvisible() == 1 then
return t "<C-n>"
elseif vim.fn.call("vsnip#available", {1}) == 1 then
return t "<Plug>(vsnip-expand-or-jump)"
elseif check_back_space() then
return t "<Tab>"
else

View file

@ -163,6 +163,11 @@ local init = function ()
'kshenoy/vim-signature',
'gennaro-tedesco/nvim-peekup'
}
-- Snippets
use {
'rafamadriz/friendly-snippets',
'hrsh7th/vim-vsnip'
}
end
return require('packer').startup(init)