- Implemented target placeholder.

This commit is contained in:
Shougo Matsushita 2012-10-30 07:38:37 +09:00
parent be5b8e37cd
commit efecca4fb8
4 changed files with 132 additions and 20 deletions

View File

@ -518,7 +518,9 @@ function! s:snippets_jump_or_expand(cur_text, col)"{{{
endfunction"}}} endfunction"}}}
function! neosnippet#expand(cur_text, col, trigger_name)"{{{ function! neosnippet#expand(cur_text, col, trigger_name)"{{{
if a:trigger_name == '' let snippets = neosnippet#get_snippets()
if a:trigger_name == '' || !has_key(snippets, a:trigger_name)
let pos = getpos('.') let pos = getpos('.')
let pos[2] = len(a:cur_text)+1 let pos[2] = len(a:cur_text)+1
call setpos('.', pos) call setpos('.', pos)
@ -532,7 +534,6 @@ function! neosnippet#expand(cur_text, col, trigger_name)"{{{
return return
endif endif
let snippets = neosnippet#get_snippets()
let snippet = snippets[a:trigger_name] let snippet = snippets[a:trigger_name]
let cur_text = a:cur_text[: -1-len(a:trigger_name)] let cur_text = a:cur_text[: -1-len(a:trigger_name)]
@ -577,7 +578,9 @@ function! neosnippet#expand(cur_text, col, trigger_name)"{{{
call append('.', snippet_lines[1:]) call append('.', snippet_lines[1:])
endif endif
call s:indent_snippet(begin_line, end_line) if begin_line != end_line
call s:indent_snippet(begin_line, end_line)
endif
let begin_patterns = (begin_line > 1) ? let begin_patterns = (begin_line > 1) ?
\ [getline(begin_line - 1)] : [] \ [getline(begin_line - 1)] : []
@ -591,7 +594,7 @@ function! neosnippet#expand(cur_text, col, trigger_name)"{{{
\ 'holder_cnt' : 1, \ 'holder_cnt' : 1,
\ }) \ })
if next_col < col('$') if next_line != ''
startinsert startinsert
else else
startinsert! startinsert!
@ -610,6 +613,24 @@ function! neosnippet#expand(cur_text, col, trigger_name)"{{{
let &l:iminsert = 0 let &l:iminsert = 0
let &l:imsearch = 0 let &l:imsearch = 0
endfunction"}}} endfunction"}}}
function! neosnippet#expand_target()"{{{
let trigger = input('Please input snippet trigger: ',
\ '', 'customlist,neosnippet#snippet_complete')
let neosnippet = neosnippet#get_current_neosnippet()
if !has_key(neosnippet#get_snippets(), trigger)
let neosnippet.target = ''
return
endif
let neosnippet.target = substitute(
\ neosnippet#get_selected_text(visualmode(), 1), '\n$', '', '')
call neosnippet#delete_selected_text(visualmode(), 1)
call setline('.', matchstr(neosnippet.target, '^\s*') . trigger)
startinsert!
call neosnippet#expand(getline('.'), col('$'), trigger)
endfunction"}}}
function! s:indent_snippet(begin, end)"{{{ function! s:indent_snippet(begin, end)"{{{
let equalprg = &l:equalprg let equalprg = &l:equalprg
let pos = getpos('.') let pos = getpos('.')
@ -729,14 +750,24 @@ function! s:expand_placeholder(start, end, holder_cnt, line)"{{{
\ '\\d\\+', a:holder_cnt, '') \ '\\d\\+', a:holder_cnt, '')
let current_line = getline(a:line) let current_line = getline(a:line)
let match = match(current_line, pattern) let match = match(current_line, pattern)
let neosnippet = neosnippet#get_current_neosnippet()
let default_pattern = substitute( let default_pattern = substitute(
\ s:get_placeholder_marker_default_pattern(), \ s:get_placeholder_marker_default_pattern(),
\ '\\d\\+', a:holder_cnt, '') \ '\\d\\+', a:holder_cnt, '')
let default = substitute( let default = substitute(
\ matchstr(current_line, default_pattern), '\\\ze[^\\]', '', 'g') \ matchstr(current_line, default_pattern),
\ '\\\ze[^\\]', '', 'g')
if default =~ '^TARGET\>' && neosnippet.target != ''
let default = ''
let is_target = 1
else
let is_target = 0
endif
let default = substitute(default, '^TARGET:\?\>', '', '')
let neosnippet = neosnippet#get_current_neosnippet()
let neosnippet.selected_text = default let neosnippet.selected_text = default
" Substitute marker. " Substitute marker.
@ -768,6 +799,11 @@ function! s:expand_placeholder(start, end, holder_cnt, line)"{{{
call setpos('.', pos) call setpos('.', pos)
if is_target
" Expand target
return s:expand_target_placeholder(a:line, match+1)
endif
if default_len > 0 if default_len > 0
" Select default value. " Select default value.
let len = default_len-1 let len = default_len-1
@ -783,6 +819,51 @@ function! s:expand_placeholder(start, end, holder_cnt, line)"{{{
startinsert! startinsert!
endif endif
endfunction"}}} endfunction"}}}
function! s:expand_target_placeholder(line, col)"{{{
" Expand target
let neosnippet = neosnippet#get_current_neosnippet()
let next_line = getline(a:line)[a:col-1 :]
let target_lines = split(neosnippet.target, '\n', 1)
let cur_text = getline(a:line)[: a:col-2]
let target_lines[0] = cur_text . target_lines[0]
let next_col = len(target_lines[-1]) + 1
let target_lines[-1] = target_lines[-1] . next_line
let begin_line = a:line
let end_line = a:line + len(target_lines) - 1
if has('folding')
let foldmethod = &l:foldmethod
let &l:foldmethod = 'manual'
endif
try
call setline(a:line, target_lines[0])
if len(target_lines) > 1
call append(a:line, target_lines[1:])
endif
call cursor(end_line, 0)
if begin_line != end_line
call s:indent_snippet(begin_line, end_line)
endif
if next_line != ''
startinsert
else
startinsert!
endif
finally
if has('folding')
let &l:foldmethod = foldmethod
silent! execute begin_line . ',' . end_line . 'foldopen!'
endif
endtry
let neosnippet.target = ''
endfunction"}}}
function! s:search_sync_placeholder(start, end, number)"{{{ function! s:search_sync_placeholder(start, end, number)"{{{
if a:end == 0 if a:end == 0
" Search in current buffer. " Search in current buffer.
@ -866,6 +947,7 @@ function! neosnippet#get_current_neosnippet()"{{{
if !exists('b:neosnippet') if !exists('b:neosnippet')
let b:neosnippet = { let b:neosnippet = {
\ 'selected_text' : '', \ 'selected_text' : '',
\ 'target' : '',
\} \}
endif endif
@ -924,6 +1006,10 @@ function! neosnippet#filetype_complete(arglead, cmdline, cursorpos)"{{{
return sort(keys(ret)) return sort(keys(ret))
endfunction"}}} endfunction"}}}
function! neosnippet#snippet_complete(arglead, cmdline, cursorpos)"{{{
return filter(keys(neosnippet#get_snippets()),
\ 'stridx(v:val, a:arglead) == 0')
endfunction"}}}
function! s:get_placeholder_marker_pattern()"{{{ function! s:get_placeholder_marker_pattern()"{{{
return '<`\d\+\%(:.\{-}\)\?\\\@<!`>' return '<`\d\+\%(:.\{-}\)\?\\\@<!`>'
@ -973,7 +1059,7 @@ function! s:trigger(function)"{{{
return expr return expr
endfunction"}}} endfunction"}}}
function! neosnippet#get_selected_text(type, ...) function! neosnippet#get_selected_text(type, ...)"{{{
let sel_save = &selection let sel_save = &selection
let &selection = 'inclusive' let &selection = 'inclusive'
let reg_save = @@ let reg_save = @@
@ -991,14 +1077,36 @@ function! neosnippet#get_selected_text(type, ...)
silent exe "normal! `[v`]y" silent exe "normal! `[v`]y"
endif endif
let neosnippet = neosnippet#get_current_neosnippet() return @@
let neosnippet.selected_text = @@
finally finally
let &selection = sel_save let &selection = sel_save
let @@ = reg_save let @@ = reg_save
call setpos('.', pos) call setpos('.', pos)
endtry endtry
endfunction endfunction"}}}
function! neosnippet#delete_selected_text(type, ...)"{{{
let sel_save = &selection
let &selection = 'inclusive'
let reg_save = @@
let pos = getpos('.')
try
" Invoked from Visual mode, use '< and '> marks.
if a:0
silent exe "normal! `<" . a:type . "`>d"
elseif a:type == 'line'
silent exe "normal! '[V']d"
elseif a:type == 'block'
silent exe "normal! `[\<C-V>`]d"
else
silent exe "normal! `[v`]d"
endif
finally
let &selection = sel_save
let @@ = reg_save
call setpos('.', pos)
endtry
endfunction"}}}
function! neosnippet#clear_select_mode_mappings()"{{{ function! neosnippet#clear_select_mode_mappings()"{{{
if !g:neosnippet#disable_select_mode_mappings if !g:neosnippet#disable_select_mode_mappings

View File

@ -2,19 +2,19 @@ snippet if
abbr if endif abbr if endif
options head options head
if ${1:#:condition} if ${1:#:condition}
${0} ${0:TARGET}
endif endif
snippet elseif snippet elseif
options head options head
elseif ${1:/* condition */} elseif ${1:/* condition */}
${0} ${0:TARGET}
snippet ifelse snippet ifelse
abbr if else endif abbr if else endif
options head options head
if ${1:#:condition} if ${1:#:condition}
${2} ${2:TARGET}
else else
${3} ${3}
endif endif
@ -23,14 +23,14 @@ snippet for
abbr for in endfor abbr for in endfor
options head options head
for ${1:#:var} in ${2:#:list} for ${1:#:var} in ${2:#:list}
${0} ${0:TARGET}
endfor endfor
snippet while snippet while
abbr while endwhile abbr while endwhile
options head options head
while ${1:#:condition} while ${1:#:condition}
${0} ${0:TARGET}
endwhile endwhile
snippet function snippet function
@ -38,14 +38,14 @@ abbr func endfunc
alias func alias func
options head options head
function! ${1:#:func_name}(${2}) function! ${1:#:func_name}(${2})
${0} ${0:TARGET}
endfunction endfunction
snippet try snippet try
abbr try endtry abbr try endtry
options head options head
try try
${1} ${1:TARGET}
catch /${2:#:pattern}/ catch /${2:#:pattern}/
${3} ${3}
endtry endtry
@ -55,7 +55,7 @@ abbr try ... finally ... endtry
alias tryf alias tryf
options head options head
try try
${1} ${1:TARGET}
finally finally
${2} ${2}
endtry endtry
@ -67,7 +67,7 @@ options head
snippet echomsg snippet echomsg
alias log alias log
options head options head
echomsg string(${1}) echomsg string(${1:TARGET})
snippet command snippet command
abbr command call function abbr command call function
@ -93,6 +93,6 @@ snippet redir
abbr redir => var abbr redir => var
options head options head
redir => ${1:#:var} redir => ${1:#:var}
${2:} ${2::TARGET}
redir END redir END

View File

@ -468,6 +468,7 @@ CHANGELOG *neosnippet-changelog*
- Implemented commented placeholder. - Implemented commented placeholder.
- Improved python snippets. - Improved python snippets.
- Fixed for alias. - Fixed for alias.
- Implemented target placeholder.
2012-10-29 2012-10-29
- Improved parse of snippets file. - Improved parse of snippets file.

View File

@ -75,6 +75,9 @@ imap <silent> <Plug>(neocomplcache_snippets_force_jump)
smap <silent> <Plug>(neocomplcache_snippets_force_jump) smap <silent> <Plug>(neocomplcache_snippets_force_jump)
\ <Plug>(neosnippet_jump) \ <Plug>(neosnippet_jump)
xnoremap <silent> <Plug>(neosnippet_expand_target)
\ :<C-u>call neosnippet#expand_target()<CR>
imap <silent> <Plug>(neocomplcache_start_unite_snippet) imap <silent> <Plug>(neocomplcache_start_unite_snippet)
\ <Plug>(neosnippet_start_unite_snippet) \ <Plug>(neosnippet_start_unite_snippet)
inoremap <expr><silent> <Plug>(neosnippet_start_unite_snippet) inoremap <expr><silent> <Plug>(neosnippet_start_unite_snippet)