2012-08-17 03:41:25 +00:00
|
|
|
" config which can be overridden (shared lines)
|
|
|
|
if !exists('g:snipMate')
|
|
|
|
let g:snipMate = {}
|
|
|
|
endif
|
|
|
|
|
|
|
|
try
|
|
|
|
call tlib#input#List('mi', '', [])
|
|
|
|
catch /.*/
|
2013-08-03 12:50:12 +00:00
|
|
|
echoe "you're missing tlib. See install instructions at ".expand('<sfile>:h:h').'/README.md'
|
2012-08-17 03:41:25 +00:00
|
|
|
endtry
|
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
fun! Filename(...) abort
|
2012-08-17 03:41:25 +00:00
|
|
|
let filename = expand('%:t:r')
|
|
|
|
if filename == '' | return a:0 == 2 ? a:2 : '' | endif
|
|
|
|
return !a:0 || a:1 == '' ? filename : substitute(a:1, '$1', filename, 'g')
|
|
|
|
endf
|
|
|
|
|
2014-03-02 14:35:00 +00:00
|
|
|
let s:cache = {}
|
2013-04-13 17:45:21 +00:00
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
function! snipMate#expandSnip(snip, version, col) abort
|
2015-02-04 10:43:54 +00:00
|
|
|
let lnum = line('.')
|
|
|
|
let col = a:col
|
|
|
|
let line = getline(lnum)
|
|
|
|
let indent = match(line, '\S\|$') + 1
|
|
|
|
let b:snip_state = snipmate#jumping#state()
|
2012-08-17 03:41:25 +00:00
|
|
|
|
2015-02-04 10:43:54 +00:00
|
|
|
if a:version == 1
|
|
|
|
let [snippet, b:snip_state.stops] = snipmate#parse#snippet(a:snip)
|
|
|
|
" Build stop/mirror info
|
|
|
|
let b:snip_state.stop_count = s:build_stops(snippet, b:snip_state.stops, lnum, col, indent)
|
|
|
|
let snipLines = snipMate#sniplist_str(snippet, b:snip_state.stops)
|
|
|
|
else
|
|
|
|
let snippet = snipmate#legacy#process_snippet(a:snip)
|
|
|
|
let [b:snip_state.stops, b:snip_state.stop_count] = snipmate#legacy#build_stops(snippet, lnum, col - indent, indent)
|
|
|
|
let snipLines = split(substitute(snippet, printf('%s\d\+\|%s{\d\+.\{-}}',
|
|
|
|
\ g:snipmate#legacy#sigil, g:snipmate#legacy#sigil), '', 'g'), "\n", 1)
|
|
|
|
endif
|
2012-08-17 03:41:25 +00:00
|
|
|
|
2015-02-04 10:43:54 +00:00
|
|
|
" Abort if the snippet is empty
|
|
|
|
if empty(snippet)
|
|
|
|
return ''
|
|
|
|
endif
|
2012-08-17 03:41:25 +00:00
|
|
|
|
2015-02-04 10:43:54 +00:00
|
|
|
" Expand snippet onto current position
|
2012-08-17 03:41:25 +00:00
|
|
|
let afterCursor = strpart(line, col - 1)
|
|
|
|
" Keep text after the cursor
|
|
|
|
if afterCursor != "\t" && afterCursor != ' '
|
|
|
|
let line = strpart(line, 0, col - 1)
|
|
|
|
let snipLines[-1] .= afterCursor
|
|
|
|
else
|
|
|
|
let afterCursor = ''
|
|
|
|
" For some reason the cursor needs to move one right after this
|
|
|
|
if line != '' && col == 1 && &ve != 'all' && &ve != 'onemore'
|
|
|
|
let col += 1
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2013-07-17 23:06:05 +00:00
|
|
|
" Insert snippet with proper indentation
|
|
|
|
call setline(lnum, line . snipLines[0])
|
|
|
|
call append(lnum, map(snipLines[1:], "empty(v:val) ? v:val : '" . strpart(line, 0, indent - 1) . "' . v:val"))
|
2012-08-17 03:41:25 +00:00
|
|
|
|
|
|
|
" Open any folds snippet expands into
|
2015-02-04 10:43:54 +00:00
|
|
|
if &foldenable
|
|
|
|
silent! exec lnum . ',' . (lnum + len(snipLines) - 1) . 'foldopen'
|
2012-08-17 03:41:25 +00:00
|
|
|
endif
|
2013-04-13 17:45:21 +00:00
|
|
|
|
2015-02-04 10:43:54 +00:00
|
|
|
aug snipmate_changes
|
|
|
|
au CursorMoved,CursorMovedI <buffer> if exists('b:snip_state') |
|
|
|
|
\ call b:snip_state.update_changes() |
|
|
|
|
\ else |
|
|
|
|
\ silent! au! snipmate_changes * <buffer> |
|
|
|
|
\ endif
|
|
|
|
aug END
|
2012-08-17 03:41:25 +00:00
|
|
|
|
2015-02-04 10:43:54 +00:00
|
|
|
let b:snip_state.stop_no = 0
|
|
|
|
return b:snip_state.set_stop(0)
|
|
|
|
endfunction
|
2012-08-17 03:41:25 +00:00
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
function! snipMate#placeholder_str(num, stops) abort
|
2015-02-04 10:43:54 +00:00
|
|
|
return snipMate#sniplist_str(a:stops[a:num].placeholder, a:stops)[0]
|
|
|
|
endfunction
|
2013-08-03 12:50:12 +00:00
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
function! snipMate#sniplist_str(snippet, stops) abort
|
2015-02-04 10:43:54 +00:00
|
|
|
let lines = ['']
|
|
|
|
let pos = 0
|
|
|
|
let add_to = 1
|
|
|
|
let seen_stops = []
|
2012-08-17 03:41:25 +00:00
|
|
|
|
2015-02-04 10:43:54 +00:00
|
|
|
while pos < len(a:snippet)
|
|
|
|
let item = a:snippet[pos]
|
2012-08-17 03:41:25 +00:00
|
|
|
|
2015-02-04 10:43:54 +00:00
|
|
|
if type(item) == type('')
|
|
|
|
if add_to
|
|
|
|
let lines[-1] .= item
|
|
|
|
else
|
|
|
|
call add(lines, item)
|
|
|
|
endif
|
|
|
|
let add_to = 0
|
|
|
|
elseif type(item) == type([])
|
|
|
|
let lines[-1] .= snipMate#placeholder_str(item[0], a:stops)
|
|
|
|
let add_to = 1
|
2012-08-17 03:41:25 +00:00
|
|
|
endif
|
|
|
|
|
2015-02-04 10:43:54 +00:00
|
|
|
let pos += 1
|
|
|
|
unlet item " avoid E706
|
|
|
|
endwhile
|
2012-08-17 03:41:25 +00:00
|
|
|
|
2015-02-04 10:43:54 +00:00
|
|
|
return lines
|
2013-04-13 17:45:21 +00:00
|
|
|
endfunction
|
2012-08-17 03:41:25 +00:00
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
function! s:build_stops(snippet, stops, lnum, col, indent) abort
|
2015-02-04 10:43:54 +00:00
|
|
|
let stops = a:stops
|
|
|
|
let line = a:lnum
|
|
|
|
let col = a:col
|
|
|
|
|
|
|
|
for [id, dict] in items(stops)
|
|
|
|
for i in dict.instances
|
|
|
|
if len(i) > 1 && type(i[1]) != type({})
|
|
|
|
if !has_key(dict, 'placeholder')
|
|
|
|
let dict.placeholder = i[1:]
|
|
|
|
else
|
|
|
|
unlet i[1:]
|
2012-08-17 03:41:25 +00:00
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfor
|
2015-02-04 10:43:54 +00:00
|
|
|
if !has_key(dict, 'placeholder')
|
|
|
|
let dict.placeholder = []
|
|
|
|
let j = 0
|
|
|
|
while len(dict.instances[j]) > 1
|
|
|
|
let j += 1
|
|
|
|
endwhile
|
|
|
|
call add(dict.instances[j], '')
|
|
|
|
endif
|
|
|
|
unlet dict.instances
|
|
|
|
endfor
|
2012-08-17 03:41:25 +00:00
|
|
|
|
2015-02-04 10:43:54 +00:00
|
|
|
let [line, col] = s:build_loc_info(a:snippet, stops, line, col, a:indent)
|
2013-04-13 17:45:21 +00:00
|
|
|
|
2015-02-04 10:43:54 +00:00
|
|
|
" add zero tabstop if it doesn't exist and then link it to the highest stop
|
|
|
|
" number
|
|
|
|
let stops[0] = get(stops, 0,
|
|
|
|
\ { 'placeholder' : [], 'line' : line, 'col' : col })
|
|
|
|
let stop_count = max(keys(stops)) + 2
|
|
|
|
let stops[stop_count - 1] = stops[0]
|
2012-08-17 03:41:25 +00:00
|
|
|
|
2015-02-04 10:43:54 +00:00
|
|
|
return stop_count
|
2013-04-13 17:45:21 +00:00
|
|
|
endfunction
|
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
function! s:build_loc_info(snippet, stops, line, col, indent) abort
|
2015-02-04 10:43:54 +00:00
|
|
|
let stops = a:stops
|
|
|
|
let line = a:line
|
|
|
|
let col = a:col
|
|
|
|
let pos = 0
|
|
|
|
let in_text = 0
|
|
|
|
|
|
|
|
while pos < len(a:snippet)
|
|
|
|
let item = a:snippet[pos]
|
|
|
|
|
|
|
|
if type(item) == type('')
|
|
|
|
if in_text
|
|
|
|
let line += 1
|
|
|
|
let col = a:indent
|
2012-08-17 03:41:25 +00:00
|
|
|
endif
|
2015-02-04 10:43:54 +00:00
|
|
|
let col += len(item)
|
|
|
|
let in_text = 1
|
|
|
|
elseif type(item) == type([])
|
|
|
|
let id = item[0]
|
|
|
|
if len(item) > 1 && type(item[1]) != type({})
|
|
|
|
let stops[id].line = line
|
|
|
|
let stops[id].col = col
|
|
|
|
let [line, col] = s:build_loc_info(item[1:], stops, line, col, a:indent)
|
|
|
|
else
|
|
|
|
call s:add_mirror(stops, id, line, col, item)
|
|
|
|
let col += len(snipMate#placeholder_str(id, stops))
|
2012-08-17 03:41:25 +00:00
|
|
|
endif
|
2015-02-04 10:43:54 +00:00
|
|
|
let in_text = 0
|
2012-08-17 03:41:25 +00:00
|
|
|
endif
|
|
|
|
|
2015-02-04 10:43:54 +00:00
|
|
|
let pos += 1
|
|
|
|
unlet item " avoid E706
|
|
|
|
endwhile
|
2013-04-26 16:17:22 +00:00
|
|
|
|
2015-02-04 10:43:54 +00:00
|
|
|
return [line, col]
|
|
|
|
endfunction
|
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
function! s:add_mirror(stops, id, line, col, item) abort
|
2015-02-04 10:43:54 +00:00
|
|
|
let stops = a:stops
|
|
|
|
let item = a:item
|
|
|
|
let stops[a:id].mirrors = get(stops[a:id], 'mirrors', [])
|
|
|
|
let mirror = get(a:item, 1, {})
|
|
|
|
let mirror.line = a:line
|
|
|
|
let mirror.col = a:col
|
|
|
|
call add(stops[a:id].mirrors, mirror)
|
|
|
|
if len(item) == 1
|
|
|
|
call add(item, mirror)
|
2013-04-26 16:17:22 +00:00
|
|
|
endif
|
2013-04-13 17:45:21 +00:00
|
|
|
endfunction
|
2012-08-17 03:41:25 +00:00
|
|
|
|
|
|
|
" reads a .snippets file
|
|
|
|
" returns list of
|
|
|
|
" ['triggername', 'name', 'contents']
|
|
|
|
" if triggername is not set 'default' is assumed
|
2014-07-02 11:18:18 +00:00
|
|
|
" TODO: better error checking
|
2015-02-13 00:19:27 +00:00
|
|
|
fun! snipMate#ReadSnippetsFile(file) abort
|
2012-08-17 03:41:25 +00:00
|
|
|
let result = []
|
2013-07-17 23:06:05 +00:00
|
|
|
let new_scopes = []
|
|
|
|
if !filereadable(a:file) | return [result, new_scopes] | endif
|
2012-08-17 03:41:25 +00:00
|
|
|
let inSnip = 0
|
2014-07-02 11:18:18 +00:00
|
|
|
let line_no = 0
|
2015-02-04 10:43:54 +00:00
|
|
|
let snipversion = get(g:snipMate, 'snippet_version', 0)
|
2012-08-17 03:41:25 +00:00
|
|
|
for line in readfile(a:file) + ["\n"]
|
2014-07-02 11:18:18 +00:00
|
|
|
let line_no += 1
|
|
|
|
|
2014-03-02 14:35:00 +00:00
|
|
|
if inSnip && (line[0] == "\t" || line == '')
|
2012-08-17 03:41:25 +00:00
|
|
|
let content .= strpart(line, 1)."\n"
|
|
|
|
continue
|
|
|
|
elseif inSnip
|
2015-02-04 10:43:54 +00:00
|
|
|
call add(result, [trigger, name,
|
|
|
|
\ content[:-2], bang, snipversion])
|
2012-08-17 03:41:25 +00:00
|
|
|
let inSnip = 0
|
|
|
|
endif
|
|
|
|
|
|
|
|
if line[:6] == 'snippet'
|
2014-03-02 14:35:00 +00:00
|
|
|
let inSnip = 1
|
2015-02-04 10:43:54 +00:00
|
|
|
let bang = (line[7] == '!')
|
|
|
|
let trigger = strpart(line, 8 + bang)
|
2012-08-17 03:41:25 +00:00
|
|
|
let name = ''
|
|
|
|
let space = stridx(trigger, ' ') + 1
|
|
|
|
if space " Process multi snip
|
|
|
|
let name = strpart(trigger, space)
|
|
|
|
let trigger = strpart(trigger, 0, space - 1)
|
|
|
|
endif
|
|
|
|
let content = ''
|
2014-07-02 11:18:18 +00:00
|
|
|
if trigger =~ '^\s*$' " discard snippets with empty triggers
|
|
|
|
echom 'Invalid snippet in' a:file 'near line' line_no
|
|
|
|
let inSnip = 0
|
|
|
|
endif
|
2013-07-17 23:06:05 +00:00
|
|
|
elseif line[:6] == 'extends'
|
|
|
|
call extend(new_scopes, map(split(strpart(line, 8)),
|
|
|
|
\ "substitute(v:val, ',*$', '', '')"))
|
2015-02-04 10:43:54 +00:00
|
|
|
elseif line[:6] == 'version'
|
|
|
|
let snipversion = +strpart(line, 8)
|
2012-08-17 03:41:25 +00:00
|
|
|
endif
|
|
|
|
endfor
|
2013-07-17 23:06:05 +00:00
|
|
|
return [result, new_scopes]
|
2012-08-17 03:41:25 +00:00
|
|
|
endf
|
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
function! s:GetScopes() abort
|
2014-07-02 11:18:18 +00:00
|
|
|
let ret = exists('b:snipMate.scope_aliases') ? copy(b:snipMate.scope_aliases) : {}
|
2014-04-18 12:58:02 +00:00
|
|
|
let global = get(g:snipMate, 'scope_aliases', {})
|
|
|
|
for alias in keys(global)
|
|
|
|
if has_key(ret, alias)
|
|
|
|
let ret[alias] = join(split(ret[alias], ',')
|
|
|
|
\ + split(global[alias], ','), ',')
|
|
|
|
else
|
|
|
|
let ret[alias] = global[alias]
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
return ret
|
|
|
|
endfunction
|
|
|
|
|
2012-08-17 03:41:25 +00:00
|
|
|
" adds scope aliases to list.
|
|
|
|
" returns new list
|
|
|
|
" the aliases of aliases are added recursively
|
2015-02-13 00:19:27 +00:00
|
|
|
fun! s:AddScopeAliases(list) abort
|
2012-08-17 03:41:25 +00:00
|
|
|
let did = {}
|
2014-04-18 12:58:02 +00:00
|
|
|
let scope_aliases = s:GetScopes()
|
2012-08-17 03:41:25 +00:00
|
|
|
let new = a:list
|
|
|
|
let new2 = []
|
|
|
|
while !empty(new)
|
|
|
|
for i in new
|
|
|
|
if !has_key(did, i)
|
|
|
|
let did[i] = 1
|
|
|
|
call extend(new2, split(get(scope_aliases,i,''),','))
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
let new = new2
|
|
|
|
let new2 = []
|
|
|
|
endwhile
|
|
|
|
return keys(did)
|
|
|
|
endf
|
|
|
|
|
2015-02-04 10:43:54 +00:00
|
|
|
au SourceCmd *.snippet,*.snippets call s:source_snippet()
|
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
function! s:info_from_filename(file) abort
|
2015-02-04 10:43:54 +00:00
|
|
|
let parts = split(fnamemodify(a:file, ':r'), '/')
|
|
|
|
let snipidx = len(parts) - index(reverse(copy(parts)), 'snippets') - 1
|
|
|
|
let rtp_prefix = join(parts[(snipidx -
|
|
|
|
\ (parts[snipidx - 1] == 'after' ? 3 : 2)):snipidx - 1], '/')
|
|
|
|
let trigger = get(parts, snipidx + 2, '')
|
|
|
|
let desc = get(parts, snipidx + 3, get(g:snipMate, 'override', 0) ?
|
|
|
|
\ '' : fnamemodify(a:file, ':t'))
|
|
|
|
return [rtp_prefix, trigger, desc]
|
2013-05-26 00:31:29 +00:00
|
|
|
endfunction
|
2012-08-17 03:41:25 +00:00
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
function! s:source_snippet() abort
|
2015-02-04 10:43:54 +00:00
|
|
|
let file = expand('<afile>:p')
|
|
|
|
let [rtp_prefix, trigger, desc] = s:info_from_filename(file)
|
|
|
|
let new_snips = []
|
|
|
|
if fnamemodify(file, ':e') == 'snippet'
|
|
|
|
call add(new_snips, [trigger, desc, join(readfile(file), "\n"), 0,
|
|
|
|
\ get(g:snipMate, 'snippet_version', 0)])
|
|
|
|
else
|
|
|
|
let [snippets, extends] = s:CachedSnips(file)
|
|
|
|
let new_snips = deepcopy(snippets)
|
|
|
|
call extend(s:lookup_state.extends, extends)
|
2014-03-02 14:35:00 +00:00
|
|
|
endif
|
2015-02-04 10:43:54 +00:00
|
|
|
for snip in new_snips
|
|
|
|
if get(g:snipMate, 'override', 0)
|
|
|
|
let snip[1] = join([s:lookup_state.scope, snip[1]])
|
|
|
|
else
|
|
|
|
let snip[1] = join([s:lookup_state.scope, rtp_prefix,
|
|
|
|
\ empty(snip[1]) ? desc : snip[1]])
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
call extend(s:lookup_state.snips, new_snips)
|
2014-03-02 14:35:00 +00:00
|
|
|
endfunction
|
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
function! s:CachedSnips(file) abort
|
2014-03-02 14:35:00 +00:00
|
|
|
let mtime = getftime(a:file)
|
|
|
|
if has_key(s:cache, a:file) && s:cache[a:file].mtime >= mtime
|
|
|
|
return s:cache[a:file].contents
|
|
|
|
endif
|
|
|
|
let s:cache[a:file] = {}
|
|
|
|
let s:cache[a:file].mtime = mtime
|
|
|
|
let s:cache[a:file].contents = snipMate#ReadSnippetsFile(a:file)
|
|
|
|
return s:cache[a:file].contents
|
|
|
|
endfunction
|
2012-08-17 03:41:25 +00:00
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
function! s:snippet_filenames(scope, trigger) abort
|
2015-02-04 10:43:54 +00:00
|
|
|
let mid = ['', '_*', '/*']
|
2015-02-24 10:45:22 +00:00
|
|
|
let mid += map(copy(mid), "'/' . a:trigger . '*' . v:val")
|
|
|
|
call map(mid, "'snippets/' . a:scope . v:val . '.snippet'")
|
|
|
|
return join(map(mid[:2], 'v:val . "s"') + mid[3:])
|
2015-02-04 10:43:54 +00:00
|
|
|
endfunction
|
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
function! snipMate#SetByPath(dict, trigger, path, snippet, bang, snipversion) abort
|
2015-02-04 10:43:54 +00:00
|
|
|
let d = a:dict
|
|
|
|
if !has_key(d, a:trigger) || a:bang
|
|
|
|
let d[a:trigger] = {}
|
|
|
|
endif
|
|
|
|
let d[a:trigger][a:path] = [a:snippet, a:snipversion]
|
|
|
|
endfunction
|
|
|
|
|
2012-08-17 03:41:25 +00:00
|
|
|
" default triggers based on paths
|
2015-02-13 00:19:27 +00:00
|
|
|
function! snipMate#DefaultPool(scopes, trigger, result) abort
|
2015-02-04 10:43:54 +00:00
|
|
|
let scopes = s:AddScopeAliases(a:scopes)
|
|
|
|
let scopes_done = []
|
|
|
|
let rtp_save = &rtp
|
|
|
|
let &rtp = join(g:snipMate.snippet_dirs, ',')
|
|
|
|
let s:lookup_state = {}
|
|
|
|
let s:lookup_state.snips = []
|
|
|
|
|
|
|
|
while !empty(scopes)
|
|
|
|
let scope = remove(scopes, 0)
|
|
|
|
let s:lookup_state.scope = scope
|
|
|
|
let s:lookup_state.extends = []
|
|
|
|
|
|
|
|
exec 'runtime!' s:snippet_filenames(scope, escape(a:trigger, "*[]?{}`'$|#%"))
|
|
|
|
|
|
|
|
call add(scopes_done, scope)
|
|
|
|
call extend(scopes, s:lookup_state.extends)
|
|
|
|
call filter(scopes, 'index(scopes_done, v:val) == -1')
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
for [trigger, desc, contents, bang, snipversion] in s:lookup_state.snips
|
|
|
|
if trigger =~ '\V\^' . escape(a:trigger, '\')
|
|
|
|
call snipMate#SetByPath(a:result, trigger, desc, contents, bang, snipversion)
|
2012-08-17 03:41:25 +00:00
|
|
|
endif
|
|
|
|
endfor
|
2014-03-02 14:35:00 +00:00
|
|
|
|
2015-02-04 10:43:54 +00:00
|
|
|
let &rtp = rtp_save
|
2014-03-02 14:35:00 +00:00
|
|
|
endfunction
|
2012-08-17 03:41:25 +00:00
|
|
|
|
|
|
|
" return a dict of snippets found in runtimepath matching trigger
|
|
|
|
" scopes: list of scopes. usually this is the filetype. eg ['c','cpp']
|
|
|
|
" trigger may contain glob patterns. Thus use '*' to get all triggers
|
|
|
|
"
|
2015-02-13 00:19:27 +00:00
|
|
|
fun! snipMate#GetSnippets(scopes, trigger) abort
|
2012-08-17 03:41:25 +00:00
|
|
|
let result = {}
|
|
|
|
|
|
|
|
for F in values(g:snipMateSources)
|
|
|
|
call funcref#Call(F, [a:scopes, a:trigger, result])
|
|
|
|
endfor
|
|
|
|
return result
|
|
|
|
endf
|
|
|
|
|
|
|
|
" adds leading tab
|
|
|
|
" and replaces leading spaces by tabs
|
|
|
|
" see ftplugin/snippet.vim
|
2015-02-13 00:19:27 +00:00
|
|
|
fun! snipMate#RetabSnip() range abort
|
2012-08-17 03:41:25 +00:00
|
|
|
let leadingTab = expand('%:e') == 'snippets'
|
|
|
|
|
|
|
|
let lines = getline(a:firstline, a:lastline)
|
|
|
|
|
|
|
|
" remove leading "\t"
|
|
|
|
let allIndented = 1
|
|
|
|
for l in lines
|
|
|
|
if l[0] != '\t' | let allIndented = 0 | endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
" retab
|
|
|
|
if allIndented
|
|
|
|
call map(lines, 'v:val[1:]')
|
|
|
|
endif
|
|
|
|
|
|
|
|
let leadingSp = filter(map(copy(lines),'matchstr(v:val,"^\\s*") '),'v:val !=""')
|
|
|
|
if !empty(leadingSp)
|
|
|
|
" lines containing leading spaces found
|
|
|
|
let smallestInd = len(sort(leadingSp)[-1])
|
|
|
|
let ind = input('retab, spaces per tab: ', smallestInd)
|
|
|
|
for i in range(0, len(lines)-1)
|
|
|
|
let ml = matchlist(lines[i], '^\(\s*\)\(.*\)')
|
|
|
|
let lines[i] = repeat("\t", len(ml[1]) / ind)
|
|
|
|
\ . repeat( " ", len(ml[1]) % ind)
|
|
|
|
\ . ml[2]
|
|
|
|
endfor
|
|
|
|
endif
|
|
|
|
" readd tab
|
|
|
|
let tab = leadingTab ? "\t" : ""
|
|
|
|
for i in range(0,len(lines)-1)
|
|
|
|
call setline(a:firstline + i, tab.lines[i])
|
|
|
|
endfor
|
|
|
|
endf
|
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
fun! snipMate#OpenSnippetFiles() abort
|
2012-08-17 03:41:25 +00:00
|
|
|
let dict = snipMate#GetSnippetFiles(0, snipMate#ScopesByFile(), '*')
|
|
|
|
" sort by files wether they exist - put existing files first
|
|
|
|
let exists = []
|
|
|
|
let notExists = []
|
|
|
|
for [file, v] in items(dict)
|
|
|
|
let v['file'] = file
|
|
|
|
if v['exists']
|
|
|
|
call add(exists, v)
|
|
|
|
else
|
|
|
|
call add(notExists, v)
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
let all = exists + notExists
|
|
|
|
let show = map(copy(all),'(v:val["exists"] ? "exists:" : "does not exist yet:")." ".v:val["file"]')
|
|
|
|
let select = tlib#input#List('mi', 'select files to be opened in splits', show)
|
|
|
|
for idx in select
|
|
|
|
exec 'sp '.all[idx - 1]['file']
|
|
|
|
endfor
|
|
|
|
endf
|
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
fun! snipMate#ScopesByFile() abort
|
2012-08-17 03:41:25 +00:00
|
|
|
" duplicates are removed in AddScopeAliases
|
2014-04-18 12:58:02 +00:00
|
|
|
return filter(funcref#Call(g:snipMate.get_scopes), "v:val != ''")
|
2012-08-17 03:41:25 +00:00
|
|
|
endf
|
|
|
|
|
|
|
|
" used by both: completion and insert snippet
|
2015-02-13 00:19:27 +00:00
|
|
|
fun! snipMate#GetSnippetsForWordBelowCursor(word, exact) abort
|
2012-08-17 03:41:25 +00:00
|
|
|
" Setup lookups: '1.2.3' becomes [1.2.3] + [3, 2.3]
|
|
|
|
let parts = split(a:word, '\W\zs')
|
2015-02-04 10:43:54 +00:00
|
|
|
" Since '\W\zs' results in splitting *after* a non-keyword character, the
|
|
|
|
" first \W stays connected to whatever's before it, so split it off
|
2015-02-24 10:45:22 +00:00
|
|
|
if !empty(parts) && parts[0] =~ '\W$'
|
2015-02-04 10:43:54 +00:00
|
|
|
let parts = [ parts[0][:-2], strpart(parts[0], len(parts[0]) - 1) ]
|
|
|
|
\ + parts[1:]
|
|
|
|
endif
|
|
|
|
" Only look at the last few possibilities. Too many can be slow.
|
|
|
|
if len(parts) > 5
|
|
|
|
let parts = parts[-5:]
|
2012-08-17 03:41:25 +00:00
|
|
|
endif
|
2014-03-11 20:10:50 +00:00
|
|
|
let lookups = [a:word]
|
2012-08-17 03:41:25 +00:00
|
|
|
let lookup = ''
|
|
|
|
for w in reverse(parts)
|
|
|
|
let lookup = w . lookup
|
|
|
|
if index(lookups, lookup) == -1
|
2014-03-11 20:10:50 +00:00
|
|
|
call add(lookups, lookup)
|
2012-08-17 03:41:25 +00:00
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
2014-03-11 20:10:50 +00:00
|
|
|
" Remove empty lookup entries, but only if there are other nonempty lookups
|
|
|
|
if len(lookups) > 1
|
|
|
|
call filter(lookups, 'v:val != ""')
|
|
|
|
endif
|
2012-08-17 03:41:25 +00:00
|
|
|
|
|
|
|
let matching_snippets = []
|
|
|
|
let snippet = ''
|
|
|
|
" prefer longest word
|
|
|
|
for word in lookups
|
2014-04-18 12:58:02 +00:00
|
|
|
let g:snipMate.word = word
|
|
|
|
for [k,snippetD] in items(funcref#Call(g:snipMate['get_snippets'], [snipMate#ScopesByFile(), word]))
|
2014-03-11 20:10:50 +00:00
|
|
|
" hack: require exact match
|
|
|
|
if a:exact && k !=# word
|
|
|
|
continue
|
2012-08-17 03:41:25 +00:00
|
|
|
endif
|
|
|
|
call add(matching_snippets, [k, snippetD])
|
2014-03-11 20:10:50 +00:00
|
|
|
if a:exact
|
|
|
|
break
|
|
|
|
endif
|
2012-08-17 03:41:25 +00:00
|
|
|
endfor
|
|
|
|
endfor
|
|
|
|
return matching_snippets
|
|
|
|
endf
|
|
|
|
|
|
|
|
" snippets: dict containing snippets by name
|
|
|
|
" usually this is just {'default' : snippet_contents }
|
2015-02-13 00:19:27 +00:00
|
|
|
fun! s:ChooseSnippet(snippets) abort
|
2012-08-17 03:41:25 +00:00
|
|
|
let snippet = []
|
|
|
|
let keys = keys(a:snippets)
|
|
|
|
let i = 1
|
|
|
|
for snip in keys
|
|
|
|
let snippet += [i.'. '.snip]
|
|
|
|
let i += 1
|
|
|
|
endfor
|
|
|
|
if len(snippet) == 1
|
|
|
|
" there's only a single snippet, choose it
|
|
|
|
let idx = 0
|
|
|
|
else
|
|
|
|
let idx = tlib#input#List('si','select snippet by name',snippet) -1
|
|
|
|
if idx == -1
|
|
|
|
return ''
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
" if a:snippets[..] is a String Call returns it
|
|
|
|
" If it's a function or a function string the result is returned
|
|
|
|
return funcref#Call(a:snippets[keys(a:snippets)[idx]])
|
|
|
|
endf
|
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
fun! snipMate#WordBelowCursor() abort
|
2013-12-28 18:23:13 +00:00
|
|
|
return matchstr(getline('.'), '\S\+\%' . col('.') . 'c')
|
|
|
|
endf
|
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
fun! snipMate#GetSnippetsForWordBelowCursorForComplete(word) abort
|
2014-03-11 20:10:50 +00:00
|
|
|
let snippets = map(snipMate#GetSnippetsForWordBelowCursor(a:word, 0), 'v:val[0]')
|
2014-03-02 14:35:00 +00:00
|
|
|
return filter(snippets, 'v:val =~# "\\V\\^' . escape(a:word, '"\') . '"')
|
2013-12-28 18:23:13 +00:00
|
|
|
endf
|
2013-04-13 17:45:21 +00:00
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
fun! snipMate#CanBeTriggered() abort
|
2013-12-28 18:23:13 +00:00
|
|
|
let word = snipMate#WordBelowCursor()
|
|
|
|
let matches = snipMate#GetSnippetsForWordBelowCursorForComplete(word)
|
|
|
|
return len(matches) > 0
|
|
|
|
endf
|
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
fun! snipMate#ShowAvailableSnips() abort
|
2013-12-28 18:23:13 +00:00
|
|
|
let col = col('.')
|
|
|
|
let word = snipMate#WordBelowCursor()
|
|
|
|
let matches = snipMate#GetSnippetsForWordBelowCursorForComplete(word)
|
2012-08-17 03:41:25 +00:00
|
|
|
|
|
|
|
" Pretty hacky, but really can't have the tab swallowed!
|
|
|
|
if len(matches) == 0
|
2014-04-18 12:58:02 +00:00
|
|
|
call feedkeys(g:snipMate['no_match_completion_feedkeys_chars'], 'n')
|
2012-08-17 03:41:25 +00:00
|
|
|
return ""
|
|
|
|
endif
|
|
|
|
|
2013-04-13 17:45:21 +00:00
|
|
|
call complete(col - len(word), sort(matches))
|
2012-08-17 03:41:25 +00:00
|
|
|
return ''
|
|
|
|
endf
|
|
|
|
|
2013-11-16 19:45:48 +00:00
|
|
|
" Pass an argument to force snippet expansion instead of triggering or jumping
|
2015-02-13 00:19:27 +00:00
|
|
|
function! snipMate#TriggerSnippet(...) abort
|
2012-08-17 03:41:25 +00:00
|
|
|
if exists('g:SuperTabMappingForward')
|
|
|
|
if g:SuperTabMappingForward == "<tab>"
|
|
|
|
let SuperTabPlug = maparg('<Plug>SuperTabForward', 'i')
|
|
|
|
if SuperTabPlug == ""
|
|
|
|
let SuperTabKey = "\<c-n>"
|
|
|
|
else
|
|
|
|
exec "let SuperTabKey = \"" . escape(SuperTabPlug, '<') . "\""
|
|
|
|
endif
|
|
|
|
elseif g:SuperTabMappingBackward == "<tab>"
|
|
|
|
let SuperTabPlug = maparg('<Plug>SuperTabBackward', 'i')
|
|
|
|
if SuperTabPlug == ""
|
|
|
|
let SuperTabKey = "\<c-p>"
|
|
|
|
else
|
|
|
|
exec "let SuperTabKey = \"" . escape(SuperTabPlug, '<') . "\""
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
|
|
|
if pumvisible() " Update snippet if completion is used, or deal with supertab
|
|
|
|
if exists('SuperTabKey')
|
|
|
|
call feedkeys(SuperTabKey) | return ''
|
|
|
|
endif
|
|
|
|
call feedkeys("\<esc>a", 'n') " Close completion menu
|
|
|
|
call feedkeys("\<tab>") | return ''
|
|
|
|
endif
|
|
|
|
|
2013-11-16 19:45:48 +00:00
|
|
|
if exists('b:snip_state') && a:0 == 0 " Jump only if no arguments
|
2013-04-13 17:45:21 +00:00
|
|
|
let jump = b:snip_state.jump_stop(0)
|
|
|
|
if type(jump) == 1 " returned a string
|
|
|
|
return jump
|
|
|
|
endif
|
|
|
|
endif
|
2012-08-17 03:41:25 +00:00
|
|
|
|
|
|
|
let word = matchstr(getline('.'), '\S\+\%'.col('.').'c')
|
2014-03-11 20:10:50 +00:00
|
|
|
let list = snipMate#GetSnippetsForWordBelowCursor(word, 1)
|
2012-08-17 03:41:25 +00:00
|
|
|
if empty(list)
|
|
|
|
let snippet = ''
|
|
|
|
else
|
|
|
|
let [trigger, snippetD] = list[0]
|
2015-02-04 10:43:54 +00:00
|
|
|
let snippet = s:ChooseSnippet(snippetD)
|
2013-04-13 17:45:21 +00:00
|
|
|
" Before expanding snippet, create new undo point |i_CTRL-G|
|
|
|
|
let &undolevels = &undolevels
|
2012-08-17 03:41:25 +00:00
|
|
|
let col = col('.') - len(trigger)
|
|
|
|
sil exe 's/\V'.escape(trigger, '/\.').'\%#//'
|
2015-02-04 10:43:54 +00:00
|
|
|
return snipMate#expandSnip(snippet[0], snippet[1], col)
|
2012-08-17 03:41:25 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
" should allow other plugins to register hooks instead (duplicate code)
|
|
|
|
if exists('SuperTabKey')
|
|
|
|
call feedkeys(SuperTabKey)
|
|
|
|
return ''
|
|
|
|
endif
|
|
|
|
return word == ''
|
|
|
|
\ ? "\<tab>"
|
|
|
|
\ : "\<c-r>=snipMate#ShowAvailableSnips()\<cr>"
|
2013-11-16 19:45:48 +00:00
|
|
|
endfunction
|
2012-08-17 03:41:25 +00:00
|
|
|
|
2015-02-13 00:19:27 +00:00
|
|
|
fun! snipMate#BackwardsSnippet() abort
|
2013-04-13 17:45:21 +00:00
|
|
|
if exists('b:snip_state') | return b:snip_state.jump_stop(1) | endif
|
2012-08-17 03:41:25 +00:00
|
|
|
|
|
|
|
if exists('g:SuperTabMappingForward')
|
|
|
|
if g:SuperTabMappingForward == "<s-tab>"
|
|
|
|
let SuperTabPlug = maparg('<Plug>SuperTabForward', 'i')
|
|
|
|
if SuperTabPlug == ""
|
|
|
|
let SuperTabKey = "\<c-n>"
|
|
|
|
else
|
|
|
|
exec "let SuperTabKey = \"" . escape(SuperTabPlug, '<') . "\""
|
|
|
|
endif
|
|
|
|
elseif g:SuperTabMappingBackward == "<s-tab>"
|
|
|
|
let SuperTabPlug = maparg('<Plug>SuperTabBackward', 'i')
|
|
|
|
if SuperTabPlug == ""
|
|
|
|
let SuperTabKey = "\<c-p>"
|
|
|
|
else
|
|
|
|
exec "let SuperTabKey = \"" . escape(SuperTabPlug, '<') . "\""
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
" should allow other plugins to register hooks instead (duplicate code)
|
|
|
|
if exists('SuperTabKey')
|
|
|
|
call feedkeys(SuperTabKey)
|
|
|
|
return ''
|
|
|
|
endif
|
|
|
|
return "\<s-tab>"
|
|
|
|
endf
|
|
|
|
|
|
|
|
" vim:noet:sw=4:ts=4:ft=vim
|