Skip to content

Commit

Permalink
replace vsnip with LuaSnip
Browse files Browse the repository at this point in the history
* vsnip is very buggy. hrsh7th/vim-vsnip#254
* luasnip loads too many files at startup. Lazy-load it at InsertEnter
  and <C-l>.
* My patch to break undo sequence before expanding. Upstream it?
* Make vsnip snippets available for luasnip.
* Port some advanced vsnip snippets to luasnip.
  • Loading branch information
tomtomjhj committed Nov 19, 2022
1 parent 91e4bd4 commit d8cb9b7
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 83 deletions.
30 changes: 21 additions & 9 deletions configs.vim
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ elseif g:ide_client == 'nvim'
Plug 'hrsh7th/cmp-nvim-lsp'
" hrsh7th/cmp-nvim-lsp-signature-help
Plug 'hrsh7th/cmp-path'
Plug 'tomtomjhj/vim-vsnip' " my fork with in-word expansion and better undo
Plug 'hrsh7th/cmp-vsnip'
Plug 'L3MON4D3/LuaSnip', { 'on': [] }
Plug 'saadparwaiz1/cmp_luasnip', { 'on': [] }
Plug 'neovim/nvim-lspconfig'
Plug 'williamboman/mason.nvim'
Plug 'williamboman/mason-lspconfig.nvim'
Expand Down Expand Up @@ -483,14 +483,26 @@ if g:ide_client == 'coc'
elseif g:ide_client == 'nvim'
lua require'tomtomjhj/cmp'

let g:vsnip_snippet_dir = expand('~/.vim/vsnip')
let g:vsnip_filetypes = { 'pandoc': ['markdown', 'tex'] }
" lazy-load luasnip
let s:loaded_luasnip = get(s:, 'loaded_luasnip', 0)
function! s:LoadLuaSnip() abort
if s:loaded_luasnip | return | endif
call plug#load('LuaSnip', 'cmp_luasnip')

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>'
imap <expr> <C-h> vsnip#jumpable(-1) ? '<Plug>(vsnip-jump-prev)' : '<C-h>'
smap <expr> <C-h> vsnip#jumpable(-1) ? '<Plug>(vsnip-jump-prev)' : '<C-h>'
xmap <C-l> <Plug>(vsnip-cut-text)
lua require("luasnip.loaders.from_vscode").lazy_load { paths = { "~/.vim/vsnip", "~/.vim/plugged/friendly-snippets" } }
lua require("luasnip.loaders.from_lua").lazy_load { paths = "~/.vim/lsnip/" }
lua require("luasnip").config.setup { store_selection_keys = "<C-L>" }

imap <silent><expr> <C-l> luasnip#expand_or_jumpable() ? '<Plug>luasnip-expand-or-jump' : '<C-l>'
inoremap <silent> <C-h> <Cmd>lua require('luasnip').jump(-1)<CR>
snoremap <silent> <C-l> <Cmd>lua require('luasnip').jump(1)<CR>
snoremap <silent> <C-h> <Cmd>lua require('luasnip').jump(-1)<CR>
endfunction

if has('vim_starting')
au InsertEnter * ++once call s:LoadLuaSnip()
xnoremap <silent> <C-l> :<C-u>call <SID>LoadLuaSnip()<CR>gv<Cmd>call feedkeys("<C-l>")<CR>
endif
endif
" }}}

Expand Down
6 changes: 6 additions & 0 deletions lsnip/all.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
return {
s("date", p(os.date, "%Y-%m-%d")),
s("diso", p(os.date, "%Y-%m-%d %H:%M:%S%z")),
s("time", p(os.date, "%H:%M")),
s("modeline", fmt([[vim: set {}:]], { i(0, "opt=value") })),
}
6 changes: 3 additions & 3 deletions lua/tomtomjhj/cmp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ cmp.setup {
get_bufnrs = get_visible_bufnrs,
}
},
{ name = 'vsnip' },
{ name = 'luasnip' },
{ name = 'tags' },
},
experimental = {
Expand Down Expand Up @@ -82,7 +82,7 @@ cmp.setup {
},
snippet = {
expand = function(args)
vim.fn["vsnip#anonymous"](args.body)
require('luasnip').lsp_expand(args.body)
end,
},
formatting = {
Expand All @@ -94,7 +94,7 @@ cmp.setup {
vim_item.menu = ({
buffer = "[B]",
nvim_lsp = "[LS]",
vsnip = "[SN]",
luasnip = "[SN]",
tags = "[T]",
})[entry.source.name]
return vim_item
Expand Down
12 changes: 12 additions & 0 deletions patches/LuaSnip.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/lua/luasnip/init.lua b/lua/luasnip/init.lua
index 99e9284..e62b88a 100644
--- a/lua/luasnip/init.lua
+++ b/lua/luasnip/init.lua
@@ -284,6 +284,7 @@ local function expand(opts)
local jump_into_func = opts and opts.jump_into_func

local cursor = util.get_cursor_0ind()
+ vim.cmd[[let &undolevels = &undolevels]]
-- override snip with expanded copy.
snip = snip_expand(snip, {
expand_params = expand_params,
36 changes: 0 additions & 36 deletions vsnip/global.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,5 @@
"prefix": "lorem",
"description": "Lorem Ipsum",
"body": "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
},
"modeline": {
"prefix": "modeline",
"description": "Vim modeline",
"body": "vim${VIM:':set '. (&expandtab ? printf('et sw=%i ts=%i', &sw, &ts) : printf('noet sts=%i sw=%i ts=%i', &sts, &sw, &ts)) . (&tw ? ' tw='. &tw : '') . ':'}"
},
"date": {
"prefix": "date",
"description": "YYYY-MM-DD",
"body": "${VIM:strftime(\"%Y-%m-%d\")}"
},
"ddate": {
"prefix": "ddate",
"description": "Month DD, YYYY",
"body": "${VIM:strftime(\"%b %d, %Y\")}"
},
"diso": {
"prefix": "diso",
"description": "ISO format datetime",
"body": "${VIM:strftime(\"%Y-%m-%d %H:%M:%S%z\")}"
},
"time": {
"prefix": "time",
"description": "hh:mm",
"body": "${VIM:strftime(\"%H:%M\")}"
},
"datetime": {
"prefix": "datetime",
"description": "YYYY-MM-DD hh:mm",
"body": "${VIM:strftime(\"%Y-%m-%d %H:%M\")}"
},
"uuid": {
"prefix": "uuid",
"body": [
"${VIM:system('python -c \"import uuid, sys; sys.stdout.write(str(uuid.uuid4()))\"')}"
]
}
}
39 changes: 39 additions & 0 deletions vsnip/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "vscode snippets for luasnip",
"contributes": {
"snippets": [
{
"language": [
"markdown",
"pandoc"
],
"path": "./markdown.json"
},
{
"language": [
"all"
],
"path": "./global.json"
},
{
"language": [
"coq"
],
"path": "./coq.json"
},
{
"language": [
"rust"
],
"path": "./rust.json"
},
{
"language": [
"tex",
"pandoc"
],
"path": "./tex.json"
}
]
}
}
Loading

0 comments on commit d8cb9b7

Please sign in to comment.