Skip to content

Commit

Permalink
Add expand and expandable function+vim-versions (close #79).
Browse files Browse the repository at this point in the history
  • Loading branch information
L3MON4D3 committed Jul 20, 2021
1 parent d7638d0 commit d4ddebd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
21 changes: 17 additions & 4 deletions lua/luasnip/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,16 @@ local function jumpable(dir)
return (node ~= nil and node:jumpable(dir))
end

local function expand_or_jumpable()
local function expandable()
next_expand = match_snippet(util.get_current_line_to_cursor())
return (next_expand ~= nil) or jumpable(1)
return next_expand ~= nil
end

-- return true and expand snippet if expandable, return false if not.
local function expand_or_jump()
local function expand_or_jumpable()
return expandable() or jumpable(1)
end

local function expand()
if next_expand ~= nil then
next_expand:trigger_expand(
Luasnip_current_nodes[vim.api.nvim_get_current_buf()]
Expand All @@ -95,6 +98,14 @@ local function expand_or_jump()
return true
end
end
return false
end

-- return true and expand snippet if expandable, return false if not.
local function expand_or_jump()
if expand() then
return true
end
if jump(1) then
return true
end
Expand Down Expand Up @@ -166,6 +177,8 @@ end
ls = {
expand_or_jumpable = expand_or_jumpable,
jumpable = jumpable,
expandable = expandable,
expand = expand,
expand_or_jump = expand_or_jump,
jump = jump,
get_active_snip = get_active_snip,
Expand Down
8 changes: 7 additions & 1 deletion plugin/luasnip.vim
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
inoremap <silent> <Plug>luasnip-expand-or-jump <cmd>lua require'luasnip'.expand_or_jump()<Cr>
inoremap <silent> <Plug>luasnip-expand-snippet <cmd>lua require'luasnip'.expand()<Cr>
inoremap <silent> <Plug>luasnip-next-choice <cmd>lua require'luasnip'.change_choice(1)<Cr>
inoremap <silent> <Plug>luasnip-jump-next <cmd>lua require'luasnip'.jump(1)<Cr>
inoremap <silent> <Plug>luasnip-jump-prev <cmd>lua require'luasnip'.jump(-1)<Cr>
snoremap <silent> <Plug>luasnip-expand-or-jump <cmd>lua require'luasnip'.expand_or_jump()<Cr>
inoremap <silent> <Plug>luasnip-expand-snippet <cmd>lua require'luasnip'.expand()<Cr>
snoremap <silent> <Plug>luasnip-next-choice <cmd>lua require'luasnip'.change_choice(1)<Cr>
snoremap <silent> <Plug>luasnip-jump-next <cmd>lua require'luasnip'.jump(1)<Cr>
snoremap <silent> <Plug>luasnip-jump-prev <cmd>lua require'luasnip'.jump(-1)<Cr>
command! LuaSnipUnlinkCurrent lua require'luasnip'.unlink_current()
command! LuaSnipListAvailable lua print(vim.inspect(require'luasnip'.available()))

function! luasnip#expandable()
return luaeval('require("luasnip").expandable()')
endfunction

function! luasnip#expand_or_jumpable()
return luaeval('require("luasnip").expand_or_jumpable()')
endfunction
Expand All @@ -23,4 +29,4 @@ function! luasnip#choice_active()
return luaeval('require("luasnip").choice_active()')
endfunction

lua require('luasnip.config')._setup()
lua require('luasnip.config')._setup()

0 comments on commit d4ddebd

Please sign in to comment.