From 795a13701fed7e310a9945931fe286dbdc6691bd Mon Sep 17 00:00:00 2001 From: Shougo Matsushita Date: Sat, 26 Dec 2015 12:12:08 +0900 Subject: [PATCH] Add tests --- autoload/neosnippet/handlers.vim | 26 -------------- autoload/neosnippet/parser.vim | 61 +++++++++++++++++++++----------- test/functions.vim | 58 ++++++++++++++++++++++++++---- 3 files changed, 92 insertions(+), 53 deletions(-) diff --git a/autoload/neosnippet/handlers.vim b/autoload/neosnippet/handlers.vim index 92ef05a..9bcdacf 100644 --- a/autoload/neosnippet/handlers.vim +++ b/autoload/neosnippet/handlers.vim @@ -73,32 +73,6 @@ function! neosnippet#handlers#_all_clear_markers() "{{{ endtry endfunction"}}} -function! neosnippet#handlers#_get_in_paren(str) abort "{{{ - let s = '' - let level = 0 - for c in split(a:str, '\zs') - if c == '(' - let level += 1 - - if level == 1 - continue - endif - elseif c == ')' - if level == 1 && s != '' - return s - else - let level -= 1 - endif - endif - - if level > 0 - let s .= c - endif - endfor - - return '' -endfunction"}}} - function! s:is_auto_pairs() abort "{{{ return get(g:, 'neopairs#enable', 0) endfunction"}}} diff --git a/autoload/neosnippet/parser.vim b/autoload/neosnippet/parser.vim index 4a2213d..651da6b 100644 --- a/autoload/neosnippet/parser.vim +++ b/autoload/neosnippet/parser.vim @@ -315,34 +315,55 @@ function! neosnippet#parser#_get_completed_snippet(completed_item) "{{{ " Make snippet arguments let cnt = 1 let snippet = '' - if key == '(' - for arg in split(substitute(neosnippet#handlers#_get_in_paren(abbr), - \ '(\zs.\{-}\ze)', '', 'g'), '[^[]\zs\s*,\s*') - if arg ==# 'self' && &filetype ==# 'python' - " Ignore self argument - continue - endif + for arg in split(substitute( + \ neosnippet#parser#_get_in_paren(key, pair, abbr), + \ key.'\zs.\{-}\ze'.pair, '', 'g'), '[^[]\zs\s*,\s*') + if key ==# '(' && arg ==# 'self' && &filetype ==# 'python' + " Ignore self argument + continue + endif - if cnt != 1 - let snippet .= ', ' - endif - let snippet .= printf('${%d:#:%s}', cnt, escape(arg, '{}')) - let cnt += 1 - endfor - endif - - if key != '(' && snippet[-1:] ==# key - let snippet .= '${' . cnt . '}' . pair + if cnt != 1 + let snippet .= ', ' + endif + let snippet .= printf('${%d:#:%s}', cnt, escape(arg, '{}')) let cnt += 1 - elseif snippet[-1:] !=# pair - let snippet .= pair - endif + endfor + + let snippet .= pair let snippet .= '${' . cnt . '}' return snippet endfunction"}}} +function! neosnippet#parser#_get_in_paren(key, pair, str) abort "{{{ + let s = '' + let level = 0 + for c in split(a:str, '\zs') + if c ==# a:key + let level += 1 + + if level == 1 + continue + endif + elseif c ==# a:pair + if level == 1 && s != '' + return s + else + let level -= 1 + endif + endif + + if level > 0 + let s .= c + endif + endfor + + return '' +endfunction"}}} + + let &cpo = s:save_cpo unlet s:save_cpo diff --git a/test/functions.vim b/test/functions.vim index 29797aa..4515921 100644 --- a/test/functions.vim +++ b/test/functions.vim @@ -2,26 +2,70 @@ let s:suite = themis#suite('toml') let s:assert = themis#helper('assert') function! s:suite.get_in_paren() - call s:assert.equals(neosnippet#handlers#_get_in_paren( + call s:assert.equals(neosnippet#parser#_get_in_paren( + \ '(', ')', \ '(foobar)'), \ 'foobar') - call s:assert.equals(neosnippet#handlers#_get_in_paren( + call s:assert.equals(neosnippet#parser#_get_in_paren( + \ '(', ')', \ '(foobar, baz)'), \ 'foobar, baz') - call s:assert.equals(neosnippet#handlers#_get_in_paren( + call s:assert.equals(neosnippet#parser#_get_in_paren( + \ '(', ')', \ '(foobar, (baz))'), \ 'foobar, (baz)') - call s:assert.equals(neosnippet#handlers#_get_in_paren( + call s:assert.equals(neosnippet#parser#_get_in_paren( + \ '(', ')', \ 'foobar('), \ '') - call s:assert.equals(neosnippet#handlers#_get_in_paren( + call s:assert.equals(neosnippet#parser#_get_in_paren( + \ '(', ')', \ 'foobar()'), \ '') - call s:assert.equals(neosnippet#handlers#_get_in_paren( + call s:assert.equals(neosnippet#parser#_get_in_paren( + \ '(', ')', \ 'foobar(fname)'), \ 'fname') - call s:assert.equals(neosnippet#handlers#_get_in_paren( + call s:assert.equals(neosnippet#parser#_get_in_paren( + \ '(', ')', \ 'wait() wait(long, int)'), \ 'long, int') endfunction +function! s:suite.get_completed_snippet() + call s:assert.equals(neosnippet#parser#_get_completed_snippet({ + \ 'word' : 'foo(', 'abbr' : 'foo()', + \ 'menu' : '', 'info' : '' + \ }), ')${1}') + + call s:assert.equals(neosnippet#parser#_get_completed_snippet({ + \ 'word' : 'foo(', 'abbr' : 'foo(hoge)', + \ 'menu' : '', 'info' : '' + \ }), '${1:#:hoge})${2}') + + call s:assert.equals(neosnippet#parser#_get_completed_snippet({ + \ 'word' : 'foo', 'abbr' : 'foo(hoge)', + \ 'menu' : '', 'info' : '' + \ }), '') + + call s:assert.equals(neosnippet#parser#_get_completed_snippet({ + \ 'word' : 'foo(', 'abbr' : 'foo(hoge, piyo)', + \ 'menu' : '', 'info' : '' + \ }), '${1:#:hoge}, ${2:#:piyo})${3}') + + call s:assert.equals(neosnippet#parser#_get_completed_snippet({ + \ 'word' : 'foo(', 'abbr' : 'foo(hoge, piyo(foobar))', + \ 'menu' : '', 'info' : '' + \ }), '${1:#:hoge}, ${2:#:piyo()})${3}') + + call s:assert.equals(neosnippet#parser#_get_completed_snippet({ + \ 'word' : 'foo{', 'abbr' : 'foo{}', + \ 'menu' : '', 'info' : '' + \ }), '}${1}') + + call s:assert.equals(neosnippet#parser#_get_completed_snippet({ + \ 'word' : 'foo{', 'abbr' : 'foo{piyo}', + \ 'menu' : '', 'info' : '' + \ }), '${1:#:piyo}}${2}') +endfunction +