From a243c5b22d450ef6f4f3a13079e139bbd8e0489a Mon Sep 17 00:00:00 2001 From: mattn Date: Mon, 11 Jun 2012 21:42:50 +0900 Subject: fix inline template. Just be possible to expand: var f = |; to var f = (function() { | }; --- autoload/sonictemplate.vim | 104 +++++++++++++++++++++++++++++++++------------ plugin/sonictemplate.vim | 16 +++++-- 2 files changed, 90 insertions(+), 30 deletions(-) diff --git a/autoload/sonictemplate.vim b/autoload/sonictemplate.vim index 274775b..5699a6f 100644 --- a/autoload/sonictemplate.vim +++ b/autoload/sonictemplate.vim @@ -1,7 +1,7 @@ "============================================================================= " sonictemplate.vim " Author: Yasuhiro Matsumoto -" Last Change: 06-Jun-2012. +" Last Change: 11-Jun-2012. let s:save_cpo = &cpo set cpo&vim @@ -19,25 +19,75 @@ call add(s:tmpldir, expand(':p:h:h') . '/template/') function! sonictemplate#select(mode) abort let name = input(':Template ', '', 'customlist,sonictemplate#complete') if name == '' - return + return '' + endif + call sonictemplate#apply(name, a:mode, 0) + return '' +endfunction + +function! sonictemplate#select_intelligent(mode) abort + let name = input(':Template ', '', 'customlist,sonictemplate#complete_intelligent') + if name == '' + return '' endif - call sonictemplate#apply(name, a:mode) + call sonictemplate#apply(name, a:mode, 1) + return '' endfunction +function! sonictemplate#get_filetype() + let c = col('.') + if c == col('$') + let c -= 1 + endif + let ft = tolower(synIDattr(synID(line("."), c, 1), "name")) + if ft =~ '^css\w' + let ft = 'css' + elseif ft =~ '^html\w' + let ft = 'html' + elseif ft =~ '^javaScript' + let ft = 'javascript' + elseif ft =~ '^xml\w' + let ft = 'xml' + endif + return ft +endfunction + function! sonictemplate#complete(lead, cmdline, curpos) abort let ft = &ft let tmp = [] + let fts = [&ft, sonictemplate#get_filetype()] for tmpldir in s:tmpldir - let tmp += map(split(globpath(join([tmpldir, ft], '/'), (search('[^ \t]', 'wn') ? 'snip-' : 'base-') . a:lead . '*.*'), "\n"), 'fnamemodify(v:val, ":t:r")[5:]') + for ft in fts + let tmp += map(split(globpath(join([tmpldir, ft], '/'), (search('[^ \t]', 'wn') ? 'snip-' : 'base-') . a:lead . '*.*'), "\n"), 'fnamemodify(v:val, ":t:r")[5:]') + if len(tmp) > 0 + break + endif + endfor + endfor + for tmpldir in s:tmpldir + let tmp += map(split(globpath(join([tmpldir, '_'], '/'), (search('[^ \t]', 'wn') ? 'snip-' : 'base-') . a:lead . '*.*'), "\n"), 'fnamemodify(v:val, ":t:r")[5:]') endfor - if len(tmp) == 0 - let ft = tolower(synIDattr(synID(line("."), col("."), 1), "name")) - if len(ft) > 0 - for tmpldir in s:tmpldir - let tmp += map(split(globpath(join([tmpldir, ft], '/'), (search('[^ \t]', 'wn') ? 'snip-' : 'base-') . a:lead . '*.*'), "\n"), 'fnamemodify(v:val, ":t:r")[5:]') - endfor + let candidate = [] + for c in tmp + if index(candidate, c) == -1 + call add(candidate, c) endif - endif + endfor + return candidate +endfunction + +function! sonictemplate#complete_intelligent(lead, cmdline, curpos) abort + let ft = &ft + let tmp = [] + let fts = [tolower(sonictemplate#get_filetype()), &ft] + for tmpldir in s:tmpldir + for ft in fts + let tmp += map(split(globpath(join([tmpldir, ft], '/'), (search('[^ \t]', 'wn') ? 'snip-' : 'base-') . a:lead . '*.*'), "\n"), 'fnamemodify(v:val, ":t:r")[5:]') + if len(tmp) > 0 + break + endif + endfor + endfor for tmpldir in s:tmpldir let tmp += map(split(globpath(join([tmpldir, '_'], '/'), (search('[^ \t]', 'wn') ? 'snip-' : 'base-') . a:lead . '*.*'), "\n"), 'fnamemodify(v:val, ":t:r")[5:]') endfor @@ -50,23 +100,21 @@ function! sonictemplate#complete(lead, cmdline, curpos) abort return candidate endfunction -function! sonictemplate#apply(name, mode) abort + +function! sonictemplate#apply(name, mode, ...) abort let name = matchstr(a:name, '\S\+') let buffer_is_not_empty = search('[^ \t]', 'wn') let fs = [] + if get(a:000, 0, 0) + let fts = [sonictemplate#get_filetype(), &ft, '_'] + else + let fts = [&ft, sonictemplate#get_filetype(), '_'] + endif + let prefix = search('[^ \t]', 'wn') ? 'snip-' : 'base-' for tmpldir in s:tmpldir - let ft = &ft - let fsl = split(globpath(join([tmpldir, ft], '/'), (search('[^ \t]', 'wn') ? 'snip-' : 'base-') . name . '.*'), "\n") - if len(fsl) == 0 - let ft = tolower(synIDattr(synID(line("."), col("."), 1), "name")) - if len(ft) > 0 - let fsl = split(globpath(join([tmpldir, ft], '/'), (search('[^ \t]', 'wn') ? 'snip-' : 'base-') . name . '.*'), "\n") - endif - endif - if len(fsl) == 0 - let fsl = split(globpath(join([tmpldir, '_'], '/'), (search('[^ \t]', 'wn') ? 'snip-' : 'base-') . name . '.*'), "\n") - endif - let fs += fsl + for ft in fts + let fs += split(globpath(join([tmpldir, ft], '/'), prefix . name . '.*'), "\n") + endfor endfor if len(fs) == 0 echomsg 'Template '.name.' is not exists.' @@ -122,9 +170,13 @@ function! sonictemplate#apply(name, mode) abort else let line = getline('.') let indent = matchstr(line, '^\(\s*\)') - if line =~ '^\s*$' && line('.') != line('$') - silent! normal dd + if line !~ '^\s*$' + let lhs = col('.') > 1 ? line[:col('.')-2] : "" + let rhs = line[len(lhs):] + let lhs = lhs[len(indent):] + let c = lhs . c . rhs endif + silent! normal dd let c = indent . substitute(substitute(c, "\n", "\n".indent, 'g'), "\n".indent."\n", "\n\n", 'g') if len(indent) && (&expandtab || &tabstop != &shiftwidth || indent =~ '^ \+$') let c = substitute(c, "\t", repeat(' ', min([len(indent), &shiftwidth])), 'g') diff --git a/plugin/sonictemplate.vim b/plugin/sonictemplate.vim index 8e5f895..dd91430 100644 --- a/plugin/sonictemplate.vim +++ b/plugin/sonictemplate.vim @@ -1,7 +1,7 @@ "============================================================================= " File: sonictemplate.vim " Author: Yasuhiro Matsumoto -" Last Change: 10-Jun-2012. +" Last Change: 11-Jun-2012. " Version: 0.10 " WebPage: http://github.com/mattn/sonictemplate-vim " Description: Easy and high speed coding method @@ -24,13 +24,21 @@ command! -nargs=1 -complete=customlist,sonictemplate#complete Template call soni if get(g:, 'sonictemplate_key', '') == '' nnoremap (sonictemplate) :call sonictemplate#select('n') - inoremap (sonictemplate) :call sonictemplate#select('i') - + inoremap (sonictemplate) =sonictemplate#select('i') nmap t (sonictemplate) imap t (sonictemplate) else exe "nnoremap" g:sonictemplate_key ":call sonictemplate#select('n')" - exe "inoremap" g:sonictemplate_key ":call sonictemplate#select('i')" + exe "inoremap" g:sonictemplate_key "=sonictemplate#select('i')" +endif +if get(g:, 'sonictemplate_intelligent_key', '') == '' + nnoremap (sonictemplate-intelligent) :call sonictemplate#select_intelligent('n') + inoremap (sonictemplate-intelligent) =sonictemplate#select_intelligent('i') + nmap T (sonictemplate-intelligent) + imap T (sonictemplate-intelligent) +else + exe "nnoremap" g:sonictemplate_intelligent_key ":call sonictemplate#select_intelligent('n')" + exe "inoremap" g:sonictemplate_intelligent_key "=sonictemplate#select_intelligent('i')" endif let &cpo = s:save_cpo -- cgit v1.2.3-54-g00ecf